KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > collection > CachedMapNullTest


1 package org.jboss.cache.pojo.collection;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.jboss.aop.proxy.ClassProxy;
9 import org.jboss.cache.pojo.PojoCache;
10 import org.jboss.cache.pojo.PojoCacheFactory;
11 import org.jboss.cache.pojo.test.Address;
12
13 import java.util.Collection JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 /**
21  * Set interface testing.
22  *
23  * @author Scott Marlow
24  */

25
26 public class CachedMapNullTest extends TestCase
27 {
28    Log log = LogFactory.getLog(CachedMapNullTest.class);
29    PojoCache cache_;
30    Map JavaDoc hobbies;
31
32    public CachedMapNullTest(String JavaDoc name)
33    {
34       super(name);
35    }
36
37    protected void setUp() throws Exception JavaDoc
38    {
39       super.setUp();
40       log.info("setUp() ....");
41       String JavaDoc configFile = "META-INF/local-service.xml";
42       boolean toStart = false;
43       cache_ = PojoCacheFactory.createCache(configFile, toStart);
44       cache_.start();
45
46       stage();
47    }
48
49    protected void tearDown() throws Exception JavaDoc
50    {
51       super.tearDown();
52       cache_.stop();
53    }
54
55    static final int NUMBER_OF_STAGED_HOBBIES = 5;
56
57    protected void stage() throws Exception JavaDoc
58    {
59       hobbies = new HashMap JavaDoc();
60       hobbies.put("1", "golf");
61       hobbies.put("2", "tennis");
62       hobbies.put("3", "polo");
63       hobbies.put(null, "Non-null value but the key is null");
64       hobbies.put("key is non-null but value is null", null);
65
66       cache_.attach("/person/test7", hobbies);
67       hobbies = (Map JavaDoc) cache_.find("/person/test7");
68       assertEquals("Map size", NUMBER_OF_STAGED_HOBBIES, hobbies.size());
69
70       if (!(hobbies instanceof ClassProxy || hobbies instanceof Map JavaDoc))
71       {
72          fail("testPut(): hobbies is not instance of ClassProxy nor Map");
73       }
74    }
75
76    /**
77     * Test simple put
78     *
79     * @throws Throwable
80     */

81    public void testPut() throws Throwable JavaDoc
82    {
83       int size = hobbies.size();
84       assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES, size);
85
86       hobbies.put("6", "baseball");
87       size = hobbies.size();
88       assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES + 1, size);
89
90    }
91
92    public void testAddAndRemoveIndex() throws Throwable JavaDoc
93    {
94       hobbies.put("4", "baseball");
95       int size = hobbies.size();
96       assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES + 1, size);
97
98       assertTrue("Skill contain Golf ", hobbies.containsKey("3"));
99
100       hobbies.remove("3");
101       size = hobbies.size();
102       assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES, size);
103       assertFalse("Skill does not contain " + NUMBER_OF_STAGED_HOBBIES + " anymore ", hobbies.containsKey("3"));
104
105       assertTrue("search for null key returned non-null value " + hobbies.get(null), hobbies.get(null) != null);
106
107       hobbies.remove(null);
108       size = hobbies.size();
109       assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES - 1, size);
110       assertFalse("Skill does not contain " + (NUMBER_OF_STAGED_HOBBIES - 1) + " ", hobbies.containsKey(null));
111
112       hobbies.clear();
113       size = hobbies.size();
114       assertEquals("Size is ", 0, size);
115
116       assertTrue("Should be empty", hobbies.isEmpty());
117    }
118
119    public void testPutAllEtc() throws Throwable JavaDoc
120    {
121       Map JavaDoc map = new HashMap JavaDoc();
122       map.put("4", "pingpong");
123       map.put("5", "handball");
124
125       hobbies.putAll(map);
126       int size = hobbies.size();
127       assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES + 2, size);
128
129       assertTrue("Key is ", hobbies.containsKey("4"));
130
131       Set JavaDoc keys = hobbies.keySet();
132       assertEquals("Key size ", NUMBER_OF_STAGED_HOBBIES + 2, keys.size());
133
134       Set JavaDoc entries = hobbies.entrySet();
135       assertEquals("Entry size ", NUMBER_OF_STAGED_HOBBIES + 2, entries.size());
136
137    }
138
139    public void testEntrySet() throws Throwable JavaDoc
140    {
141       System.out.println("Map " + hobbies.toString());
142       for (Iterator JavaDoc i = hobbies.entrySet().iterator(); i.hasNext();)
143       {
144          Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
145          System.out.println("Entry key and value " + entry.getKey() + " " + entry.getValue());
146       }
147    }
148
149    public void testValues() throws Throwable JavaDoc
150    {
151       System.out.println("Map " + hobbies.toString());
152
153       Set JavaDoc correct = new HashSet JavaDoc();
154       correct.add("golf");
155       correct.add("tennis");
156       correct.add("polo");
157       correct.add("Non-null value but the key is null");
158       correct.add(null);
159
160       Collection JavaDoc values = hobbies.values();
161       assertEquals("Correct number of elements in value collection",
162               correct.size(), values.size());
163
164       Iterator JavaDoc iter = null;
165       for (iter = correct.iterator(); iter.hasNext();)
166          assertTrue(values.contains(iter.next()));
167
168       for (iter = values.iterator(); iter.hasNext();)
169       {
170          Object JavaDoc value = iter.next();
171          assertTrue(value + " expected", correct.remove(value));
172       }
173       assertTrue("No missing elements from iterator", correct.size() == 0);
174
175       iter.remove();
176       assertTrue("2 elements left after remove via iter", values.size() == NUMBER_OF_STAGED_HOBBIES - 1);
177       assertTrue("Iter removal reflected in map", hobbies.size() == NUMBER_OF_STAGED_HOBBIES - 1);
178
179       Object JavaDoc[] data = values.toArray();
180       assertTrue("2 elements in values array", data.length == NUMBER_OF_STAGED_HOBBIES - 1);
181
182       values.remove(data[0]);
183       assertTrue("1 element left after remove", values.size() == NUMBER_OF_STAGED_HOBBIES - 2);
184       assertTrue("Removal reflected in map", hobbies.size() == NUMBER_OF_STAGED_HOBBIES - 2);
185
186       values.clear();
187       assertTrue("0 elements left after clear", values.size() == 0);
188       assertTrue("Clear reflected in map", hobbies.size() == 0);
189    }
190
191    public void testContainsValue() throws Throwable JavaDoc
192    {
193       System.out.println("Map " + hobbies.toString());
194       assertTrue("contains golf", hobbies.containsValue("golf"));
195       assertTrue("contains tennis", hobbies.containsValue("tennis"));
196       assertTrue("contains polo", hobbies.containsValue("polo"));
197       assertFalse("does not contain squash", hobbies.containsValue("squash"));
198    }
199
200    public void testEquals() throws Throwable JavaDoc
201    {
202       Map JavaDoc map = new HashMap JavaDoc();
203       map.put("1", "test");
204       map.put("4", "test");
205       map.put("2", "tennis");
206       assertFalse("Map should not be the same ", map.equals(hobbies));
207
208       map.clear();
209       map.put("1", "golf");
210       map.put("2", "tennis");
211       map.put("3", "polo");
212       map.put(null, "Non-null value but the key is null");
213       map.put("key is non-null but value is null", null);
214       assertTrue("Map should be the same ", map.equals(hobbies));
215       assertTrue("Map should be the same, hobbies=" + hobbies.toString() + ", map=" + map.toString(), hobbies.equals(map));
216       assertTrue("Map should be the same ", hobbies.equals(hobbies));
217    }
218
219    public void testAttachAndDetach() throws Exception JavaDoc
220    {
221       Map JavaDoc map = new HashMap JavaDoc();
222       map.put("1", "English");
223       map.put("2", "French");
224       map.put("3", "Taiwanese");
225
226       cache_.attach("/test", map); // attach
227
map = (Map JavaDoc) cache_.find("/test");
228       assertEquals("Size ", 3, map.size());
229
230       map = (Map JavaDoc) cache_.detach("/test"); // detach
231
assertEquals("Size ", 3, map.size());
232
233       System.out.println("**** End of cache content **** ");
234       map.remove("2");
235       map.put("2", "Hoklo");
236       assertEquals("Size ", 3, map.size());
237       assertEquals("Content ", "Hoklo", map.get("2"));
238
239       // Try to re-attach
240
cache_.attach("/test", map);
241       map.remove("3");
242       assertEquals("Size ", 2, map.size());
243    }
244
245    public void testPojoAttachAndDetach() throws Exception JavaDoc
246    {
247       Address add1 = new Address();
248       add1.setCity("San Jose");
249       add1.setZip(95123);
250
251       Address add2 = new Address();
252       add1.setCity("Sunnyvale");
253       add1.setZip(94086);
254
255       Address add3 = new Address();
256       add1.setCity("Santa Clara");
257       add1.setZip(951131);
258
259       Map JavaDoc map = new HashMap JavaDoc();
260       map.put("1", add1);
261       map.put("2", add2);
262       map.put("3", add3);
263
264       cache_.attach("/test", map); // attach
265
map = (Map JavaDoc) cache_.find("/test");
266       assertEquals("Size ", 3, map.size());
267
268       map = (Map JavaDoc) cache_.detach("/test");
269       assertEquals("Size ", 3, map.size());
270
271       System.out.println("**** End of cache content **** ");
272       map.remove("2");
273       map.put("2", add2);
274       assertEquals("Size ", 3, map.size());
275       assertEquals("Content ", add2, map.get("2"));
276
277       // Try to re-attach
278
cache_.attach("/test", map);
279       map.remove("2");
280       assertEquals("Size ", 2, map.size());
281    }
282
283    public static Test suite() throws Exception JavaDoc
284    {
285       return new TestSuite(CachedMapNullTest.class);
286    }
287
288    public static void main(String JavaDoc[] args) throws Exception JavaDoc
289    {
290       junit.textui.TestRunner.run(suite());
291    }
292 }
293
294
Popular Tags