1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, The Scope team
4 * All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * Neither the name "Scope" nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 *
36 * $Id: TestSAbstractListModel.java,v 1.9 2002/09/13 17:21:31 ludovicc Exp $
37 */
38 package test.view.swing;
39
40
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.List;
44 import javax.swing.JList;
45 import javax.swing.ListModel;
46 import javax.swing.event.ListDataEvent;
47 import javax.swing.event.ListDataListener;
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50 import junit.framework.AssertionFailedError;
51 import junit.framework.TestCase;
52 import org.scopemvc.view.swing.SAbstractListModel;
53
54 /***
55 * <P>
56 *
57 * </P>
58 *
59 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net>Steve Meyfroidt</A>
60 * @created 10 September 2002
61 * @version $Revision: 1.9 $ $Date: 2002/09/13 17:21:31 $
62 */
63 public class TestSAbstractListModel extends TestCase {
64
65 /***
66 * TODO: describe of the Field
67 */
68 protected SAbstractListModel listModel;
69 /***
70 * TODO: describe of the Field
71 */
72 protected List list;
73 /***
74 * TODO: describe of the Field
75 */
76 protected org.scopemvc.model.collection.ListModel list2;
77 /***
78 * TODO: describe of the Field
79 */
80 protected SwingDummyModel model;
81
82
83 /***
84 * Constructor for the TestSAbstractListModel object
85 *
86 * @param inName Name of the test
87 */
88 public TestSAbstractListModel(String inName) {
89 super(inName);
90 }
91
92
93 /***
94 * A unit test for JUnit
95 *
96 * @throws Exception Any abnormal exception
97 */
98 public void testUnbound() throws Exception {
99 assertNull(listModel.getBoundModel());
100 assertTrue(listModel.getSize() == 0);
101 assertNull(listModel.getElementAt(0));
102 }
103
104
105 /***
106 * A unit test for JUnit
107 *
108 * @throws Exception Any abnormal exception
109 */
110 public void testBind1() throws Exception {
111 listModel.setBoundModel(list);
112 SuiteViewSwing.waitForAWT();
113
114 assertSame(list, listModel.getBoundModel());
115 assertTrue(listModel.getSize() == list.size());
116 assertSame(list.get(0), listModel.getElementAt(0));
117 assertSame(list.get(1), listModel.getElementAt(1));
118 assertSame(list.get(2), listModel.getElementAt(2));
119 }
120
121
122 /***
123 * A unit test for JUnit
124 *
125 * @throws Exception Any abnormal exception
126 */
127 public void testBind2() throws Exception {
128 listModel.setBoundModel(list2);
129 SuiteViewSwing.waitForAWT();
130
131 assertSame(list2, listModel.getBoundModel());
132 assertTrue(listModel.getSize() == list2.size());
133 assertSame(list2.get(0), listModel.getElementAt(0));
134 assertSame(list2.get(1), listModel.getElementAt(1));
135 assertSame(list2.get(2), listModel.getElementAt(2));
136 }
137
138
139 /***
140 * A unit test for JUnit
141 *
142 * @throws Exception Any abnormal exception
143 */
144 public void testModelChangeEvent() throws Exception {
145 listModel.setBoundModel(list2);
146 SuiteViewSwing.waitForAWT();
147
148 Listener l = new Listener();
149 listModel.addListDataListener(l);
150
151 l.reset();
152 list2.add("jkl");
153 SuiteViewSwing.waitForAWT();
154
155 assertEquals(ListDataEvent.INTERVAL_ADDED, l.type);
156 assertEquals("index0: " + l.index0, 3, l.index0);
157 assertEquals("index1: " + l.index1, 3, l.index1);
158
159 l.reset();
160 list2.remove("jkl");
161
162 assertEquals(ListDataEvent.INTERVAL_REMOVED, l.type);
163 assertEquals("index0: " + l.index0, 3, l.index0);
164 assertEquals("index1: " + l.index1, 3, l.index1);
165
166 l.reset();
167 list2.set(1, "stu");
168
169 assertEquals(ListDataEvent.CONTENTS_CHANGED, l.type);
170 assertEquals("index0: " + l.index0, 1, l.index0);
171 assertEquals("index1: " + l.index1, 1, l.index1);
172 }
173
174
175 /***
176 * A unit test for JUnit
177 *
178 * @throws Exception Any abnormal exception
179 */
180 public void testBadBind() throws Exception {
181 listModel.setBoundModel(new Integer(1));
182 SuiteViewSwing.waitForAWT();
183
184 try {
185 assertTrue(listModel.getSize() == 0);
186 assertNull(listModel.getElementAt(0));
187 } catch (AssertionFailedError ex) {
188 throw ex;
189 } catch (IllegalArgumentException ok) {
190 // expecting some kind of failure here!
191 System.out.println("Caught excepted exception " + ok);
192 }
193 }
194
195
196 /***
197 * A unit test for JUnit
198 *
199 * @throws Exception Any abnormal exception
200 */
201 public void testListPropertyBind() throws Exception {
202 listModel.setSelectorString("stringNonIndexedProperty");
203 listModel.setBoundModel(model);
204 SuiteViewSwing.waitForAWT();
205
206 assertSame(model.getStringNonIndexedProperty(), listModel.getShownModel());
207 assertTrue(listModel.getSize() == model.getStringNonIndexedProperty().size());
208 }
209
210
211 /***
212 * A unit test for JUnit
213 *
214 * @throws Exception Any abnormal exception
215 */
216 public void testArrayPropertyBind() throws Exception {
217 listModel.setSelectorString("stringNonIndexedProperty2");
218 listModel.setBoundModel(model);
219 SuiteViewSwing.waitForAWT();
220
221 assertSame(model.getStringNonIndexedProperty2(), listModel.getShownModel());
222 assertTrue("getSize: " + listModel.getSize() + ", length: " + model.getStringNonIndexedProperty2().length, listModel.getSize() == model.getStringNonIndexedProperty2().length);
223 }
224
225
226 /***
227 * A unit test for JUnit
228 *
229 * @throws Exception Any abnormal exception
230 */
231 public void testBindNoMCE() throws Exception {
232 SwingDummyModelNoMCE model = new SwingDummyModelNoMCE();
233
234 listModel.setSelectorString("stringNonIndexedProperty");
235 listModel.setBoundModel(model);
236 SuiteViewSwing.waitForAWT();
237
238 assertSame(model, listModel.getBoundModel());
239 assertSame(model.getStringNonIndexedProperty(), listModel.getShownModel());
240
241 ArrayList oldList = model.getStringNonIndexedProperty();
242 ArrayList newList = new ArrayList();
243 model.setStringNonIndexedProperty(newList);
244
245 assertSame(oldList, listModel.getShownModel());
246
247 listModel.refresh();
248 SuiteViewSwing.waitForAWT();
249
250 assertSame(newList, listModel.getShownModel());
251 }
252
253
254 /***
255 * A unit test for JUnit
256 *
257 * @throws Exception Any abnormal exception
258 */
259 public void testIndexed() throws Exception {
260 listModel.setSelectorString("hiddenStringIndexedProperty");
261 listModel.setSizeSelectorString("hiddenStringIndexedPropertySize");
262 listModel.setBoundModel(model);
263 SuiteViewSwing.waitForAWT();
264
265 assertEquals(model.getHiddenStringIndexedPropertySize(), listModel.getSize());
266
267 assertSame(model.getHiddenStringIndexedProperty(0), listModel.getElementAt(0));
268 }
269
270
271 /***
272 * A unit test for JUnit
273 *
274 * @throws Exception Any abnormal exception
275 */
276 public void testFixedListModel() throws Exception {
277 listModel.setListModel(list2);
278 listModel.setSelectorString("stringNonIndexedProperty");
279 listModel.setBoundModel(model);
280 SuiteViewSwing.waitForAWT();
281
282 assertSame(list2, listModel.getShownModel());
283
284 model.setStringNonIndexedProperty(new ArrayList());
285 SuiteViewSwing.waitForAWT();
286
287 assertSame(list2, listModel.getShownModel());
288 }
289
290
291 /***
292 * The JUnit setup method
293 *
294 * @throws Exception Any abnormal exception
295 */
296 protected void setUp() throws Exception {
297
298 listModel =
299 new SAbstractListModel() {
300 };
301
302 Object[] strings = {"abc", "def", "xyz"};
303 list = Arrays.asList(strings);
304
305 strings = new Object[]{"123", "456", "789"};
306 list2 = new org.scopemvc.model.collection.ListModel(new ArrayList(Arrays.asList(strings)));
307
308 model = new SwingDummyModel();
309 }
310
311
312 /***
313 * The teardown method for JUnit
314 */
315 protected void tearDown() { }
316
317
318 static class Listener implements ListDataListener {
319 int index0;
320 int index1;
321 int type;
322
323 /***
324 * TODO: document the method
325 *
326 * @param e TODO: Describe the Parameter
327 */
328 public void intervalAdded(ListDataEvent e) {
329 index0 = e.getIndex0();
330 index1 = e.getIndex1();
331 type = e.getType();
332 }
333
334 /***
335 * TODO: document the method
336 *
337 * @param e TODO: Describe the Parameter
338 */
339 public void intervalRemoved(ListDataEvent e) {
340 index0 = e.getIndex0();
341 index1 = e.getIndex1();
342 type = e.getType();
343 }
344
345 /***
346 * TODO: document the method
347 *
348 * @param e TODO: Describe the Parameter
349 */
350 public void contentsChanged(ListDataEvent e) {
351 index0 = e.getIndex0();
352 index1 = e.getIndex1();
353 type = e.getType();
354 }
355
356 /***
357 * TODO: document the method
358 */
359 void reset() {
360 index0 = -99;
361 index1 = -99;
362 type = -99;
363 }
364 }
365 }
This page was automatically generated by Maven