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.bytecode.TCMap;
0007: import com.tc.object.config.ConfigVisitor;
0008: import com.tc.object.config.DSOClientConfigHelper;
0009: import com.tc.object.config.TransparencyClassSpec;
0010: import com.tc.simulator.app.ApplicationConfig;
0011: import com.tc.simulator.listener.ListenerProvider;
0012: import com.tc.util.Assert;
0013:
0014: import java.util.ArrayList;
0015: import java.util.Collection;
0016: import java.util.HashMap;
0017: import java.util.Iterator;
0018: import java.util.List;
0019: import java.util.Map;
0020: import java.util.Set;
0021: import java.util.Map.Entry;
0022: import java.util.concurrent.ConcurrentHashMap;
0023:
0024: @SuppressWarnings("unchecked")
0025: public class ConcurrentHashMapTestApp extends GenericTestApp {
0026:
0027: private final DataKey[] keyRoots = new DataKey[] { new DataKey(1),
0028: new DataKey(2), new DataKey(3), new DataKey(4) };
0029: private final DataValue[] valueRoots = new DataValue[] {
0030: new DataValue(10), new DataValue(20), new DataValue(30),
0031: new DataValue(40) };
0032:
0033: private final HashKey[] hashKeys = new HashKey[] { new HashKey(1),
0034: new HashKey(2), new HashKey(3), new HashKey(4) };
0035: private final HashValue[] hashValues = new HashValue[] {
0036: new HashValue(10), new HashValue(20), new HashValue(30),
0037: new HashValue(40) };
0038:
0039: public ConcurrentHashMapTestApp(String appId,
0040: ApplicationConfig cfg, ListenerProvider listenerProvider) {
0041: super (appId, cfg, listenerProvider, ConcurrentHashMap.class);
0042: }
0043:
0044: protected Object getTestObject(String test) {
0045: return sharedMap.get("map");
0046: }
0047:
0048: protected void setupTestObject(String test) {
0049: List listOfMaps = new ArrayList();
0050: listOfMaps.add(new ConcurrentHashMap());
0051: sharedMap.put("maps", listOfMaps);
0052: sharedMap.put("map", new ConcurrentHashMap());
0053: sharedMap.put("arrayforConcurrentHashMap", new Object[4]);
0054: sharedMap.put("arrayforConcurrentHashMapWithHashKeys",
0055: new Object[4]);
0056: }
0057:
0058: void testPut1(ConcurrentHashMap map, boolean validate)
0059: throws Exception {
0060: if (validate) {
0061: Assert.assertFalse(map.isEmpty());
0062: Assert.assertEquals(1, map.size());
0063: Assert.assertEquals(20,
0064: ((DataValue) (map.get(keyRoots[0]))).getInt());
0065: } else {
0066: DataValue value1 = new DataValue(10);
0067: DataValue value2 = new DataValue(20);
0068: Object o = map.put(keyRoots[0], value1);
0069: Assert.assertNull(o);
0070:
0071: o = map.put(keyRoots[0], value2);
0072: Assert.assertTrue(o == value1);
0073: }
0074: }
0075:
0076: void testPut2(ConcurrentHashMap map, boolean validate)
0077: throws Exception {
0078: if (validate) {
0079: Assert.assertFalse(map.isEmpty());
0080: Assert.assertEquals(1, map.size());
0081: Assert.assertEquals(hashValues[1], map.get(hashKeys[0]));
0082: } else {
0083: Object o = map.put(hashKeys[0], hashValues[0]);
0084: Assert.assertNull(o);
0085:
0086: o = map.put(hashKeys[0], hashValues[1]);
0087: Assert.assertTrue(o == hashValues[0]);
0088: }
0089: }
0090:
0091: void testPutWithClassKey(ConcurrentHashMap map, boolean validate)
0092: throws Exception {
0093: if (validate) {
0094: Assert.assertFalse(map.isEmpty());
0095: Assert.assertEquals(1, map.size());
0096: Assert.assertEquals(hashValues[0], map.get(HashKey.class));
0097: } else {
0098: Object o = map.put(HashKey.class, hashValues[0]);
0099: Assert.assertNull(o);
0100: }
0101: }
0102:
0103: void testPutIfAbsent(ConcurrentHashMap map, boolean validate)
0104: throws Exception {
0105: if (validate) {
0106: Assert.assertFalse(map.isEmpty());
0107: Assert.assertEquals(1, map.size());
0108: Assert.assertEquals(10,
0109: ((DataValue) (map.get(keyRoots[0]))).getInt());
0110: } else {
0111: DataValue value1 = new DataValue(10);
0112: DataValue value2 = new DataValue(20);
0113: Object o = map.put(keyRoots[0], value1);
0114:
0115: o = map.putIfAbsent(keyRoots[0], value2);
0116: Assert.assertTrue(o == value1);
0117: }
0118: }
0119:
0120: void testPutIfAbsent2(ConcurrentHashMap map, boolean validate)
0121: throws Exception {
0122: if (validate) {
0123: Assert.assertFalse(map.isEmpty());
0124: Assert.assertEquals(1, map.size());
0125: Assert.assertEquals(hashValues[0], map.get(hashKeys[0]));
0126: } else {
0127: Object o = map.put(hashKeys[0], hashValues[0]);
0128:
0129: o = map.putIfAbsent(hashKeys[0], hashValues[1]);
0130: Assert.assertTrue(o == hashValues[0]);
0131: }
0132: }
0133:
0134: void testPutAll1(ConcurrentHashMap map, boolean validate)
0135: throws Exception {
0136: Map toPut = new HashMap();
0137: toPut.put(keyRoots[0], valueRoots[0]);
0138: toPut.put(keyRoots[1], valueRoots[1]);
0139: toPut.put(keyRoots[2], valueRoots[2]);
0140: toPut.put(keyRoots[3], valueRoots[3]);
0141:
0142: if (validate) {
0143: assertMappingsEqual(toPut, map);
0144: } else {
0145: map.putAll(toPut);
0146: }
0147: }
0148:
0149: void testPutAll2(ConcurrentHashMap map, boolean validate)
0150: throws Exception {
0151: Map toPut = new HashMap();
0152: toPut.put(hashKeys[0], hashValues[0]);
0153: toPut.put(hashKeys[1], hashValues[1]);
0154: toPut.put(hashKeys[2], hashValues[2]);
0155: toPut.put(hashKeys[3], hashValues[3]);
0156:
0157: if (validate) {
0158: assertMappingsHashEqual(toPut, map);
0159: } else {
0160: map.putAll(toPut);
0161: }
0162: }
0163:
0164: void testRemove1(ConcurrentHashMap map, boolean validate)
0165: throws Exception {
0166: Map toPut = new HashMap();
0167: toPut.put(keyRoots[0], valueRoots[0]);
0168: toPut.put(keyRoots[1], valueRoots[1]);
0169: toPut.put(keyRoots[2], valueRoots[2]);
0170: toPut.put(keyRoots[3], valueRoots[3]);
0171:
0172: if (validate) {
0173: toPut.remove(keyRoots[1]);
0174: assertMappingsEqual(toPut, map);
0175: } else {
0176: map.putAll(toPut);
0177: map.remove(keyRoots[1]);
0178: }
0179: }
0180:
0181: void testRemoveLogical(ConcurrentHashMap map, boolean validate)
0182: throws Exception {
0183: Map toPut = new HashMap();
0184: toPut.put(keyRoots[0], valueRoots[0]);
0185: toPut.put(keyRoots[1], valueRoots[1]);
0186: toPut.put(keyRoots[2], valueRoots[2]);
0187: toPut.put(keyRoots[3], valueRoots[3]);
0188:
0189: if (validate) {
0190: ((TCMap) toPut).__tc_remove_logical(keyRoots[1]);
0191: assertMappingsEqual(toPut, map);
0192: } else {
0193: map.putAll(toPut);
0194: ((TCMap) map).__tc_remove_logical(keyRoots[1]);
0195: }
0196: }
0197:
0198: void testHashRemove1(ConcurrentHashMap map, boolean validate)
0199: throws Exception {
0200: Map toPut = new HashMap();
0201: toPut.put(hashKeys[0], hashValues[0]);
0202: toPut.put(hashKeys[1], hashValues[1]);
0203: toPut.put(hashKeys[2], hashValues[2]);
0204: toPut.put(hashKeys[3], hashValues[3]);
0205:
0206: if (validate) {
0207: toPut.remove(hashKeys[1]);
0208: assertMappingsHashEqual(toPut, map);
0209: } else {
0210: map.putAll(toPut);
0211: map.remove(hashKeys[1]);
0212: }
0213: }
0214:
0215: void testRemove2(ConcurrentHashMap map, boolean validate)
0216: throws Exception {
0217: Map toPut = new HashMap();
0218: toPut.put(keyRoots[0], valueRoots[0]);
0219: toPut.put(keyRoots[1], valueRoots[1]);
0220: toPut.put(keyRoots[2], valueRoots[2]);
0221: toPut.put(keyRoots[3], valueRoots[3]);
0222:
0223: if (validate) {
0224: assertMappingsEqual(toPut, map);
0225: } else {
0226: map.putAll(toPut);
0227: map.remove(keyRoots[1], new DataValue(30));
0228: }
0229: }
0230:
0231: void testHashRemove2(ConcurrentHashMap map, boolean validate)
0232: throws Exception {
0233: Map toPut = new HashMap();
0234: toPut.put(hashKeys[0], hashValues[0]);
0235: toPut.put(hashKeys[1], hashValues[1]);
0236: toPut.put(hashKeys[2], hashValues[2]);
0237: toPut.put(hashKeys[3], hashValues[3]);
0238:
0239: if (validate) {
0240: assertMappingsHashEqual(toPut, map);
0241: } else {
0242: map.putAll(toPut);
0243: map.remove(hashKeys[1], new HashValue(30));
0244: }
0245: }
0246:
0247: void testRemove3(ConcurrentHashMap map, boolean validate)
0248: throws Exception {
0249: Map toPut = new HashMap();
0250: toPut.put(keyRoots[0], valueRoots[0]);
0251: toPut.put(keyRoots[1], valueRoots[1]);
0252: toPut.put(keyRoots[2], valueRoots[2]);
0253: toPut.put(keyRoots[3], valueRoots[3]);
0254:
0255: if (validate) {
0256: toPut.remove(keyRoots[3]);
0257: assertMappingsEqual(toPut, map);
0258: } else {
0259: map.putAll(toPut);
0260: map.remove(keyRoots[3], valueRoots[3]);
0261: }
0262: }
0263:
0264: void testHashRemove3(ConcurrentHashMap map, boolean validate)
0265: throws Exception {
0266: Map toPut = new HashMap();
0267: toPut.put(hashKeys[0], hashValues[0]);
0268: toPut.put(hashKeys[1], hashValues[1]);
0269: toPut.put(hashKeys[2], hashValues[2]);
0270: toPut.put(hashKeys[3], hashValues[3]);
0271:
0272: if (validate) {
0273: toPut.remove(hashKeys[3]);
0274: assertMappingsHashEqual(toPut, map);
0275: } else {
0276: map.putAll(toPut);
0277: //map.remove(hashKeys[3], new HashValue(40));
0278: map.remove(hashKeys[3], hashValues[3]);
0279: }
0280: }
0281:
0282: void testReplace1(ConcurrentHashMap map, boolean validate)
0283: throws Exception {
0284: if (validate) {
0285: Assert.assertEquals(10, ((DataValue) map.get(keyRoots[0]))
0286: .getInt());
0287: } else {
0288: DataValue value1 = new DataValue(10);
0289: Object o = map.put(keyRoots[0], value1);
0290: Assert.assertNull(o);
0291: o = map.replace(new DataKey(1), new DataValue(20));
0292: Assert.assertNull(o);
0293: }
0294: }
0295:
0296: void testHashReplace1(ConcurrentHashMap map, boolean validate)
0297: throws Exception {
0298: if (validate) {
0299: assertSingleHashMapping(hashKeys[0], hashValues[0], map);
0300: } else {
0301: Object o = map.put(hashKeys[0], hashValues[0]);
0302: Assert.assertNull(o);
0303: o = map.replace(hashKeys[1], hashValues[1]);
0304: Assert.assertNull(o);
0305: }
0306: }
0307:
0308: void testReplace2(ConcurrentHashMap map, boolean validate)
0309: throws Exception {
0310: if (validate) {
0311: Assert.assertEquals(20, ((DataValue) map.get(keyRoots[0]))
0312: .getInt());
0313: } else {
0314: DataValue value1 = new DataValue(10);
0315: Object o = map.put(keyRoots[0], value1);
0316: Assert.assertNull(o);
0317: o = map.replace(keyRoots[0], new DataValue(20));
0318: Assert.assertEquals(10, ((DataValue) o).getInt());
0319: }
0320: }
0321:
0322: void testHashReplace2(ConcurrentHashMap map, boolean validate)
0323: throws Exception {
0324: if (validate) {
0325: assertSingleHashMapping(hashKeys[0], hashValues[1], map);
0326: } else {
0327: Object o = map.put(hashKeys[0], hashValues[0]);
0328: Assert.assertNull(o);
0329: Object o2 = new HashKey(1);
0330: o = map.replace(o2, new HashValue(20));
0331: Assert.assertEquals(o, hashValues[0]);
0332: }
0333: }
0334:
0335: void testReplaceIfValueEqual1(ConcurrentHashMap map,
0336: boolean validate) throws Exception {
0337: if (validate) {
0338: Assert.assertEquals(valueRoots[0], map.get(keyRoots[0]));
0339: } else {
0340: Object o = map.put(keyRoots[0], valueRoots[0]);
0341: Assert.assertNull(o);
0342: boolean returnValue = map.replace(keyRoots[0],
0343: new DataValue(10), new DataValue(20));
0344: Assert.assertFalse(returnValue);
0345: }
0346: }
0347:
0348: void testHashReplaceIfValueEqual1(ConcurrentHashMap map,
0349: boolean validate) throws Exception {
0350: if (validate) {
0351: Assert.assertEquals(hashValues[0], map.get(hashKeys[0]));
0352: } else {
0353: Object o = map.put(hashKeys[0], hashValues[0]);
0354: Assert.assertNull(o);
0355: boolean returnValue = map.replace(new HashKey(1),
0356: new HashValue(15), new DataValue(20));
0357: Assert.assertFalse(returnValue);
0358: }
0359: }
0360:
0361: void testReplaceIfValueEqual2(ConcurrentHashMap map,
0362: boolean validate) throws Exception {
0363: if (validate) {
0364: Assert.assertEquals(20, ((DataValue) map.get(keyRoots[0]))
0365: .getInt());
0366: } else {
0367: Object o = map.put(keyRoots[0], valueRoots[0]);
0368: Assert.assertNull(o);
0369: boolean returnValue = map.replace(keyRoots[0],
0370: valueRoots[0], new DataValue(20));
0371: Assert.assertTrue(returnValue);
0372: }
0373: }
0374:
0375: void testHashReplaceIfValueEqual2(ConcurrentHashMap map,
0376: boolean validate) throws Exception {
0377: if (validate) {
0378: Assert.assertEquals(hashValues[1], map.get(hashKeys[0]));
0379: } else {
0380: Object o = map.put(hashKeys[0], hashValues[0]);
0381: Assert.assertNull(o);
0382: boolean returnValue = map.replace(new HashKey(1),
0383: new HashValue(10), new HashValue(20));
0384: Assert.assertTrue(returnValue);
0385: }
0386: }
0387:
0388: void testContains1(ConcurrentHashMap map, boolean validate)
0389: throws Exception {
0390: if (validate) {
0391: Assert.assertTrue(map.containsKey(keyRoots[0]));
0392: Assert.assertFalse(map.containsKey(new DataKey(1)));
0393:
0394: Assert.assertTrue(map.containsValue(valueRoots[0]));
0395: Assert.assertFalse(map.containsValue(new DataValue(10)));
0396:
0397: Assert.assertTrue(map.contains(valueRoots[0]));
0398: Assert.assertFalse(map.contains(new DataValue(10)));
0399: } else {
0400: map.put(keyRoots[0], valueRoots[0]);
0401: }
0402: }
0403:
0404: void testContains2(ConcurrentHashMap map, boolean validate)
0405: throws Exception {
0406: if (validate) {
0407: Assert.assertTrue(map.containsKey(hashKeys[0]));
0408: Assert.assertTrue(map.containsKey(new HashKey(1)));
0409:
0410: Assert.assertTrue(map.containsValue(hashValues[0]));
0411: Assert.assertTrue(map.containsValue(new HashValue(10)));
0412:
0413: Assert.assertTrue(map.contains(hashValues[0]));
0414: Assert.assertTrue(map.contains(new HashValue(10)));
0415: } else {
0416: map.put(hashKeys[0], hashValues[0]);
0417: }
0418: }
0419:
0420: void testEntrySetClear(ConcurrentHashMap map, boolean validate)
0421: throws Exception {
0422: Map toPut = new HashMap();
0423: DataKey key1 = new DataKey(1);
0424: DataKey key2 = new DataKey(2);
0425: DataKey key3 = new DataKey(3);
0426:
0427: DataValue value1 = new DataValue(10);
0428: DataValue value2 = new DataValue(20);
0429: DataValue value3 = new DataValue(30);
0430: toPut.put(key1, value1);
0431: toPut.put(key2, value2);
0432: toPut.put(key3, value3);
0433: if (validate) {
0434: Assert.assertEquals(0, map.size());
0435: } else {
0436: map.putAll(toPut);
0437:
0438: map.entrySet().clear();
0439: }
0440: }
0441:
0442: void testEntrySetContains1(ConcurrentHashMap map, boolean validate)
0443: throws Exception {
0444: SimpleEntry entry = new SimpleEntry(keyRoots[0], valueRoots[0]);
0445: if (validate) {
0446: Assert.assertTrue(map.entrySet().contains(entry));
0447: } else {
0448: map.put(keyRoots[0], valueRoots[0]);
0449: }
0450: }
0451:
0452: void testEntrySetContains2(ConcurrentHashMap map, boolean validate)
0453: throws Exception {
0454: SimpleEntry entry = new SimpleEntry(new HashKey(1),
0455: new HashValue(10));
0456: if (validate) {
0457: Assert.assertTrue(map.entrySet().contains(entry));
0458: } else {
0459: map.put(hashKeys[0], hashValues[0]);
0460: }
0461: }
0462:
0463: void testEntrySetContainsAll1(ConcurrentHashMap map,
0464: boolean validate) throws Exception {
0465: Map toPut = new HashMap();
0466: toPut.put(keyRoots[0], valueRoots[0]);
0467: toPut.put(keyRoots[1], valueRoots[1]);
0468: toPut.put(keyRoots[2], valueRoots[2]);
0469: toPut.put(keyRoots[3], valueRoots[3]);
0470:
0471: if (validate) {
0472: SimpleEntry entry1 = new SimpleEntry(keyRoots[1],
0473: valueRoots[1]);
0474: SimpleEntry entry2 = new SimpleEntry(keyRoots[2],
0475: valueRoots[2]);
0476: List containsList = new ArrayList(2);
0477: containsList.add(entry1);
0478: containsList.add(entry2);
0479: Assert.assertTrue(map.entrySet().containsAll(containsList));
0480: } else {
0481: map.putAll(toPut);
0482: }
0483: }
0484:
0485: void testEntrySetContainsAll2(ConcurrentHashMap map,
0486: boolean validate) throws Exception {
0487: Map toPut = new HashMap();
0488: toPut.put(hashKeys[0], hashValues[0]);
0489: toPut.put(hashKeys[1], hashValues[1]);
0490: toPut.put(hashKeys[2], hashValues[2]);
0491: toPut.put(hashKeys[3], hashValues[3]);
0492:
0493: if (validate) {
0494: SimpleEntry entry1 = new SimpleEntry(hashKeys[1],
0495: hashValues[1]);
0496: SimpleEntry entry2 = new SimpleEntry(new HashKey(3),
0497: new HashValue(30));
0498: List containsList = new ArrayList(2);
0499: containsList.add(entry1);
0500: containsList.add(entry2);
0501: Assert.assertTrue(map.entrySet().containsAll(containsList));
0502: } else {
0503: map.putAll(toPut);
0504: }
0505: }
0506:
0507: void testEntrySetRetainAll1(ConcurrentHashMap map, boolean validate)
0508: throws Exception {
0509: Map toPut = new HashMap();
0510: toPut.put(keyRoots[0], valueRoots[0]);
0511: toPut.put(keyRoots[1], valueRoots[1]);
0512: toPut.put(keyRoots[2], valueRoots[2]);
0513: toPut.put(keyRoots[3], valueRoots[3]);
0514: if (validate) {
0515: toPut.remove(keyRoots[0]);
0516: toPut.remove(keyRoots[3]);
0517: assertMappingsEqual(toPut, map);
0518: } else {
0519: map.putAll(toPut);
0520: SimpleEntry entry1 = new SimpleEntry(keyRoots[1],
0521: valueRoots[1]);
0522: SimpleEntry entry2 = new SimpleEntry(keyRoots[2],
0523: valueRoots[2]);
0524: List containsList = new ArrayList(2);
0525: containsList.add(entry1);
0526: containsList.add(entry2);
0527: map.entrySet().retainAll(containsList);
0528: }
0529: }
0530:
0531: void testEntrySetRetainAll2(ConcurrentHashMap map, boolean validate)
0532: throws Exception {
0533: Map toPut = new HashMap();
0534: toPut.put(hashKeys[0], hashValues[0]);
0535: toPut.put(hashKeys[1], hashValues[1]);
0536: toPut.put(hashKeys[2], hashValues[2]);
0537: toPut.put(hashKeys[3], hashValues[3]);
0538:
0539: if (validate) {
0540: toPut.remove(hashKeys[0]);
0541: toPut.remove(hashKeys[3]);
0542: assertMappingsHashEqual(toPut, map);
0543: } else {
0544: map.putAll(toPut);
0545: SimpleEntry entry1 = new SimpleEntry(hashKeys[1],
0546: hashValues[1]);
0547: SimpleEntry entry2 = new SimpleEntry(new HashKey(3),
0548: new HashValue(30));
0549: List containsList = new ArrayList(2);
0550: containsList.add(entry1);
0551: containsList.add(entry2);
0552: map.entrySet().retainAll(containsList);
0553: }
0554: }
0555:
0556: void testEntrySetRemove1(ConcurrentHashMap map, boolean validate)
0557: throws Exception {
0558: if (validate) {
0559: Assert.assertEquals(0, map.size());
0560: } else {
0561: map.put(keyRoots[0], valueRoots[0]);
0562: SimpleEntry entry = new SimpleEntry(keyRoots[0],
0563: valueRoots[0]);
0564: map.entrySet().remove(entry);
0565: }
0566: }
0567:
0568: void testEntrySetRemove2(ConcurrentHashMap map, boolean validate)
0569: throws Exception {
0570: if (validate) {
0571: Assert.assertEquals(0, map.size());
0572: } else {
0573: map.put(hashKeys[0], hashValues[0]);
0574: SimpleEntry entry = new SimpleEntry(hashKeys[0],
0575: hashValues[0]);
0576: map.entrySet().remove(entry);
0577: }
0578: }
0579:
0580: void testEntrySetRemoveAll1(ConcurrentHashMap map, boolean validate)
0581: throws Exception {
0582: Map toPut = new HashMap();
0583: toPut.put(keyRoots[0], valueRoots[0]);
0584: toPut.put(keyRoots[1], valueRoots[1]);
0585: toPut.put(keyRoots[2], valueRoots[2]);
0586: toPut.put(keyRoots[3], valueRoots[3]);
0587: if (validate) {
0588: toPut.remove(keyRoots[1]);
0589: toPut.remove(keyRoots[2]);
0590: assertMappingsEqual(toPut, map);
0591: } else {
0592: map.putAll(toPut);
0593:
0594: SimpleEntry entry1 = new SimpleEntry(keyRoots[1],
0595: valueRoots[1]);
0596: SimpleEntry entry2 = new SimpleEntry(keyRoots[2],
0597: valueRoots[2]);
0598: List toRemove = new ArrayList(2);
0599: toRemove.add(entry1);
0600: toRemove.add(entry2);
0601: map.entrySet().removeAll(toRemove);
0602: }
0603: }
0604:
0605: void testEntrySetRemoveAll2(ConcurrentHashMap map, boolean validate)
0606: throws Exception {
0607: Map toPut = new HashMap();
0608: toPut.put(hashKeys[0], hashValues[0]);
0609: toPut.put(hashKeys[1], hashValues[1]);
0610: toPut.put(hashKeys[2], hashValues[2]);
0611: toPut.put(hashKeys[3], hashValues[3]);
0612: if (validate) {
0613: toPut.remove(hashKeys[1]);
0614: toPut.remove(hashKeys[2]);
0615: assertMappingsHashEqual(toPut, map);
0616: } else {
0617: map.putAll(toPut);
0618:
0619: SimpleEntry entry1 = new SimpleEntry(hashKeys[1],
0620: hashValues[1]);
0621: SimpleEntry entry2 = new SimpleEntry(new HashKey(3),
0622: new HashValue(30));
0623: List toRemove = new ArrayList(2);
0624: toRemove.add(entry1);
0625: toRemove.add(entry2);
0626: map.entrySet().removeAll(toRemove);
0627: }
0628: }
0629:
0630: void testEntrySetSize1(ConcurrentHashMap map, boolean validate)
0631: throws Exception {
0632: if (validate) {
0633: Assert.assertEquals(1, map.entrySet().size());
0634: } else {
0635: map.put(keyRoots[0], valueRoots[0]);
0636: }
0637: }
0638:
0639: void testEntrySetSize2(ConcurrentHashMap map, boolean validate)
0640: throws Exception {
0641: if (validate) {
0642: Assert.assertEquals(1, map.entrySet().size());
0643: } else {
0644: map.put(hashKeys[0], hashValues[0]);
0645: }
0646: }
0647:
0648: void testEntrySetSetValue1(ConcurrentHashMap map, boolean validate)
0649: throws Exception {
0650: Map toPut = new HashMap();
0651: toPut.put(keyRoots[0], valueRoots[0]);
0652: toPut.put(keyRoots[1], valueRoots[1]);
0653: toPut.put(keyRoots[2], valueRoots[2]);
0654: toPut.put(keyRoots[3], valueRoots[3]);
0655: if (validate) {
0656: Assert.assertEquals(15, ((DataValue) map.get(keyRoots[1]))
0657: .getInt());
0658: } else {
0659: map.putAll(toPut);
0660: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
0661: Map.Entry entry = (Map.Entry) i.next();
0662: if (((DataKey) entry.getKey()).getInt() == 2) {
0663: entry.setValue(new DataValue(15));
0664: }
0665: }
0666: }
0667: }
0668:
0669: void testEntrySetSetValue2(ConcurrentHashMap map, boolean validate)
0670: throws Exception {
0671: Map toPut = new HashMap();
0672: toPut.put(hashKeys[0], hashValues[0]);
0673: toPut.put(hashKeys[1], hashValues[1]);
0674: toPut.put(hashKeys[2], hashValues[2]);
0675: toPut.put(hashKeys[3], hashValues[3]);
0676: if (validate) {
0677: Assert
0678: .assertEquals(new HashValue(15), map
0679: .get(hashKeys[1]));
0680: } else {
0681: map.putAll(toPut);
0682: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
0683: Map.Entry entry = (Map.Entry) i.next();
0684: if (((HashKey) entry.getKey()).getInt() == 2) {
0685: entry.setValue(new HashValue(15));
0686: }
0687: }
0688: }
0689: }
0690:
0691: void testEntrySetIteratorRemove1(ConcurrentHashMap map,
0692: boolean validate) {
0693: Map toPut = new HashMap();
0694: toPut.put(keyRoots[0], valueRoots[0]);
0695: toPut.put(keyRoots[1], valueRoots[1]);
0696: toPut.put(keyRoots[2], valueRoots[2]);
0697: toPut.put(keyRoots[3], valueRoots[3]);
0698: if (validate) {
0699: toPut.remove(keyRoots[1]);
0700: assertMappingsEqual(toPut, map);
0701: } else {
0702: map.putAll(toPut);
0703: assertMappingsEqual(toPut, map);
0704:
0705: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
0706: Map.Entry e = (Map.Entry) i.next();
0707: if (e.getKey().equals(keyRoots[1])) {
0708: i.remove();
0709: break;
0710: }
0711: }
0712: }
0713: }
0714:
0715: void testEntrySetIteratorRemove2(ConcurrentHashMap map,
0716: boolean validate) {
0717: Map toPut = new HashMap();
0718: toPut.put(hashKeys[0], hashValues[0]);
0719: toPut.put(hashKeys[1], hashValues[1]);
0720: toPut.put(hashKeys[2], hashValues[2]);
0721: toPut.put(hashKeys[3], hashValues[3]);
0722: if (validate) {
0723: toPut.remove(hashKeys[1]);
0724: assertMappingsHashEqual(toPut, map);
0725: } else {
0726: map.putAll(toPut);
0727: assertMappingsHashEqual(toPut, map);
0728:
0729: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
0730: Map.Entry e = (Map.Entry) i.next();
0731: if (e.getKey().equals(hashKeys[1])) {
0732: i.remove();
0733: break;
0734: }
0735: }
0736: }
0737: }
0738:
0739: void testEntrySetToArray1(ConcurrentHashMap map, boolean validate) {
0740: Map toPut = new HashMap();
0741: toPut.put(keyRoots[0], valueRoots[0]);
0742: toPut.put(keyRoots[1], valueRoots[1]);
0743: toPut.put(keyRoots[2], valueRoots[2]);
0744: toPut.put(keyRoots[3], valueRoots[3]);
0745: Object[] array = getArray(map, false);
0746:
0747: if (validate) {
0748: assertMappingsEqual(array, map);
0749: } else {
0750: map.putAll(toPut);
0751: synchronized (array) {
0752: Object[] returnArray = map.entrySet().toArray(array);
0753: Assert.assertTrue(returnArray == array);
0754: }
0755: }
0756: }
0757:
0758: void testEntrySetToArray2(ConcurrentHashMap map, boolean validate) {
0759: Map toPut = new HashMap();
0760: toPut.put(hashKeys[0], hashValues[0]);
0761: toPut.put(hashKeys[1], hashValues[1]);
0762: toPut.put(hashKeys[2], hashValues[2]);
0763: toPut.put(hashKeys[3], hashValues[3]);
0764: Object[] array = getArray(map, true);
0765:
0766: if (validate) {
0767: assertMappingsEqual(array, map);
0768: } else {
0769: map.putAll(toPut);
0770: synchronized (array) {
0771: Object[] returnArray = map.entrySet().toArray(array);
0772: Assert.assertTrue(returnArray == array);
0773: }
0774: }
0775: }
0776:
0777: void testAllEntriesSnapshot(ConcurrentHashMap map, boolean validate)
0778: throws Exception {
0779: if (validate) {
0780: Collection set1 = map.entrySet();
0781: Collection set2 = ((TCMap) map)
0782: .__tc_getAllEntriesSnapshot();
0783: Assert.assertTrue(set1 != set2);
0784: assertCollectionsEqual(set1, set2);
0785: } else {
0786: Map toPut = new HashMap();
0787: toPut.put(keyRoots[0], valueRoots[0]);
0788: toPut.put(keyRoots[1], valueRoots[1]);
0789: toPut.put(keyRoots[2], valueRoots[2]);
0790: toPut.put(keyRoots[3], valueRoots[3]);
0791: map.putAll(toPut);
0792: }
0793: }
0794:
0795: void testAllLocalEntriesSnapshot(ConcurrentHashMap map,
0796: boolean validate) throws Exception {
0797: if (validate) {
0798: if (isMutator()) {
0799: // if this is the mutator app, all the values are local
0800: Collection set1 = map.entrySet();
0801: Collection set2 = ((TCMap) map)
0802: .__tc_getAllLocalEntriesSnapshot();
0803:
0804: Assert.assertTrue(set1 != set2);
0805: assertCollectionsEqual(set2, set1);
0806: } else {
0807: // fault in one root locally
0808: map.get(keyRoots[0]);
0809:
0810: Collection set1a = map.entrySet();
0811: Collection set1b = ((TCMap) map)
0812: .__tc_getAllLocalEntriesSnapshot();
0813:
0814: Assert.assertTrue(set1a != set1b);
0815: Assert.assertTrue(set1a.size() != set1b.size());
0816: Assert.assertEquals(1, set1b.size());
0817:
0818: // fault in a second root locally
0819: map.get(keyRoots[3]);
0820:
0821: Collection set2a = map.entrySet();
0822: Collection set2b = ((TCMap) map)
0823: .__tc_getAllLocalEntriesSnapshot();
0824:
0825: Assert.assertTrue(set2a != set2b);
0826: Assert.assertTrue(set2a.size() != set2b.size());
0827: Assert.assertEquals(2, set2b.size());
0828:
0829: // fault in a third root locally
0830: map.get(keyRoots[2]);
0831:
0832: Collection set3a = map.entrySet();
0833: Collection set3b = ((TCMap) map)
0834: .__tc_getAllLocalEntriesSnapshot();
0835:
0836: Assert.assertTrue(set3a != set3b);
0837: Assert.assertTrue(set3a.size() != set3b.size());
0838: Assert.assertEquals(3, set3b.size());
0839: }
0840: } else {
0841: Map toPut = new HashMap();
0842: toPut.put(keyRoots[0], valueRoots[0]);
0843: toPut.put(keyRoots[1], valueRoots[1]);
0844: toPut.put(keyRoots[2], valueRoots[2]);
0845: toPut.put(keyRoots[3], valueRoots[3]);
0846: map.putAll(toPut);
0847: }
0848: }
0849:
0850: // This test doesn't work yet, since I need to figure out a way to reset
0851: // the recently accessed count of the map entry values. This is normally
0852: // done by the evictor, but in this test I'd like to enforce it.
0853: // void testClearable(ConcurrentHashMap map, boolean validate) throws Exception {
0854: // // make sure that all the clients are run in sequential fashion
0855: // // this ensures that the entry values are not cleared by another client
0856: // // after they have been faulted in and before they have been cleared
0857: // if (validate) {
0858: // if (isMutator()) {
0859: // // in the mutator, all the map entries are local
0860: // int result = ((Clearable)map).__tc_clearReferences(100);
0861: // Assert.assertEquals(4, result);
0862: // } else {
0863: // // fault in two values
0864: // map.get(keyRoots[0]);
0865: // map.get(keyRoots[2]);
0866: // // check that two have been cleared
0867: // Assert.assertEquals(2, ((Clearable)map).__tc_clearReferences(100));
0868: //
0869: // // fault in three values
0870: // map.get(keyRoots[0]);
0871: // map.get(keyRoots[3]);
0872: // map.get(keyRoots[2]);
0873: // // check that one has been cleared
0874: // Assert.assertEquals(1, ((Clearable)map).__tc_clearReferences(1));
0875: // // check that two have been cleared
0876: // Assert.assertEquals(2, ((Clearable)map).__tc_clearReferences(100));
0877: // }
0878: // } else {
0879: // Map toPut = new HashMap();
0880: // toPut.put(keyRoots[0], valueRoots[0]);
0881: // toPut.put(keyRoots[1], valueRoots[1]);
0882: // toPut.put(keyRoots[2], valueRoots[2]);
0883: // toPut.put(keyRoots[3], valueRoots[3]);
0884: // map.putAll(toPut);
0885: // }
0886: // }
0887:
0888: void testValuesClear1(ConcurrentHashMap map, boolean validate) {
0889: Map toPut = new HashMap();
0890: toPut.put(keyRoots[0], valueRoots[0]);
0891: toPut.put(keyRoots[1], valueRoots[1]);
0892: toPut.put(keyRoots[2], valueRoots[2]);
0893: toPut.put(keyRoots[3], valueRoots[3]);
0894:
0895: if (validate) {
0896: Assert.assertEquals(0, map.size());
0897: } else {
0898: map.putAll(toPut);
0899: map.values().clear();
0900: }
0901: }
0902:
0903: void testValuesClear2(ConcurrentHashMap map, boolean validate) {
0904: Map toPut = new HashMap();
0905: toPut.put(hashKeys[0], hashValues[0]);
0906: toPut.put(hashKeys[1], hashValues[1]);
0907: toPut.put(hashKeys[2], hashValues[2]);
0908: toPut.put(hashKeys[3], hashValues[3]);
0909:
0910: if (validate) {
0911: Assert.assertEquals(0, map.size());
0912: } else {
0913: map.putAll(toPut);
0914: map.values().clear();
0915: }
0916: }
0917:
0918: void testValuesContains1(ConcurrentHashMap map, boolean validate) {
0919: Map toPut = new HashMap();
0920: toPut.put(keyRoots[0], valueRoots[0]);
0921: toPut.put(keyRoots[1], valueRoots[1]);
0922: toPut.put(keyRoots[2], valueRoots[2]);
0923: toPut.put(keyRoots[3], valueRoots[3]);
0924:
0925: if (validate) {
0926: Assert.assertTrue(map.values().contains(valueRoots[2]));
0927: } else {
0928: map.putAll(toPut);
0929: }
0930: }
0931:
0932: void testValuesContains2(ConcurrentHashMap map, boolean validate) {
0933: Map toPut = new HashMap();
0934: toPut.put(hashKeys[0], hashValues[0]);
0935: toPut.put(hashKeys[1], hashValues[1]);
0936: toPut.put(hashKeys[2], hashValues[2]);
0937: toPut.put(hashKeys[3], hashValues[3]);
0938:
0939: if (validate) {
0940: Assert.assertTrue(map.values().contains(new HashValue(20)));
0941: } else {
0942: map.putAll(toPut);
0943: }
0944: }
0945:
0946: void testValuesContainsAll1(ConcurrentHashMap map, boolean validate) {
0947: Map toPut = new HashMap();
0948: toPut.put(keyRoots[0], valueRoots[0]);
0949: toPut.put(keyRoots[1], valueRoots[1]);
0950: toPut.put(keyRoots[2], valueRoots[2]);
0951: toPut.put(keyRoots[3], valueRoots[3]);
0952:
0953: if (validate) {
0954: Assert.assertTrue(map.values().containsAll(toPut.values()));
0955: } else {
0956: map.putAll(toPut);
0957: }
0958: }
0959:
0960: void testValuesContainsAll2(ConcurrentHashMap map, boolean validate) {
0961: Map toPut = new HashMap();
0962: toPut.put(hashKeys[0], hashValues[0]);
0963: toPut.put(hashKeys[1], hashValues[1]);
0964: toPut.put(hashKeys[2], hashValues[2]);
0965: toPut.put(hashKeys[3], hashValues[3]);
0966:
0967: if (validate) {
0968: Assert.assertTrue(map.values().containsAll(toPut.values()));
0969: } else {
0970: map.putAll(toPut);
0971: }
0972: }
0973:
0974: void testValuesRemove1(ConcurrentHashMap map, boolean validate) {
0975: Map toPut = new HashMap();
0976: toPut.put(keyRoots[0], valueRoots[0]);
0977: toPut.put(keyRoots[1], valueRoots[1]);
0978: toPut.put(keyRoots[2], valueRoots[2]);
0979: toPut.put(keyRoots[3], valueRoots[3]);
0980:
0981: if (validate) {
0982: toPut.remove(keyRoots[1]);
0983: assertMappingsEqual(toPut, map);
0984: } else {
0985: map.putAll(toPut);
0986: map.values().remove(valueRoots[1]);
0987: }
0988: }
0989:
0990: void testValuesRemove2(ConcurrentHashMap map, boolean validate) {
0991: Map toPut = new HashMap();
0992: toPut.put(hashKeys[0], hashValues[0]);
0993: toPut.put(hashKeys[1], hashValues[1]);
0994: toPut.put(hashKeys[2], hashValues[2]);
0995: toPut.put(hashKeys[3], hashValues[3]);
0996:
0997: if (validate) {
0998: toPut.remove(hashKeys[1]);
0999: assertMappingsHashEqual(toPut, map);
1000: } else {
1001: map.putAll(toPut);
1002: map.values().remove(new HashValue(20));
1003: }
1004: }
1005:
1006: void testValuesRemoveAll1(ConcurrentHashMap map, boolean validate) {
1007: Map toPut = new HashMap();
1008: toPut.put(keyRoots[0], valueRoots[0]);
1009: toPut.put(keyRoots[1], valueRoots[1]);
1010: toPut.put(keyRoots[2], valueRoots[2]);
1011: toPut.put(keyRoots[3], valueRoots[3]);
1012:
1013: if (validate) {
1014: List expect = new ArrayList();
1015: expect.add(valueRoots[0]);
1016: expect.add(valueRoots[2]);
1017: assertCollectionsEqual(expect, map.values());
1018: } else {
1019: map.putAll(toPut);
1020: List toRemove = new ArrayList(2);
1021: toRemove.add(valueRoots[1]);
1022: toRemove.add(valueRoots[3]);
1023: map.values().removeAll(toRemove);
1024: }
1025: }
1026:
1027: void testValuesRemoveAll2(ConcurrentHashMap map, boolean validate) {
1028: Map toPut = new HashMap();
1029: toPut.put(hashKeys[0], hashValues[0]);
1030: toPut.put(hashKeys[1], hashValues[1]);
1031: toPut.put(hashKeys[2], hashValues[2]);
1032: toPut.put(hashKeys[3], hashValues[3]);
1033:
1034: if (validate) {
1035: List expect = new ArrayList();
1036: expect.add(hashValues[0]);
1037: expect.add(hashValues[2]);
1038: assertCollectionsEqual(expect, map.values());
1039: } else {
1040: map.putAll(toPut);
1041: List toRemove = new ArrayList(2);
1042: toRemove.add(new HashValue(20));
1043: toRemove.add(new HashValue(40));
1044: map.values().removeAll(toRemove);
1045: }
1046: }
1047:
1048: void testValuesRetainAll1(ConcurrentHashMap map, boolean validate) {
1049: Map toPut = new HashMap();
1050: toPut.put(keyRoots[0], valueRoots[0]);
1051: toPut.put(keyRoots[1], valueRoots[1]);
1052: toPut.put(keyRoots[2], valueRoots[2]);
1053: toPut.put(keyRoots[3], valueRoots[3]);
1054:
1055: if (validate) {
1056: List expect = new ArrayList();
1057: expect.add(valueRoots[1]);
1058: expect.add(valueRoots[3]);
1059: assertCollectionsEqual(expect, map.values());
1060: } else {
1061: map.putAll(toPut);
1062: List toRetain = new ArrayList(2);
1063: toRetain.add(valueRoots[1]);
1064: toRetain.add(valueRoots[3]);
1065: map.values().retainAll(toRetain);
1066: }
1067: }
1068:
1069: void testValuesRetainAll2(ConcurrentHashMap map, boolean validate) {
1070: Map toPut = new HashMap();
1071: toPut.put(hashKeys[0], hashValues[0]);
1072: toPut.put(hashKeys[1], hashValues[1]);
1073: toPut.put(hashKeys[2], hashValues[2]);
1074: toPut.put(hashKeys[3], hashValues[3]);
1075:
1076: if (validate) {
1077: List expect = new ArrayList();
1078: expect.add(hashValues[1]);
1079: expect.add(hashValues[3]);
1080: assertCollectionsEqual(expect, map.values());
1081: } else {
1082: map.putAll(toPut);
1083: List toRetain = new ArrayList(2);
1084: toRetain.add(new HashValue(20));
1085: toRetain.add(new HashValue(40));
1086: map.values().retainAll(toRetain);
1087: }
1088: }
1089:
1090: void testValuesToArray1(ConcurrentHashMap map, boolean validate) {
1091: Map toPut = new HashMap();
1092: toPut.put(keyRoots[0], valueRoots[0]);
1093: toPut.put(keyRoots[1], valueRoots[1]);
1094: toPut.put(keyRoots[2], valueRoots[2]);
1095: toPut.put(keyRoots[3], valueRoots[3]);
1096: Object[] array = getArray(map, false);
1097:
1098: if (validate) {
1099: assertCollectionsEqual(array, map.values());
1100: } else {
1101: map.putAll(toPut);
1102: synchronized (array) {
1103: Object[] returnArray = map.values().toArray(array);
1104: Assert.assertTrue(returnArray == array);
1105: }
1106: }
1107: }
1108:
1109: void testValuesToArray2(ConcurrentHashMap map, boolean validate) {
1110: Map toPut = new HashMap();
1111: toPut.put(hashKeys[0], hashValues[0]);
1112: toPut.put(hashKeys[1], hashValues[1]);
1113: toPut.put(hashKeys[2], hashValues[2]);
1114: toPut.put(hashKeys[3], hashValues[3]);
1115: Object[] array = getArray(map, false);
1116:
1117: if (validate) {
1118: assertCollectionsEqual(array, map.values());
1119: } else {
1120: map.putAll(toPut);
1121: synchronized (array) {
1122: Object[] returnArray = map.values().toArray(array);
1123: Assert.assertTrue(returnArray == array);
1124: }
1125: }
1126: }
1127:
1128: void testValuesIteratorRemove1(ConcurrentHashMap map,
1129: boolean validate) {
1130: Map toPut = new HashMap();
1131: toPut.put(keyRoots[0], valueRoots[0]);
1132: toPut.put(keyRoots[1], valueRoots[1]);
1133: toPut.put(keyRoots[2], valueRoots[2]);
1134: toPut.put(keyRoots[3], valueRoots[3]);
1135:
1136: if (validate) {
1137: List expect = new ArrayList();
1138: expect.add(valueRoots[0]);
1139: expect.add(valueRoots[2]);
1140: expect.add(valueRoots[3]);
1141: assertCollectionsEqual(expect, map.values());
1142: } else {
1143: map.putAll(toPut);
1144: for (Iterator i = map.values().iterator(); i.hasNext();) {
1145: DataValue value = (DataValue) i.next();
1146: if (value.getInt() == 20) {
1147: i.remove();
1148: }
1149: }
1150: }
1151: }
1152:
1153: void testValuesIteratorRemove2(ConcurrentHashMap map,
1154: boolean validate) {
1155: Map toPut = new HashMap();
1156: toPut.put(hashKeys[0], hashValues[0]);
1157: toPut.put(hashKeys[1], hashValues[1]);
1158: toPut.put(hashKeys[2], hashValues[2]);
1159: toPut.put(hashKeys[3], hashValues[3]);
1160:
1161: if (validate) {
1162: List expect = new ArrayList();
1163: expect.add(hashValues[0]);
1164: expect.add(hashValues[2]);
1165: expect.add(hashValues[3]);
1166: assertCollectionsEqual(expect, map.values());
1167: } else {
1168: map.putAll(toPut);
1169: for (Iterator i = map.values().iterator(); i.hasNext();) {
1170: Object value = i.next();
1171: if (value.equals(new HashValue(20))) {
1172: i.remove();
1173: }
1174: }
1175: }
1176: }
1177:
1178: void testKeySetClear1(ConcurrentHashMap map, boolean validate) {
1179: Map toPut = new HashMap();
1180: toPut.put(keyRoots[0], valueRoots[0]);
1181: toPut.put(keyRoots[1], valueRoots[1]);
1182: toPut.put(keyRoots[2], valueRoots[2]);
1183: toPut.put(keyRoots[3], valueRoots[3]);
1184:
1185: if (validate) {
1186: Assert.assertEquals(0, map.size());
1187: } else {
1188: map.putAll(toPut);
1189: map.keySet().clear();
1190: }
1191: }
1192:
1193: void testKeySetClear2(ConcurrentHashMap map, boolean validate) {
1194: Map toPut = new HashMap();
1195: toPut.put(hashKeys[0], hashValues[0]);
1196: toPut.put(hashKeys[1], hashValues[1]);
1197: toPut.put(hashKeys[2], hashValues[2]);
1198: toPut.put(hashKeys[3], hashValues[3]);
1199:
1200: if (validate) {
1201: Assert.assertEquals(0, map.size());
1202: } else {
1203: map.putAll(toPut);
1204: map.keySet().clear();
1205: }
1206: }
1207:
1208: void testKeySetContains1(ConcurrentHashMap map, boolean validate) {
1209: Map toPut = new HashMap();
1210: toPut.put(keyRoots[0], valueRoots[0]);
1211: toPut.put(keyRoots[1], valueRoots[1]);
1212: toPut.put(keyRoots[2], valueRoots[2]);
1213: toPut.put(keyRoots[3], valueRoots[3]);
1214:
1215: if (validate) {
1216: Assert.assertTrue(map.keySet().contains(keyRoots[2]));
1217: } else {
1218: map.putAll(toPut);
1219: }
1220: }
1221:
1222: void testKeySetContains2(ConcurrentHashMap map, boolean validate) {
1223: Map toPut = new HashMap();
1224: toPut.put(hashKeys[0], hashValues[0]);
1225: toPut.put(hashKeys[1], hashValues[1]);
1226: toPut.put(hashKeys[2], hashValues[2]);
1227: toPut.put(hashKeys[3], hashValues[3]);
1228:
1229: if (validate) {
1230: Assert.assertTrue(map.keySet().contains(new HashKey(2)));
1231: } else {
1232: map.putAll(toPut);
1233: }
1234: }
1235:
1236: void testKeySetContainsAll1(ConcurrentHashMap map, boolean validate) {
1237: Map toPut = new HashMap();
1238: toPut.put(keyRoots[0], valueRoots[0]);
1239: toPut.put(keyRoots[1], valueRoots[1]);
1240: toPut.put(keyRoots[2], valueRoots[2]);
1241: toPut.put(keyRoots[3], valueRoots[3]);
1242:
1243: if (validate) {
1244: Assert.assertTrue(map.keySet().containsAll(toPut.keySet()));
1245: } else {
1246: map.putAll(toPut);
1247: }
1248: }
1249:
1250: void testKeySetContainsAll2(ConcurrentHashMap map, boolean validate) {
1251: Map toPut = new HashMap();
1252: toPut.put(hashKeys[0], hashValues[0]);
1253: toPut.put(hashKeys[1], hashValues[1]);
1254: toPut.put(hashKeys[2], hashValues[2]);
1255: toPut.put(hashKeys[3], hashValues[3]);
1256:
1257: if (validate) {
1258: Assert.assertTrue(map.keySet().containsAll(toPut.keySet()));
1259: } else {
1260: map.putAll(toPut);
1261: }
1262: }
1263:
1264: void testKeySetRemove1(ConcurrentHashMap map, boolean validate) {
1265: Map toPut = new HashMap();
1266: toPut.put(keyRoots[0], valueRoots[0]);
1267: toPut.put(keyRoots[1], valueRoots[1]);
1268: toPut.put(keyRoots[2], valueRoots[2]);
1269: toPut.put(keyRoots[3], valueRoots[3]);
1270:
1271: if (validate) {
1272: toPut.remove(keyRoots[1]);
1273: assertMappingsEqual(toPut, map);
1274: } else {
1275: map.putAll(toPut);
1276: map.keySet().remove(keyRoots[1]);
1277: }
1278: }
1279:
1280: void testKeySetRemove2(ConcurrentHashMap map, boolean validate) {
1281: Map toPut = new HashMap();
1282: toPut.put(hashKeys[0], hashValues[0]);
1283: toPut.put(hashKeys[1], hashValues[1]);
1284: toPut.put(hashKeys[2], hashValues[2]);
1285: toPut.put(hashKeys[3], hashValues[3]);
1286:
1287: if (validate) {
1288: toPut.remove(hashKeys[1]);
1289: assertMappingsHashEqual(toPut, map);
1290: } else {
1291: map.putAll(toPut);
1292: map.keySet().remove(new HashKey(2));
1293: }
1294: }
1295:
1296: void testKeySetRemoveAll1(ConcurrentHashMap map, boolean validate) {
1297: Map toPut = new HashMap();
1298: toPut.put(keyRoots[0], valueRoots[0]);
1299: toPut.put(keyRoots[1], valueRoots[1]);
1300: toPut.put(keyRoots[2], valueRoots[2]);
1301: toPut.put(keyRoots[3], valueRoots[3]);
1302:
1303: if (validate) {
1304: List expect = new ArrayList();
1305: expect.add(keyRoots[0]);
1306: expect.add(keyRoots[2]);
1307: assertCollectionsEqual(expect, map.keySet());
1308: } else {
1309: map.putAll(toPut);
1310: List toRemove = new ArrayList(2);
1311: toRemove.add(keyRoots[1]);
1312: toRemove.add(keyRoots[3]);
1313: map.keySet().removeAll(toRemove);
1314: }
1315: }
1316:
1317: void testKeySetRemoveAll2(ConcurrentHashMap map, boolean validate) {
1318: Map toPut = new HashMap();
1319: toPut.put(hashKeys[0], hashValues[0]);
1320: toPut.put(hashKeys[1], hashValues[1]);
1321: toPut.put(hashKeys[2], hashValues[2]);
1322: toPut.put(hashKeys[3], hashValues[3]);
1323:
1324: if (validate) {
1325: List expect = new ArrayList();
1326: expect.add(hashKeys[0]);
1327: expect.add(hashKeys[2]);
1328: assertCollectionsEqual(expect, map.keySet());
1329: } else {
1330: map.putAll(toPut);
1331: List toRemove = new ArrayList(2);
1332: toRemove.add(new HashKey(2));
1333: toRemove.add(new HashKey(4));
1334: map.keySet().removeAll(toRemove);
1335: }
1336: }
1337:
1338: void testKeySetRetainAll1(ConcurrentHashMap map, boolean validate) {
1339: Map toPut = new HashMap();
1340: toPut.put(keyRoots[0], valueRoots[0]);
1341: toPut.put(keyRoots[1], valueRoots[1]);
1342: toPut.put(keyRoots[2], valueRoots[2]);
1343: toPut.put(keyRoots[3], valueRoots[3]);
1344:
1345: if (validate) {
1346: List expect = new ArrayList();
1347: expect.add(keyRoots[1]);
1348: expect.add(keyRoots[3]);
1349: assertCollectionsEqual(expect, map.keySet());
1350: } else {
1351: map.putAll(toPut);
1352: List toRetain = new ArrayList(2);
1353: toRetain.add(keyRoots[1]);
1354: toRetain.add(keyRoots[3]);
1355: map.keySet().retainAll(toRetain);
1356: }
1357: }
1358:
1359: void testKeySetRetainAll2(ConcurrentHashMap map, boolean validate) {
1360: Map toPut = new HashMap();
1361: toPut.put(hashKeys[0], hashValues[0]);
1362: toPut.put(hashKeys[1], hashValues[1]);
1363: toPut.put(hashKeys[2], hashValues[2]);
1364: toPut.put(hashKeys[3], hashValues[3]);
1365:
1366: if (validate) {
1367: List expect = new ArrayList();
1368: expect.add(hashKeys[1]);
1369: expect.add(hashKeys[3]);
1370: assertCollectionsEqual(expect, map.keySet());
1371: } else {
1372: map.putAll(toPut);
1373: List toRetain = new ArrayList(2);
1374: toRetain.add(hashKeys[1]);
1375: toRetain.add(hashKeys[3]);
1376: map.keySet().retainAll(toRetain);
1377: }
1378: }
1379:
1380: void testKeySetToArray1(ConcurrentHashMap map, boolean validate) {
1381: Map toPut = new HashMap();
1382: toPut.put(keyRoots[0], valueRoots[0]);
1383: toPut.put(keyRoots[1], valueRoots[1]);
1384: toPut.put(keyRoots[2], valueRoots[2]);
1385: toPut.put(keyRoots[3], valueRoots[3]);
1386: Object[] array = getArray(map, false);
1387:
1388: if (validate) {
1389: assertCollectionsEqual(array, map.keySet());
1390: } else {
1391: map.putAll(toPut);
1392: synchronized (array) {
1393: Object[] returnArray = map.keySet().toArray(array);
1394: Assert.assertTrue(returnArray == array);
1395: }
1396: }
1397: }
1398:
1399: void testKeySetToArray2(ConcurrentHashMap map, boolean validate) {
1400: Map toPut = new HashMap();
1401: toPut.put(hashKeys[0], hashValues[0]);
1402: toPut.put(hashKeys[1], hashValues[1]);
1403: toPut.put(hashKeys[2], hashValues[2]);
1404: toPut.put(hashKeys[3], hashValues[3]);
1405: Object[] array = getArray(map, false);
1406:
1407: if (validate) {
1408: assertCollectionsEqual(array, map.keySet());
1409: } else {
1410: map.putAll(toPut);
1411: synchronized (array) {
1412: Object[] returnArray = map.keySet().toArray(array);
1413: Assert.assertTrue(returnArray == array);
1414: }
1415: }
1416: }
1417:
1418: void testKeySetIteratorRemove1(ConcurrentHashMap map,
1419: boolean validate) {
1420: Map toPut = new HashMap();
1421: toPut.put(keyRoots[0], valueRoots[0]);
1422: toPut.put(keyRoots[1], valueRoots[1]);
1423: toPut.put(keyRoots[2], valueRoots[2]);
1424: toPut.put(keyRoots[3], valueRoots[3]);
1425:
1426: if (validate) {
1427: List expect = new ArrayList();
1428: expect.add(keyRoots[0]);
1429: expect.add(keyRoots[2]);
1430: expect.add(keyRoots[3]);
1431: assertCollectionsEqual(expect, map.keySet());
1432: } else {
1433: map.putAll(toPut);
1434: for (Iterator i = map.keySet().iterator(); i.hasNext();) {
1435: DataKey key = (DataKey) i.next();
1436: if (key.getInt() == 2) {
1437: i.remove();
1438: }
1439: }
1440: }
1441: }
1442:
1443: void testKeySetIteratorRemove2(ConcurrentHashMap map,
1444: boolean validate) {
1445: Map toPut = new HashMap();
1446: toPut.put(hashKeys[0], hashValues[0]);
1447: toPut.put(hashKeys[1], hashValues[1]);
1448: toPut.put(hashKeys[2], hashValues[2]);
1449: toPut.put(hashKeys[3], hashValues[3]);
1450:
1451: if (validate) {
1452: List expect = new ArrayList();
1453: expect.add(hashKeys[0]);
1454: expect.add(hashKeys[2]);
1455: expect.add(hashKeys[3]);
1456: assertCollectionsEqual(expect, map.keySet());
1457: } else {
1458: map.putAll(toPut);
1459: for (Iterator i = map.keySet().iterator(); i.hasNext();) {
1460: Object key = i.next();
1461: if (key.equals(new HashKey(2))) {
1462: i.remove();
1463: }
1464: }
1465: }
1466: }
1467:
1468: void assertSingleHashMapping(Object expectedKey,
1469: Object expectedValue, Map map) {
1470: Assert.assertEquals(1, map.size());
1471: Assert.assertEquals(expectedValue, map.get(expectedKey));
1472: }
1473:
1474: void assertMappingsEqual(Object[] expect, Map map) {
1475: Assert.assertEquals(expect.length, map.size());
1476: for (int i = 0; i < expect.length; i++) {
1477: Entry entry = (Entry) expect[i];
1478: Object val = map.get(entry.getKey());
1479: Assert.assertEquals(entry.getValue(), val);
1480: }
1481: }
1482:
1483: void assertCollectionsEqual(Object[] expect, Collection collection) {
1484: Assert.assertEquals(expect.length, collection.size());
1485: for (int i = 0; i < expect.length; i++) {
1486: Assert.assertTrue(collection.contains(expect[i]));
1487: }
1488: }
1489:
1490: void assertCollectionsEqual(Collection expect, Collection collection) {
1491: Assert.assertEquals(expect.size(), collection.size());
1492: for (Iterator i = expect.iterator(); i.hasNext();) {
1493: Object next = i.next();
1494: Assert.assertTrue(collection.contains(next));
1495: }
1496: }
1497:
1498: void assertMappingsHashEqual(Map expect, Map actual) {
1499: Assert.assertEquals(expect.size(), actual.size());
1500:
1501: Set expectEntries = expect.entrySet();
1502: Set actualEntries = actual.entrySet();
1503:
1504: for (Iterator i = expectEntries.iterator(); i.hasNext();) {
1505: Entry entry = (Entry) i.next();
1506: Assert.assertEquals(entry.getValue(), actual.get(entry
1507: .getKey()));
1508: }
1509:
1510: for (Iterator i = actualEntries.iterator(); i.hasNext();) {
1511: Entry entry = (Entry) i.next();
1512: Assert.assertEquals(entry.getValue(), expect.get(entry
1513: .getKey()));
1514: }
1515: }
1516:
1517: void assertMappingsEqual(Map expect, Map actual) {
1518: Assert.assertEquals(expect.size(), actual.size());
1519:
1520: Set expectEntries = expect.entrySet();
1521: Set actualEntries = actual.entrySet();
1522:
1523: for (Iterator i = expectEntries.iterator(); i.hasNext();) {
1524: Entry entry = (Entry) i.next();
1525: Assert.assertEquals(
1526: ((DataValue) entry.getValue()).getInt(),
1527: ((DataValue) actual.get(entry.getKey())).getInt());
1528: }
1529:
1530: for (Iterator i = actualEntries.iterator(); i.hasNext();) {
1531: Entry entry = (Entry) i.next();
1532: Assert.assertEquals(
1533: ((DataValue) entry.getValue()).getInt(),
1534: ((DataValue) expect.get(entry.getKey())).getInt());
1535: }
1536: }
1537:
1538: private Object[] getArray(Map map, boolean hashKey) {
1539: if (!hashKey) {
1540: return (Object[]) sharedMap
1541: .get("arrayforConcurrentHashMap");
1542: } else {
1543: return (Object[]) sharedMap
1544: .get("arrayforConcurrentHashMapWithHashKeys");
1545: }
1546: }
1547:
1548: public static void visitL1DSOConfig(ConfigVisitor visitor,
1549: DSOClientConfigHelper config) {
1550: String testClass = ConcurrentHashMapTestApp.class.getName();
1551: String methodExpression = "* " + testClass + "*.*(..)";
1552: config.addWriteAutolock(methodExpression);
1553: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
1554: config.addIncludePattern(testClass + "$*");
1555: spec.addRoot("keyRoots", "keyRoots");
1556: spec.addRoot("valueRoots", "valueRoots");
1557: spec.addRoot("hashKeys", "hashKeys");
1558: spec.addRoot("hashValues", "hashValues");
1559: }
1560:
1561: private static class SimpleEntry implements Map.Entry {
1562:
1563: private final Object key;
1564: private Object value;
1565:
1566: public SimpleEntry(Object key, Object value) {
1567: this .key = key;
1568: this .value = value;
1569: }
1570:
1571: public SimpleEntry(Map.Entry e) {
1572: this .key = e.getKey();
1573: this .value = e.getValue();
1574: }
1575:
1576: public Object getKey() {
1577: return key;
1578: }
1579:
1580: public Object getValue() {
1581: return value;
1582: }
1583:
1584: public Object setValue(Object value) {
1585: Object oldValue = this .value;
1586: this .value = value;
1587: return oldValue;
1588: }
1589:
1590: public boolean equals(Object o) {
1591: if (!(o instanceof Map.Entry))
1592: return false;
1593: Map.Entry e = (Map.Entry) o;
1594: return eq(key, e.getKey()) && eq(value, e.getValue());
1595: }
1596:
1597: public int hashCode() {
1598: return ((key == null) ? 0 : key.hashCode())
1599: ^ ((value == null) ? 0 : value.hashCode());
1600: }
1601:
1602: public String toString() {
1603: return key + "=" + value;
1604: }
1605:
1606: private static boolean eq(Object o1, Object o2) {
1607: return (o1 == null ? o2 == null : o1.equals(o2));
1608: }
1609: }
1610:
1611: private static class DataKey {
1612: private int i;
1613:
1614: public DataKey(int i) {
1615: super ();
1616: this .i = i;
1617: }
1618:
1619: public int getInt() {
1620: return this .i;
1621: }
1622: }
1623:
1624: private static class DataValue {
1625: private int i;
1626:
1627: public DataValue(int i) {
1628: super ();
1629: this .i = i;
1630: }
1631:
1632: public int getInt() {
1633: return this .i;
1634: }
1635: }
1636:
1637: private static class HashKey {
1638: private int i;
1639:
1640: public HashKey(int i) {
1641: super ();
1642: this .i = i;
1643: }
1644:
1645: public int getInt() {
1646: return this .i;
1647: }
1648:
1649: public int hashCode() {
1650: return i;
1651: }
1652:
1653: public boolean equals(Object obj) {
1654: if (obj == null)
1655: return false;
1656: if (!(obj instanceof HashKey))
1657: return false;
1658: return ((HashKey) obj).i == i;
1659: }
1660: }
1661:
1662: private static class HashValue {
1663: private int i;
1664:
1665: public HashValue(int i) {
1666: super ();
1667: this .i = i;
1668: }
1669:
1670: public int getInt() {
1671: return this .i;
1672: }
1673:
1674: public int hashCode() {
1675: return i;
1676: }
1677:
1678: public boolean equals(Object obj) {
1679: if (obj == null)
1680: return false;
1681: if (!(obj instanceof HashValue))
1682: return false;
1683: return ((HashValue) obj).i == i;
1684: }
1685:
1686: public String toString() {
1687: return super .toString() + ", i: " + i;
1688: }
1689: }
1690: }
|