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