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; |
14 |
| import java.util.HashMap; |
15 |
| import java.util.HashSet; |
16 |
| import java.util.Iterator; |
17 |
| import java.util.Map; |
18 |
| import java.util.Set; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| public class CachedMapNullTest extends TestCase |
27 |
| { |
28 |
| Log log = LogFactory.getLog(CachedMapNullTest.class); |
29 |
| PojoCache cache_; |
30 |
| Map hobbies; |
31 |
| |
32 |
18
| public CachedMapNullTest(String name)
|
33 |
| { |
34 |
18
| super(name);
|
35 |
| } |
36 |
| |
37 |
18
| protected void setUp() throws Exception
|
38 |
| { |
39 |
18
| super.setUp();
|
40 |
18
| log.info("setUp() ....");
|
41 |
18
| String configFile = "META-INF/local-service.xml";
|
42 |
18
| boolean toStart = false;
|
43 |
18
| cache_ = PojoCacheFactory.createCache(configFile, toStart);
|
44 |
18
| cache_.start();
|
45 |
| |
46 |
18
| stage();
|
47 |
| } |
48 |
| |
49 |
18
| protected void tearDown() throws Exception
|
50 |
| { |
51 |
18
| super.tearDown();
|
52 |
18
| cache_.stop();
|
53 |
| } |
54 |
| |
55 |
| static final int NUMBER_OF_STAGED_HOBBIES = 5; |
56 |
| |
57 |
18
| protected void stage() throws Exception
|
58 |
| { |
59 |
18
| hobbies = new HashMap();
|
60 |
18
| hobbies.put("1", "golf");
|
61 |
18
| hobbies.put("2", "tennis");
|
62 |
18
| hobbies.put("3", "polo");
|
63 |
18
| hobbies.put(null, "Non-null value but the key is null");
|
64 |
18
| hobbies.put("key is non-null but value is null", null);
|
65 |
| |
66 |
18
| cache_.attach("/person/test7", hobbies);
|
67 |
18
| hobbies = (Map) cache_.find("/person/test7");
|
68 |
18
| assertEquals("Map size", NUMBER_OF_STAGED_HOBBIES, hobbies.size());
|
69 |
| |
70 |
18
| if (!(hobbies instanceof ClassProxy || hobbies instanceof Map))
|
71 |
| { |
72 |
0
| fail("testPut(): hobbies is not instance of ClassProxy nor Map");
|
73 |
| } |
74 |
| } |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
2
| public void testPut() throws Throwable
|
82 |
| { |
83 |
2
| int size = hobbies.size();
|
84 |
2
| assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES, size);
|
85 |
| |
86 |
2
| hobbies.put("6", "baseball");
|
87 |
2
| size = hobbies.size();
|
88 |
2
| assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES + 1, size);
|
89 |
| |
90 |
| } |
91 |
| |
92 |
2
| public void testAddAndRemoveIndex() throws Throwable
|
93 |
| { |
94 |
2
| hobbies.put("4", "baseball");
|
95 |
2
| int size = hobbies.size();
|
96 |
2
| assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES + 1, size);
|
97 |
| |
98 |
2
| assertTrue("Skill contain Golf ", hobbies.containsKey("3"));
|
99 |
| |
100 |
2
| hobbies.remove("3");
|
101 |
2
| size = hobbies.size();
|
102 |
2
| assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES, size);
|
103 |
2
| assertFalse("Skill does not contain " + NUMBER_OF_STAGED_HOBBIES + " anymore ", hobbies.containsKey("3"));
|
104 |
| |
105 |
2
| assertTrue("search for null key returned non-null value " + hobbies.get(null), hobbies.get(null) != null);
|
106 |
| |
107 |
2
| hobbies.remove(null);
|
108 |
2
| size = hobbies.size();
|
109 |
2
| assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES - 1, size);
|
110 |
2
| assertFalse("Skill does not contain " + (NUMBER_OF_STAGED_HOBBIES - 1) + " ", hobbies.containsKey(null));
|
111 |
| |
112 |
2
| hobbies.clear();
|
113 |
2
| size = hobbies.size();
|
114 |
2
| assertEquals("Size is ", 0, size);
|
115 |
| |
116 |
2
| assertTrue("Should be empty", hobbies.isEmpty());
|
117 |
| } |
118 |
| |
119 |
2
| public void testPutAllEtc() throws Throwable
|
120 |
| { |
121 |
2
| Map map = new HashMap();
|
122 |
2
| map.put("4", "pingpong");
|
123 |
2
| map.put("5", "handball");
|
124 |
| |
125 |
2
| hobbies.putAll(map);
|
126 |
2
| int size = hobbies.size();
|
127 |
2
| assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES + 2, size);
|
128 |
| |
129 |
2
| assertTrue("Key is ", hobbies.containsKey("4"));
|
130 |
| |
131 |
2
| Set keys = hobbies.keySet();
|
132 |
2
| assertEquals("Key size ", NUMBER_OF_STAGED_HOBBIES + 2, keys.size());
|
133 |
| |
134 |
2
| Set entries = hobbies.entrySet();
|
135 |
2
| assertEquals("Entry size ", NUMBER_OF_STAGED_HOBBIES + 2, entries.size());
|
136 |
| |
137 |
| } |
138 |
| |
139 |
2
| public void testEntrySet() throws Throwable
|
140 |
| { |
141 |
2
| System.out.println("Map " + hobbies.toString());
|
142 |
2
| for (Iterator i = hobbies.entrySet().iterator(); i.hasNext();)
|
143 |
| { |
144 |
10
| Map.Entry entry = (Map.Entry) i.next();
|
145 |
10
| System.out.println("Entry key and value " + entry.getKey() + " " + entry.getValue());
|
146 |
| } |
147 |
| } |
148 |
| |
149 |
2
| public void testValues() throws Throwable
|
150 |
| { |
151 |
2
| System.out.println("Map " + hobbies.toString());
|
152 |
| |
153 |
2
| Set correct = new HashSet();
|
154 |
2
| correct.add("golf");
|
155 |
2
| correct.add("tennis");
|
156 |
2
| correct.add("polo");
|
157 |
2
| correct.add("Non-null value but the key is null");
|
158 |
2
| correct.add(null);
|
159 |
| |
160 |
2
| Collection values = hobbies.values();
|
161 |
2
| assertEquals("Correct number of elements in value collection",
|
162 |
| correct.size(), values.size()); |
163 |
| |
164 |
2
| Iterator iter = null;
|
165 |
2
| for (iter = correct.iterator(); iter.hasNext();)
|
166 |
10
| assertTrue(values.contains(iter.next()));
|
167 |
| |
168 |
2
| for (iter = values.iterator(); iter.hasNext();)
|
169 |
| { |
170 |
10
| Object value = iter.next();
|
171 |
10
| assertTrue(value + " expected", correct.remove(value));
|
172 |
| } |
173 |
2
| assertTrue("No missing elements from iterator", correct.size() == 0);
|
174 |
| |
175 |
2
| iter.remove();
|
176 |
2
| assertTrue("2 elements left after remove via iter", values.size() == NUMBER_OF_STAGED_HOBBIES - 1);
|
177 |
2
| assertTrue("Iter removal reflected in map", hobbies.size() == NUMBER_OF_STAGED_HOBBIES - 1);
|
178 |
| |
179 |
2
| Object[] data = values.toArray();
|
180 |
2
| assertTrue("2 elements in values array", data.length == NUMBER_OF_STAGED_HOBBIES - 1);
|
181 |
| |
182 |
2
| values.remove(data[0]);
|
183 |
2
| assertTrue("1 element left after remove", values.size() == NUMBER_OF_STAGED_HOBBIES - 2);
|
184 |
2
| assertTrue("Removal reflected in map", hobbies.size() == NUMBER_OF_STAGED_HOBBIES - 2);
|
185 |
| |
186 |
2
| values.clear();
|
187 |
2
| assertTrue("0 elements left after clear", values.size() == 0);
|
188 |
2
| assertTrue("Clear reflected in map", hobbies.size() == 0);
|
189 |
| } |
190 |
| |
191 |
2
| public void testContainsValue() throws Throwable
|
192 |
| { |
193 |
2
| System.out.println("Map " + hobbies.toString());
|
194 |
2
| assertTrue("contains golf", hobbies.containsValue("golf"));
|
195 |
2
| assertTrue("contains tennis", hobbies.containsValue("tennis"));
|
196 |
2
| assertTrue("contains polo", hobbies.containsValue("polo"));
|
197 |
2
| assertFalse("does not contain squash", hobbies.containsValue("squash"));
|
198 |
| } |
199 |
| |
200 |
2
| public void testEquals() throws Throwable
|
201 |
| { |
202 |
2
| Map map = new HashMap();
|
203 |
2
| map.put("1", "test");
|
204 |
2
| map.put("4", "test");
|
205 |
2
| map.put("2", "tennis");
|
206 |
2
| assertFalse("Map should not be the same ", map.equals(hobbies));
|
207 |
| |
208 |
2
| map.clear();
|
209 |
2
| map.put("1", "golf");
|
210 |
2
| map.put("2", "tennis");
|
211 |
2
| map.put("3", "polo");
|
212 |
2
| map.put(null, "Non-null value but the key is null");
|
213 |
2
| map.put("key is non-null but value is null", null);
|
214 |
2
| assertTrue("Map should be the same ", map.equals(hobbies));
|
215 |
2
| assertTrue("Map should be the same, hobbies=" + hobbies.toString() + ", map=" + map.toString(), hobbies.equals(map));
|
216 |
2
| assertTrue("Map should be the same ", hobbies.equals(hobbies));
|
217 |
| } |
218 |
| |
219 |
2
| public void testAttachAndDetach() throws Exception
|
220 |
| { |
221 |
2
| Map map = new HashMap();
|
222 |
2
| map.put("1", "English");
|
223 |
2
| map.put("2", "French");
|
224 |
2
| map.put("3", "Taiwanese");
|
225 |
| |
226 |
2
| cache_.attach("/test", map);
|
227 |
2
| map = (Map) cache_.find("/test");
|
228 |
2
| assertEquals("Size ", 3, map.size());
|
229 |
| |
230 |
2
| map = (Map) cache_.detach("/test");
|
231 |
2
| assertEquals("Size ", 3, map.size());
|
232 |
| |
233 |
2
| System.out.println("**** End of cache content **** ");
|
234 |
2
| map.remove("2");
|
235 |
2
| map.put("2", "Hoklo");
|
236 |
2
| assertEquals("Size ", 3, map.size());
|
237 |
2
| assertEquals("Content ", "Hoklo", map.get("2"));
|
238 |
| |
239 |
| |
240 |
2
| cache_.attach("/test", map);
|
241 |
2
| map.remove("3");
|
242 |
2
| assertEquals("Size ", 2, map.size());
|
243 |
| } |
244 |
| |
245 |
2
| public void testPojoAttachAndDetach() throws Exception
|
246 |
| { |
247 |
2
| Address add1 = new Address();
|
248 |
2
| add1.setCity("San Jose");
|
249 |
2
| add1.setZip(95123);
|
250 |
| |
251 |
2
| Address add2 = new Address();
|
252 |
2
| add1.setCity("Sunnyvale");
|
253 |
2
| add1.setZip(94086);
|
254 |
| |
255 |
2
| Address add3 = new Address();
|
256 |
2
| add1.setCity("Santa Clara");
|
257 |
2
| add1.setZip(951131);
|
258 |
| |
259 |
2
| Map map = new HashMap();
|
260 |
2
| map.put("1", add1);
|
261 |
2
| map.put("2", add2);
|
262 |
2
| map.put("3", add3);
|
263 |
| |
264 |
2
| cache_.attach("/test", map);
|
265 |
2
| map = (Map) cache_.find("/test");
|
266 |
2
| assertEquals("Size ", 3, map.size());
|
267 |
| |
268 |
2
| map = (Map) cache_.detach("/test");
|
269 |
2
| assertEquals("Size ", 3, map.size());
|
270 |
| |
271 |
2
| System.out.println("**** End of cache content **** ");
|
272 |
2
| map.remove("2");
|
273 |
2
| map.put("2", add2);
|
274 |
2
| assertEquals("Size ", 3, map.size());
|
275 |
2
| assertEquals("Content ", add2, map.get("2"));
|
276 |
| |
277 |
| |
278 |
2
| cache_.attach("/test", map);
|
279 |
2
| map.remove("2");
|
280 |
2
| assertEquals("Size ", 2, map.size());
|
281 |
| } |
282 |
| |
283 |
2
| public static Test suite() throws Exception
|
284 |
| { |
285 |
2
| return new TestSuite(CachedMapNullTest.class);
|
286 |
| } |
287 |
| |
288 |
0
| public static void main(String[] args) throws Exception
|
289 |
| { |
290 |
0
| junit.textui.TestRunner.run(suite());
|
291 |
| } |
292 |
| } |
293 |
| |