KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > GenericListTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tctest;
6
7 import com.tc.exception.TCNonPortableObjectError;
8 import com.tc.object.config.ConfigVisitor;
9 import com.tc.object.config.DSOClientConfigHelper;
10 import com.tc.object.tx.ReadOnlyException;
11 import com.tc.simulator.app.ApplicationConfig;
12 import com.tc.simulator.listener.ListenerProvider;
13 import com.tc.util.Assert;
14
15 import java.lang.reflect.Method JavaDoc;
16 import java.util.AbstractList JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.ListIterator JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.Stack JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 public class GenericListTestApp extends GenericTestApp {
29
30   public GenericListTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
31     super(appId, cfg, listenerProvider, List JavaDoc.class);
32   }
33
34   protected Object JavaDoc getTestObject(String JavaDoc testName) {
35     List JavaDoc lists = (List JavaDoc) sharedMap.get("lists");
36     return lists.iterator();
37   }
38
39   protected void setupTestObject(String JavaDoc testName) {
40     List JavaDoc lists = new ArrayList JavaDoc();
41     lists.add(new LinkedList JavaDoc());
42     lists.add(new ArrayList JavaDoc());
43     lists.add(new Vector JavaDoc());
44     lists.add(new Stack JavaDoc());
45     lists.add(new MyArrayList());
46     lists.add(new MyArrayList5());
47     lists.add(new MyArrayList6());
48     lists.add(new MyLinkedList());
49     lists.add(new MyVector());
50     lists.add(new MyStack());
51     lists.add(new MyAbstractListSubclass());
52
53     sharedMap.put("lists", lists);
54     sharedMap.put("arrayforLinkedList", new Object JavaDoc[2]);
55     sharedMap.put("arrayforArrayList", new Object JavaDoc[2]);
56     sharedMap.put("arrayforVector", new Object JavaDoc[2]);
57     sharedMap.put("arrayforStack", new Object JavaDoc[2]);
58     sharedMap.put("arrayforAbstractListSubclass", new Object JavaDoc[2]);
59     sharedMap.put("arrayforMyArrayList", new Object JavaDoc[2]);
60     sharedMap.put("arrayforMyArrayList5", new Object JavaDoc[2]);
61     sharedMap.put("arrayforMyArrayList6", new Object JavaDoc[2]);
62     sharedMap.put("arrayforMyLinkedList", new Object JavaDoc[2]);
63     sharedMap.put("arrayforMyVector", new Object JavaDoc[2]);
64     sharedMap.put("arrayforMyStack", new Object JavaDoc[2]);
65     sharedMap.put("arrayforMyAbstractListSubclass", new Object JavaDoc[2]);
66   }
67
68   void testBasicAdd(List JavaDoc list, boolean validate) {
69     if (validate) {
70       assertSingleElement(list, "rollin in my 6-4");
71     } else {
72       synchronized (list) {
73         boolean added = list.add("rollin in my 6-4");
74         Assert.assertTrue(added);
75       }
76     }
77   }
78
79   void testVectorSetSizeGrow(List JavaDoc list, boolean validate) {
80     if (!(list instanceof Vector JavaDoc)) { return; }
81
82     int size = 5;
83     Vector JavaDoc v = (Vector JavaDoc) list;
84
85     if (validate) {
86       Assert.assertEquals("start", v.get(0));
87       for (int i = 1; i < size; i++) {
88         Object JavaDoc val = v.get(i);
89         Assert.assertNull("element " + i + " is " + val, val);
90       }
91       Assert.assertEquals("end", v.get(size));
92     } else {
93       synchronized (v) {
94         v.add("start");
95         v.setSize(size);
96         v.add("end");
97       }
98     }
99   }
100
101   void testVectorSetSizeShrink(List JavaDoc list, boolean validate) {
102     if (!(list instanceof Vector JavaDoc)) { return; }
103     
104     Vector JavaDoc v = (Vector JavaDoc) list;
105
106     if (validate) {
107       Assert.assertEquals("start", v.get(0));
108       Assert.assertEquals("end", v.get(1));
109     } else {
110       synchronized (v) {
111         v.add("start");
112         v.add("ho hum");
113         v.add("ho hum2");
114         v.add("ho hum3");
115
116         v.setSize(1);
117         v.add("end");
118       }
119     }
120   }
121
122   void testVectorAddElement(List JavaDoc list, boolean validate) {
123     if (!(list instanceof Vector JavaDoc)) { return; }
124
125     Vector JavaDoc vector = (Vector JavaDoc) list;
126
127     if (validate) {
128       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element" }), vector);
129     } else {
130       synchronized (vector) {
131         vector.addElement("first element");
132         vector.addElement("second element");
133       }
134     }
135   }
136
137   void testVectorRetainAll(List JavaDoc list, boolean validate) {
138     if (!(list instanceof Vector JavaDoc)) { return; }
139
140     Vector JavaDoc vector = (Vector JavaDoc) list;
141
142     if (validate) {
143       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "second element" }), vector);
144     } else {
145
146       synchronized (vector) {
147         vector.addElement("first element");
148         vector.addElement("second element");
149         vector.addElement("third element");
150
151         List JavaDoc retainList = new ArrayList JavaDoc(1);
152         retainList.add("second element");
153
154         vector.retainAll(retainList);
155       }
156     }
157   }
158
159   void testVectorRemoveAll(List JavaDoc list, boolean validate) {
160     if (!(list instanceof Vector JavaDoc)) { return; }
161     
162
163     Vector JavaDoc vector = (Vector JavaDoc) list;
164
165     if (validate) {
166       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "third element" }), vector);
167     } else {
168       synchronized (vector) {
169         vector.addElement("first element");
170         vector.addElement("second element");
171         vector.addElement("third element");
172
173         List JavaDoc removeList = new ArrayList JavaDoc(1);
174         removeList.add("second element");
175         vector.removeAll(removeList);
176       }
177     }
178   }
179
180   void testVectorRemoveAllElements(List JavaDoc list, boolean validate) {
181     if (!(list instanceof Vector JavaDoc)) { return; }
182     
183
184     Vector JavaDoc vector = (Vector JavaDoc) list;
185
186     if (validate) {
187       assertEmptyList(vector);
188     } else {
189       synchronized (vector) {
190         vector.addElement("first element");
191         vector.addElement("second element");
192         vector.addElement("third element");
193
194         vector.removeAllElements();
195       }
196     }
197   }
198
199   void testVectorSetElementAt(List JavaDoc list, boolean validate) {
200     if (!(list instanceof Vector JavaDoc)) { return; }
201     
202
203     Vector JavaDoc vector = (Vector JavaDoc) list;
204
205     if (validate) {
206       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element", "third element" }), vector);
207     } else {
208       synchronized (vector) {
209         vector.addElement("first element");
210         vector.addElement(null);
211         vector.addElement("third element");
212
213         vector.setElementAt("second element", 1);
214       }
215     }
216   }
217
218   void testVectorInsertElementAt(List JavaDoc list, boolean validate) {
219     if (!(list instanceof Vector JavaDoc)) { return; }
220     
221
222     Vector JavaDoc vector = (Vector JavaDoc) list;
223
224     if (validate) {
225       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element", "third element" }), vector);
226     } else {
227       synchronized (vector) {
228         vector.addElement("first element");
229         vector.addElement("third element");
230
231         vector.insertElementAt("second element", 1);
232       }
233     }
234   }
235
236   void testLinkedListRemoveFirst(List JavaDoc list, boolean validate) {
237     if (!(list instanceof LinkedList JavaDoc)) { return; }
238     
239
240     LinkedList JavaDoc linkedList = (LinkedList JavaDoc) list;
241
242     if (validate) {
243       assertSingleElement(linkedList, "teck");
244     } else {
245       synchronized (linkedList) {
246         linkedList.add("timmy");
247         linkedList.add("teck");
248       }
249
250       synchronized (linkedList) {
251         linkedList.removeFirst();
252       }
253     }
254   }
255
256   void testLinkedListRemoveLast(List JavaDoc list, boolean validate) {
257     if (!(list instanceof LinkedList JavaDoc)) { return; }
258     
259
260     LinkedList JavaDoc linkedList = (LinkedList JavaDoc) list;
261
262     if (validate) {
263       assertSingleElement(linkedList, "timmy");
264     } else {
265       synchronized (linkedList) {
266         linkedList.add("timmy");
267         linkedList.add("teck");
268       }
269
270       synchronized (linkedList) {
271         linkedList.removeLast();
272       }
273     }
274   }
275
276   void testLinkedListAddFirst(List JavaDoc list, boolean validate) {
277     if (!(list instanceof LinkedList JavaDoc)) { return; }
278     
279
280     LinkedList JavaDoc linkedList = (LinkedList JavaDoc) list;
281
282     if (validate) {
283       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element" }), list);
284     } else {
285       synchronized (linkedList) {
286         linkedList.add("second element");
287       }
288
289       synchronized (linkedList) {
290         linkedList.addFirst("first element");
291       }
292     }
293   }
294
295   void testLinkedListAddLast(List JavaDoc list, boolean validate) {
296     if (!(list instanceof LinkedList JavaDoc)) { return; }
297     
298
299     LinkedList JavaDoc linkedList = (LinkedList JavaDoc) list;
300
301     if (validate) {
302       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element" }), list);
303     } else {
304       synchronized (linkedList) {
305         linkedList.add("first element");
306       }
307
308       synchronized (linkedList) {
309         linkedList.addLast("second element");
310       }
311     }
312   }
313
314   void testBasicAddNull(List JavaDoc list, boolean validate) {
315     
316     
317     if (validate) {
318       assertListsEqual(Arrays.asList(new Object JavaDoc[] { null, null, "my cat hates you", null }), list);
319     } else {
320       synchronized (list) {
321         boolean added;
322         added = list.add(null);
323         Assert.assertTrue(added);
324         added = list.add(null);
325         Assert.assertTrue(added);
326         added = list.add("my cat hates you");
327         Assert.assertTrue(added);
328         added = list.add(null);
329         Assert.assertTrue(added);
330       }
331     }
332   }
333
334   void testBasicAddAt(List JavaDoc list, boolean validate) {
335     
336     
337     if (validate) {
338       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "1", "2", "3", "4" }), list);
339     } else {
340       synchronized (list) {
341         list.add(0, "2");
342       }
343       synchronized (list) {
344         list.add(0, "1");
345       }
346       synchronized (list) {
347         list.add(2, "4");
348       }
349       synchronized (list) {
350         list.add(2, "3");
351       }
352     }
353   }
354
355   void testAdd(List JavaDoc list, boolean validate) {
356     
357     
358     if (validate) {
359       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "element" }), list);
360     } else {
361       synchronized (list) {
362         list.add("element");
363       }
364     }
365   }
366
367   void testAddAll(List JavaDoc list, boolean validate) {
368     
369     
370     if (validate) {
371       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "patty", "calahan", "was", "here" }), list);
372     } else {
373       List JavaDoc toAdd = new ArrayList JavaDoc();
374       toAdd.add("patty");
375       toAdd.add("calahan");
376       toAdd.add("was");
377       toAdd.add("here");
378
379       synchronized (list) {
380         list.addAll(toAdd);
381       }
382     }
383   }
384
385   void testAddAllAt(List JavaDoc list, boolean validate) {
386     
387     
388     if (validate) {
389       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "uno", "dos", "tres", "catorce?" }), list);
390     } else {
391       synchronized (list) {
392         list.add("uno");
393       }
394
395       List JavaDoc toAdd = new ArrayList JavaDoc();
396       toAdd.add("dos");
397       toAdd.add("tres");
398       toAdd.add("catorce?");
399
400       synchronized (list) {
401         list.addAll(1, toAdd);
402       }
403     }
404   }
405
406   void testClear(List JavaDoc list, boolean validate) {
407     
408     
409     if (validate) {
410       assertEmptyList(list);
411     } else {
412       synchronized (list) {
413         list.add("clear me baby");
414         list.clear();
415       }
416
417       synchronized (list) {
418         list.add("clear me baby one more time");
419       }
420
421       synchronized (list) {
422         list.clear();
423       }
424     }
425   }
426
427   void testSetElementAt(List JavaDoc list, boolean validate) {
428     
429     
430     if (validate) {
431       assertSingleElement(list, "new");
432     } else {
433       synchronized (list) {
434         list.add("orig");
435       }
436
437       synchronized (list) {
438         list.set(0, "new");
439       }
440     }
441   }
442
443   void testRemoveAt(List JavaDoc list, boolean validate) {
444     
445     
446     if (validate) {
447       String JavaDoc item0 = (String JavaDoc) list.get(0);
448       String JavaDoc item1 = (String JavaDoc) list.get(1);
449       Assert.assertEquals("value", item0);
450       Assert.assertEquals("different value", item1);
451     } else {
452       synchronized (list) {
453         list.add("value");
454         list.add("different value");
455         list.add("value");
456       }
457
458       synchronized (list) {
459         Object JavaDoc prev = list.remove(2);
460         Assert.assertEquals("value", prev);
461       }
462     }
463   }
464
465   void testRemoveNull1(List JavaDoc list, boolean validate) {
466     
467     
468     if (validate) {
469       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", null, "third element" }), list);
470     } else {
471       synchronized (list) {
472         list.add("first element");
473         list.add(null);
474         list.add(null);
475         list.add("third element");
476       }
477       synchronized (list) {
478         list.remove(null);
479       }
480     }
481   }
482
483   void testRemoveNull2(List JavaDoc list, boolean validate) {
484     
485     
486     if (validate) {
487       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element" }), list);
488     } else {
489       synchronized (list) {
490         list.add("first element");
491         list.add(null);
492         list.add("second element");
493         list.add(null);
494       }
495       synchronized (list) {
496         list.remove(null);
497         list.remove(null);
498       }
499     }
500   }
501
502   void testSubList(List JavaDoc list, boolean validate) {
503     
504     
505     if (validate) {
506       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element", "third element",
507           "fourth element" }), list);
508     } else {
509       synchronized (list) {
510         list.add("first element");
511         list.add("third element");
512         list.add("fourth element");
513       }
514       List JavaDoc subList = list.subList(1, 2);
515       ListIterator JavaDoc listIterator = subList.listIterator();
516       synchronized (list) {
517         listIterator.add("second element");
518       }
519     }
520   }
521
522   void testRemoveAll(List JavaDoc list, boolean validate) {
523     
524     
525     if (validate) {
526       assertEmptyList(list);
527     } else {
528       synchronized (list) {
529         list.add("first element");
530         list.add("second element");
531       }
532       List JavaDoc removeList = new ArrayList JavaDoc(2);
533       removeList.add("first element");
534       removeList.add("second element");
535       synchronized (list) {
536         list.removeAll(removeList);
537       }
538     }
539   }
540
541   void testRemoveRange(List JavaDoc list, boolean validate) {
542     // using reflection to invoke protected method of a logical subclass does not work.
543
if (list instanceof MyArrayList6) { return; }
544     
545     if (validate) {
546       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "fourth element" }), list);
547     } else {
548       synchronized (list) {
549         list.add("first element");
550         list.add("second element");
551         list.add("third element");
552         list.add("fourth element");
553       }
554       Class JavaDoc listClass = AbstractList JavaDoc.class;
555       Class JavaDoc[] parameterType = new Class JavaDoc[2];
556       parameterType[0] = Integer.TYPE;
557       parameterType[1] = Integer.TYPE;
558
559       try {
560         synchronized (list) {
561           Method JavaDoc m = listClass.getDeclaredMethod("removeRange", parameterType);
562           m.setAccessible(true); // suppressing java access checking since removeRange is
563
// a protected method.
564
m.invoke(list, new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(3) });
565         }
566       } catch (Exception JavaDoc e) {
567         // ignore Exception in test.
568
}
569     }
570   }
571
572   void testToArray(List JavaDoc list, boolean validate) {
573     
574     
575     Object JavaDoc[] array = getArray(list);
576
577     if (validate) {
578       assertListsEqual(Arrays.asList(array), list);
579     } else {
580       synchronized (list) {
581         list.add("first element");
582         list.add("second element");
583       }
584       synchronized (array) {
585         Object JavaDoc[] returnArray = list.toArray(array);
586         Assert.assertTrue(returnArray == array);
587       }
588     }
589   }
590
591   // List Iterator testing methods.
592
void testListIteratorSet1(List JavaDoc list, boolean validate) {
593     
594     
595     if (validate) {
596       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "modified first element", "second element", "third element" }),
597                        list);
598     } else {
599       synchronized (list) {
600         list.add("first element");
601         list.add("second element");
602         list.add("third element");
603       }
604       synchronized (list) {
605         ListIterator JavaDoc lIterator = list.listIterator();
606         lIterator.next();
607         lIterator.set("modified first element");
608       }
609     }
610   }
611
612   void testListIteratorSet2(List JavaDoc list, boolean validate) {
613     
614     
615     if (validate) {
616       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "modified second element", "third element" }),
617                        list);
618     } else {
619       synchronized (list) {
620         list.add("first element");
621         list.add("second element");
622         list.add("third element");
623       }
624       synchronized (list) {
625         ListIterator JavaDoc lIterator = list.listIterator();
626         lIterator.next();
627         lIterator.next();
628         lIterator.set("modified second element");
629       }
630     }
631   }
632
633   void testListIteratorSetRemove1(List JavaDoc list, boolean validate) {
634     
635     
636     if (validate) {
637       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "modified first element", "third element" }), list);
638     } else {
639       synchronized (list) {
640         list.add("first element");
641         list.add("second element");
642         list.add("third element");
643       }
644       synchronized (list) {
645         ListIterator JavaDoc lIterator = list.listIterator();
646         lIterator.next();
647         lIterator.next();
648         lIterator.remove();
649         lIterator.previous();
650         lIterator.set("modified first element");
651       }
652     }
653   }
654
655   void testListIteratorSetRemove2(List JavaDoc list, boolean validate) {
656     
657     
658     if (validate) {
659       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "modified second element" }), list);
660     } else {
661       synchronized (list) {
662         list.add("first element");
663         list.add("second element");
664         list.add("third element");
665       }
666       synchronized (list) {
667         ListIterator JavaDoc lIterator = list.listIterator();
668         lIterator.next();
669         lIterator.next();
670         lIterator.set("modified second element");
671         lIterator.next();
672         lIterator.remove();
673       }
674     }
675   }
676
677   void testListIteratorDuplicateElementRemove(List JavaDoc list, boolean validate) {
678     
679     
680     if (validate) {
681       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element" }), list);
682     } else {
683       synchronized (list) {
684         list.add("first element");
685         list.add("second element");
686         list.add("first element");
687       }
688       synchronized (list) {
689         ListIterator JavaDoc lIterator = list.listIterator();
690         lIterator.next();
691         lIterator.next();
692         lIterator.next();
693         lIterator.remove();
694       }
695     }
696   }
697
698   void testListIteratorAdd1(List JavaDoc list, boolean validate) {
699     
700     
701     if (validate) {
702       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element", "third element" }), list);
703       // assertListsEqual(Arrays.asList(new Object[] { "second element", "third element" }), list);
704
} else {
705       synchronized (list) {
706         list.add("second element");
707         list.add("third element");
708       }
709       synchronized (list) {
710         ListIterator JavaDoc lIterator = list.listIterator();
711         lIterator.add("first element");
712       }
713     }
714   }
715
716   void testListIteratorAdd2(List JavaDoc list, boolean validate) {
717     
718     
719     if (validate) {
720       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element", "third element" }), list);
721     } else {
722       synchronized (list) {
723         list.add("first element");
724         list.add("third element");
725       }
726       synchronized (list) {
727         ListIterator JavaDoc lIterator = list.listIterator();
728         lIterator.next();
729         lIterator.add("second element");
730       }
731     }
732   }
733
734   void testListIteratorAddSet1(List JavaDoc list, boolean validate) {
735     
736     
737     if (validate) {
738       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "modified first element", "second element", "third element" }),
739                        list);
740     } else {
741       synchronized (list) {
742         list.add("second element");
743         list.add("third element");
744       }
745       synchronized (list) {
746         ListIterator JavaDoc lIterator = list.listIterator();
747         lIterator.add("first element");
748         lIterator.previous();
749         lIterator.set("modified first element");
750       }
751     }
752   }
753
754   void testListIteratorAddSet2(List JavaDoc list, boolean validate) {
755     
756     
757     if (validate) {
758       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element", "modified third element" }),
759                        list);
760     } else {
761       synchronized (list) {
762         list.add("first element");
763         list.add("third element");
764       }
765       synchronized (list) {
766         ListIterator JavaDoc lIterator = list.listIterator();
767         lIterator.next();
768         lIterator.add("second element");
769         lIterator.next();
770         lIterator.set("modified third element");
771       }
772     }
773   }
774
775   void testListIteratorAddSet3(List JavaDoc list, boolean validate) {
776     
777     
778     if (validate) {
779       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element", "modified third element",
780           "fourth element" }), list);
781     } else {
782       synchronized (list) {
783         list.add("first element");
784         list.add("second element");
785         list.add("fourth element");
786       }
787       synchronized (list) {
788         ListIterator JavaDoc lIterator = list.listIterator(1);
789         lIterator.next();
790         lIterator.add("third element");
791         lIterator.previous();
792         lIterator.set("modified third element");
793       }
794     }
795   }
796
797   void testListIteratorAddNull(List JavaDoc list, boolean validate) {
798     
799     
800     if (validate) {
801       assertListsEqual(Arrays.asList(new Object JavaDoc[] { null, null, "third element" }), list);
802     } else {
803       synchronized (list) {
804         ListIterator JavaDoc lIterator = list.listIterator();
805         lIterator.add(null);
806         lIterator.add(null);
807         lIterator.add("third element");
808       }
809     }
810   }
811
812   void testListIteratorAddRemove(List JavaDoc list, boolean validate) {
813     
814     
815     if (validate) {
816       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "second element", "third element" }), list);
817     } else {
818       synchronized (list) {
819         list.add("second element");
820         list.add("third element");
821       }
822       synchronized (list) {
823         ListIterator JavaDoc lIterator = list.listIterator();
824         lIterator.add("first element");
825         lIterator.previous();
826         lIterator.remove();
827       }
828     }
829   }
830
831   void testListIteratorRemoveNull(List JavaDoc list, boolean validate) {
832     
833     
834     if (validate) {
835       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", null, "third element" }), list);
836     } else {
837       synchronized (list) {
838         list.add("first element");
839         list.add(null);
840         list.add(null);
841         list.add("third element");
842       }
843       synchronized (list) {
844         ListIterator JavaDoc lIterator = list.listIterator();
845         lIterator.next();
846         lIterator.next();
847         lIterator.remove();
848       }
849     }
850   }
851
852   // Read only testing methods.
853
void testReadOnlyAdd(List JavaDoc list, boolean validate) {
854     
855     
856     if (list instanceof Vector JavaDoc) { return; }
857
858     if (validate) {
859       assertEmptyList(list);
860     } else {
861       synchronized (list) {
862         try {
863           list.add("first element");
864           throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
865         } catch (ReadOnlyException t) {
866           // Expected
867
}
868       }
869     }
870   }
871
872   void testReadOnlySet(List JavaDoc list, boolean validate) {
873     if (list instanceof Vector JavaDoc) { return; }
874     
875
876     if (validate) {
877       assertEmptyList(list);
878     } else {
879       synchronized (list) {
880         try {
881           list.set(0, "first element");
882           throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
883         } catch (ReadOnlyException t) {
884           // Expected
885
}
886       }
887     }
888   }
889
890   // Setting up for the ReadOnly test for remove.
891
void testSetUpRemove(List JavaDoc list, boolean validate) {
892     if (list instanceof Vector JavaDoc) { return; }
893     
894
895     if (validate) {
896       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element" }), list);
897     } else {
898       synchronized (list) {
899         list.add("first element");
900         list.add("second element");
901       }
902       tryReadOnlyRemove(list);
903     }
904   }
905
906   // tryReadOnlyRemove() goes hand in hand with testSetUpRemove().
907
private void tryReadOnlyRemove(List JavaDoc list) {
908     synchronized (list) {
909       try {
910         list.remove("second element");
911         throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
912       } catch (ReadOnlyException t) {
913         // Expected
914
}
915     }
916   }
917
918   void testSetUpToArray(List JavaDoc list, boolean validate) {
919     if (list instanceof Vector JavaDoc) { return; }
920     
921
922     Object JavaDoc[] array = getArray(list);
923     if (validate) {
924       assertEmptyObjectArray(array);
925     } else {
926       synchronized (list) {
927         list.add("first element");
928         list.add("second element");
929       }
930       tryReadOnlyToArray(list);
931     }
932   }
933
934   void tryReadOnlyToArray(List JavaDoc list) {
935     Object JavaDoc[] array = getArray(list);
936     synchronized (array) {
937       try {
938         Object JavaDoc[] returnArray = list.toArray(array);
939         Assert.assertTrue(returnArray == array);
940         throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
941       } catch (ReadOnlyException t) {
942         // Expected
943
}
944     }
945   }
946
947   // Setting up for the ReadOnly test for Iterator remove.
948
void testSetUpIteratorRemove(List JavaDoc list, boolean validate) {
949     if (list instanceof Vector JavaDoc) { return; }
950     
951
952     if (validate) {
953       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element" }), list);
954     } else {
955       synchronized (list) {
956         list.add("first element");
957         list.add("second element");
958       }
959       tryReadOnlyIteratorRemove(list);
960     }
961   }
962
963   // tryReadOnlyIteratorRemove() goes hand in hand with testSetUpIteratorRemove().
964
private void tryReadOnlyIteratorRemove(List JavaDoc list) {
965     synchronized (list) {
966       try {
967         Iterator JavaDoc iterator = list.iterator();
968         iterator.next();
969         iterator.remove();
970         throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
971       } catch (ReadOnlyException t) {
972         // Expected
973
}
974     }
975   }
976
977   // Setting up for the ReadOnly test for clear.
978
void testSetUpClear(List JavaDoc list, boolean validate) {
979     if (list instanceof Vector JavaDoc) { return; }
980     
981
982     if (validate) {
983       assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element" }), list);
984     } else {
985       synchronized (list) {
986         list.add("first element");
987         list.add("second element");
988       }
989       tryReadOnlyClear(list);
990     }
991   }
992
993   // tryReadOnlyClear() goes hand in hand with testSetUpClear().
994
private void tryReadOnlyClear(List JavaDoc list) {
995     synchronized (list) {
996       try {
997         list.clear();
998         throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
999       } catch (ReadOnlyException t) {
1000        // Expected
1001
}
1002    }
1003  }
1004
1005  // Setting up for the ReadOnly test for retainAll.
1006
void testSetUpRetainAll(List JavaDoc list, boolean validate) {
1007    if (list instanceof Vector JavaDoc) { return; }
1008    
1009
1010    if (validate) {
1011      assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element" }), list);
1012    } else {
1013      synchronized (list) {
1014        list.add("first element");
1015        list.add("second element");
1016      }
1017      tryReadOnlyRetainAll(list);
1018    }
1019  }
1020
1021  // tryReadOnlyRetainAll() goes hand in hand with testSetUpRetainAll().
1022
private void tryReadOnlyRetainAll(List JavaDoc list) {
1023    synchronized (list) {
1024      List JavaDoc toRetain = new ArrayList JavaDoc();
1025      toRetain.add("first element");
1026      try {
1027        list.retainAll(toRetain);
1028        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1029      } catch (ReadOnlyException t) {
1030        // Expected
1031
}
1032    }
1033  }
1034
1035  // Setting up for the ReadOnly test for removeAll.
1036
void testSetUpRemoveAll(List JavaDoc list, boolean validate) {
1037    if (list instanceof Vector JavaDoc) { return; }
1038    
1039
1040    if (validate) {
1041      assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element" }), list);
1042    } else {
1043      synchronized (list) {
1044        list.add("first element");
1045        list.add("second element");
1046      }
1047      tryReadOnlyRemoveAll(list);
1048    }
1049  }
1050
1051  // tryReadOnlyRemoveAll() goes hand in hand with testSetUpRemoveAll().
1052
private void tryReadOnlyRemoveAll(List JavaDoc list) {
1053    synchronized (list) {
1054      List JavaDoc toRemove = new ArrayList JavaDoc();
1055      toRemove.add("first element");
1056      try {
1057        list.removeAll(toRemove);
1058        throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1059      } catch (ReadOnlyException e) {
1060        // Expected
1061
}
1062    }
1063  }
1064
1065  void testListIteratorReadOnlyAdd(List JavaDoc list, boolean validate) {
1066    if (list instanceof Vector JavaDoc) { return; }
1067    
1068
1069    if (validate) {
1070      assertEmptyList(list);
1071    } else {
1072      synchronized (list) {
1073        ListIterator JavaDoc lIterator = list.listIterator();
1074        try {
1075          lIterator.add("first element");
1076          throw new AssertionError JavaDoc("Should have thrown a ReadOnlyException");
1077        } catch (ReadOnlyException e) {
1078          // Expected
1079
}
1080      }
1081    }
1082  }
1083
1084  void testCollectionsAddAll(List JavaDoc list, boolean validate) {
1085    
1086    
1087    if (validate) {
1088      assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element", "third element" }), list);
1089    } else {
1090      synchronized (list) {
1091        list.addAll(Arrays.asList(new Object JavaDoc[] { "first element", "second element", "third element" }));
1092      }
1093    }
1094  }
1095
1096  // Iterator testing methods.
1097
void testIteratorRemove(List JavaDoc list, boolean validate) {
1098    
1099    
1100    if (validate) {
1101      assertListsEqual(Arrays.asList(new Object JavaDoc[] { "second element" }), list);
1102    } else {
1103      synchronized (list) {
1104        list.add("first element");
1105        list.add("second element");
1106      }
1107      synchronized (list) {
1108        Iterator JavaDoc iterator = list.iterator();
1109        Assert.assertEquals(true, iterator.hasNext());
1110        iterator.next();
1111        iterator.remove();
1112      }
1113    }
1114  }
1115
1116  void testIteratorDuplicateElementRemove(List JavaDoc list, boolean validate) {
1117    
1118    
1119    if (validate) {
1120      assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element" }), list);
1121    } else {
1122      synchronized (list) {
1123        list.add("first element");
1124        list.add("second element");
1125        list.add("first element");
1126      }
1127      synchronized (list) {
1128        Iterator JavaDoc iterator = list.iterator();
1129        Assert.assertEquals(true, iterator.hasNext());
1130        iterator.next();
1131        iterator.next();
1132        iterator.next();
1133        iterator.remove();
1134      }
1135    }
1136  }
1137
1138  void testIteratorRemoveNull(List JavaDoc list, boolean validate) {
1139    
1140    
1141    if (validate) {
1142      assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", null, "second element" }), list);
1143    } else {
1144      synchronized (list) {
1145        list.add("first element");
1146        list.add(null);
1147        list.add(null);
1148        list.add("second element");
1149      }
1150      synchronized (list) {
1151        Iterator JavaDoc iterator = list.iterator();
1152        Assert.assertEquals(true, iterator.hasNext());
1153        iterator.next();
1154        iterator.next();
1155        iterator.remove();
1156      }
1157    }
1158  }
1159
1160  // Stack specific testing method.
1161
void testStackPush(List JavaDoc list, boolean validate) {
1162    if (!(list instanceof Stack JavaDoc)) { return; }
1163    
1164
1165    if (validate) {
1166      assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element", "second element" }), list);
1167    } else {
1168      synchronized (list) {
1169        Stack JavaDoc s = (Stack JavaDoc) list;
1170        s.push("first element");
1171        s.push("second element");
1172      }
1173    }
1174  }
1175
1176  void testStackPop(List JavaDoc list, boolean validate) {
1177    if (!(list instanceof Stack JavaDoc)) { return; }
1178    
1179
1180    if (validate) {
1181      assertListsEqual(Arrays.asList(new Object JavaDoc[] { "first element" }), list);
1182    } else {
1183      Stack JavaDoc s = (Stack JavaDoc) list;
1184      synchronized (list) {
1185        s.push("first element");
1186        s.push("second element");
1187      }
1188      synchronized (list) {
1189        Object JavaDoc o = s.pop();
1190        Assert.assertEquals("second element", o);
1191      }
1192    }
1193  }
1194  
1195  void testAddNonPortableObject(List JavaDoc list, boolean validate) {
1196    if (!validate) {
1197      synchronized (list) {
1198        try {
1199          list.add(new MyArrayList2());
1200          throw new AssertionError JavaDoc("Should have thrown a TCNonPortableObjectError.");
1201        } catch (TCNonPortableObjectError e) {
1202          // expected
1203
}
1204      }
1205      synchronized (list) {
1206        try {
1207          list.add(new MyArrayList3());
1208          throw new AssertionError JavaDoc("Should have thrown a TCNonPortableObjectError.");
1209        } catch (TCNonPortableObjectError e) {
1210          // expected
1211
}
1212      }
1213    }
1214  }
1215  
1216  private Object JavaDoc getMySubclassArray(List JavaDoc list) {
1217    if (list instanceof MyArrayList6) { return (Object JavaDoc[])sharedMap.get("arrayforMyArrayList6"); }
1218    if (list instanceof MyArrayList5) { return (Object JavaDoc[])sharedMap.get("arrayforMyArrayList5"); }
1219    if (list instanceof MyArrayList) { return (Object JavaDoc[])sharedMap.get("arrayforMyArrayList"); }
1220    if (list instanceof MyLinkedList) { return (Object JavaDoc[])sharedMap.get("arrayforMyLinkedList"); }
1221    if (list instanceof MyVector) { return (Object JavaDoc[])sharedMap.get("arrayforMyVector"); }
1222    if (list instanceof MyStack) { return (Object JavaDoc[])sharedMap.get("arrayforMyStack"); }
1223    if (list instanceof MyAbstractListSubclass) { return (Object JavaDoc[])sharedMap.get("arrayforMyAbstractListSubclass"); }
1224    return null;
1225  }
1226
1227  private Object JavaDoc[] getArray(List JavaDoc list) {
1228    Object JavaDoc o = getMySubclassArray(list);
1229    if (o != null) { return (Object JavaDoc[]) o; }
1230    
1231    if (list instanceof LinkedList JavaDoc) { return (Object JavaDoc[]) sharedMap.get("arrayforLinkedList"); }
1232    if (list instanceof ArrayList JavaDoc) { return (Object JavaDoc[]) sharedMap.get("arrayforArrayList"); }
1233    if (list instanceof Stack JavaDoc) { // need to check instanceof Stack first before checking instance of Vector
1234
// as Stack is a subclass of Vector.
1235
return (Object JavaDoc[]) sharedMap.get("arrayforStack");
1236    }
1237    if (list instanceof Vector JavaDoc) { return (Object JavaDoc[]) sharedMap.get("arrayforVector"); }
1238    if (list instanceof MyAbstractListSubclass) { return (Object JavaDoc[]) sharedMap.get("arrayforAbstractListSubclass"); }
1239    return null;
1240  }
1241
1242  private static void assertEmptyObjectArray(Object JavaDoc[] array) {
1243    for (int i = 0; i < array.length; i++) {
1244      Assert.assertNull(array[i]);
1245    }
1246  }
1247
1248  private static void assertEmptyList(List JavaDoc list) {
1249    Assert.assertEquals(0, list.size());
1250    Assert.assertTrue(list.isEmpty());
1251
1252    int count = 0;
1253    for (Iterator JavaDoc i = list.iterator(); i.hasNext();) {
1254      count++;
1255    }
1256    Assert.assertEquals(0, count);
1257  }
1258
1259  private static void assertListsEqual(List JavaDoc expect, List JavaDoc actual) {
1260    Assert.assertEquals(expect.size(), actual.size());
1261
1262    Assert.assertTrue(expect.containsAll(actual));
1263    Assert.assertTrue(actual.containsAll(expect));
1264
1265    for (int i = 0, n = expect.size(); i < n; i++) {
1266      Assert.assertEquals(expect.get(i), actual.get(i));
1267    }
1268
1269    if (expect.isEmpty()) {
1270      Assert.assertTrue(actual.isEmpty());
1271    } else {
1272      Assert.assertFalse(actual.isEmpty());
1273    }
1274
1275    for (Iterator JavaDoc iExpect = expect.iterator(), iActual = actual.iterator(); iExpect.hasNext();) {
1276      Assert.assertEquals(iExpect.next(), iActual.next());
1277    }
1278
1279  }
1280
1281  public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
1282    String JavaDoc testClass = GenericListTestApp.class.getName();
1283    config.getOrCreateSpec(testClass);
1284    String JavaDoc writeAllowedMethodExpression = "* " + testClass + "*.*(..)";
1285    config.addWriteAutolock(writeAllowedMethodExpression);
1286    String JavaDoc readOnlyMethodExpression = "* " + testClass + "*.*ReadOnly*(..)";
1287    config.addReadAutolock(readOnlyMethodExpression);
1288    config.addIncludePattern(testClass + "$*");
1289  }
1290
1291  private static void assertSingleElement(List JavaDoc list, Object JavaDoc obj) {
1292    Assert.assertEquals(1, list.size());
1293    Assert.assertEquals(obj, list.get(0));
1294    Assert.assertFalse(list.isEmpty());
1295    Assert.assertTrue(list.contains(obj));
1296
1297    int count = 0;
1298    for (Iterator JavaDoc i = list.iterator(); i.hasNext();) {
1299      count++;
1300      Assert.assertEquals(obj, i.next());
1301    }
1302    Assert.assertEquals(1, count);
1303
1304  }
1305  
1306  private static class MyArrayList extends ArrayList JavaDoc {
1307    public MyArrayList() {
1308      super();
1309    }
1310  }
1311  
1312  private static class MyArrayList2 extends ArrayList JavaDoc {
1313    private Vector JavaDoc v;
1314
1315    protected void removeRange(int fromIndex, int toIndex) {
1316      super.removeRange(fromIndex, toIndex);
1317    }
1318  }
1319
1320  private static class MyArrayList3 extends ArrayList JavaDoc {
1321    private Vector JavaDoc v;
1322
1323    public void removeRangeLocal(int fromIndex, int toIndex) {
1324      super.removeRange(fromIndex, toIndex);
1325    }
1326  }
1327  
1328  private static class MyArrayList4 extends ArrayList JavaDoc {
1329    //
1330
}
1331  
1332  private static class MyArrayList5 extends MyArrayList4 {
1333    //
1334
}
1335  
1336  private static class MyArrayList6 extends ArrayList JavaDoc {
1337
1338    int i = 3;
1339
1340    MyArrayList6() {
1341      Set JavaDoc s = new HashSet JavaDoc();
1342      s.add("test");
1343      new ArrayList JavaDoc(s);
1344      if (size() != 0) { throw new AssertionError JavaDoc(); }
1345    }
1346    
1347    MyArrayList6(Set JavaDoc s1) {
1348      super(s1);
1349      Set JavaDoc s = new HashSet JavaDoc();
1350      s.add("test");
1351      ArrayList JavaDoc l = new ArrayList JavaDoc(s);
1352      if (size() != 0) { throw new AssertionError JavaDoc(); }
1353    }
1354  }
1355
1356  private static class MyLinkedList extends LinkedList JavaDoc {
1357    public MyLinkedList() {
1358      super();
1359    }
1360  }
1361
1362  private static class MyVector extends Vector JavaDoc {
1363    public MyVector() {
1364      super();
1365    }
1366  }
1367
1368  private static class MyStack extends Stack JavaDoc {
1369    public MyStack() {
1370      super();
1371    }
1372  }
1373
1374  private static class MyAbstractListSubclass extends AbstractList JavaDoc {
1375    // This is in here to make sure that a subclass of AbstractList is shareable in DSO, not that this is a good/proper
1376
// List implementation ;-)
1377

1378    private ArrayList JavaDoc data = new ArrayList JavaDoc();
1379
1380    public void add(int index, Object JavaDoc element) {
1381      data.add(index, element);
1382    }
1383
1384    public Object JavaDoc set(int index, Object JavaDoc element) {
1385      return data.set(index, element);
1386    }
1387
1388    public Object JavaDoc get(int index) {
1389      return data.get(index);
1390    }
1391
1392    public int size() {
1393      return data.size();
1394    }
1395
1396    public Object JavaDoc remove(int index) {
1397      return data.remove(index);
1398    }
1399
1400  }
1401
1402}
1403
Popular Tags