0001: /*
0002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
0003: * notice. All rights reserved.
0004: */
0005: package com.tctest;
0006:
0007: import com.tc.exception.TCNonPortableObjectError;
0008: import com.tc.object.config.ConfigVisitor;
0009: import com.tc.object.config.DSOClientConfigHelper;
0010: import com.tc.object.util.ReadOnlyException;
0011: import com.tc.simulator.app.ApplicationConfig;
0012: import com.tc.simulator.listener.ListenerProvider;
0013: import com.tc.util.Assert;
0014:
0015: import java.lang.reflect.Method;
0016: import java.util.AbstractList;
0017: import java.util.ArrayList;
0018: import java.util.Arrays;
0019: import java.util.HashSet;
0020: import java.util.Iterator;
0021: import java.util.LinkedList;
0022: import java.util.List;
0023: import java.util.ListIterator;
0024: import java.util.Set;
0025: import java.util.Stack;
0026: import java.util.Vector;
0027:
0028: public class GenericListTestApp extends GenericTestApp {
0029:
0030: private static final int LITERAL_VARIANT = 1;
0031: private static final int OBJECT_VARIANT = 2;
0032:
0033: public GenericListTestApp(String appId, ApplicationConfig cfg,
0034: ListenerProvider listenerProvider) {
0035: super (appId, cfg, listenerProvider, List.class, 2);
0036: }
0037:
0038: protected Object getTestObject(String testName) {
0039: List lists = (List) sharedMap.get("lists");
0040: return lists.iterator();
0041: }
0042:
0043: // This method is kind of like a macro, it returns an element (E == element) to be used
0044: // in the set based on the variant value
0045: Object E(String val, int variant) {
0046: switch (variant) {
0047: case LITERAL_VARIANT: {
0048: return val;
0049: }
0050: case OBJECT_VARIANT: {
0051: return new Foo(val);
0052: }
0053: default: {
0054: throw new AssertionError("unknown variant: " + variant);
0055: }
0056: }
0057:
0058: // unreachable
0059: }
0060:
0061: protected void setupTestObject(String testName) {
0062: List lists = new ArrayList();
0063: lists.add(new LinkedList());
0064: lists.add(new ArrayList());
0065: lists.add(new Vector());
0066: lists.add(new Stack());
0067: lists.add(new MyArrayList());
0068: lists.add(new MyArrayList5());
0069: lists.add(new MyArrayList6());
0070: lists.add(new MyLinkedList());
0071: lists.add(new MyVector());
0072: lists.add(new MyStack());
0073: lists.add(new MyAbstractListSubclass());
0074:
0075: sharedMap.put("lists", lists);
0076: sharedMap.put("arrayforLinkedList", new Object[2]);
0077: sharedMap.put("arrayforArrayList", new Object[2]);
0078: sharedMap.put("arrayforVector", new Object[2]);
0079: sharedMap.put("arrayforStack", new Object[2]);
0080: sharedMap.put("arrayforAbstractListSubclass", new Object[2]);
0081: sharedMap.put("arrayforMyArrayList", new Object[2]);
0082: sharedMap.put("arrayforMyArrayList5", new Object[2]);
0083: sharedMap.put("arrayforMyArrayList6", new Object[2]);
0084: sharedMap.put("arrayforMyLinkedList", new Object[2]);
0085: sharedMap.put("arrayforMyVector", new Object[2]);
0086: sharedMap.put("arrayforMyStack", new Object[2]);
0087: sharedMap.put("arrayforMyAbstractListSubclass", new Object[2]);
0088: }
0089:
0090: void testBasicAdd(List list, boolean validate, int v) {
0091: if (validate) {
0092: assertSingleElement(list, E("rollin in my 6-4", v));
0093: } else {
0094: synchronized (list) {
0095: boolean added = list.add(E("rollin in my 6-4", v));
0096: Assert.assertTrue(added);
0097: }
0098: }
0099: }
0100:
0101: void testBasicRemove(List list, boolean validate, int v) {
0102: if (validate) {
0103: assertEmptyList(list);
0104: } else {
0105: synchronized (list) {
0106: boolean added = list.add(E(
0107: "http://en.wikipedia.org/wiki/PEBKAC", v));
0108: Assert.assertTrue(added);
0109: }
0110:
0111: synchronized (list) {
0112: boolean removed = list.remove(E(
0113: "http://en.wikipedia.org/wiki/PEBKAC", v));
0114: Assert.assertTrue(removed);
0115: }
0116:
0117: }
0118: }
0119:
0120: void testVectorSetSizeGrow(List list, boolean validate, int v) {
0121: if (!(list instanceof Vector)) {
0122: return;
0123: }
0124:
0125: int size = 5;
0126: Vector vector = (Vector) list;
0127:
0128: if (validate) {
0129: Assert.assertEquals(E("start", v), vector.get(0));
0130: for (int i = 1; i < size; i++) {
0131: Object val = vector.get(i);
0132: Assert.assertNull("element " + i + " is " + val, val);
0133: }
0134: Assert.assertEquals(E("end", v), vector.get(size));
0135: } else {
0136: synchronized (vector) {
0137: vector.add(E("start", v));
0138: vector.setSize(size);
0139: vector.add(E("end", v));
0140: }
0141: }
0142: }
0143:
0144: void testVectorSetSizeShrink(List list, boolean validate, int v) {
0145: if (!(list instanceof Vector)) {
0146: return;
0147: }
0148:
0149: Vector vector = (Vector) list;
0150:
0151: if (validate) {
0152: Assert.assertEquals(E("start", v), vector.get(0));
0153: Assert.assertEquals(E("end", v), vector.get(1));
0154: } else {
0155: synchronized (vector) {
0156: vector.add(E("start", v));
0157: vector.add(E("ho hum", v));
0158: vector.add(E("ho hum2", v));
0159: vector.add(E("ho hum3", v));
0160:
0161: vector.setSize(1);
0162: vector.add(E("end", v));
0163: }
0164: }
0165: }
0166:
0167: void testVectorAddElement(List list, boolean validate, int v) {
0168: if (!(list instanceof Vector)) {
0169: return;
0170: }
0171:
0172: Vector vector = (Vector) list;
0173:
0174: if (validate) {
0175: assertListsEqual(Arrays.asList(new Object[] {
0176: E("first element", v), E("second element", v) }),
0177: vector);
0178: } else {
0179: synchronized (vector) {
0180: vector.addElement(E("first element", v));
0181: vector.addElement(E("second element", v));
0182: }
0183: }
0184: }
0185:
0186: void testVectorRetainAll(List list, boolean validate, int v) {
0187: if (!(list instanceof Vector)) {
0188: return;
0189: }
0190:
0191: Vector vector = (Vector) list;
0192:
0193: if (validate) {
0194: assertListsEqual(Arrays.asList(new Object[] { E(
0195: "second element", v) }), vector);
0196: } else {
0197:
0198: synchronized (vector) {
0199: vector.addElement(E("first element", v));
0200: vector.addElement(E("second element", v));
0201: vector.addElement(E("third element", v));
0202:
0203: List retainList = new ArrayList(1);
0204: retainList.add(E("second element", v));
0205:
0206: vector.retainAll(retainList);
0207: }
0208: }
0209: }
0210:
0211: void testVectorRemoveAll(List list, boolean validate, int v) {
0212: if (!(list instanceof Vector)) {
0213: return;
0214: }
0215:
0216: Vector vector = (Vector) list;
0217:
0218: if (validate) {
0219: assertListsEqual(Arrays.asList(new Object[] {
0220: E("first element", v), E("third element", v) }),
0221: vector);
0222: } else {
0223: synchronized (vector) {
0224: vector.addElement(E("first element", v));
0225: vector.addElement(E("second element", v));
0226: vector.addElement(E("third element", v));
0227:
0228: List removeList = new ArrayList(1);
0229: removeList.add(E("second element", v));
0230: vector.removeAll(removeList);
0231: }
0232: }
0233: }
0234:
0235: void testVectorRemoveAllElements(List list, boolean validate, int v) {
0236: if (!(list instanceof Vector)) {
0237: return;
0238: }
0239:
0240: Vector vector = (Vector) list;
0241:
0242: if (validate) {
0243: assertEmptyList(vector);
0244: } else {
0245: synchronized (vector) {
0246: vector.addElement(E("first element", v));
0247: vector.addElement(E("second element", v));
0248: vector.addElement(E("third element", v));
0249:
0250: vector.removeAllElements();
0251: }
0252: }
0253: }
0254:
0255: void testVectorSetElementAt(List list, boolean validate, int v) {
0256: if (!(list instanceof Vector)) {
0257: return;
0258: }
0259:
0260: Vector vector = (Vector) list;
0261:
0262: if (validate) {
0263: assertListsEqual(Arrays.asList(new Object[] {
0264: E("first element", v), E("second element", v),
0265: E("third element", v) }), vector);
0266: } else {
0267: synchronized (vector) {
0268: vector.addElement(E("first element", v));
0269: vector.addElement(null);
0270: vector.addElement(E("third element", v));
0271:
0272: vector.setElementAt(E("second element", v), 1);
0273: }
0274: }
0275: }
0276:
0277: void testVectorInsertElementAt(List list, boolean validate, int v) {
0278: if (!(list instanceof Vector)) {
0279: return;
0280: }
0281:
0282: Vector vector = (Vector) list;
0283:
0284: if (validate) {
0285: assertListsEqual(Arrays.asList(new Object[] {
0286: E("first element", v), E("second element", v),
0287: E("third element", v) }), vector);
0288: } else {
0289: synchronized (vector) {
0290: vector.addElement(E("first element", v));
0291: vector.addElement(E("third element", v));
0292:
0293: vector.insertElementAt(E("second element", v), 1);
0294: }
0295: }
0296: }
0297:
0298: void testLinkedListRemoveFirst(List list, boolean validate, int v) {
0299: if (!(list instanceof LinkedList)) {
0300: return;
0301: }
0302:
0303: LinkedList linkedList = (LinkedList) list;
0304:
0305: if (validate) {
0306: assertSingleElement(linkedList, E("teck", v));
0307: } else {
0308: synchronized (linkedList) {
0309: linkedList.add(E("timmy", v));
0310: linkedList.add(E("teck", v));
0311: }
0312:
0313: synchronized (linkedList) {
0314: linkedList.removeFirst();
0315: }
0316: }
0317: }
0318:
0319: void testLinkedListRemoveLast(List list, boolean validate, int v) {
0320: if (!(list instanceof LinkedList)) {
0321: return;
0322: }
0323:
0324: LinkedList linkedList = (LinkedList) list;
0325:
0326: if (validate) {
0327: assertSingleElement(linkedList, E("timmy", v));
0328: } else {
0329: synchronized (linkedList) {
0330: linkedList.add(E("timmy", v));
0331: linkedList.add(E("teck", v));
0332: }
0333:
0334: synchronized (linkedList) {
0335: linkedList.removeLast();
0336: }
0337: }
0338: }
0339:
0340: void testLinkedListAddFirst(List list, boolean validate, int v) {
0341: if (!(list instanceof LinkedList)) {
0342: return;
0343: }
0344:
0345: LinkedList linkedList = (LinkedList) list;
0346:
0347: if (validate) {
0348: assertListsEqual(Arrays.asList(new Object[] {
0349: E("first element", v), E("second element", v) }),
0350: list);
0351: } else {
0352: synchronized (linkedList) {
0353: linkedList.add(E("second element", v));
0354: }
0355:
0356: synchronized (linkedList) {
0357: linkedList.addFirst(E("first element", v));
0358: }
0359: }
0360: }
0361:
0362: void testLinkedListAddLast(List list, boolean validate, int v) {
0363: if (!(list instanceof LinkedList)) {
0364: return;
0365: }
0366:
0367: LinkedList linkedList = (LinkedList) list;
0368:
0369: if (validate) {
0370: assertListsEqual(Arrays.asList(new Object[] {
0371: E("first element", v), E("second element", v) }),
0372: list);
0373: } else {
0374: synchronized (linkedList) {
0375: linkedList.add(E("first element", v));
0376: }
0377:
0378: synchronized (linkedList) {
0379: linkedList.addLast(E("second element", v));
0380: }
0381: }
0382: }
0383:
0384: void testBasicAddNull(List list, boolean validate, int v) {
0385:
0386: if (validate) {
0387: assertListsEqual(Arrays.asList(new Object[] { null, null,
0388: E("my cat hates you", v), null }), list);
0389: } else {
0390: synchronized (list) {
0391: boolean added;
0392: added = list.add(null);
0393: Assert.assertTrue(added);
0394: added = list.add(null);
0395: Assert.assertTrue(added);
0396: added = list.add(E("my cat hates you", v));
0397: Assert.assertTrue(added);
0398: added = list.add(null);
0399: Assert.assertTrue(added);
0400: }
0401: }
0402: }
0403:
0404: void testBasicAddAt(List list, boolean validate, int v) {
0405:
0406: if (validate) {
0407: assertListsEqual(Arrays.asList(new Object[] { E("1", v),
0408: E("2", v), E("3", v), E("4", v) }), list);
0409: } else {
0410: synchronized (list) {
0411: list.add(0, E("2", v));
0412: }
0413: synchronized (list) {
0414: list.add(0, E("1", v));
0415: }
0416: synchronized (list) {
0417: list.add(2, E("4", v));
0418: }
0419: synchronized (list) {
0420: list.add(2, E("3", v));
0421: }
0422: }
0423: }
0424:
0425: void testAdd(List list, boolean validate, int v) {
0426:
0427: if (validate) {
0428: assertListsEqual(Arrays.asList(new Object[] { E("element",
0429: v) }), list);
0430: } else {
0431: synchronized (list) {
0432: list.add(E("element", v));
0433: }
0434: }
0435: }
0436:
0437: void testAddAll(List list, boolean validate, int v) {
0438:
0439: if (validate) {
0440: assertListsEqual(Arrays.asList(new Object[] {
0441: E("patty", v), E("calahan", v), E("was", v),
0442: E("here", v) }), list);
0443: } else {
0444: List toAdd = new ArrayList();
0445: toAdd.add(E("patty", v));
0446: toAdd.add(E("calahan", v));
0447: toAdd.add(E("was", v));
0448: toAdd.add(E("here", v));
0449:
0450: synchronized (list) {
0451: list.addAll(toAdd);
0452: }
0453: }
0454: }
0455:
0456: void testAddAllAt(List list, boolean validate, int v) {
0457:
0458: if (validate) {
0459: assertListsEqual(Arrays.asList(new Object[] { E("uno", v),
0460: E("dos", v), E("tres", v), E("catorce?", v) }),
0461: list);
0462: } else {
0463: synchronized (list) {
0464: list.add(E("uno", v));
0465: }
0466:
0467: List toAdd = new ArrayList();
0468: toAdd.add(E("dos", v));
0469: toAdd.add(E("tres", v));
0470: toAdd.add(E("catorce?", v));
0471:
0472: synchronized (list) {
0473: list.addAll(1, toAdd);
0474: }
0475: }
0476: }
0477:
0478: void testClear(List list, boolean validate, int v) {
0479:
0480: if (validate) {
0481: assertEmptyList(list);
0482: } else {
0483: synchronized (list) {
0484: list.add(E("clear me baby", v));
0485: list.clear();
0486: }
0487:
0488: synchronized (list) {
0489: list.add(E("clear me baby one more time", v));
0490: }
0491:
0492: synchronized (list) {
0493: list.clear();
0494: }
0495: }
0496: }
0497:
0498: void testSetElementAt(List list, boolean validate, int v) {
0499:
0500: if (validate) {
0501: assertSingleElement(list, E("new", v));
0502: } else {
0503: synchronized (list) {
0504: list.add(E("orig", v));
0505: }
0506:
0507: synchronized (list) {
0508: list.set(0, E("new", v));
0509: }
0510: }
0511: }
0512:
0513: void testRemoveAt(List list, boolean validate, int v) {
0514:
0515: if (validate) {
0516: Object item0 = list.get(0);
0517: Object item1 = list.get(1);
0518: Assert.assertEquals(E("value", v), item0);
0519: Assert.assertEquals(E("different value", v), item1);
0520: } else {
0521: synchronized (list) {
0522: list.add(E("value", v));
0523: list.add(E("different value", v));
0524: list.add(E("value", v));
0525: }
0526:
0527: synchronized (list) {
0528: Object prev = list.remove(2);
0529: Assert.assertEquals(E("value", v), prev);
0530: }
0531: }
0532: }
0533:
0534: void testRemoveNull1(List list, boolean validate, int v) {
0535:
0536: if (validate) {
0537: assertListsEqual(Arrays
0538: .asList(new Object[] { E("first element", v), null,
0539: E("third element", v) }), list);
0540: } else {
0541: synchronized (list) {
0542: list.add(E("first element", v));
0543: list.add(null);
0544: list.add(null);
0545: list.add(E("third element", v));
0546: }
0547: synchronized (list) {
0548: list.remove(null);
0549: }
0550: }
0551: }
0552:
0553: void testRemoveNull2(List list, boolean validate, int v) {
0554:
0555: if (validate) {
0556: assertListsEqual(Arrays.asList(new Object[] {
0557: E("first element", v), E("second element", v) }),
0558: list);
0559: } else {
0560: synchronized (list) {
0561: list.add(E("first element", v));
0562: list.add(null);
0563: list.add(E("second element", v));
0564: list.add(null);
0565: }
0566: synchronized (list) {
0567: list.remove(null);
0568: list.remove(null);
0569: }
0570: }
0571: }
0572:
0573: void testSubList(List list, boolean validate, int v) {
0574:
0575: if (validate) {
0576: assertListsEqual(Arrays.asList(new Object[] {
0577: E("first element", v), E("second element", v),
0578: E("third element", v), E("fourth element", v) }),
0579: list);
0580: } else {
0581: synchronized (list) {
0582: list.add(E("first element", v));
0583: list.add(E("third element", v));
0584: list.add(E("fourth element", v));
0585: }
0586: List subList = list.subList(1, 2);
0587: ListIterator listIterator = subList.listIterator();
0588: synchronized (list) {
0589: listIterator.add(E("second element", v));
0590: }
0591: }
0592: }
0593:
0594: void testRemoveAll(List list, boolean validate, int v) {
0595:
0596: if (validate) {
0597: assertEmptyList(list);
0598: } else {
0599: synchronized (list) {
0600: list.add(E("first element", v));
0601: list.add(E("second element", v));
0602: }
0603: List removeList = new ArrayList(2);
0604: removeList.add(E("first element", v));
0605: removeList.add(E("second element", v));
0606: synchronized (list) {
0607: list.removeAll(removeList);
0608: }
0609: }
0610: }
0611:
0612: void testRemoveRange(List list, boolean validate, int v) {
0613: // using reflection to invoke protected method of a logical subclass does not work.
0614: if (list instanceof MyArrayList6) {
0615: return;
0616: }
0617:
0618: if (validate) {
0619: assertListsEqual(Arrays.asList(new Object[] {
0620: E("first element", v), E("fourth element", v) }),
0621: list);
0622: } else {
0623: synchronized (list) {
0624: list.add(E("first element", v));
0625: list.add(E("second element", v));
0626: list.add(E("third element", v));
0627: list.add(E("fourth element", v));
0628: }
0629: Class listClass = AbstractList.class;
0630: Class[] parameterType = new Class[2];
0631: parameterType[0] = Integer.TYPE;
0632: parameterType[1] = Integer.TYPE;
0633:
0634: try {
0635: synchronized (list) {
0636: Method m = listClass.getDeclaredMethod(
0637: "removeRange", parameterType);
0638: m.setAccessible(true); // suppressing java access checking since removeRange is
0639: // a protected method.
0640: m.invoke(list, new Object[] { new Integer(1),
0641: new Integer(3) });
0642: }
0643: } catch (Exception e) {
0644: // ignore Exception in test.
0645: }
0646: }
0647: }
0648:
0649: void testToArray(List list, boolean validate, int v) {
0650:
0651: Object[] array = getArray(list);
0652:
0653: if (validate) {
0654: assertListsEqual(Arrays.asList(array), list);
0655: } else {
0656: synchronized (list) {
0657: list.add(E("first element", v));
0658: list.add(E("second element", v));
0659: }
0660: synchronized (array) {
0661: Object[] returnArray = list.toArray(array);
0662: Assert.assertTrue(returnArray == array);
0663: }
0664: }
0665: }
0666:
0667: void testToArray2(List list, boolean validate, int v) {
0668: Object[] array = getArray(list);
0669:
0670: if (validate) {
0671: Assert.assertEquals(list.getClass(), E("January", v),
0672: array[0]);
0673: Assert.assertEquals(list.getClass(), null, array[1]);
0674: } else {
0675: synchronized (list) {
0676: list.add(E("January", v));
0677: }
0678:
0679: // ensure that the array is bigger than the list size
0680: // This test case makes sure the array get's null terminated
0681: Assert.assertEquals(1, list.size());
0682: Assert.assertEquals(2, array.length);
0683:
0684: // make sure the array contains no nulls
0685: synchronized (array) {
0686: Arrays.fill(array, new Object());
0687: }
0688:
0689: synchronized (array) {
0690: Object[] returnArray = list.toArray(array);
0691: Assert.assertTrue(returnArray == array);
0692: }
0693: }
0694: }
0695:
0696: // List Iterator testing methods.
0697: void testListIteratorSet1(List list, boolean validate, int v) {
0698:
0699: if (validate) {
0700: assertListsEqual(Arrays.asList(new Object[] {
0701: E("modified first element", v),
0702: E("second element", v), E("third element", v) }),
0703: list);
0704: } else {
0705: synchronized (list) {
0706: list.add(E("first element", v));
0707: list.add(E("second element", v));
0708: list.add(E("third element", v));
0709: }
0710: synchronized (list) {
0711: ListIterator lIterator = list.listIterator();
0712: lIterator.next();
0713: lIterator.set(E("modified first element", v));
0714: }
0715: }
0716: }
0717:
0718: void testListIteratorSet2(List list, boolean validate, int v) {
0719:
0720: if (validate) {
0721: assertListsEqual(Arrays.asList(new Object[] {
0722: E("first element", v),
0723: E("modified second element", v),
0724: E("third element", v) }), list);
0725: } else {
0726: synchronized (list) {
0727: list.add(E("first element", v));
0728: list.add(E("second element", v));
0729: list.add(E("third element", v));
0730: }
0731: synchronized (list) {
0732: ListIterator lIterator = list.listIterator();
0733: lIterator.next();
0734: lIterator.next();
0735: lIterator.set(E("modified second element", v));
0736: }
0737: }
0738: }
0739:
0740: void testListIteratorSetRemove1(List list, boolean validate, int v) {
0741:
0742: if (validate) {
0743: assertListsEqual(Arrays.asList(new Object[] {
0744: E("modified first element", v),
0745: E("third element", v) }), list);
0746: } else {
0747: synchronized (list) {
0748: list.add(E("first element", v));
0749: list.add(E("second element", v));
0750: list.add(E("third element", v));
0751: }
0752: synchronized (list) {
0753: ListIterator lIterator = list.listIterator();
0754: lIterator.next();
0755: lIterator.next();
0756: lIterator.remove();
0757: lIterator.previous();
0758: lIterator.set(E("modified first element", v));
0759: }
0760: }
0761: }
0762:
0763: void testListIteratorSetRemove2(List list, boolean validate, int v) {
0764:
0765: if (validate) {
0766: assertListsEqual(Arrays.asList(new Object[] {
0767: E("first element", v),
0768: E("modified second element", v) }), list);
0769: } else {
0770: synchronized (list) {
0771: list.add(E("first element", v));
0772: list.add(E("second element", v));
0773: list.add(E("third element", v));
0774: }
0775: synchronized (list) {
0776: ListIterator lIterator = list.listIterator();
0777: lIterator.next();
0778: lIterator.next();
0779: lIterator.set(E("modified second element", v));
0780: lIterator.next();
0781: lIterator.remove();
0782: }
0783: }
0784: }
0785:
0786: void testListIteratorDuplicateElementRemove(List list,
0787: boolean validate, int v) {
0788:
0789: if (validate) {
0790: assertListsEqual(Arrays.asList(new Object[] {
0791: E("first element", v), E("second element", v) }),
0792: list);
0793: } else {
0794: synchronized (list) {
0795: list.add(E("first element", v));
0796: list.add(E("second element", v));
0797: list.add(E("first element", v));
0798: }
0799: synchronized (list) {
0800: ListIterator lIterator = list.listIterator();
0801: lIterator.next();
0802: lIterator.next();
0803: lIterator.next();
0804: lIterator.remove();
0805: }
0806: }
0807: }
0808:
0809: void testListIteratorAdd1(List list, boolean validate, int v) {
0810:
0811: if (validate) {
0812: assertListsEqual(Arrays.asList(new Object[] {
0813: E("first element", v), E("second element", v),
0814: E("third element", v) }), list);
0815: // assertListsEqual(Arrays.asList(new Object[] { E("second element", v), E("third element", v) }), list);
0816: } else {
0817: synchronized (list) {
0818: list.add(E("second element", v));
0819: list.add(E("third element", v));
0820: }
0821: synchronized (list) {
0822: ListIterator lIterator = list.listIterator();
0823: lIterator.add(E("first element", v));
0824: }
0825: }
0826: }
0827:
0828: void testListIteratorAdd2(List list, boolean validate, int v) {
0829:
0830: if (validate) {
0831: assertListsEqual(Arrays.asList(new Object[] {
0832: E("first element", v), E("second element", v),
0833: E("third element", v) }), list);
0834: } else {
0835: synchronized (list) {
0836: list.add(E("first element", v));
0837: list.add(E("third element", v));
0838: }
0839: synchronized (list) {
0840: ListIterator lIterator = list.listIterator();
0841: lIterator.next();
0842: lIterator.add(E("second element", v));
0843: }
0844: }
0845: }
0846:
0847: void testListIteratorAddSet1(List list, boolean validate, int v) {
0848:
0849: if (validate) {
0850: assertListsEqual(Arrays.asList(new Object[] {
0851: E("modified first element", v),
0852: E("second element", v), E("third element", v) }),
0853: list);
0854: } else {
0855: synchronized (list) {
0856: list.add(E("second element", v));
0857: list.add(E("third element", v));
0858: }
0859: synchronized (list) {
0860: ListIterator lIterator = list.listIterator();
0861: lIterator.add(E("first element", v));
0862: lIterator.previous();
0863: lIterator.set(E("modified first element", v));
0864: }
0865: }
0866: }
0867:
0868: void testListIteratorAddSet2(List list, boolean validate, int v) {
0869:
0870: if (validate) {
0871: assertListsEqual(Arrays.asList(new Object[] {
0872: E("first element", v), E("second element", v),
0873: E("modified third element", v) }), list);
0874: } else {
0875: synchronized (list) {
0876: list.add(E("first element", v));
0877: list.add(E("third element", v));
0878: }
0879: synchronized (list) {
0880: ListIterator lIterator = list.listIterator();
0881: lIterator.next();
0882: lIterator.add(E("second element", v));
0883: lIterator.next();
0884: lIterator.set(E("modified third element", v));
0885: }
0886: }
0887: }
0888:
0889: void testListIteratorAddSet3(List list, boolean validate, int v) {
0890:
0891: if (validate) {
0892: assertListsEqual(Arrays.asList(new Object[] {
0893: E("first element", v), E("second element", v),
0894: E("modified third element", v),
0895: E("fourth element", v) }), list);
0896: } else {
0897: synchronized (list) {
0898: list.add(E("first element", v));
0899: list.add(E("second element", v));
0900: list.add(E("fourth element", v));
0901: }
0902: synchronized (list) {
0903: ListIterator lIterator = list.listIterator(1);
0904: lIterator.next();
0905: lIterator.add(E("third element", v));
0906: lIterator.previous();
0907: lIterator.set(E("modified third element", v));
0908: }
0909: }
0910: }
0911:
0912: void testListIteratorAddNull(List list, boolean validate, int v) {
0913:
0914: if (validate) {
0915: assertListsEqual(Arrays.asList(new Object[] { null, null,
0916: E("third element", v) }), list);
0917: } else {
0918: synchronized (list) {
0919: ListIterator lIterator = list.listIterator();
0920: lIterator.add(null);
0921: lIterator.add(null);
0922: lIterator.add(E("third element", v));
0923: }
0924: }
0925: }
0926:
0927: void testListIteratorAddRemove(List list, boolean validate, int v) {
0928:
0929: if (validate) {
0930: assertListsEqual(Arrays.asList(new Object[] {
0931: E("second element", v), E("third element", v) }),
0932: list);
0933: } else {
0934: synchronized (list) {
0935: list.add(E("second element", v));
0936: list.add(E("third element", v));
0937: }
0938: synchronized (list) {
0939: ListIterator lIterator = list.listIterator();
0940: lIterator.add(E("first element", v));
0941: lIterator.previous();
0942: lIterator.remove();
0943: }
0944: }
0945: }
0946:
0947: void testListIteratorRemoveNull(List list, boolean validate, int v) {
0948:
0949: if (validate) {
0950: assertListsEqual(Arrays
0951: .asList(new Object[] { E("first element", v), null,
0952: E("third element", v) }), list);
0953: } else {
0954: synchronized (list) {
0955: list.add(E("first element", v));
0956: list.add(null);
0957: list.add(null);
0958: list.add(E("third element", v));
0959: }
0960: synchronized (list) {
0961: ListIterator lIterator = list.listIterator();
0962: lIterator.next();
0963: lIterator.next();
0964: lIterator.remove();
0965: }
0966: }
0967: }
0968:
0969: // Read only testing methods.
0970: void testReadOnlyAdd(List list, boolean validate, int v) {
0971:
0972: if (list instanceof Vector) {
0973: return;
0974: }
0975:
0976: if (validate) {
0977: assertEmptyList(list);
0978: } else {
0979: synchronized (list) {
0980: try {
0981: list.add(E("first element", v));
0982: throw new AssertionError(
0983: "Should have thrown a ReadOnlyException");
0984: } catch (ReadOnlyException t) {
0985: // Expected
0986: }
0987: }
0988: }
0989: }
0990:
0991: void testReadOnlySet(List list, boolean validate, int v) {
0992: if (list instanceof Vector) {
0993: return;
0994: }
0995:
0996: if (validate) {
0997: assertEmptyList(list);
0998: } else {
0999: synchronized (list) {
1000: try {
1001: list.set(0, E("first element", v));
1002: throw new AssertionError(
1003: "Should have thrown a ReadOnlyException");
1004: } catch (ReadOnlyException t) {
1005: // Expected
1006: }
1007: }
1008: }
1009: }
1010:
1011: // Setting up for the ReadOnly test for remove.
1012: void testSetUpRemove(List list, boolean validate, int v) {
1013: if (list instanceof Vector) {
1014: return;
1015: }
1016:
1017: if (validate) {
1018: assertListsEqual(Arrays.asList(new Object[] {
1019: E("first element", v), E("second element", v) }),
1020: list);
1021: } else {
1022: synchronized (list) {
1023: list.add(E("first element", v));
1024: list.add(E("second element", v));
1025: }
1026: tryReadOnlyRemove(list, v);
1027: }
1028: }
1029:
1030: // tryReadOnlyRemove() goes hand in hand with testSetUpRemove().
1031: private void tryReadOnlyRemove(List list, int v) {
1032: synchronized (list) {
1033: try {
1034: list.remove(E("second element", v));
1035: throw new AssertionError(
1036: "Should have thrown a ReadOnlyException");
1037: } catch (ReadOnlyException t) {
1038: // Expected
1039: }
1040: }
1041: }
1042:
1043: void testSetUpToArray(List list, boolean validate, int v) {
1044: if (list instanceof Vector) {
1045: return;
1046: }
1047:
1048: Object[] array = getArray(list);
1049: if (validate) {
1050: assertEmptyObjectArray(array);
1051: } else {
1052: synchronized (list) {
1053: list.add(E("first element", v));
1054: list.add(E("second element", v));
1055: }
1056: tryReadOnlyToArray(list);
1057: }
1058: }
1059:
1060: void tryReadOnlyToArray(List list) {
1061: Object[] array = getArray(list);
1062: synchronized (array) {
1063: try {
1064: Object[] returnArray = list.toArray(array);
1065: Assert.assertTrue(returnArray == array);
1066: throw new AssertionError(
1067: "Should have thrown a ReadOnlyException");
1068: } catch (ReadOnlyException t) {
1069: // Expected
1070: }
1071: }
1072: }
1073:
1074: // Setting up for the ReadOnly test for Iterator remove.
1075: void testSetUpIteratorRemove(List list, boolean validate, int v) {
1076: if (list instanceof Vector) {
1077: return;
1078: }
1079:
1080: if (validate) {
1081: assertListsEqual(Arrays.asList(new Object[] {
1082: E("first element", v), E("second element", v) }),
1083: list);
1084: } else {
1085: synchronized (list) {
1086: list.add(E("first element", v));
1087: list.add(E("second element", v));
1088: }
1089: tryReadOnlyIteratorRemove(list);
1090: }
1091: }
1092:
1093: // tryReadOnlyIteratorRemove() goes hand in hand with testSetUpIteratorRemove().
1094: private void tryReadOnlyIteratorRemove(List list) {
1095: synchronized (list) {
1096: try {
1097: Iterator iterator = list.iterator();
1098: iterator.next();
1099: iterator.remove();
1100: throw new AssertionError(
1101: "Should have thrown a ReadOnlyException");
1102: } catch (ReadOnlyException t) {
1103: // Expected
1104: }
1105: }
1106: }
1107:
1108: // Setting up for the ReadOnly test for clear.
1109: void testSetUpClear(List list, boolean validate, int v) {
1110: if (list instanceof Vector) {
1111: return;
1112: }
1113:
1114: if (validate) {
1115: assertListsEqual(Arrays.asList(new Object[] {
1116: E("first element", v), E("second element", v) }),
1117: list);
1118: } else {
1119: synchronized (list) {
1120: list.add(E("first element", v));
1121: list.add(E("second element", v));
1122: }
1123: tryReadOnlyClear(list);
1124: }
1125: }
1126:
1127: // tryReadOnlyClear() goes hand in hand with testSetUpClear().
1128: private void tryReadOnlyClear(List list) {
1129: synchronized (list) {
1130: try {
1131: list.clear();
1132: throw new AssertionError(
1133: "Should have thrown a ReadOnlyException");
1134: } catch (ReadOnlyException t) {
1135: // Expected
1136: }
1137: }
1138: }
1139:
1140: // Setting up for the ReadOnly test for retainAll.
1141: void testSetUpRetainAll(List list, boolean validate, int v) {
1142: if (list instanceof Vector) {
1143: return;
1144: }
1145:
1146: if (validate) {
1147: assertListsEqual(Arrays.asList(new Object[] {
1148: E("first element", v), E("second element", v) }),
1149: list);
1150: } else {
1151: synchronized (list) {
1152: list.add(E("first element", v));
1153: list.add(E("second element", v));
1154: }
1155: tryReadOnlyRetainAll(list, v);
1156: }
1157: }
1158:
1159: // tryReadOnlyRetainAll() goes hand in hand with testSetUpRetainAll().
1160: private void tryReadOnlyRetainAll(List list, int v) {
1161: synchronized (list) {
1162: List toRetain = new ArrayList();
1163: toRetain.add(E("first element", v));
1164: try {
1165: list.retainAll(toRetain);
1166: throw new AssertionError(
1167: "Should have thrown a ReadOnlyException");
1168: } catch (ReadOnlyException t) {
1169: // Expected
1170: }
1171: }
1172: }
1173:
1174: // Setting up for the ReadOnly test for removeAll.
1175: void testSetUpRemoveAll(List list, boolean validate, int v) {
1176: if (list instanceof Vector) {
1177: return;
1178: }
1179:
1180: if (validate) {
1181: assertListsEqual(Arrays.asList(new Object[] {
1182: E("first element", v), E("second element", v) }),
1183: list);
1184: } else {
1185: synchronized (list) {
1186: list.add(E("first element", v));
1187: list.add(E("second element", v));
1188: }
1189: tryReadOnlyRemoveAll(list, v);
1190: }
1191: }
1192:
1193: // tryReadOnlyRemoveAll() goes hand in hand with testSetUpRemoveAll().
1194: private void tryReadOnlyRemoveAll(List list, int v) {
1195: synchronized (list) {
1196: List toRemove = new ArrayList();
1197: toRemove.add(E("first element", v));
1198: try {
1199: list.removeAll(toRemove);
1200: throw new AssertionError(
1201: "Should have thrown a ReadOnlyException");
1202: } catch (ReadOnlyException e) {
1203: // Expected
1204: }
1205: }
1206: }
1207:
1208: void testListIteratorReadOnlyAdd(List list, boolean validate, int v) {
1209: if (list instanceof Vector) {
1210: return;
1211: }
1212:
1213: if (validate) {
1214: assertEmptyList(list);
1215: } else {
1216: synchronized (list) {
1217: ListIterator lIterator = list.listIterator();
1218: try {
1219: lIterator.add(E("first element", v));
1220: throw new AssertionError(
1221: "Should have thrown a ReadOnlyException");
1222: } catch (ReadOnlyException e) {
1223: // Expected
1224: }
1225: }
1226: }
1227: }
1228:
1229: void testCollectionsAddAll(List list, boolean validate, int v) {
1230:
1231: if (validate) {
1232: assertListsEqual(Arrays.asList(new Object[] {
1233: E("first element", v), E("second element", v),
1234: E("third element", v) }), list);
1235: } else {
1236: synchronized (list) {
1237: list.addAll(Arrays.asList(new Object[] {
1238: E("first element", v), E("second element", v),
1239: E("third element", v) }));
1240: }
1241: }
1242: }
1243:
1244: // Iterator testing methods.
1245: void testIteratorRemove(List list, boolean validate, int v) {
1246:
1247: if (validate) {
1248: assertListsEqual(Arrays.asList(new Object[] { E(
1249: "second element", v) }), list);
1250: } else {
1251: synchronized (list) {
1252: list.add(E("first element", v));
1253: list.add(E("second element", v));
1254: }
1255: synchronized (list) {
1256: Iterator iterator = list.iterator();
1257: Assert.assertEquals(true, iterator.hasNext());
1258: iterator.next();
1259: iterator.remove();
1260: }
1261: }
1262: }
1263:
1264: void testIteratorDuplicateElementRemove(List list,
1265: boolean validate, int v) {
1266:
1267: if (validate) {
1268: assertListsEqual(Arrays.asList(new Object[] {
1269: E("first element", v), E("second element", v) }),
1270: list);
1271: } else {
1272: synchronized (list) {
1273: list.add(E("first element", v));
1274: list.add(E("second element", v));
1275: list.add(E("first element", v));
1276: }
1277: synchronized (list) {
1278: Iterator iterator = list.iterator();
1279: Assert.assertEquals(true, iterator.hasNext());
1280: iterator.next();
1281: iterator.next();
1282: iterator.next();
1283: iterator.remove();
1284: }
1285: }
1286: }
1287:
1288: void testIteratorRemoveNull(List list, boolean validate, int v) {
1289:
1290: if (validate) {
1291: assertListsEqual(Arrays
1292: .asList(new Object[] { E("first element", v), null,
1293: E("second element", v) }), list);
1294: } else {
1295: synchronized (list) {
1296: list.add(E("first element", v));
1297: list.add(null);
1298: list.add(null);
1299: list.add(E("second element", v));
1300: }
1301: synchronized (list) {
1302: Iterator iterator = list.iterator();
1303: Assert.assertEquals(true, iterator.hasNext());
1304: iterator.next();
1305: iterator.next();
1306: iterator.remove();
1307: }
1308: }
1309: }
1310:
1311: // Stack specific testing method.
1312: void testStackPush(List list, boolean validate, int v) {
1313: if (!(list instanceof Stack)) {
1314: return;
1315: }
1316:
1317: if (validate) {
1318: assertListsEqual(Arrays.asList(new Object[] {
1319: E("first element", v), E("second element", v) }),
1320: list);
1321: } else {
1322: synchronized (list) {
1323: Stack s = (Stack) list;
1324: s.push(E("first element", v));
1325: s.push(E("second element", v));
1326: }
1327: }
1328: }
1329:
1330: void testStackPop(List list, boolean validate, int v) {
1331: if (!(list instanceof Stack)) {
1332: return;
1333: }
1334:
1335: if (validate) {
1336: assertListsEqual(Arrays.asList(new Object[] { E(
1337: "first element", v) }), list);
1338: } else {
1339: Stack s = (Stack) list;
1340: synchronized (list) {
1341: s.push(E("first element", v));
1342: s.push(E("second element", v));
1343: }
1344: synchronized (list) {
1345: Object o = s.pop();
1346: Assert.assertEquals(E("second element", v), o);
1347: }
1348: }
1349: }
1350:
1351: void testAddNonPortableObject(List list, boolean validate, int v) {
1352: if (!validate) {
1353: synchronized (list) {
1354: try {
1355: list.add(new MyArrayList2());
1356: throw new AssertionError(
1357: "Should have thrown a TCNonPortableObjectError.");
1358: } catch (TCNonPortableObjectError e) {
1359: // expected
1360: }
1361: }
1362: synchronized (list) {
1363: try {
1364: list.add(new MyArrayList3());
1365: throw new AssertionError(
1366: "Should have thrown a TCNonPortableObjectError.");
1367: } catch (TCNonPortableObjectError e) {
1368: // expected
1369: }
1370: }
1371: }
1372: }
1373:
1374: private Object getMySubclassArray(List list) {
1375: if (list instanceof MyArrayList6) {
1376: return sharedMap.get("arrayforMyArrayList6");
1377: }
1378: if (list instanceof MyArrayList5) {
1379: return sharedMap.get("arrayforMyArrayList5");
1380: }
1381: if (list instanceof MyArrayList) {
1382: return sharedMap.get("arrayforMyArrayList");
1383: }
1384: if (list instanceof MyLinkedList) {
1385: return sharedMap.get("arrayforMyLinkedList");
1386: }
1387: if (list instanceof MyVector) {
1388: return sharedMap.get("arrayforMyVector");
1389: }
1390: if (list instanceof MyStack) {
1391: return sharedMap.get("arrayforMyStack");
1392: }
1393: if (list instanceof MyAbstractListSubclass) {
1394: return sharedMap.get("arrayforMyAbstractListSubclass");
1395: }
1396: return null;
1397: }
1398:
1399: private Object[] getArray(List list) {
1400: Object o = getMySubclassArray(list);
1401: if (o != null) {
1402: return (Object[]) o;
1403: }
1404:
1405: if (list instanceof LinkedList) {
1406: return (Object[]) sharedMap.get("arrayforLinkedList");
1407: }
1408: if (list instanceof ArrayList) {
1409: return (Object[]) sharedMap.get("arrayforArrayList");
1410: }
1411: if (list instanceof Stack) { // need to check instanceof Stack first before checking instance of Vector
1412: // as Stack is a subclass of Vector.
1413: return (Object[]) sharedMap.get("arrayforStack");
1414: }
1415: if (list instanceof Vector) {
1416: return (Object[]) sharedMap.get("arrayforVector");
1417: }
1418: if (list instanceof MyAbstractListSubclass) {
1419: return (Object[]) sharedMap
1420: .get("arrayforAbstractListSubclass");
1421: }
1422: return null;
1423: }
1424:
1425: private static void assertEmptyObjectArray(Object[] array) {
1426: for (int i = 0; i < array.length; i++) {
1427: Assert.assertNull(array[i]);
1428: }
1429: }
1430:
1431: private static void assertEmptyList(List list) {
1432: Assert.assertEquals(list.getClass(), 0, list.size());
1433: Assert.assertTrue(list.getClass(), list.isEmpty());
1434:
1435: int count = 0;
1436: for (Iterator i = list.iterator(); i.hasNext();) {
1437: count++;
1438: }
1439:
1440: Assert.assertEquals(list.getClass(), 0, count);
1441:
1442: for (Iterator i = list.listIterator(); i.hasNext();) {
1443: count++;
1444: }
1445:
1446: Assert.assertEquals(list.getClass(), 0, count);
1447: }
1448:
1449: private static void assertListsEqual(List expect, List actual) {
1450: Assert.assertEquals(expect.size(), actual.size());
1451:
1452: Assert.assertTrue(expect.containsAll(actual));
1453: Assert.assertTrue(actual.containsAll(expect));
1454:
1455: for (int i = 0, n = expect.size(); i < n; i++) {
1456: Assert.assertEquals(expect.get(i), actual.get(i));
1457: }
1458:
1459: if (expect.isEmpty()) {
1460: Assert.assertTrue(actual.isEmpty());
1461: } else {
1462: Assert.assertFalse(actual.isEmpty());
1463: }
1464:
1465: for (Iterator iExpect = expect.iterator(), iActual = actual
1466: .iterator(); iExpect.hasNext();) {
1467: Assert.assertEquals(iExpect.next(), iActual.next());
1468: }
1469:
1470: }
1471:
1472: public static void visitL1DSOConfig(ConfigVisitor visitor,
1473: DSOClientConfigHelper config) {
1474: String testClass = GenericListTestApp.class.getName();
1475: config.getOrCreateSpec(testClass);
1476: String writeAllowedMethodExpression = "* " + testClass
1477: + "*.*(..)";
1478: config.addWriteAutolock(writeAllowedMethodExpression);
1479: String readOnlyMethodExpression = "* " + testClass
1480: + "*.*ReadOnly*(..)";
1481: config.addReadAutolock(readOnlyMethodExpression);
1482: config.addIncludePattern(testClass + "$*");
1483: }
1484:
1485: private static void assertSingleElement(List list, Object obj) {
1486: Assert.assertEquals(1, list.size());
1487: Assert.assertEquals(obj, list.get(0));
1488: Assert.assertFalse(list.isEmpty());
1489: Assert.assertTrue(list.contains(obj));
1490:
1491: int count = 0;
1492: for (Iterator i = list.iterator(); i.hasNext();) {
1493: count++;
1494: Assert.assertEquals(obj, i.next());
1495: }
1496: Assert.assertEquals(1, count);
1497:
1498: }
1499:
1500: private static class MyArrayList extends ArrayList {
1501: public MyArrayList() {
1502: super ();
1503: }
1504: }
1505:
1506: private static class MyArrayList2 extends ArrayList {
1507: private Vector vector;
1508:
1509: protected void removeRange(int fromIndex, int toIndex) {
1510: super .removeRange(fromIndex, toIndex);
1511: }
1512:
1513: public Vector getVector() {
1514: return vector;
1515: }
1516: }
1517:
1518: private static class MyArrayList3 extends ArrayList {
1519: private Vector vector;
1520:
1521: public void removeRangeLocal(int fromIndex, int toIndex) {
1522: super .removeRange(fromIndex, toIndex);
1523: }
1524:
1525: public Vector getVector() {
1526: return vector;
1527: }
1528: }
1529:
1530: private static class MyArrayList4 extends ArrayList {
1531: //
1532: }
1533:
1534: private static class MyArrayList5 extends MyArrayList4 {
1535: //
1536: }
1537:
1538: private static class MyArrayList6 extends ArrayList {
1539:
1540: int i = 3;
1541:
1542: MyArrayList6() {
1543: Set s = new HashSet();
1544: s.add("test");
1545: new ArrayList(s);
1546: if (size() != 0) {
1547: throw new AssertionError();
1548: }
1549: }
1550:
1551: MyArrayList6(Set s1) {
1552: super (s1);
1553: Set s = new HashSet();
1554: s.add("test");
1555: ArrayList l = new ArrayList(s);
1556: if (size() != 0) {
1557: throw new AssertionError(l.size());
1558: }
1559: }
1560: }
1561:
1562: private static class MyLinkedList extends LinkedList {
1563: public MyLinkedList() {
1564: super ();
1565: }
1566: }
1567:
1568: private static class MyVector extends Vector {
1569: public MyVector() {
1570: super ();
1571: }
1572: }
1573:
1574: private static class MyStack extends Stack {
1575: public MyStack() {
1576: super ();
1577: }
1578: }
1579:
1580: private static class MyAbstractListSubclass extends AbstractList {
1581: // This is in here to make sure that a subclass of AbstractList is sharable in DSO, not that this is a good/proper
1582: // List implementation ;-)
1583:
1584: private ArrayList data = new ArrayList();
1585:
1586: public void add(int index, Object element) {
1587: data.add(index, element);
1588: }
1589:
1590: public Object set(int index, Object element) {
1591: return data.set(index, element);
1592: }
1593:
1594: public Object get(int index) {
1595: return data.get(index);
1596: }
1597:
1598: public int size() {
1599: return data.size();
1600: }
1601:
1602: public Object remove(int index) {
1603: return data.remove(index);
1604: }
1605:
1606: }
1607:
1608: private static class Foo implements Comparable {
1609: private final String value;
1610:
1611: Foo(String value) {
1612: this .value = value;
1613: }
1614:
1615: public int hashCode() {
1616: return value.hashCode();
1617: }
1618:
1619: public boolean equals(Object obj) {
1620: if (obj instanceof Foo) {
1621: return value.equals(((Foo) obj).value);
1622: }
1623: return false;
1624: }
1625:
1626: public int compareTo(Object o) {
1627: return value.compareTo(((Foo) o).value);
1628: }
1629: }
1630:
1631: }
|