KickJava   Java API By Example, From Geeks To Geeks.

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


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: PMapTestCases.java,v 1.2 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.apache.log4j.*;
28 import org.enhydra.barracuda.testbed.*;
29
30
31 /**
32  * Test case for any PMap. The idea here is that you can test
33  * any PMap by simply extending this class and implementing the
34  * PData factory methods.
35  */

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

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

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

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

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

109     public void testParentalInheritance() {
110         if (logger.isInfoEnabled()) logger.info("testing parental inheritance methods");
111         PMap proot = null;
112         PMap pchild1 = null;
113         PMap pchild2 = null;
114         PMap pchild3 = null;
115         Map tmap = null;
116
117
118         //test the auto-assignment via adding as data (w/ inherit parents)
119
//...basic add method
120
proot = getPMapInstance();
121         pchild1 = getPMapInstance();
122         pchild2 = getPMapInstance();
123         proot.put("k1", pchild1);
124         pchild1.put("k2", pchild2);
125         assertTrue("Parental Inheritance check 1 failed...parent inheritance failed (should've but didn't)", proot.getParent()==null);
126         assertTrue("Parental Inheritance check 1a failed...parent inheritance failed (should've but didn't)", pchild1.getParent()==proot);
127         assertTrue("Parental Inheritance check 1b failed...parent inheritance failed (should've but didn't)", pchild2.getParent()==pchild1);
128         //...putAll method
129
proot = getPMapInstance();
130         pchild1 = getPMapInstance();
131         pchild2 = getPMapInstance();
132         tmap = new HashMap();
133         tmap.put("c1", pchild1);
134         tmap.put("c2", pchild2);
135         proot.putAll(tmap);
136         assertTrue("Parental Inheritance check 1d failed...parent inheritance failed (should've but didn't)", pchild1.getParent()==proot);
137         assertTrue("Parental Inheritance check 1e failed...parent inheritance failed (should've but didn't)", pchild2.getParent()==proot);
138         //...remove method
139
proot = getPMapInstance();
140         pchild1 = getPMapInstance();
141         proot.put("key1", pchild1);
142         proot.remove("key1");
143         assertTrue("Parental Inheritance check 1n failed...child retained parental setting", pchild1.getParent()==null);
144
145
146         //test the auto-assignment via adding as data (w/out inherit parents)
147
//...basic add method
148
proot = getPMapInstance();
149         pchild1 = getPMapInstance();
150         pchild1.setInheritParents(false);
151         pchild2 = getPMapInstance();
152         pchild2.setInheritParents(false);
153         proot.put("k1", pchild1);
154         pchild1.put("k2", pchild2);
155         assertTrue("Parental Inheritance check 2 failed...parent inheritance failed (shouldn't have but did)", proot.getParent()==null);
156         assertTrue("Parental Inheritance check 2a failed...parent inheritance failed (shouldn't have but did)", pchild1.getParent()!=proot);
157         assertTrue("Parental Inheritance check 2b failed...parent inheritance failed (shouldn't have but did)", pchild2.getParent()!=pchild1);
158         //...putAll method
159
proot = getPMapInstance();
160         pchild1 = getPMapInstance();
161         pchild1.setInheritParents(false);
162         pchild2 = getPMapInstance();
163         pchild2.setInheritParents(false);
164         tmap = new HashMap();
165         tmap.put("c1", pchild1);
166         tmap.put("c2", pchild2);
167         proot.putAll(tmap);
168         assertTrue("Parental Inheritance check 2d failed...parent inheritance failed (shouldn't have but did)", pchild1.getParent()!=proot);
169         assertTrue("Parental Inheritance check 2e failed...parent inheritance failed (shouldn't have but did)", pchild2.getParent()!=proot);
170         //...remove method
171
proot = getPMapInstance();
172         pchild1 = getPMapInstance();
173         proot.put("key1", pchild1);
174         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)
175
proot.remove(pchild1);
176         assertTrue("Parental Inheritance check 2n failed...child's parental value was improperly cleared", pchild1.getParent()==proot);
177     }
178
179     /**
180      * Test basic data manipulation
181      */

182     public void testMapManipulation() {
183         if (logger.isInfoEnabled()) logger.info("testing basic data manipulation");
184         PMap proot = null;
185         PMap pmap1 = null;
186         PMap pmap2 = null;
187         PMap pmap3 = null;
188         Map tmap = null;
189         Object JavaDoc[] keys = new Object JavaDoc[] {"key1", "key2", "key3"};
190         Object JavaDoc[] vals = new Object JavaDoc[] {"Foo", new Integer JavaDoc(99), null};
191
192         //test the single put/remove methods
193
//...put it in
194
proot = getPMapInstance();
195         for (int i=0; i<vals.length; i++) {
196             proot.put(keys[i], vals[i]);
197             assertTrue("Add data check 1a, idx["+i+"] - key missing", proot.containsKey(keys[i]));
198             assertTrue("Add data check 1b, idx["+i+"] - value missing", proot.containsValue(vals[i]));
199             assertTrue("Add data check 1c, idx["+i+"] - incorrect size", proot.size()==i+1);
200             assertTrue("Add data check 1d, idx["+i+"] - data not retrieved", proot.get(keys[i])==vals[i]);
201             assertTrue("Add data check 1e, idx["+i+"] - map claims to be empty", !proot.isEmpty());
202         }
203         //...take it back out
204
for (int i=vals.length-1; i>=0; i--) {
205             proot.remove(keys[i]);
206             assertTrue("Remove data check 1a, idx["+i+"] - key not removed", !proot.containsKey(keys[i]));
207             assertTrue("Remove data check 1b, idx["+i+"] - value not removed", !proot.containsValue(vals[i]));
208             assertTrue("Remove data check 1c, idx["+i+"] - incorrect size", proot.size()==i);
209             assertTrue("Remove data check 1d, idx["+i+"] - data not removed", proot.get(keys[i])==null);
210         }
211         assertTrue("Remove data check 1e - map not empty", proot.isEmpty());
212         
213         //test the Map add/remove methods
214
tmap = new HashMap();
215         for (int i=0; i<vals.length; i++) {tmap.put(keys[i], vals[i]);}
216         //...put it in
217
proot = getPMapInstance();
218         proot.putAll(tmap);
219         assertTrue("Add data check 2a - incorrect size", proot.size()==vals.length);
220         for (int i=0; i<vals.length; i++) {
221             assertTrue("Add data check 2a, idx["+i+"] - key missing", proot.containsKey(keys[i]));
222             assertTrue("Add data check 2b, idx["+i+"] - value missing", proot.containsValue(vals[i]));
223             assertTrue("Add data check 2c, idx["+i+"] - data not retrieved", proot.get(keys[i])==vals[i]);
224             assertTrue("Add data check 2d, idx["+i+"] - map claims to be empty", !proot.isEmpty());
225         }
226         
227         //misc tests
228
//...test the clear method
229
proot = getPMapInstance();
230         for (int i=0; i<vals.length; i++) {
231             proot.put(keys[i], vals[i]);
232         }
233         assertTrue("Clear check 3a failed - data not added", proot.size()==vals.length);
234         proot.clear();
235         assertTrue("Clear check 3b failed - data not removed", proot.size()==0);
236     }
237
238
239 }
240
Popular Tags