KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > data > PListTestCases


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: PListTestCases.java,v 1.3 2004/02/01 05:16:33 christianc Exp $
19  */

20 package org.enhydra.barracuda.plankton.data;
21
22 import java.io.*;
23 import java.util.*;
24
25 import junit.framework.*;
26
27 import org.enhydra.barracuda.plankton.data.*;
28 import org.apache.log4j.*;
29 import org.enhydra.barracuda.testbed.*;
30
31
32 /**
33  * Test case for any PList. The idea here is that you can test
34  * any PList by simply extending this class and implementing the
35  * PData factory methods.
36  */

37 public abstract class PListTestCases extends PDataTestCases {
38     
39     //-------------------- Basics --------------------------------
40
/**
41      * Public Constructor
42      */

43     public PListTestCases(String JavaDoc name) {
44         super(name);
45     }
46     
47     //-------------------- Actual Tests --------------------------
48
//Note: all the methods herein should follow the testXXXX naming convention
49
//Also keep in mind that local vars set in one test method are NOT retained
50
//when the next method is invoked because JUnit makes a separate instance of
51
//the test class for each testXXXX method!!!
52

53     /**
54      * Verify the clone yields an equivalent object
55      */

56     public abstract void testClone();
57     
58     /**
59      * Verify the equality check (the clone test may depend on this)
60      */

61     public void testEquals() {
62         if (logger.isInfoEnabled()) logger.info("testing equals()");
63         PList plist1 = null;
64         PList plist2 = null;
65
66         //test empty lists (should be equal)
67
plist1 = this.getPListInstance();
68         plist2 = this.getPListInstance();
69         assertEquals("Empty lists equality check failed", plist1, plist2);
70
71         //test non-empty list (same objects)
72
plist1 = this.getPListInstance();
73         plist2 = this.getPListInstance();
74         String JavaDoc foo1 = "foo1";
75         String JavaDoc foo2 = "foo2";
76         String JavaDoc foo3 = "foo3";
77         plist1 = this.getPListInstance();
78         plist1.add(foo1);
79         plist1.add(foo2);
80         plist1.add(foo3);
81         plist2 = this.getPListInstance();
82         plist2.add(foo1);
83         plist2.add(foo2);
84         plist2.add(foo3);
85         assertEquals("Non-empty lists equality check 1a failed", plist1, plist2);
86         plist2.add("blah");
87         assertTrue("Non-empty lists inequality check 1b failed", !(plist1.equals(plist2)));
88         assertTrue("Non-empty lists inequality check 1c failed", !(plist2.equals(plist1)));
89         
90         //test non-empty list (equal objects)
91
plist1 = this.getPListInstance();
92         plist2 = this.getPListInstance();
93         plist1.add("foo1");
94         plist1.add("foo2");
95         plist1.add("foo3");
96         plist2 = this.getPListInstance();
97         plist2.add("foo1");
98         plist2.add("foo2");
99         plist2.add("foo3");
100         assertEquals("Non-empty lists equality check 2a failed", plist1, plist2);
101         plist2.add("blah");
102         assertTrue("Non-empty lists inequality check 2b failed", !(plist1.equals(plist2)));
103         assertTrue("Non-empty lists inequality check 2c failed", !(plist2.equals(plist1)));
104
105     }
106     
107     /**
108      * Verify the parental inheritance aspects
109      */

110     public void testParentalInheritance() {
111         if (logger.isInfoEnabled()) logger.info("testing parental inheritance methods");
112         PList proot = null;
113         PList pchild1 = null;
114         PList pchild2 = null;
115         PList pchild3 = null;
116         List tlist = null;
117
118
119         //test the auto-assignment via adding as data (w/ inherit parents)
120
//...basic add method
121
proot = getPListInstance();
122         pchild1 = getPListInstance();
123         pchild2 = getPListInstance();
124         proot.add(pchild1);
125         pchild1.add(pchild2);
126         assertTrue("Parental Inheritance check 1 failed...parent inheritance failed (should've but didn't)", proot.getParent()==null);
127         assertTrue("Parental Inheritance check 1a failed...parent inheritance failed (should've but didn't)", pchild1.getParent()==proot);
128         assertTrue("Parental Inheritance check 1b failed...parent inheritance failed (should've but didn't)", pchild2.getParent()==pchild1);
129         //...basic add @ index method
130
pchild3 = getPListInstance();
131         pchild1.add(0, pchild3);
132         assertTrue("Parental Inheritance check 1c failed...parent inheritance failed (should've but didn't)", pchild3.getParent()==pchild1);
133         //...basic add all method
134
proot = getPListInstance();
135         pchild1 = getPListInstance();
136         pchild2 = getPListInstance();
137         tlist = new ArrayList();
138         tlist.add(pchild1);
139         tlist.add(pchild2);
140         proot.addAll(tlist);
141         assertTrue("Parental Inheritance check 1d failed...parent inheritance failed (should've but didn't)", pchild1.getParent()==proot);
142         assertTrue("Parental Inheritance check 1e failed...parent inheritance failed (should've but didn't)", pchild2.getParent()==proot);
143         //...basic add all @ index method
144
proot = getPListInstance();
145         pchild1 = getPListInstance();
146         pchild2 = getPListInstance();
147         tlist = new ArrayList();
148         tlist.add(pchild1);
149         tlist.add(pchild2);
150         proot.addAll(0, tlist);
151         assertTrue("Parental Inheritance check 1f failed...parent inheritance failed (should've but didn't)", pchild1.getParent()==proot);
152         assertTrue("Parental Inheritance check 1g failed...parent inheritance failed (should've but didn't)", pchild2.getParent()==proot);
153         //...set @ index method
154
proot = getPListInstance();
155         pchild1 = getPListInstance();
156         pchild2 = getPListInstance();
157         proot.add(pchild1);
158         proot.set(0, pchild2);
159         assertTrue("Parental Inheritance check 1h failed...child1 retained parental setting", pchild1.getParent()==null);
160         assertTrue("Parental Inheritance check 1i failed...child2 didn't inherit parent", pchild2.getParent()==proot);
161         //...clear method
162
proot = getPListInstance();
163         pchild1 = getPListInstance();
164         pchild2 = getPListInstance();
165         proot.add(pchild1);
166         proot.add(pchild2);
167         proot.clear();
168         assertTrue("Parental Inheritance check 1j failed...child1 retained parental setting", pchild1.getParent()==null);
169         assertTrue("Parental Inheritance check 1k failed...child2 retained parental setting", pchild2.getParent()==null);
170         //...remove by index method
171
proot = getPListInstance();
172         pchild1 = getPListInstance();
173         proot.add(pchild1);
174         proot.remove(0);
175         assertTrue("Parental Inheritance check 1m failed...child retained parental setting", pchild1.getParent()==null);
176         //...remove by object method
177
proot = getPListInstance();
178         pchild1 = getPListInstance();
179         proot.add(pchild1);
180         proot.remove(pchild1);
181         assertTrue("Parental Inheritance check 1n failed...child retained parental setting", pchild1.getParent()==null);
182         //...removeAll method
183
proot = getPListInstance();
184         pchild1 = getPListInstance();
185         pchild2 = getPListInstance();
186         pchild3 = getPListInstance();
187         proot.add(pchild1);
188         proot.add(pchild2);
189         proot.add(pchild3);
190         tlist = new ArrayList();
191         tlist.add(pchild2);
192         tlist.add(pchild3);
193         proot.removeAll(tlist);
194         assertTrue("Parental Inheritance check 1o failed...child1 did not retain parental setting", pchild1.getParent()==proot);
195         assertTrue("Parental Inheritance check 1p failed...child2 retained parental setting", pchild2.getParent()==null);
196         assertTrue("Parental Inheritance check 1q failed...child3 retained parental setting", pchild3.getParent()==null);
197         //...retainAll method
198
proot = getPListInstance();
199         pchild1 = getPListInstance();
200         pchild2 = getPListInstance();
201         pchild3 = getPListInstance();
202         pchild1.add("pchild 1");
203         pchild2.add("pchild 2");
204         pchild3.add("pchild 3");
205         proot.add(pchild1);
206         proot.add(pchild2);
207         proot.add(pchild3);
208         tlist = new ArrayList();
209         tlist.add(pchild2);
210         tlist.add(pchild3);
211         proot.retainAll(tlist);
212         assertTrue("Parental Inheritance check 1r failed...child1 retained parental setting", pchild1.getParent()==null);
213         assertTrue("Parental Inheritance check 1s failed...child2 did not retain parental setting", pchild2.getParent()==proot);
214         assertTrue("Parental Inheritance check 1t failed...child3 did not retain parental setting", pchild3.getParent()==proot);
215
216
217         //test the auto-assignment via adding as data (w/out inherit parents)
218
//...basic add method
219
proot = getPListInstance();
220         pchild1 = getPListInstance();
221         pchild1.setInheritParents(false);
222         pchild2 = getPListInstance();
223         pchild2.setInheritParents(false);
224         proot.add(pchild1);
225         pchild1.add(pchild2);
226         assertTrue("Parental Inheritance check 2 failed...parent inheritance failed (shouldn't have but did)", proot.getParent()==null);
227         assertTrue("Parental Inheritance check 2a failed...parent inheritance failed (shouldn't have but did)", pchild1.getParent()!=proot);
228         assertTrue("Parental Inheritance check 2b failed...parent inheritance failed (shouldn't have but did)", pchild2.getParent()!=pchild1);
229         //...basic add @ index method
230
pchild3 = getPListInstance();
231         pchild3.setInheritParents(false);
232         pchild1.add(0, pchild3);
233         assertTrue("Parental Inheritance check 2c failed...parent inheritance failed (shouldn't have but did)", pchild3.getParent()!=pchild1);
234         //...basic add all method
235
proot = getPListInstance();
236         pchild1 = getPListInstance();
237         pchild1.setInheritParents(false);
238         pchild2 = getPListInstance();
239         pchild2.setInheritParents(false);
240         tlist = new ArrayList();
241         tlist.add(pchild1);
242         tlist.add(pchild2);
243         proot.addAll(tlist);
244         assertTrue("Parental Inheritance check 2d failed...parent inheritance failed (shouldn't have but did)", pchild1.getParent()!=proot);
245         assertTrue("Parental Inheritance check 2e failed...parent inheritance failed (shouldn't have but did)", pchild2.getParent()!=proot);
246         //...basic add all @ index method
247
proot = getPListInstance();
248         pchild1 = getPListInstance();
249         pchild1.setInheritParents(false);
250         pchild2 = getPListInstance();
251         pchild2.setInheritParents(false);
252         tlist = new ArrayList();
253         tlist.add(pchild1);
254         tlist.add(pchild2);
255         proot.addAll(0, tlist);
256         assertTrue("Parental Inheritance check 2f failed...parent inheritance failed (shouldn't have but did)", pchild1.getParent()!=proot);
257         assertTrue("Parental Inheritance check 2g failed...parent inheritance failed (shouldn't have but did)", pchild2.getParent()!=proot);
258         //...set @ index method
259
proot = getPListInstance();
260         pchild1 = getPListInstance();
261         pchild2 = getPListInstance();
262         proot.add(pchild1);
263         pchild1.setInheritParents(false); //note, by setting this false _after_ the item has been added the parental value won't be touched (it'll still be pointing to root)
264
pchild2.setInheritParents(false);
265         proot.set(0, pchild2);
266         assertTrue("Parental Inheritance check 2h failed...child1's parental value was improperly cleared", pchild1.getParent()==proot);
267         assertTrue("Parental Inheritance check 2i failed...child2's parental value was improperly set", pchild2.getParent()==null);
268         //...clear method
269
proot = getPListInstance();
270         pchild1 = getPListInstance();
271         pchild2 = getPListInstance();
272         proot.add(pchild1);
273         proot.add(pchild2);
274         pchild1.setInheritParents(false); //note, by setting this false _after_ the item has been added the parental value won't be touched (it'll still be pointing to root)
275
pchild2.setInheritParents(false); //ditto
276
proot.clear();
277         assertTrue("Parental Inheritance check 2j failed...child1's parental value was improperly cleared", pchild1.getParent()==proot);
278         assertTrue("Parental Inheritance check 2k failed...child2's parental value was improperly cleared", pchild2.getParent()==proot);
279         //...remove by index method
280
proot = getPListInstance();
281         pchild1 = getPListInstance();
282         proot.add(pchild1);
283         pchild1.setInheritParents(false); //note, by setting this false _after_ the item has been added the parental value won't be touched (it'll still be pointing to root)
284
proot.remove(0);
285         assertTrue("Parental Inheritance check 2m failed...child's parental value was improperly cleared", pchild1.getParent()==proot);
286         //...remove by object method
287
proot = getPListInstance();
288         pchild1 = getPListInstance();
289         proot.add(pchild1);
290         pchild1.setInheritParents(false); //note, by setting this false _after_ the item has been added the parental value won't be touched (it'll still be pointing to root)
291
proot.remove(pchild1);
292         assertTrue("Parental Inheritance check 2n failed...child's parental value was improperly cleared", pchild1.getParent()==proot);
293         //...removeAll method
294
proot = getPListInstance();
295         pchild1 = getPListInstance();
296         pchild2 = getPListInstance();
297         pchild3 = getPListInstance();
298         proot.add(pchild1);
299         proot.add(pchild2);
300         proot.add(pchild3);
301         pchild1.setInheritParents(false);
302         pchild2.setInheritParents(false);
303         pchild3.setInheritParents(false);
304         tlist = new ArrayList();
305         tlist.add(pchild2);
306         tlist.add(pchild3);
307         proot.removeAll(tlist);
308         assertTrue("Parental Inheritance check 2p failed...child2 did not retain parental setting", pchild2.getParent()==proot);
309         assertTrue("Parental Inheritance check 2q failed...child3 did not retain parental setting", pchild3.getParent()==proot);
310         //...retainAll method
311
proot = getPListInstance();
312         pchild1 = getPListInstance();
313         pchild2 = getPListInstance();
314         pchild3 = getPListInstance();
315         proot.add(pchild1);
316         proot.add(pchild2);
317         proot.add(pchild3);
318         tlist = new ArrayList();
319         tlist.add(pchild2);
320         tlist.add(pchild3);
321         proot.retainAll(tlist);
322         assertTrue("Parental Inheritance check 2r failed...child1 did not retain parental setting", pchild1.getParent()==proot);
323     }
324
325     /**
326      * Test basic data manipulation
327      */

328     public void testListManipulation() {
329         if (logger.isInfoEnabled()) logger.info("testing basic data manipulation");
330         PList proot = null;
331         PList plist1 = null;
332         PList plist2 = null;
333         PList plist3 = null;
334         List tlist = null;
335         Object JavaDoc[] els = new Object JavaDoc[] {"Foo", new Integer JavaDoc(99), null};
336
337         //test the single add/remove methods
338
//...put it in
339
proot = getPListInstance();
340         for (int i=0; i<els.length; i++) {
341             proot.add(els[i]);
342             assertTrue("Add data check 1a, idx["+i+"] - data not added", proot.contains(els[i]));
343             assertTrue("Add data check 1b, idx["+i+"] - incorrect size", proot.size()==i+1);
344             assertTrue("Add data check 1c, idx["+i+"] - incorrect index", proot.indexOf(els[i])==i);
345             assertTrue("Add data check 1d, idx["+i+"] - incorrect last index", proot.lastIndexOf(els[i])==i);
346             assertTrue("Add data check 1e, idx["+i+"] - data not retrieved", proot.get(i)==els[i]);
347             assertTrue("Add data check 1f, idx["+i+"] - list claims to be empty", !proot.isEmpty());
348         }
349         //...take it back out
350
for (int i=els.length-1; i>=0; i--) {
351             proot.remove(els[i]);
352             assertTrue("Remove data check 1a, idx["+i+"] - data not removed", !proot.contains(els[i]));
353             assertTrue("Remove data check 1b, idx["+i+"] - incorrect size", proot.size()==i);
354             assertTrue("Remove data check 1c, idx["+i+"] - incorrect index", proot.indexOf(els[i])==-1);
355             assertTrue("Remove data check 1d, idx["+i+"] - incorrect last index", proot.lastIndexOf(els[i])==-1);
356             try {
357                 Object JavaDoc o = proot.get(i);
358                 fail("Remove data check 1e, idx["+i+"] - exception not generated");
359             } catch (IndexOutOfBoundsException JavaDoc e) {}
360         }
361         assertTrue("Remove data check 1f - list not empty", proot.isEmpty());
362         
363         //test the Collection add/remove methods
364
tlist = new ArrayList();
365         for (int i=0; i<els.length; i++) {tlist.add(els[i]);}
366         //...put it in
367
proot = getPListInstance();
368         proot.addAll(tlist);
369         assertTrue("Add data check 2a - incorrect size", proot.size()==els.length);
370         assertTrue("Add data check 2b - doesn't contain all items", proot.containsAll(tlist));
371         for (int i=0; i<els.length; i++) {
372             assertTrue("Add data check 2c, idx["+i+"] - data not added", proot.contains(els[i]));
373             assertTrue("Add data check 2d, idx["+i+"] - incorrect index", proot.indexOf(els[i])==i);
374             assertTrue("Add data check 2e, idx["+i+"] - incorrect last index", proot.lastIndexOf(els[i])==i);
375             assertTrue("Add data check 2f, idx["+i+"] - data not retrieved", proot.get(i)==els[i]);
376             assertTrue("Add data check 2f, idx["+i+"] - list claims to be empty", !proot.isEmpty());
377         }
378         //...take it back out
379
proot.removeAll(tlist);
380         assertTrue("Remove data check 2a - incorrect size", proot.size()==0);
381         assertTrue("Remove data check 2b - still contain all items", !proot.containsAll(tlist));
382         for (int i=els.length-1; i>=0; i--) {
383             proot.remove(els[i]);
384             assertTrue("Remove data check 2c, idx["+i+"] - data not removed", !proot.contains(els[i]));
385             assertTrue("Remove data check 2d, idx["+i+"] - incorrect index", proot.indexOf(els[i])==-1);
386             assertTrue("Remove data check 2e, idx["+i+"] - incorrect last index", proot.lastIndexOf(els[i])==-1);
387             try {
388                 Object JavaDoc o = proot.get(i);
389                 fail("Remove data check 2f, idx["+i+"] - exception not generated");
390             } catch (IndexOutOfBoundsException JavaDoc e) {}
391         }
392         assertTrue("Remove data check 2f - list not empty", proot.isEmpty());
393         //...put it in at a given index
394
proot = getPListInstance();
395         proot.add("some other item");
396         proot.addAll(0, tlist);
397         assertTrue("Add data check 3a - incorrect size", proot.size()==els.length+1);
398         assertTrue("Add data check 3b - doesn't contain all items", proot.containsAll(tlist));
399         for (int i=0; i<els.length; i++) {
400             assertTrue("Add data check 3c, idx["+i+"] - data not added", proot.contains(els[i]));
401             assertTrue("Add data check 3d, idx["+i+"] - incorrect index", proot.indexOf(els[i])==i);
402             assertTrue("Add data check 3e, idx["+i+"] - incorrect last index", proot.lastIndexOf(els[i])==i);
403             assertTrue("Add data check 3f, idx["+i+"] - data not retrieved", proot.get(i)==els[i]);
404         }
405         //...test retention
406
proot.retainAll(tlist);
407         assertTrue("Remove data check 3a - incorrect size", proot.size()==els.length);
408         
409         //misc tests
410
//...test the set method
411
proot = getPListInstance();
412         proot.add("Foo");
413         proot.set(0, "Blah");
414         assertTrue("Set method failed", proot.get(0).equals("Blah"));
415         //...test subList method
416
proot = getPListInstance();
417         proot.add("Foo");
418         tlist = new ArrayList();
419         for (int i=0; i<els.length; i++) {tlist.add(els[i]);}
420         proot.addAll(tlist);
421         proot.add("Blah");
422         List sublist = proot.subList(1,els.length+1);
423         assertTrue("subList method failed", sublist.size()==els.length);
424         //...test the clear method
425
proot = getPListInstance();
426         for (int i=0; i<els.length; i++) {
427             proot.add(els[i]);
428         }
429         assertTrue("Clear check 4a failed - data not added", proot.size()==els.length);
430         proot.clear();
431         assertTrue("Clear check 4b failed - data not removed", proot.size()==0);
432         //...toArray
433
//(todo)
434

435     }
436
437
438 }
439
Popular Tags