1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, Steve Meyfroidt
4 * All rights reserved.
5 * Email: smeyfroi@users.sourceforge.net
6 *
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * Neither the name "Scope" nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 *
37 * $Id: TestSList.java,v 1.8 2002/08/05 13:19:26 ludovicc Exp $
38 */
39
40
41 package test.view.swing;
42
43
44 import java.awt.Dimension;
45 import java.util.ArrayList;
46 import java.util.Arrays;
47 import junit.framework.TestCase;
48 import org.apache.commons.logging.LogFactory;import org.apache.commons.logging.Log;
49 import org.scopemvc.core.Selector;
50 import org.scopemvc.view.swing.SList;
51 import org.scopemvc.view.swing.SListCellRenderer;
52 import org.scopemvc.view.swing.SListModel;
53 import org.scopemvc.view.swing.SListSelectionModel;
54 import org.scopemvc.view.swing.SPanel;
55
56
57 /***
58 * <P>
59 * </P>
60 *
61 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net>Steve Meyfroidt</A>
62 * @version $Revision: 1.8 $ $Date: 2002/08/05 13:19:26 $
63 */
64 public final class TestSList extends TestCase {
65
66
67 private static final Log LOG = LogFactory.getLog(TestSList.class);
68
69
70 private SList list;
71 private SwingDummyController controller;
72 private SPanel view;
73 private SwingDummyModel model;
74
75
76 public TestSList(String inName) {
77 super(inName);
78 }
79
80
81 protected void setUp() throws Exception {
82
83 list = new SList();
84 list.setPreferredSize(new Dimension(50, 60));
85
86 view = new SPanel();
87 view.add(list);
88
89 controller = new SwingDummyController();
90 controller.setView(view);
91 controller.startup(); // does showView()
92
93 model = new SwingDummyModel();
94 }
95
96
97 protected void tearDown() {
98 controller.shutdown();
99 }
100
101
102 public void testCreateSelectionModel() throws Exception {
103 assertTrue(list.getSelectionModel() instanceof SListSelectionModel);
104 }
105
106
107 public void testCreateRenderer() throws Exception {
108 assertTrue(list.getCellRenderer() instanceof SListCellRenderer);
109 }
110
111
112 public void testCreateListModel() throws Exception {
113 assertTrue(list.getModel() instanceof SListModel);
114 }
115
116
117 public void testSetup() throws Exception {
118 Selector s = Selector.fromString("xyz");
119
120 assertNull(((SListSelectionModel)list.getSelectionModel()).getSelector());
121 list.setSelectionSelector(s);
122 assertSame(s, ((SListSelectionModel)list.getSelectionModel()).getSelector());
123
124 assertNull(((SListCellRenderer)list.getCellRenderer()).getTextSelector());
125 list.setRendererSelector(s);
126 assertSame(s, ((SListCellRenderer)list.getCellRenderer()).getTextSelector());
127
128 assertNull(((SListCellRenderer)list.getCellRenderer()).getIconSelector());
129 list.setRendererIconSelector(s);
130 assertSame(s, ((SListCellRenderer)list.getCellRenderer()).getIconSelector());
131
132 assertNull(((SListModel)list.getModel()).getSizeSelector());
133 list.setSizeSelector(s);
134 assertSame(s, ((SListModel)list.getModel()).getSizeSelector());
135 }
136
137
138 public void testSetup2() throws Exception {
139 Selector s = Selector.fromString("xyz");
140
141 assertNull(((SListSelectionModel)list.getSelectionModel()).getSelector());
142 list.setSelectionSelectorString("xyz");
143 assertEquals(s, ((SListSelectionModel)list.getSelectionModel()).getSelector());
144
145 assertNull(((SListCellRenderer)list.getCellRenderer()).getTextSelector());
146 list.setRendererSelectorString("xyz");
147 assertEquals(s, ((SListCellRenderer)list.getCellRenderer()).getTextSelector());
148
149 assertNull(((SListCellRenderer)list.getCellRenderer()).getIconSelector());
150 list.setRendererIconSelectorString("xyz");
151 assertEquals(s, ((SListCellRenderer)list.getCellRenderer()).getIconSelector());
152
153 assertNull(((SListModel)list.getModel()).getSizeSelector());
154 list.setSizeSelectorString("xyz");
155 assertEquals(s, ((SListModel)list.getModel()).getSizeSelector());
156 }
157
158
159 public void testSetup3() throws Exception {
160 Object[] l = { "a", "b" };
161
162 assertEquals(0, list.getModel().getSize());
163 list.setListModel(l);
164 controller.setModel(model);
165 SuiteViewSwing.waitForAWT();
166
167 assertEquals(2, list.getModel().getSize());
168 }
169
170
171 public void testFindIndexFor() throws Exception {
172 Object[] l = { "a", "b" };
173 list.setListModel(l);
174 controller.setModel(model);
175 SuiteViewSwing.waitForAWT();
176
177 assertEquals(0, list.findIndexFor("a"));
178 assertEquals(1, list.findIndexFor("b"));
179 assertEquals(-1, list.findIndexFor(null));
180 assertEquals(-1, list.findIndexFor("x"));
181 }
182
183
184 public void testFindElementAt() throws Exception {
185 Object[] l = { "a", "b" };
186 list.setListModel(l);
187 controller.setModel(model);
188 SuiteViewSwing.waitForAWT();
189
190 assertEquals("a", list.findElementAt(0));
191 assertEquals("b", list.findElementAt(1));
192 try {
193 assertNull(list.findElementAt(2));
194 } catch (IndexOutOfBoundsException okEx) {
195 // expected
196 }
197 try {
198 assertNull(list.findElementAt(-1));
199 } catch (IndexOutOfBoundsException okEx) {
200 // expected
201 }
202 }
203
204
205 public void testUnbound() throws Exception {
206 SuiteViewSwing.waitForAWT();
207 assertTrue(! list.isEnabled());
208 assertNull(list.getBoundModel());
209
210 list.setSelector(Selector.fromString("stringNonIndexedProperty"));
211 SuiteViewSwing.waitForAWT();
212 assertEquals(Selector.fromString("stringNonIndexedProperty"), ((SListModel)list.getModel()).getSelector());
213 assertTrue(! list.isEnabled());
214 }
215
216
217 public void testBind1() throws Exception {
218 list.setSelector(Selector.fromString("stringNonIndexedProperty"));
219 assertEquals(Selector.fromString("stringNonIndexedProperty"), ((SListModel)list.getModel()).getSelector());
220
221 controller.setModel(model);
222 SuiteViewSwing.waitForAWT();
223
224 assertSame(view.getBoundModel(), model);
225 assertSame(model, list.getBoundModel());
226 assertTrue(! list.isEnabled()); // because no selection
227 assertSame(model.getStringNonIndexedProperty(), ((SListModel)list.getModel()).getShownModel());
228 assertSame(model, ((SListSelectionModel)list.getSelectionModel()).getBoundModel());
229 }
230
231
232 public void testBind2() throws Exception {
233 list.setSelector(Selector.fromString("stringNonIndexedProperty"));
234 controller.setModel(model);
235 SuiteViewSwing.waitForAWT();
236 assertTrue(! list.isEnabled());
237
238 ArrayList newList = new ArrayList( java.util.Arrays.asList(new String[] { "1", "2", "3" }) );
239 model.setStringNonIndexedProperty(newList);
240 SuiteViewSwing.waitForAWT();
241
242 assertSame(view.getBoundModel(), model);
243 assertSame(model, list.getBoundModel());
244 assertTrue(! list.isEnabled()); // because no selection
245 assertSame(newList, ((SListModel)list.getModel()).getShownModel());
246 assertSame(model, ((SListSelectionModel)list.getSelectionModel()).getBoundModel());
247 }
248
249
250 public void testValidation() throws Exception {
251 model.getStringNonIndexedProperty().add("Illegal");
252 list.setSelector(Selector.fromString("stringNonIndexedProperty"));
253 list.setSelectionSelector(Selector.fromString("stringProperty"));
254 model.setStringProperty(null);
255 controller.setModel(model);
256 SuiteViewSwing.waitForAWT();
257
258 list.setSelectedIndex(2);
259 SuiteViewSwing.waitForAWT();
260 assertTrue(list.getSelectedIndex() == 2);
261 assertTrue("getStringProperty <" + model.getStringProperty() + ">", model.getStringProperty() == null);
262 }
263
264
265 public void testKeepSelection1() throws Exception {
266 list.setSelector(Selector.fromString("stringNonIndexedProperty"));
267 list.setSelectionSelector(Selector.fromString("stringProperty"));
268 controller.setModel(model);
269 SuiteViewSwing.waitForAWT();
270 assertTrue(list.isEnabled());
271
272 list.setSelectedIndex(1);
273 java.util.ArrayList newList = (ArrayList)model.getStringNonIndexedProperty().clone();
274 model.setStringNonIndexedProperty(newList);
275 SuiteViewSwing.waitForAWT();
276 assertTrue("getSelectedIndex == " + list.getSelectedIndex(), list.getSelectedIndex() == 1);
277 }
278
279
280 public void testKeepSelection2() throws Exception {
281 list.setSelector(Selector.fromString("stringNonIndexedProperty"));
282 list.setSelectionSelector(Selector.fromString("stringProperty"));
283 controller.setModel(model);
284 SuiteViewSwing.waitForAWT();
285
286 list.setSelectedIndex(1);
287 ArrayList newList = new ArrayList( java.util.Arrays.asList(new String[] { "1", "2", "3" }) );
288 model.setStringNonIndexedProperty(newList);
289 SuiteViewSwing.waitForAWT();
290 assertTrue("getSelectedIndex == " + list.getSelectedIndex() + ", " + list.getSelectedValue(), list.getSelectedIndex() == -1);
291 }
292
293
294 public void testControlIssue() throws Exception {
295 // ***** How to implement?
296 }
297
298
299 public void testBindNoMCE() throws Exception {
300 list.setSelector(Selector.fromString("stringNonIndexedProperty"));
301 list.setSelectionSelectorString("stringProperty");
302 assertEquals(Selector.fromString("stringNonIndexedProperty"),((SListModel)list.getModel()).getSelector());
303
304 SwingDummyModelNoMCE model = new SwingDummyModelNoMCE();
305
306 controller.setModel(model);
307 SuiteViewSwing.waitForAWT();
308
309 list.setSelectedIndex(1);
310 assertSame(view.getBoundModel(), model);
311 assertSame(model, list.getBoundModel());
312 assertSame(model.getStringNonIndexedProperty(), ((SListModel)list.getModel()).getShownModel());
313 assertSame(model, ((SListSelectionModel)list.getSelectionModel()).getBoundModel());
314 assertSame(model.getStringNonIndexedProperty().get(1), ((SListSelectionModel)list.getSelectionModel()).getViewValue());
315 assertSame(model.getStringProperty(), ((SListSelectionModel)list.getSelectionModel()).getViewValue());
316
317 ArrayList oldList = model.getStringNonIndexedProperty();
318 ArrayList newList = new ArrayList();
319 model.setStringNonIndexedProperty(newList);
320
321 assertSame(oldList, ((SListModel)list.getModel()).getShownModel());
322 assertSame(oldList.get(1), ((SListSelectionModel)list.getSelectionModel()).getViewValue());
323 assertSame(model.getStringProperty(), ((SListSelectionModel)list.getSelectionModel()).getViewValue());
324
325 list.refresh();
326 SuiteViewSwing.waitForAWT();
327
328 assertSame(model.getStringNonIndexedProperty(), ((SListModel)list.getModel()).getShownModel());
329 assertNull(model.getStringProperty());
330 }
331 }
This page was automatically generated by Maven