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: TestListModel.java,v 1.8 2002/09/12 19:09:35 ludovicc Exp $
37 */
38 package test.model.collection;
39
40
41 import java.util.ArrayList;
42 import java.util.LinkedList;
43 import java.util.List;
44 import junit.framework.TestCase;
45 import org.scopemvc.core.ModelChangeEvent;
46 import org.scopemvc.core.ModelChangeListener;
47 import org.scopemvc.model.collection.ListModel;
48 import test.model.basic.BasicTestModel;
49
50 /***
51 * <P>
52 *
53 * </P>
54 *
55 * @author <A HREF="mailto:haruki_zaemon@users.sourceforge.net">Simon Harris</A>
56 * @created 05 September 2002
57 * @version $Revision: 1.8 $
58 */
59 public final class TestListModel extends TestCase implements ModelChangeListener {
60
61 private ListModel listModel;
62 private BasicTestModel model, submodel;
63 private List list;
64
65 private boolean modelChanged;
66 private String modelChangedName;
67
68
69 /***
70 * Constructor for the TestListModel object
71 *
72 * @param inName Name of the test
73 */
74 public TestListModel(String inName) {
75 super(inName);
76 }
77
78
79 /***
80 * A unit test for JUnit
81 *
82 * @throws Exception Any abnormal exception
83 */
84 public void testListModelDefaultConstructor() throws Exception {
85 ListModel m = new ListModel(null);
86 assertTrue(m.getSize() == 0);
87 try {
88 m.get(0);
89 } catch (NullPointerException e) {
90 // We expect this
91 try {
92 m.set(1, "test");
93 fail("get/set on no list");
94 } catch (NullPointerException e1) {
95 // expected
96 }
97 }
98 }
99
100
101 /***
102 * A unit test for JUnit
103 *
104 * @throws Exception Any abnormal exception
105 */
106 public void testListModelBooleanConstructor1() throws Exception {
107 ListModel m = new ListModel(false, null);
108 assertTrue(m.getSize() == 0);
109 try {
110 m.get(0);
111 fail("get/set on no list");
112 } catch (NullPointerException e) {
113 // expected
114 }
115 try {
116 m.set(1, "test");
117 fail("get/set on no list");
118 } catch (NullPointerException e1) {
119 // expected
120 }
121 }
122
123
124 /***
125 * A unit test for JUnit
126 *
127 * @throws Exception Any abnormal exception
128 */
129 public void testListModelBooleanConstructor2() throws Exception {
130 ListModel m = new ListModel(true);
131 assertTrue(m.getSize() == 0);
132 try {
133 m.get(0);
134 fail("get/set on no list");
135 } catch (NullPointerException e) {
136 // expected
137 }
138 try {
139 m.set(-1, "test");
140 fail("get/set on no list");
141 } catch (NullPointerException e1) {
142 // expected
143 }
144 }
145
146
147 /***
148 * A unit test for JUnit
149 *
150 * @throws Exception Any abnormal exception
151 */
152 public void testListModelListConstructor() throws Exception {
153 ListModel m = new ListModel(list);
154 assertTrue(m.getSize() == list.size());
155 for (int i = 0; i < m.getSize(); ++i) {
156 assertEquals(m.get(i), list.get(i));
157 }
158 try {
159 m.get(list.size() + 1);
160 } catch (IndexOutOfBoundsException e) {
161 // We expect this
162 try {
163 m.set(-1, "test");
164 } catch (IndexOutOfBoundsException e1) {
165 // We expect this
166 return;
167 }
168 }
169 fail("get/set beyond bounds of list");
170 }
171
172
173 /***
174 * A unit test for JUnit
175 *
176 * @throws Exception Any abnormal exception
177 */
178 public void testListModelBooleanListConstructor1() throws Exception {
179 ListModel m = new ListModel(true, list);
180 assertTrue(m.getSize() == list.size());
181 for (int i = 0; i < m.getSize(); ++i) {
182 assertEquals(m.get(i), list.get(i));
183 }
184 try {
185 m.get(list.size() + 1);
186 } catch (Exception e) {
187 // We expect this
188 try {
189 m.set(-1, "test");
190 } catch (IndexOutOfBoundsException e1) {
191 // We expect this
192 return;
193 }
194 }
195 fail("get/set beyond bounds of list");
196 }
197
198
199 /***
200 * A unit test for JUnit
201 *
202 * @throws Exception Any abnormal exception
203 */
204 public void testListModelBooleanListConstructor2() throws Exception {
205 ListModel m = new ListModel(false, list);
206 assertTrue(m.getSize() == list.size());
207 for (int i = 0; i < m.getSize(); ++i) {
208 assertEquals(m.get(i), list.get(i));
209 }
210 try {
211 m.get(list.size() + 1);
212 } catch (Exception e) {
213 // We expect this
214 try {
215 m.set(-1, "test");
216 } catch (IndexOutOfBoundsException e1) {
217 // We expect this
218 return;
219 }
220 }
221 fail("get/set beyond bounds of list");
222 }
223
224
225 /***
226 * A unit test for JUnit
227 *
228 * @throws Exception Any abnormal exception
229 */
230 public void testListModelSelectors() throws Exception {
231 for (int i = 0; i < listModel.size(); ++i) {
232 assertEquals(listModel.get(i), new Integer(i));
233 }
234 for (int i = 0; i < listModel.size(); ++i) {
235 listModel.set(i, new Integer(i + 100));
236 }
237 for (int i = 0; i < listModel.size(); ++i) {
238 assertEquals(listModel.get(i), new Integer(i + 100));
239 }
240 listModel.setList(list);
241 assertSame(listModel.getList(), list);
242 }
243
244
245 /***
246 * A unit test for JUnit
247 *
248 * @throws Exception Any abnormal exception
249 */
250 public void testListModelSetList() throws Exception {
251 List o = new ArrayList(1);
252 o.add(new Integer(99));
253
254 listModel.setList(o);
255 assertTrue(listModel.getSize() == 1);
256 assertEquals(listModel.get(0), new Integer(99));
257 }
258
259
260 /***
261 * A unit test for JUnit
262 *
263 * @throws Exception Any abnormal exception
264 */
265 public void testListModelChangeEventPropagation() throws Exception {
266 listModel.set(0, model);
267 listModel.addModelChangeListener(this);
268
269 modelChanged = false;
270 modelChangedName = "";
271
272 submodel.setName("xxx");
273
274 assertTrue(modelChanged);
275 assertTrue(modelChangedName, modelChangedName.lastIndexOf("0.subModel.name") != -1);
276 }
277
278
279 /***
280 * A unit test for JUnit
281 *
282 * @throws Exception Any abnormal exception
283 */
284 public void testSetModelChangeEventPropagation1() throws Exception {
285 listModel.addModelChangeListener(this);
286
287 modelChanged = false;
288 listModel.add("new");
289 assertTrue(modelChanged);
290 }
291
292
293 /***
294 * A unit test for JUnit
295 *
296 * @throws Exception Any abnormal exception
297 */
298 public void testListModelChangeEventPropagation2() throws Exception {
299 listModel.set(0, model);
300 listModel.addModelChangeListener(this);
301
302 modelChanged = false;
303 modelChangedName = "";
304
305 submodel.setName("xxx");
306
307 assertTrue(modelChanged);
308 assertTrue(modelChangedName, modelChangedName.lastIndexOf("0.subModel.name") != -1);
309
310 modelChanged = false;
311 listModel.remove(0);
312 assertTrue(modelChanged);
313 }
314
315
316 /***
317 * A unit test for JUnit
318 *
319 * @throws Exception Any abnormal exception
320 */
321 public void testListModelChangeEventPropagation3() throws Exception {
322 listModel.set(0, model);
323 listModel.addModelChangeListener(this);
324
325 modelChanged = false;
326 modelChangedName = "";
327
328 submodel.setName("xxx");
329
330 assertTrue(modelChanged);
331 assertTrue(modelChangedName, modelChangedName.lastIndexOf("0.subModel.name") != -1);
332
333 modelChanged = false;
334 listModel.remove(model);
335 assertTrue(modelChanged);
336 }
337
338
339 /***
340 * A unit test for JUnit
341 *
342 * @throws Exception Any abnormal exception
343 */
344 public void testSetModelChangeEventPropagation3() throws Exception {
345 listModel.addModelChangeListener(this);
346
347 modelChanged = false;
348 List l = new LinkedList();
349 l.add(model);
350 listModel.addAll(l);
351 assertTrue(modelChanged);
352 }
353
354
355 /***
356 * A unit test for JUnit
357 *
358 * @throws Exception Any abnormal exception
359 */
360 public void testSetModelChangeEventPropagation4() throws Exception {
361 listModel.addModelChangeListener(this);
362
363 listModel.add(model);
364 listModel.add(submodel);
365
366 modelChanged = false;
367 List l = new LinkedList();
368 l.add(model);
369 listModel.retainAll(l);
370 assertTrue(modelChanged);
371 }
372
373
374 /***
375 * A unit test for JUnit
376 *
377 * @throws Exception Any abnormal exception
378 */
379 public void testSetModelChangeEventPropagation5() throws Exception {
380 listModel.addModelChangeListener(this);
381
382 listModel.add(model);
383 listModel.add(submodel);
384
385 modelChanged = false;
386 List l = new LinkedList();
387 l.add(model);
388 listModel.removeAll(l);
389 assertTrue(modelChanged);
390 }
391
392
393 /***
394 * A unit test for JUnit
395 *
396 * @throws Exception Any abnormal exception
397 */
398 public void testSetModelChangeEventPropagation6() throws Exception {
399 listModel.addModelChangeListener(this);
400 listModel.add(model);
401 listModel.add(submodel);
402
403 modelChanged = false;
404 listModel.clear();
405 assertTrue(modelChanged);
406 }
407
408
409 /***
410 * TODO: document the method
411 *
412 * @param inEvent TODO: Describe the Parameter
413 */
414 public void modelChanged(ModelChangeEvent inEvent) {
415 modelChanged = true;
416 modelChangedName = "" + inEvent;
417 }
418
419
420 /***
421 * The JUnit setup method
422 *
423 * @throws Exception Any abnormal exception
424 */
425 protected void setUp() throws Exception {
426 listModel = new ListModel(new LinkedList());
427 for (int i = 0; i < 10; ++i) {
428 listModel.add(new Integer(i));
429 }
430
431 model = new BasicTestModel("model");
432 submodel = new BasicTestModel("submodel");
433 model.setSubModel(submodel);
434
435 list = new LinkedList();
436 for (int i = 0; i < 10; ++i) {
437 list.add("test" + i);
438 }
439 modelChanged = false;
440 modelChangedName = "";
441 }
442 }
This page was automatically generated by Maven