1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| package org.jboss.cache.pojo.optimistic; |
24 |
| |
25 |
| import junit.framework.TestCase; |
26 |
| import junit.framework.Test; |
27 |
| import junit.framework.TestSuite; |
28 |
| import org.apache.commons.logging.Log; |
29 |
| import org.apache.commons.logging.LogFactory; |
30 |
| import org.jboss.cache.pojo.PojoCache; |
31 |
| import org.jboss.cache.pojo.PojoCacheFactory; |
32 |
| import org.jboss.cache.pojo.test.Address; |
33 |
| |
34 |
| import java.util.List; |
35 |
| import java.util.ArrayList; |
36 |
| import java.util.Iterator; |
37 |
| import java.util.ListIterator; |
38 |
| import java.util.NoSuchElementException; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| public class CachedListTest extends AbstractOptimisticTestCase |
47 |
| { |
48 |
| Log log = LogFactory.getLog(CachedListTest.class); |
49 |
| PojoCache cache_; |
50 |
| List languages; |
51 |
| List languages2; |
52 |
| |
53 |
18
| public CachedListTest(String name)
|
54 |
| { |
55 |
18
| super(name);
|
56 |
| } |
57 |
| |
58 |
| |
59 |
18
| protected void setUp() throws Exception
|
60 |
| { |
61 |
18
| super.setUp();
|
62 |
18
| cache_ = createCache();
|
63 |
| |
64 |
18
| log.info("setUp() ....");
|
65 |
| } |
66 |
| |
67 |
18
| protected void tearDown()
|
68 |
| { |
69 |
18
| super.tearDown();
|
70 |
18
| cache_.stop();
|
71 |
| } |
72 |
| |
73 |
2
| public void testAddAndRemoveIndex() throws Throwable
|
74 |
| { |
75 |
2
| stage();
|
76 |
| |
77 |
2
| languages.add(1, "Taiwanese");
|
78 |
2
| assertEquals("Languages size ", 4, languages.size());
|
79 |
2
| assertEquals("Language ", (Object) "Taiwanese", (Object) languages.get(1));
|
80 |
2
| languages.remove(2);
|
81 |
2
| assertEquals("Languages size ", 3, languages.size());
|
82 |
2
| assertEquals("Language ", (Object) "English", (Object) languages.get(2));
|
83 |
| |
84 |
2
| languages.add("Mandarin");
|
85 |
2
| assertEquals("Languages size ", 4, languages.size());
|
86 |
2
| languages.remove("Mandarin");
|
87 |
2
| assertEquals("Languages size ", 3, languages.size());
|
88 |
| } |
89 |
| |
90 |
10
| protected void stage() throws Throwable
|
91 |
| { |
92 |
10
| languages = new ArrayList();
|
93 |
10
| languages.add("English");
|
94 |
10
| languages.add("French");
|
95 |
10
| languages.add("English");
|
96 |
10
| cache_.attach("/person/test6", languages);
|
97 |
10
| languages = (List) cache_.find("/person/test6");
|
98 |
10
| int size = languages.size();
|
99 |
10
| assertEquals("Size of list ", 3, size);
|
100 |
| |
101 |
10
| languages2 = new ArrayList();
|
102 |
10
| languages2.addAll(languages);
|
103 |
10
| assertEquals("New ArrayList().addAll(CachedList)", languages, languages2);
|
104 |
| } |
105 |
| |
106 |
2
| public void testAddAllAndClear() throws Throwable
|
107 |
| { |
108 |
2
| stage();
|
109 |
2
| List list = new ArrayList();
|
110 |
2
| list.add("Taiwanese");
|
111 |
2
| list.add("Madarin");
|
112 |
| |
113 |
2
| assertTrue("Language is Taiwanese ", list.contains("Taiwanese"));
|
114 |
| |
115 |
2
| languages.addAll(list);
|
116 |
2
| assertEquals("Languages size ", 5, languages.size());
|
117 |
| |
118 |
2
| languages.removeAll(list);
|
119 |
2
| assertEquals("Languages size ", 3, languages.size());
|
120 |
| |
121 |
2
| assertEquals("Index of French ", 1, languages.indexOf("French"));
|
122 |
| |
123 |
2
| languages.clear();
|
124 |
2
| assertEquals("Languages size ", 0, languages.size());
|
125 |
| |
126 |
2
| assertTrue("Languages empty ", languages.isEmpty());
|
127 |
| } |
128 |
| |
129 |
2
| public void testEquals() throws Throwable
|
130 |
| { |
131 |
2
| stage();
|
132 |
| |
133 |
2
| List list = (List) cache_.find("/person/test6");
|
134 |
2
| assertTrue("List should be the same ", list.equals(languages));
|
135 |
2
| list = new ArrayList();
|
136 |
2
| list.add("German");
|
137 |
2
| list.add("test");
|
138 |
2
| list.add("English");
|
139 |
2
| assertFalse("List should not be the same ", languages.equals(list));
|
140 |
2
| assertFalse("List should not be the same ", list.equals(languages));
|
141 |
| } |
142 |
| |
143 |
2
| public void testIterator() throws Throwable
|
144 |
| { |
145 |
2
| languages = new ArrayList();
|
146 |
2
| Iterator it0 = languages.iterator();
|
147 |
2
| assertFalse("Iterator should be empty ", it0.hasNext());
|
148 |
| |
149 |
2
| stage();
|
150 |
| |
151 |
2
| Iterator it = languages.iterator();
|
152 |
2
| Iterator it2 = languages2.iterator();
|
153 |
2
| int counter = 0;
|
154 |
2
| while (it.hasNext())
|
155 |
| { |
156 |
6
| counter++;
|
157 |
6
| assertEquals(it.next(), it2.next());
|
158 |
6
| it.remove();
|
159 |
6
| it2.remove();
|
160 |
| } |
161 |
| |
162 |
2
| assertEquals("Size should be ", 3, counter);
|
163 |
2
| assertEquals("Skills should be empty ", 0, languages.size());
|
164 |
| } |
165 |
| |
166 |
2
| public void testListIterator() throws Throwable
|
167 |
| { |
168 |
2
| languages = new ArrayList();
|
169 |
2
| ListIterator it0 = languages.listIterator();
|
170 |
2
| assertFalse("Iterator should be empty ", it0.hasNext());
|
171 |
2
| assertFalse("Iterator should be empty ", it0.hasPrevious());
|
172 |
| |
173 |
2
| stage();
|
174 |
| |
175 |
2
| ListIterator li = languages.listIterator();
|
176 |
2
| ListIterator li2 = languages2.listIterator();
|
177 |
2
| assertFalse("LI has no previous element ", li.hasPrevious());
|
178 |
2
| assertFalse("LI2 has no previous element ", li2.hasPrevious());
|
179 |
2
| assertTrue("LI has next element ", li.hasNext());
|
180 |
2
| assertTrue("LI2 has next element ", li2.hasNext());
|
181 |
2
| assertEquals(li.next(), li2.next());
|
182 |
2
| assertEquals("Index is ", 1, li.nextIndex());
|
183 |
2
| assertEquals("Index is ", 1, li2.nextIndex());
|
184 |
2
| assertEquals("Index is ", 0, li.previousIndex());
|
185 |
2
| assertEquals("Index is ", 0, li2.previousIndex());
|
186 |
2
| assertEquals(li.next(), li2.next());
|
187 |
2
| assertEquals(li.next(), li2.next());
|
188 |
2
| try
|
189 |
| { |
190 |
2
| li.next();
|
191 |
0
| fail("Should throw an exception here ");
|
192 |
| } |
193 |
| catch (NoSuchElementException ex) |
194 |
| { |
195 |
| ; |
196 |
| } |
197 |
2
| try
|
198 |
| { |
199 |
2
| li2.next();
|
200 |
0
| fail("Should throw an exception here ");
|
201 |
| } |
202 |
| catch (NoSuchElementException ex) |
203 |
| { |
204 |
| ; |
205 |
| } |
206 |
| |
207 |
2
| assertEquals("Index is ", 3, li.nextIndex());
|
208 |
2
| assertEquals("Index is ", 3, li2.nextIndex());
|
209 |
2
| assertEquals("Index is ", 2, li.previousIndex());
|
210 |
2
| assertEquals("Index is ", 2, li2.previousIndex());
|
211 |
2
| li.previous();
|
212 |
2
| li2.previous();
|
213 |
2
| assertEquals("Index is ", 2, li.nextIndex());
|
214 |
2
| assertEquals("Index is ", 2, li2.nextIndex());
|
215 |
2
| assertEquals("Index is ", 1, li.previousIndex());
|
216 |
2
| assertEquals("Index is ", 1, li2.previousIndex());
|
217 |
2
| li.previous();
|
218 |
2
| li2.previous();
|
219 |
2
| li.previous();
|
220 |
2
| li2.previous();
|
221 |
| |
222 |
2
| try
|
223 |
| { |
224 |
2
| li.previous();
|
225 |
0
| fail("Should throw an exception here ");
|
226 |
| } |
227 |
| catch (NoSuchElementException ex) |
228 |
| { |
229 |
| ; |
230 |
| } |
231 |
| |
232 |
2
| try
|
233 |
| { |
234 |
2
| li2.previous();
|
235 |
0
| fail("Should throw an exception here ");
|
236 |
| } |
237 |
| catch (NoSuchElementException ex) |
238 |
| { |
239 |
| ; |
240 |
| } |
241 |
| |
242 |
2
| try
|
243 |
| { |
244 |
2
| assertEquals(li.next(), li2.next());
|
245 |
2
| li.remove();
|
246 |
2
| li2.remove();
|
247 |
| } |
248 |
| catch (Exception e) |
249 |
| { |
250 |
0
| fail("ListIterator.remove failed" + e);
|
251 |
| } |
252 |
| |
253 |
| |
254 |
2
| try
|
255 |
| { |
256 |
2
| assertEquals(li.next(), li2.next());
|
257 |
2
| li.remove();
|
258 |
2
| li2.remove();
|
259 |
| } |
260 |
| catch (Exception e) |
261 |
| { |
262 |
0
| fail("ListIterator.remove failed" + e);
|
263 |
| } |
264 |
| |
265 |
2
| try
|
266 |
| { |
267 |
2
| assertEquals(li.next(), li2.next());
|
268 |
2
| assertEquals("ListIterator.remove test problem with nextIndex, cache next index=" + li.nextIndex() +
|
269 |
| ", jdk next index=" + li2.nextIndex() + "cache list size = " + languages.size() + ", jdk list size = " + languages.size(), |
270 |
| li.nextIndex(), li2.nextIndex()); |
271 |
2
| li2.set("German");
|
272 |
2
| li.set("German");
|
273 |
2
| String s1 = (String) li.previous();
|
274 |
0
| String s2 = (String) li2.previous();
|
275 |
0
| assertEquals(s1, s2);
|
276 |
0
| assertEquals(s2, "German");
|
277 |
| } |
278 |
| catch (Exception e) |
279 |
| { |
280 |
2
| fail("ListIterator.remove failed" + e + ", cache list size = " + languages.size() + ", jdk list size = " + languages.size());
|
281 |
| } |
282 |
| |
283 |
0
| try
|
284 |
| { |
285 |
0
| assertEquals(li.next(), li2.next());
|
286 |
0
| li2.add("Vulcan");
|
287 |
0
| li.add("Vulcan");
|
288 |
0
| String s1 = (String) li.previous();
|
289 |
0
| String s2 = (String) li2.previous();
|
290 |
0
| assertEquals(s1, s2);
|
291 |
0
| assertEquals(s2, "Vulcan");
|
292 |
| } |
293 |
| catch (Exception e) |
294 |
| { |
295 |
0
| fail("ListIterator.add failed" + e + ", cache list size = " + languages.size() + ", jdk list size = " + languages.size());
|
296 |
| } |
297 |
| |
298 |
| } |
299 |
| |
300 |
| |
301 |
2
| public void testAttachAndDetach() throws Exception
|
302 |
| { |
303 |
2
| List list = new ArrayList();
|
304 |
2
| list.add("English");
|
305 |
2
| list.add("French");
|
306 |
2
| list.add("Taiwanese");
|
307 |
| |
308 |
2
| cache_.attach("/test", list);
|
309 |
2
| list = (List) cache_.find("/test");
|
310 |
2
| assertEquals("Size ", 3, list.size());
|
311 |
| |
312 |
2
| list = (List) cache_.detach("/test");
|
313 |
2
| assertEquals("Size ", 3, list.size());
|
314 |
| |
315 |
2
| System.out.println("**** End of cache content **** ");
|
316 |
2
| list.remove(2);
|
317 |
2
| list.add("Hoklo");
|
318 |
2
| assertEquals("Size ", 3, list.size());
|
319 |
2
| assertEquals("Content ", "Hoklo", list.get(2));
|
320 |
| |
321 |
| |
322 |
2
| cache_.attach("/test", list);
|
323 |
2
| list.remove(2);
|
324 |
2
| assertEquals("Size ", 2, list.size());
|
325 |
| } |
326 |
| |
327 |
2
| public void testPojoAttachAndDetach() throws Exception
|
328 |
| { |
329 |
2
| Address add1 = new Address();
|
330 |
2
| add1.setCity("San Jose");
|
331 |
2
| add1.setZip(95123);
|
332 |
| |
333 |
2
| Address add2 = new Address();
|
334 |
2
| add2.setCity("Sunnyvale");
|
335 |
2
| add2.setZip(94086);
|
336 |
| |
337 |
2
| Address add3 = new Address();
|
338 |
2
| add3.setCity("Santa Clara");
|
339 |
2
| add3.setZip(951131);
|
340 |
| |
341 |
2
| List list = new ArrayList();
|
342 |
2
| list.add(add1);
|
343 |
2
| list.add(add2);
|
344 |
2
| list.add(add3);
|
345 |
| |
346 |
2
| cache_.attach("/test", list);
|
347 |
2
| list = (List) cache_.find("/test");
|
348 |
2
| assertEquals("Size ", 3, list.size());
|
349 |
| |
350 |
2
| list = (List) cache_.detach("/test");
|
351 |
2
| assertEquals("Size ", 3, list.size());
|
352 |
| |
353 |
2
| System.out.println("**** End of cache content **** ");
|
354 |
2
| list.remove(2);
|
355 |
2
| list.add(add2);
|
356 |
2
| assertEquals("Size ", 3, list.size());
|
357 |
2
| assertEquals("Content ", add2, list.get(2));
|
358 |
| |
359 |
| |
360 |
2
| cache_.attach("/test", list);
|
361 |
2
| list.remove(2);
|
362 |
2
| assertEquals("Size ", 2, list.size());
|
363 |
| } |
364 |
| |
365 |
2
| public void testEqual1() throws Exception
|
366 |
| { |
367 |
2
| List list1 = new ArrayList();
|
368 |
2
| list1.add("ID1");
|
369 |
2
| list1.add("ID2");
|
370 |
2
| cache_.attach("test1", list1);
|
371 |
2
| list1 = (List)cache_.find("test1");
|
372 |
| |
373 |
2
| List list2 = new ArrayList();
|
374 |
2
| list2.add("ID1");
|
375 |
2
| list2.add("ID2");
|
376 |
2
| cache_.attach("test2", list2);
|
377 |
2
| list2 = (List)cache_.find("test2");
|
378 |
| |
379 |
2
| List list3 = new ArrayList();
|
380 |
2
| list3.add("ID2");
|
381 |
2
| list3.add("ID1");
|
382 |
2
| cache_.attach("test3", list3);
|
383 |
2
| list3 = (List)cache_.find("test3");
|
384 |
| |
385 |
2
| assertEquals("List should be equal: ", list1, list1);
|
386 |
2
| assertTrue("List should be equal: ", list1.equals(list1));
|
387 |
2
| assertTrue("List should be equal: ", list1.equals(list2));
|
388 |
2
| assertFalse("List should not be equal: ", list1.equals(list3));
|
389 |
| } |
390 |
| |
391 |
2
| public void testEqual2() throws Exception
|
392 |
| { |
393 |
2
| List list1 = new ArrayList();
|
394 |
2
| cache_.attach("test1", list1);
|
395 |
2
| list1 = (List)cache_.find("test1");
|
396 |
2
| list1.add("ID1");
|
397 |
2
| list1.add("ID2");
|
398 |
| |
399 |
2
| List list2 = new ArrayList();
|
400 |
2
| cache_.attach("test2", list2);
|
401 |
2
| list2 = (List)cache_.find("test2");
|
402 |
2
| list2.add("ID1");
|
403 |
2
| list2.add("ID2");
|
404 |
| |
405 |
2
| List list3 = new ArrayList();
|
406 |
2
| cache_.attach("test3", list3);
|
407 |
2
| list3 = (List)cache_.find("test3");
|
408 |
2
| list3.add("ID2");
|
409 |
2
| list3.add("ID1");
|
410 |
| |
411 |
2
| assertEquals("List should be equal: ", list1, list1);
|
412 |
2
| assertTrue("List should be equal: ", list1.equals(list1));
|
413 |
2
| assertTrue("List should be equal: ", list1.equals(list2));
|
414 |
2
| assertFalse("List should not be equal: ", list1.equals(list3));
|
415 |
| } |
416 |
| |
417 |
2
| public static Test suite() throws Exception
|
418 |
| { |
419 |
2
| return new TestSuite(CachedListTest.class);
|
420 |
| } |
421 |
| |
422 |
0
| public static void main(String[] args) throws Exception
|
423 |
| { |
424 |
0
| junit.textui.TestRunner.run(suite());
|
425 |
| } |
426 |
| |
427 |
| } |