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 org.apache.commons.collections.FastHashMap;
0008:
0009: import com.tc.exception.TCNonPortableObjectError;
0010: import com.tc.object.config.ConfigVisitor;
0011: import com.tc.object.config.DSOClientConfigHelper;
0012: import com.tc.object.tx.UnlockedSharedObjectException;
0013: import com.tc.object.util.ReadOnlyException;
0014: import com.tc.simulator.app.ApplicationConfig;
0015: import com.tc.simulator.app.ErrorContext;
0016: import com.tc.simulator.listener.ListenerProvider;
0017: import com.tc.util.Assert;
0018: import com.tc.util.TIMUtil;
0019: import com.tc.util.runtime.Vm;
0020: import com.tcclient.ehcache.TimeExpiryMap;
0021:
0022: import gnu.trove.THashMap;
0023: import gnu.trove.TObjectFunction;
0024: import gnu.trove.TObjectHash;
0025:
0026: import java.io.ByteArrayInputStream;
0027: import java.io.ByteArrayOutputStream;
0028: import java.io.IOException;
0029: import java.lang.reflect.Field;
0030: import java.lang.reflect.Method;
0031: import java.util.ArrayList;
0032: import java.util.Collection;
0033: import java.util.Collections;
0034: import java.util.Comparator;
0035: import java.util.HashMap;
0036: import java.util.HashSet;
0037: import java.util.Hashtable;
0038: import java.util.Iterator;
0039: import java.util.LinkedHashMap;
0040: import java.util.List;
0041: import java.util.Map;
0042: import java.util.Properties;
0043: import java.util.Set;
0044: import java.util.TreeMap;
0045: import java.util.Map.Entry;
0046:
0047: public class GenericMapTestApp extends GenericTestApp {
0048:
0049: private static final int LITERAL_VARIANT = 1;
0050: private static final int OBJECT_VARIANT = 2;
0051:
0052: private final Map nonSharedArrayMap = new HashMap();
0053:
0054: public GenericMapTestApp(String appId, ApplicationConfig cfg,
0055: ListenerProvider listenerProvider) {
0056: super (appId, cfg, listenerProvider, Map.class, 2);
0057: }
0058:
0059: protected Object getTestObject(String test) {
0060: List maps = (List) sharedMap.get("maps");
0061:
0062: // This is just to make sure all the expected maps are here.
0063: // As new map classes get added to this test, you'll have to adjust this number obviously
0064: Assert.assertEquals(25 + (Vm.isJDK15Compliant() ? 1 : 0), maps
0065: .size());
0066:
0067: return maps.iterator();
0068: }
0069:
0070: protected void setupTestObject(String test) {
0071: List maps = new ArrayList();
0072:
0073: maps.add(new HashMap());
0074: maps.add(new Hashtable());
0075: maps.add(new TreeMap(new NullTolerantComparator()));
0076: maps.add(new LinkedHashMap());
0077: maps.add(new THashMap());
0078: maps.add(new FastHashMap());
0079: FastHashMap fm = new FastHashMap();
0080: fm.setFast(true);
0081: maps.add(fm);
0082: maps.add(new Properties());
0083: maps.add(new MyHashMap(11));
0084: maps.add(new MyHashMap(new HashMap()));
0085: maps.add(new MyHashMap2());
0086: maps.add(new MyHashMap3(0));
0087: maps.add(new MyTreeMap(new NullTolerantComparator()));
0088: maps.add(new MyTreeMap2(new NullTolerantComparator()));
0089: maps.add(new MyHashtable());
0090: maps.add(new MyHashtable2());
0091: maps.add(new MyLinkedHashMap());
0092: maps.add(new MyLinkedHashMap2());
0093: maps.add(new MyLinkedHashMap3(true));
0094: maps.add(new MyTHashMap());
0095: maps.add(new MyFastHashMap());
0096: maps.add(new MyProperties());
0097: maps.add(new MyProperties2());
0098: maps.add(new MyProperties3());
0099: maps.add(new TimeExpiryMap(1, 100, 200, "testMap")); // no invalidator is running
0100:
0101: if (Vm.isJDK15Compliant()) {
0102: maps.add(makeConcurrentHashMap());
0103: }
0104:
0105: // maps.add(new IdentityHashMap());
0106: // maps.add(new WeakHashMap());
0107:
0108: /*
0109: * sharedMap.put("maps", maps); sharedMap.put("arrayforHashMap", new Object[4]); sharedMap.put("arrayforHashtable",
0110: * new Object[4]); sharedMap.put("arrayforTreeMap", new Object[4]); sharedMap.put("arrayforTreeMap2", new
0111: * Object[4]); sharedMap.put("arrayforTHashMap", new Object[4]); sharedMap.put("arrayforLinkedHashMap", new
0112: * Object[4]); sharedMap.put("arrayforFastHashMap", new Object[4]); sharedMap.put("arrayforProperties", new
0113: * Object[4]); sharedMap.put("arrayforFastHashMapWithFast", new Object[4]); sharedMap.put("arrayforMyHashMap", new
0114: * Object[4]); sharedMap.put("arrayforMyHashMap2", new Object[4]); sharedMap.put("arrayforMyHashMap3", new
0115: * Object[4]); sharedMap.put("arrayforMyTreeMap", new Object[4]); sharedMap.put("arrayforMyHashtable", new
0116: * Object[4]); sharedMap.put("arrayforMyHashtable2", new Object[4]); sharedMap.put("arrayforMyLinkedHashMap", new
0117: * Object[4]); sharedMap.put("arrayforMyLinkedHashMap2", new Object[4]); sharedMap.put("arrayforMyTHashMap", new
0118: * Object[4]); sharedMap.put("arrayforMyFastHashMap", new Object[4]); sharedMap.put("arrayforMyProperties", new
0119: * Object[4]); sharedMap.put("arrayforMyProperties2", new Object[4]);
0120: */
0121:
0122: sharedMap.put("maps", maps);
0123: nonSharedArrayMap.put("arrayforHashMap", new Object[4]);
0124: nonSharedArrayMap.put("arrayforHashtable", new Object[4]);
0125: sharedMap.put("arrayforTreeMap", new Object[4]);
0126: sharedMap.put("arrayforTreeMap2", new Object[4]);
0127: sharedMap.put("arrayforTHashMap", new Object[4]);
0128: nonSharedArrayMap.put("arrayforLinkedHashMap", new Object[4]);
0129: nonSharedArrayMap.put("arrayforFastHashMap", new Object[4]);
0130: nonSharedArrayMap.put("arrayforProperties", new Object[4]);
0131: nonSharedArrayMap.put("arrayforFastHashMapWithFast",
0132: new Object[4]);
0133: nonSharedArrayMap.put("arrayforMyHashMap", new Object[4]);
0134: nonSharedArrayMap.put("arrayforMyHashMap2", new Object[4]);
0135: sharedMap.put("arrayforMyTreeMap", new Object[4]);
0136: nonSharedArrayMap.put("arrayforMyHashtable", new Object[4]);
0137: nonSharedArrayMap.put("arrayforMyHashtable2", new Object[4]);
0138: nonSharedArrayMap.put("arrayforMyLinkedHashMap", new Object[4]);
0139: nonSharedArrayMap
0140: .put("arrayforMyLinkedHashMap2", new Object[4]);
0141: nonSharedArrayMap
0142: .put("arrayforMyLinkedHashMap3", new Object[4]);
0143: sharedMap.put("arrayforMyTHashMap", new Object[4]);
0144: nonSharedArrayMap.put("arrayforMyFastHashMap", new Object[4]);
0145: nonSharedArrayMap.put("arrayforMyProperties", new Object[4]);
0146: nonSharedArrayMap.put("arrayforMyProperties2", new Object[4]);
0147: nonSharedArrayMap.put("arrayforMyProperties3", new Object[4]);
0148: nonSharedArrayMap.put("arrayforTimeExpiryMap", new Object[4]);
0149: sharedMap.put("arrayforConcurrentHashMap", new Object[4]);
0150: }
0151:
0152: private Object makeConcurrentHashMap() {
0153: try {
0154: return Class.forName(
0155: "java.util.concurrent.ConcurrentHashMap")
0156: .newInstance();
0157: } catch (Exception e) {
0158: throw new RuntimeException(e);
0159: }
0160: }
0161:
0162: public static void visitL1DSOConfig(ConfigVisitor visitor,
0163: DSOClientConfigHelper config) {
0164: String testClass = GenericMapTestApp.class.getName();
0165: config.addModule(TIMUtil.COMMONS_COLLECTIONS_3_1, TIMUtil
0166: .getVersion(TIMUtil.COMMONS_COLLECTIONS_3_1));
0167: config.getOrCreateSpec(testClass);
0168: String methodExpression = "* " + testClass + "*.*(..)";
0169: config.addWriteAutolock(methodExpression);
0170: String readOnlyMethodExpression = "* " + testClass
0171: + "*.*ReadOnly*(..)";
0172: config.addReadAutolock(readOnlyMethodExpression);
0173: config.addIncludePattern(testClass + "$*");
0174: config.addIncludePattern(Key.class.getName());
0175: config
0176: .addIncludePattern(NullTolerantComparator.class
0177: .getName());
0178: config.addIncludePattern(SimpleEntry.class.getName());
0179: config.addExcludePattern(MyNonPortableObject.class.getName());
0180:
0181: // this is just a quick way to add TimeExpiryMap to the instrumentation list
0182: config.addModule(TIMUtil.EHCACHE_1_2_4, TIMUtil
0183: .getVersion(TIMUtil.EHCACHE_1_2_4));
0184: }
0185:
0186: // This method is kind of like a macro, it returns an element (E == element) to be used
0187: // in the set based on the variant value
0188: Object E(String val, int variant) {
0189: switch (variant) {
0190: case LITERAL_VARIANT: {
0191: return val;
0192: }
0193: case OBJECT_VARIANT: {
0194: return new Key(null, val);
0195: }
0196: default: {
0197: throw new AssertionError("unknown variant: " + variant);
0198: }
0199: }
0200:
0201: // unreachable
0202: }
0203:
0204: void testBasicUnSynchronizedPut(Map map, boolean validate, int v) {
0205: // if (map instanceof Hashtable) { return; }
0206: if (map instanceof FastHashMap) {
0207: return;
0208: }
0209: if (map instanceof TimeExpiryMap) {
0210: return;
0211: }
0212: if (isCHM(map)) {
0213: return;
0214: }
0215:
0216: if (validate) {
0217: assertEmptyMap(map);
0218: } else {
0219: assertEmptyMap(map);
0220: try {
0221: map.put(E("January", v), "Jan");
0222: throw new AssertionError(
0223: "Should have thrown a UnlockedSharedObjectException");
0224: } catch (UnlockedSharedObjectException use) {
0225: // this is expected.
0226: }
0227: }
0228: }
0229:
0230: void testBasicPut(Map map, boolean validate, int v) {
0231: if (validate) {
0232: assertSingleMapping(map, E("timmy", v), "teck");
0233: if (map instanceof MyHashMap) {
0234: Assert.assertEquals(E("timmy", v), ((MyHashMap) map)
0235: .getKey());
0236: Assert.assertEquals("teck", ((MyHashMap) map)
0237: .getValue());
0238: } else if (map instanceof MyTreeMap2) {
0239: Assert.assertEquals(E("timmy", v), ((MyTreeMap2) map)
0240: .getKey());
0241: Assert.assertEquals("teck", ((MyTreeMap2) map)
0242: .getValue());
0243: } else if (map instanceof MyLinkedHashMap2) {
0244: Assert.assertEquals(E("timmy", v),
0245: ((MyLinkedHashMap2) map).getKey());
0246: Assert.assertEquals("teck", ((MyLinkedHashMap2) map)
0247: .getValue());
0248: } else if (map instanceof MyProperties) {
0249: Assert.assertEquals(E("timmy", v), ((MyProperties) map)
0250: .getKey());
0251: Assert.assertEquals("teck", ((MyProperties) map)
0252: .getValue());
0253: }
0254: } else {
0255: synchronized (map) {
0256: Object prev = map.put(E("timmy", v), "teck");
0257: Assert.assertNull(prev);
0258: }
0259: }
0260: }
0261:
0262: void testBasicPutAll(Map map, boolean validate, int v) {
0263: Map toAdd = getOrderSensitiveMappings(v);
0264:
0265: if (validate) {
0266: assertMappings(toAdd, map);
0267: } else {
0268: synchronized (map) {
0269: map.putAll(toAdd);
0270: }
0271: }
0272: }
0273:
0274: void testPutNullKey(Map map, boolean validate, int v) {
0275: if (!allowsNull(map)) {
0276: return;
0277: }
0278: if (validate) {
0279: assertSingleMapping(map, null, "value");
0280: } else {
0281: synchronized (map) {
0282: Object prev = map.put(null, "value");
0283: Assert.assertNull(prev);
0284: }
0285: }
0286: }
0287:
0288: void testReplaceNullKey(Map map, boolean validate, int v) {
0289: if (!allowsNull(map)) {
0290: return;
0291: }
0292: if (validate) {
0293: assertSingleMapping(map, null, "value2");
0294: } else {
0295: synchronized (map) {
0296: Object prev = map.put(null, "value");
0297: Assert.assertNull(prev);
0298: }
0299:
0300: synchronized (map) {
0301: Object prev = map.put(null, "value2");
0302: Assert.assertEquals("value", prev);
0303: }
0304: }
0305: }
0306:
0307: void testBasicReplace(Map map, boolean validate, int v) {
0308: if (validate) {
0309: assertSingleMapping(map, E("key", v), "value2");
0310: } else {
0311: synchronized (map) {
0312: Object prev = map.put(E("key", v), "value");
0313: Assert.assertNull(prev);
0314: }
0315:
0316: synchronized (map) {
0317: Object prev = map.put(E("key", v), "value2");
0318: Assert.assertEquals("value", prev);
0319: }
0320: }
0321: }
0322:
0323: void testPutNullValue(Map map, boolean validate, int v) {
0324: if (!allowsNull(map)) {
0325: return;
0326: }
0327: if (validate) {
0328: assertSingleMapping(map, E("key", v), null);
0329: } else {
0330: synchronized (map) {
0331: Object prev = map.put(E("key", v), null);
0332: Assert.assertNull(prev);
0333: }
0334: }
0335: }
0336:
0337: void testReplaceKeyRetention(Map map, boolean validate, int v) {
0338: if (validate) {
0339: assertSingleMapping(map, new Key("** doesn't matter **",
0340: "key"), "value2");
0341:
0342: Assert.assertEquals(1, map.keySet().size());
0343: Key retainedKey = (Key) map.keySet().iterator().next();
0344:
0345: String expect = (map instanceof THashMap) ? "id2" : "id1";
0346: Assert.assertEquals(expect, retainedKey.id);
0347: } else {
0348: synchronized (map) {
0349: Object prev = map.put(new Key("id1", "key"), "value");
0350: Assert.assertNull(prev);
0351: }
0352: synchronized (map) {
0353: Object prev = map.put(new Key("id2", "key"), "value2");
0354: Assert.assertEquals("value", prev);
0355: }
0356: }
0357: }
0358:
0359: void testBasicSetProperty(Map map, boolean validate, int v) {
0360: if (!(map instanceof Properties)) {
0361: return;
0362: }
0363:
0364: if (validate) {
0365: assertSingleMapping(map, "timmy", "teck");
0366: } else {
0367: synchronized (map) {
0368: Object previous = ((Properties) map).setProperty(
0369: "timmy", "teck");
0370: Assert.assertNull(previous);
0371: }
0372: }
0373: }
0374:
0375: void testBasicGetProperty(Map map, boolean validate, int v) {
0376: if (!(map instanceof Properties)) {
0377: return;
0378: }
0379:
0380: if (validate) {
0381: Assert.assertEquals("value", ((Properties) map)
0382: .getProperty("key"));
0383: Assert.assertEquals("defaultValue", ((Properties) map)
0384: .getProperty("nonsense", "defaultValue"));
0385: Assert.assertEquals("value", ((Properties) map)
0386: .getProperty("key", "defaultValue"));
0387: } else {
0388: synchronized (map) {
0389: ((Properties) map).setProperty("key", "value");
0390: }
0391: }
0392: }
0393:
0394: void testBasicLoad(Map map, boolean validate, int v) {
0395: if (!(map instanceof Properties)) {
0396: return;
0397: }
0398:
0399: if (validate) {
0400: Properties expectedMap = new Properties();
0401: expectedMap.setProperty("key1", "val1");
0402: expectedMap.setProperty("key2", "val2");
0403: expectedMap.setProperty("key3", "val3");
0404: assertMappings(expectedMap, map);
0405: } else {
0406: synchronized (map) {
0407: Properties data = new Properties();
0408: data.setProperty("key1", "val1");
0409: data.setProperty("key2", "val2");
0410: data.setProperty("key3", "val3");
0411: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
0412: try {
0413: data.store(outputStream, null);
0414: } catch (IOException ioe) {
0415: Assert.fail();
0416: }
0417: ByteArrayInputStream inputStream = new ByteArrayInputStream(
0418: outputStream.toByteArray());
0419: try {
0420: ((Properties) map).load(inputStream);
0421: } catch (IOException ioe) {
0422: Assert.fail();
0423: }
0424: }
0425: }
0426: }
0427:
0428: /*
0429: * Java 1.5 specific API used void testBasicLoadFromXML(Map map, boolean validate, int v) { if (!(map instanceof
0430: * Properties)) { return; } if(validate) { Map expectedMap = new Properties(); expectedMap.put("key1", "val1");
0431: * expectedMap.put("key2", "val2"); expectedMap.put("key3", "val3"); assertMappings(expectedMap, map); } else {
0432: * synchronized (map) { Properties data = new Properties(); data.setProperty("key1", "val1"); data.setProperty("key2",
0433: * "val2"); data.setProperty("key3", "val3"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try {
0434: * data.storeToXML(outputStream, null); } catch (IOException ioe) { Assert.fail(); } ByteArrayInputStream inputStream =
0435: * new ByteArrayInputStream(outputStream.toByteArray()); try { ((Properties)map).loadFromXML(inputStream); } catch
0436: * (IOException ioe) { Assert.fail(); } } } }
0437: */
0438:
0439: void testKeySetClear(Map map, boolean validate, int v) {
0440: if (validate) {
0441: assertEmptyMap(map);
0442: } else {
0443: addMappings(map, 42, v);
0444: synchronized (map) {
0445: map.keySet().clear();
0446: }
0447: }
0448: }
0449:
0450: void testKeySetIteratorRemove(Map map, boolean validate, int v) {
0451: if (validate) {
0452: Map expect = getOrderSensitiveMappings(v);
0453: expect.remove(E("February", v));
0454: expect.remove(E("March", v));
0455: assertMappings(expect, map);
0456: } else {
0457: synchronized (map) {
0458: map.putAll(getOrderSensitiveMappings(v));
0459: }
0460:
0461: synchronized (map) {
0462: for (Iterator i = map.keySet().iterator(); i.hasNext();) {
0463: Object key = i.next();
0464: if (E("February", v).equals(key)
0465: || E("March", v).equals(key)) {
0466: i.remove();
0467: }
0468: }
0469: }
0470: }
0471: }
0472:
0473: void testKeySetIteratorRemoveNull(Map map, boolean validate, int v) {
0474: if (!allowsNull(map)) {
0475: return;
0476: }
0477:
0478: if (validate) {
0479: assertSingleMapping(map, E("key1", v), "value1");
0480: } else {
0481: synchronized (map) {
0482: map.put(E("key1", v), "value1");
0483: map.put(null, "value for null key");
0484: }
0485:
0486: synchronized (map) {
0487: for (Iterator i = map.keySet().iterator(); i.hasNext();) {
0488: Object key = i.next();
0489: if (!E("key1", v).equals(key)) {
0490: i.remove();
0491: }
0492: }
0493: }
0494: }
0495: }
0496:
0497: void testKeySetRemove(Map map, boolean validate, int v) {
0498: if (validate) {
0499: Map expect = getOrderSensitiveMappings(v);
0500: expect.remove(E("February", v));
0501: expect.remove(E("March", v));
0502: assertMappings(expect, map);
0503: } else {
0504: synchronized (map) {
0505: map.putAll(getOrderSensitiveMappings(v));
0506: }
0507:
0508: synchronized (map) {
0509: Set keys = map.keySet();
0510: boolean removed;
0511: removed = keys.remove(E("February", v));
0512: Assert.assertTrue(removed);
0513: removed = keys.remove(E("March", v));
0514: Assert.assertTrue(removed);
0515: removed = keys.remove(E("key4", v));
0516: Assert.assertFalse(removed);
0517: }
0518: }
0519: }
0520:
0521: void testBasicGet(Map map, boolean validate, int v) {
0522: if (validate) {
0523: try {
0524: if (isAccessOrderedLinkedHashMap(map)) {
0525: synchronized (map) {
0526: Assert.assertEquals("value", map
0527: .get(E("key", v)));
0528: }
0529: } else {
0530: Assert.assertEquals("value", map.get(E("key", v)));
0531: }
0532: if (map instanceof MyHashMap) {
0533: Assert.assertEquals("value", ((MyHashMap) map)
0534: .getObject(E("key", v)));
0535: }
0536: } catch (Throwable t) {
0537: System.err.println("*******"
0538: + Thread.currentThread().getName() + ", map: "
0539: + map + "********");
0540: throw new RuntimeException(t);
0541: }
0542: } else {
0543: synchronized (map) {
0544: map.put(E("key", v), "value");
0545: }
0546: }
0547: }
0548:
0549: void testBasicRemove(Map map, boolean validate, int v) {
0550: if (validate) {
0551: assertEmptyMap(map);
0552: } else {
0553: synchronized (map) {
0554: Object prev = map.put(E("key", v), "value");
0555: Assert.assertNull(prev);
0556: }
0557: synchronized (map) {
0558: Object prev = map.remove(E("key", v));
0559: Assert.assertEquals("value", prev);
0560: }
0561: synchronized (map) {
0562: Object prev = map.remove(E("key", v));
0563: Assert.assertNull(prev);
0564: }
0565: }
0566: }
0567:
0568: void testRemoveNullKey(Map map, boolean validate, int v) {
0569: if (!allowsNull(map)) {
0570: return;
0571: }
0572: if (validate) {
0573: assertEmptyMap(map);
0574: } else {
0575: synchronized (map) {
0576: map.put(null, "value");
0577: }
0578: synchronized (map) {
0579: Object removed = map.remove(null);
0580: Assert.assertEquals("value", removed);
0581: }
0582: }
0583: }
0584:
0585: void testClearNonEmpty(Map map, boolean validate, int v) {
0586: if (validate) {
0587: assertEmptyMap(map);
0588: } else {
0589: addMappings(map, 1, v);
0590:
0591: Assert.assertFalse(map.isEmpty());
0592:
0593: synchronized (map) {
0594: map.clear();
0595: }
0596: }
0597: }
0598:
0599: void testClearEmpty(Map map, boolean validate, int v) {
0600: if (validate) {
0601: assertEmptyMap(map);
0602: } else {
0603: Assert.assertTrue(map.isEmpty());
0604: synchronized (map) {
0605: map.clear();
0606: }
0607: }
0608: }
0609:
0610: void testValuesClear(Map map, boolean validate, int v) {
0611: if (validate) {
0612: assertEmptyMap(map);
0613: } else {
0614: addMappings(map, 23, v);
0615:
0616: synchronized (map) {
0617: map.values().clear();
0618: }
0619: }
0620: }
0621:
0622: void testValuesRemove(Map map, boolean validate, int v) {
0623: if (validate) {
0624: assertEmptyMap(map);
0625:
0626: } else {
0627: synchronized (map) {
0628: map.put(E("key1", v), E("value1", v));
0629: }
0630:
0631: synchronized (map) {
0632: boolean removed = map.values().remove(E("value1", v));
0633: Assert.assertTrue(removed);
0634: }
0635: }
0636: }
0637:
0638: void testValuesDuplicateRemove(Map map, boolean validate, int v) {
0639: if (validate) {
0640: if (map instanceof THashMap) {
0641: // values().remove(Object) on THashMap will remove all mappings for the given value, not just the first
0642: assertEmptyMap(map);
0643: } else {
0644: Object expect = sharedMap.get("expect"
0645: + map.getClass().getName());
0646: assertSingleMapping(map, expect, E("value", v));
0647: }
0648: } else {
0649: synchronized (map) {
0650: map.put(E("key1", v), E("value", v));
0651: map.put(E("key2", v), E("value", v));
0652: }
0653:
0654: synchronized (map) {
0655: boolean removed = map.values().remove(E("value", v));
0656: Assert.assertTrue(removed);
0657: Object expectedKey = map.containsKey(E("key1", v)) ? E(
0658: "key1", v) : E("key2", v);
0659: sharedMap.put("expect" + map.getClass().getName(),
0660: expectedKey);
0661: }
0662: }
0663: }
0664:
0665: void testValuesRemoveNull(Map map, boolean validate, int v) {
0666: if (!allowsNull(map)) {
0667: return;
0668: }
0669: if (validate) {
0670: assertSingleMapping(map, E("key1", v), "value1");
0671: } else {
0672: synchronized (map) {
0673: map.put(E("key1", v), "value1");
0674: map.put(E("key for null value", v), null);
0675: map.put(null, E("value for null key", v));
0676: }
0677:
0678: synchronized (map) {
0679: map.values().remove(null);
0680: map.values().remove(E("value for null key", v));
0681: }
0682: }
0683: }
0684:
0685: void testValuesRemoveAll(Map map, boolean validate, int v) {
0686: if (validate) {
0687: Map expect = getOrderSensitiveMappings(v);
0688: expect.remove(E("February", v));
0689: expect.remove(E("March", v));
0690: assertMappings(expect, map);
0691: } else {
0692: synchronized (map) {
0693: map.putAll(getOrderSensitiveMappings(v));
0694: }
0695:
0696: Set toRemove = new HashSet();
0697: toRemove.add(E("Feb", v));
0698: toRemove.add(E("Mar", v));
0699:
0700: synchronized (map) {
0701: boolean removed = map.values().removeAll(toRemove);
0702: Assert.assertTrue(removed);
0703: }
0704: }
0705: }
0706:
0707: void testValuesRetainAll(Map map, boolean validate, int v) {
0708: if (validate) {
0709: assertSingleMapping(map, E("March", v), E("Mar", v));
0710: } else {
0711: synchronized (map) {
0712: map.putAll(getOrderSensitiveMappings(v));
0713: }
0714:
0715: Set toRetain = new HashSet();
0716: toRetain.add(E("Mar", v));
0717:
0718: synchronized (map) {
0719: map.values().retainAll(toRetain);
0720: }
0721: }
0722: }
0723:
0724: void testValuesIteratorRemove(Map map, boolean validate, int v) {
0725: if (validate) {
0726: Map expect = new HashMap();
0727: expect.put(E("key1", v), E("value1", v));
0728: assertMappings(expect, map);
0729: } else {
0730: synchronized (map) {
0731: map.put(E("key1", v), E("value1", v));
0732: map.put(E("key2", v), E("value2", v));
0733: }
0734:
0735: synchronized (map) {
0736: for (Iterator i = map.values().iterator(); i.hasNext();) {
0737: Object value = i.next();
0738: if (E("value2", v).equals(value)) {
0739: i.remove();
0740: }
0741: }
0742: }
0743: }
0744: }
0745:
0746: void testValuesIteratorRemoveNull(Map map, boolean validate, int v) {
0747: if (!allowsNull(map)) {
0748: return;
0749: }
0750: if (validate) {
0751: assertSingleMapping(map, E("key1", v), "value1");
0752: } else {
0753: synchronized (map) {
0754: map.put(E("key1", v), "value1");
0755: map.put(null, E("value for null key", v));
0756: map.put(E("key for null value", v), null);
0757: }
0758:
0759: synchronized (map) {
0760: for (Iterator i = map.values().iterator(); i.hasNext();) {
0761: Object value = i.next();
0762: if (!"value1".equals(value)) {
0763: i.remove();
0764: }
0765: }
0766: }
0767: }
0768: }
0769:
0770: void testValuesIterator(Map map, boolean validate, int v) {
0771: if (validate) {
0772: int count = 0;
0773: for (Iterator i = map.values().iterator(); i.hasNext();) {
0774: count++;
0775: Assert.assertEquals("teck", i.next());
0776: }
0777: Assert.assertEquals(1, count);
0778: } else {
0779: synchronized (map) {
0780: map.put(E("timmy", v), "teck");
0781: }
0782: }
0783: }
0784:
0785: void testKeySetIterator(Map map, boolean validate, int v) {
0786: if (validate) {
0787: int count = 0;
0788: for (Iterator i = map.keySet().iterator(); i.hasNext();) {
0789: count++;
0790: Assert.assertEquals(E("timmy", v), i.next());
0791: }
0792: Assert.assertEquals(1, count);
0793: } else {
0794: synchronized (map) {
0795: map.put(E("timmy", v), "teck");
0796: }
0797: }
0798: }
0799:
0800: void testEntrySetIterator(Map map, boolean validate, int v) {
0801: if (validate) {
0802: int count = 0;
0803: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
0804: count++;
0805: Map.Entry entry = (Entry) i.next();
0806: Assert.assertEquals(E("timmy", v), entry.getKey());
0807: Assert.assertEquals("teck", entry.getValue());
0808: }
0809: Assert.assertEquals(1, count);
0810: } else {
0811: synchronized (map) {
0812: map.put(E("timmy", v), "teck");
0813: }
0814: }
0815: }
0816:
0817: void testEntrySetAdd(Map map, boolean validate, int v) {
0818: // no test for entrySet().add() as HashMap, Hashtable, TreeMap, and THashMap throw
0819: // UnsupportedOperationException.
0820: }
0821:
0822: void testEntrySetValue(Map map, boolean validate, int v) {
0823: if (validate) {
0824: assertSingleMapping(map, E("key", v), "value2");
0825: } else {
0826: synchronized (map) {
0827: map.put(E("key", v), "value1");
0828: }
0829:
0830: synchronized (map) {
0831: Set set = map.entrySet();
0832: for (Iterator i = set.iterator(); i.hasNext();) {
0833: Entry entry = (Entry) i.next();
0834: Object prev = entry.setValue("value2");
0835: Assert.assertEquals("value1", prev);
0836: }
0837: }
0838: }
0839: }
0840:
0841: void testEntrySetValueNull(Map map, boolean validate, int v) {
0842: if (!allowsNull(map))
0843: return;
0844:
0845: if (validate) {
0846: assertSingleMapping(map, E("key", v), null);
0847: } else {
0848: synchronized (map) {
0849: map.put(E("key", v), "value1");
0850: }
0851:
0852: synchronized (map) {
0853: Set set = map.entrySet();
0854: for (Iterator i = set.iterator(); i.hasNext();) {
0855: Entry entry = (Entry) i.next();
0856: Object prev = entry.setValue(null);
0857: Assert.assertEquals("value1", prev);
0858: }
0859: }
0860: }
0861: }
0862:
0863: void testEntrySetRemoveNull(Map map, boolean validate, int v) {
0864: if (!allowsNull(map))
0865: return;
0866:
0867: if (validate) {
0868: assertEmptyMap(map);
0869: } else {
0870: synchronized (map) {
0871: map.put(E("key", v), null);
0872: map.put(null, "value");
0873: }
0874:
0875: addMappings(map, 3, v);
0876:
0877: synchronized (map) {
0878: Set set = map.entrySet();
0879: for (Iterator i = set.iterator(); i.hasNext();) {
0880: i.next();
0881: i.remove();
0882: }
0883: }
0884: }
0885: }
0886:
0887: void testEntrySetRemove(Map map, boolean validate, int v) {
0888: if (validate) {
0889: Map expect = getOrderSensitiveMappings(v);
0890: expect.remove(E("March", v));
0891: assertMappings(expect, map);
0892: } else {
0893: synchronized (map) {
0894: map.putAll(getOrderSensitiveMappings(v));
0895: }
0896:
0897: synchronized (map) {
0898: map.entrySet().remove(
0899: new SimpleEntry(E("March", v), E("Mar", v)));
0900: }
0901: }
0902: }
0903:
0904: void testEntrySetRemoveAll(Map map, boolean validate, int v) {
0905: if (validate) {
0906: Map expect = new HashMap();
0907: expect.put(E("die, die", v), "die, my darling");
0908: expect.put(E("die, die, die", v), "no die");
0909: assertMappings(expect, map);
0910: } else {
0911: synchronized (map) {
0912: map.put(E("die", v), "another day");
0913: map.put(E("die, die", v), "die, my darling");
0914: map.put(E("die, die, die", v), "no die");
0915: map
0916: .put(
0917: E(
0918: "on the last day of your life, don't forget to",
0919: v), "die");
0920: }
0921:
0922: Set removeSet = new HashSet(2);
0923: removeSet.add(new SimpleEntry(E("die", v), "another day"));
0924: removeSet.add(new SimpleEntry(
0925: E("on the last day of your life, don't forget to",
0926: v), "die"));
0927:
0928: synchronized (map) {
0929: map.entrySet().removeAll(removeSet);
0930: }
0931: }
0932: }
0933:
0934: void testKeySetRemoveAll(Map map, boolean validate, int v) {
0935: if (validate) {
0936: Map expect = getOrderSensitiveMappings(v);
0937: expect.remove(E("February", v));
0938: expect.remove(E("March", v));
0939: assertMappings(expect, map);
0940: } else {
0941: synchronized (map) {
0942: map.putAll(getOrderSensitiveMappings(v));
0943: }
0944:
0945: Set toRemove = new HashSet(2);
0946: toRemove.add(E("February", v));
0947: toRemove.add(E("March", v));
0948:
0949: synchronized (map) {
0950: map.keySet().removeAll(toRemove);
0951: }
0952: }
0953: }
0954:
0955: void testKeySetRetainAll(Map map, boolean validate, int v) {
0956: if (validate) {
0957: Map expect = getOrderSensitiveMappings(v);
0958: expect.remove(E("January", v));
0959: expect.remove(E("April", v));
0960: assertMappings(expect, map);
0961: } else {
0962: synchronized (map) {
0963: map.putAll(getOrderSensitiveMappings(v));
0964: }
0965:
0966: Set toRetain = new HashSet(2);
0967: toRetain.add(E("March", v));
0968: toRetain.add(E("February", v));
0969:
0970: synchronized (map) {
0971: map.keySet().retainAll(toRetain);
0972: }
0973: }
0974: }
0975:
0976: void testKeySetRemoveNull(Map map, boolean validate, int v) {
0977: if (!allowsNull(map)) {
0978: return;
0979: }
0980:
0981: if (validate) {
0982: assertSingleMapping(map, E("key1", v), "value1");
0983: } else {
0984: synchronized (map) {
0985: map.put(E("key1", v), "value1");
0986: map.put(null, "value for null key");
0987: map.put(E("key for null value", v), null);
0988: }
0989:
0990: synchronized (map) {
0991: map.keySet().remove(null);
0992: map.keySet().remove(E("key for null value", v));
0993: }
0994: }
0995:
0996: }
0997:
0998: void testEntrySetRetainAll(Map map, boolean validate, int v) {
0999: if (validate) {
1000: // NOTE: this test will fail for THashMap if using releases of trove before 1.1b5. See trove bug 1382196
1001: // http://sourceforge.net/tracker/index.php?func=detail&aid=1382196&group_id=39235&atid=424682
1002: assertSingleMapping(map, E("key1", v), "value1");
1003: } else {
1004: synchronized (map) {
1005: map.put(E("key1", v), "value1");
1006: map.put(E("key2", v), "value2");
1007: map.put(E("key3", v), "value3");
1008: }
1009:
1010: Set retainSet = new HashSet();
1011: retainSet.add(new SimpleEntry(E("key1", v), "value1"));
1012:
1013: synchronized (map) {
1014: map.entrySet().retainAll(retainSet);
1015: }
1016: }
1017: }
1018:
1019: void testEntrySetRetainAll2(Map map, boolean validate, int v) {
1020: if (validate) {
1021: Map expect = getOrderSensitiveMappings(v);
1022: expect.remove(E("January", v));
1023: expect.remove(E("April", v));
1024: assertMappings(expect, map);
1025: } else {
1026: synchronized (map) {
1027: map.putAll(getOrderSensitiveMappings(v));
1028: }
1029:
1030: Set retainSet = new HashSet();
1031: retainSet
1032: .add(new SimpleEntry(E("February", v), E("Feb", v)));
1033: retainSet.add(new SimpleEntry(E("March", v), E("Mar", v)));
1034:
1035: synchronized (map) {
1036: map.entrySet().retainAll(retainSet);
1037: }
1038: }
1039: }
1040:
1041: void testEntrySetClear(Map map, boolean validate, int v) {
1042: if (validate) {
1043: assertEmptyMap(map);
1044: } else {
1045: addMappings(map, 3, v);
1046:
1047: synchronized (map) {
1048: map.entrySet().clear();
1049: }
1050: }
1051: }
1052:
1053: void testEntrySetIteratorRemove(Map map, boolean validate, int v) {
1054: if (validate) {
1055: Map expect = getOrderSensitiveMappings(v);
1056: expect.remove(E("February", v));
1057: expect.remove(E("March", v));
1058: assertMappings(expect, map);
1059: } else {
1060: synchronized (map) {
1061: map.putAll(getOrderSensitiveMappings(v));
1062: }
1063:
1064: synchronized (map) {
1065: Set set = map.entrySet();
1066: for (Iterator i = set.iterator(); i.hasNext();) {
1067: Entry entry = (Entry) i.next();
1068: Object key = entry.getKey();
1069: if (E("February", v).equals(key)
1070: || E("March", v).equals(key)) {
1071: i.remove();
1072: }
1073: }
1074: }
1075: }
1076: }
1077:
1078: void testEntrySetIteratorRemoveNull(Map map, boolean validate, int v) {
1079: if (!allowsNull(map)) {
1080: return;
1081: }
1082: if (validate) {
1083: assertSingleMapping(map, E("key1", v), "value1");
1084: } else {
1085: synchronized (map) {
1086: map.put(E("key1", v), "value1");
1087: map.put(null, "value for null key");
1088: map.put(E("key for null value", v), null);
1089: }
1090:
1091: synchronized (map) {
1092: Set set = map.entrySet();
1093: for (Iterator i = set.iterator(); i.hasNext();) {
1094: Entry entry = (Entry) i.next();
1095: Object key = entry.getKey();
1096: if (!E("key1", v).equals(key)) {
1097: i.remove();
1098: }
1099: }
1100: }
1101: }
1102: }
1103:
1104: void testEntrySetToArray(Map map, boolean validate, int v) {
1105: Object[] array = getArray(map);
1106:
1107: if (validate) {
1108: if (canTestSharedArray(map)) {
1109: assertMappingsEqual(array, map);
1110: }
1111: } else {
1112: synchronized (map) {
1113: map.putAll(getOrderSensitiveMappings(v));
1114: }
1115: synchronized (array) {
1116: Object[] returnArray = map.entrySet().toArray(array);
1117: Assert.assertTrue(returnArray == array);
1118: }
1119: assertMappingsEqual(array, map);
1120: }
1121: }
1122:
1123: void testKeySetToArray(Map map, boolean validate, int v) {
1124: Object[] array = getArray(map);
1125:
1126: if (validate) {
1127: if (canTestSharedArray(map)) {
1128: assertMappingsKeysEqual(array, map.keySet());
1129: }
1130: } else {
1131: synchronized (map) {
1132: map.putAll(getOrderSensitiveMappings(v));
1133: }
1134: synchronized (array) {
1135: Object[] returnArray = map.keySet().toArray(array);
1136: Assert.assertTrue(returnArray == array);
1137: }
1138: assertMappingsKeysEqual(array, map.keySet());
1139: }
1140: }
1141:
1142: void testValuesToArray(Map map, boolean validate, int v) {
1143: Object[] array = getArray(map);
1144:
1145: if (validate) {
1146: if (canTestSharedArray(map)) {
1147: assertMappingsKeysEqual(array, map.values());
1148: }
1149: } else {
1150: synchronized (map) {
1151: map.putAll(getOrderSensitiveMappings(v));
1152: }
1153: synchronized (array) {
1154: Object[] returnArray = map.values().toArray(array);
1155: Assert.assertTrue(returnArray == array);
1156: }
1157: assertMappingsKeysEqual(array, map.values());
1158: }
1159: }
1160:
1161: void testFastHashMapSetFast(Map map, boolean validate, int v) {
1162: if (!(map instanceof FastHashMap)) {
1163: return;
1164: }
1165:
1166: FastHashMap fastHashMap = (FastHashMap) map;
1167: if (validate) {
1168: Assert.assertTrue(fastHashMap.getFast());
1169: } else {
1170: synchronized (fastHashMap) {
1171: fastHashMap.setFast(true);
1172: }
1173: }
1174: }
1175:
1176: // ReadOnly testing methods.
1177: void testReadOnlyPut(Map map, boolean validate, int v) {
1178: if (!canTestReadOnly(map)) {
1179: return;
1180: }
1181:
1182: if (validate) {
1183: assertEmptyMap(map);
1184: } else {
1185: synchronized (map) {
1186: try {
1187: map.put(E("timmy", v), "teck");
1188: throw new AssertionError(
1189: "Should have thrown a ReadOnlyException");
1190: } catch (ReadOnlyException re) {
1191: // expected
1192: }
1193: }
1194: }
1195: }
1196:
1197: void testReadOnlyPutAll(Map map, boolean validate, int v) {
1198: if (!canTestReadOnly(map)) {
1199: return;
1200: }
1201:
1202: if (validate) {
1203: assertEmptyMap(map);
1204: } else {
1205: synchronized (map) {
1206: try {
1207: Map toAdd = new HashMap();
1208: addMappings(toAdd, 3, v);
1209: map.putAll(toAdd);
1210: throw new AssertionError(
1211: "Should have thrown a ReadOnlyException");
1212: } catch (ReadOnlyException t) {
1213: // expected
1214: }
1215: }
1216: }
1217: }
1218:
1219: // Setting up for the ReadOnly test for remove.
1220: void testSetUpRemove(Map map, boolean validate, int v) {
1221: if (!canTestReadOnly(map)) {
1222: return;
1223: }
1224:
1225: Map toAdd = new HashMap();
1226: addMappings(toAdd, 3, v);
1227:
1228: if (validate) {
1229: assertMappings(toAdd, map);
1230: } else {
1231: synchronized (map) {
1232: map.putAll(toAdd);
1233: }
1234: tryReadOnlyRemove(map, v);
1235: }
1236:
1237: }
1238:
1239: // tryReadOnlyRemove() goes hand in hand with testSetUpRemove().
1240: private void tryReadOnlyRemove(Map map, int v) {
1241: synchronized (map) {
1242: try {
1243: map.remove(E("key2", v));
1244: throw new AssertionError(
1245: "Should have thrown a ReadOnlyException");
1246: } catch (ReadOnlyException t) {
1247: // expected
1248: }
1249: }
1250: }
1251:
1252: // Setting up for the ReadOnly test for clear.
1253: void testSetUpClear(Map map, boolean validate, int v) {
1254: if (!canTestReadOnly(map)) {
1255: return;
1256: }
1257:
1258: Map toAdd = new HashMap();
1259: addMappings(toAdd, 3, v);
1260:
1261: if (validate) {
1262: assertMappings(toAdd, map);
1263: } else {
1264: synchronized (map) {
1265: map.putAll(toAdd);
1266: }
1267: tryReadOnlyClear(map);
1268: }
1269:
1270: }
1271:
1272: // tryReadOnlyClear() goes hand in hand with testSetUpClear().
1273: private void tryReadOnlyClear(Map map) {
1274: synchronized (map) {
1275: try {
1276: map.clear();
1277: throw new AssertionError(
1278: "Should have thrown a ReadOnlyException");
1279: } catch (ReadOnlyException t) {
1280: // expected
1281: }
1282: }
1283: }
1284:
1285: // Setting up for the ReadOnly test for entry set clear.
1286: void testSetUpEntrySetClear(Map map, boolean validate, int v) {
1287: if (!canTestReadOnly(map)) {
1288: return;
1289: }
1290:
1291: Map toAdd = new HashMap();
1292: addMappings(toAdd, 3, v);
1293:
1294: if (validate) {
1295: assertMappings(toAdd, map);
1296: } else {
1297: synchronized (map) {
1298: map.putAll(toAdd);
1299: }
1300: tryReadOnlyEntrySetClear(map);
1301: }
1302:
1303: }
1304:
1305: // tryReadOnlyEntrySetClear() goes hand in hand with testSetUpEntrySetClear().
1306: private void tryReadOnlyEntrySetClear(Map map) {
1307: synchronized (map) {
1308: Set entrySet = map.entrySet();
1309: try {
1310: entrySet.clear();
1311: throw new AssertionError(
1312: "Should have thrown a ReadOnlyException");
1313: } catch (ReadOnlyException t) {
1314: // expected
1315: }
1316: }
1317: }
1318:
1319: // Setting up for the ReadOnly test for entry set remove.
1320: void testSetUpEntrySetRemove(Map map, boolean validate, int v) {
1321: if (!canTestReadOnly(map)) {
1322: return;
1323: }
1324:
1325: Map toAdd = new HashMap();
1326: addMappings(toAdd, 3, v);
1327:
1328: if (validate) {
1329: assertMappings(toAdd, map);
1330: } else {
1331: synchronized (map) {
1332: map.putAll(toAdd);
1333: }
1334: tryReadOnlyEntrySetRemove(map);
1335: }
1336:
1337: }
1338:
1339: // tryReadOnlyEntrySetRemove() goes hand in hand with testSetUpEntrySetRemove().
1340: private void tryReadOnlyEntrySetRemove(Map map) {
1341: synchronized (map) {
1342: Set entrySet = map.entrySet();
1343: Iterator iterator = entrySet.iterator();
1344: Object o = iterator.next();
1345: try {
1346: entrySet.remove(o);
1347: throw new AssertionError(
1348: "Should have thrown a ReadOnlyException");
1349: } catch (ReadOnlyException t) {
1350: // expected
1351: }
1352: }
1353: }
1354:
1355: // Setting up for the ReadOnly test for entry set retainAll.
1356: void testSetUpEntrySetRetainAll(Map map, boolean validate, int v) {
1357: if (!canTestReadOnly(map)) {
1358: return;
1359: }
1360:
1361: Map toAdd = new HashMap();
1362: addMappings(toAdd, 3, v);
1363:
1364: if (validate) {
1365: assertMappings(toAdd, map);
1366: } else {
1367: synchronized (map) {
1368: map.putAll(toAdd);
1369: }
1370: tryReadOnlyEntrySetRetainAll(map);
1371: }
1372:
1373: }
1374:
1375: // tryReadOnlyEntrySetRetainAll() goes hand in hand with testSetUpEntrySetRetainAll().
1376: private void tryReadOnlyEntrySetRetainAll(Map map) {
1377: synchronized (map) {
1378: Set entrySet = map.entrySet();
1379: Object o = entrySet.iterator().next();
1380: Set retainAll = new HashSet();
1381: retainAll.add(o);
1382: try {
1383: entrySet.retainAll(retainAll);
1384: throw new AssertionError(
1385: "Should have thrown a ReadOnlyException");
1386: } catch (ReadOnlyException t) {
1387: // ignore the ReadOnlyException in test
1388: }
1389: }
1390: }
1391:
1392: // Setting up for the ReadOnly test for entry set removeAll.
1393: void testSetUpEntrySetRemoveAll(Map map, boolean validate, int v) {
1394: if (!canTestReadOnly(map)) {
1395: return;
1396: }
1397:
1398: Map toAdd = new HashMap();
1399: addMappings(toAdd, 3, v);
1400:
1401: if (validate) {
1402: assertMappings(toAdd, map);
1403: } else {
1404: synchronized (map) {
1405: map.putAll(toAdd);
1406: }
1407: tryReadOnlyEntrySetRemoveAll(map);
1408: }
1409:
1410: }
1411:
1412: // tryReadOnlyEntrySetRemoveAll() goes hand in hand with testSetUpEntrySetRemoveAll().
1413: private void tryReadOnlyEntrySetRemoveAll(Map map) {
1414: synchronized (map) {
1415: Set entrySet = map.entrySet();
1416: Object o = entrySet.iterator().next();
1417: Set removeAll = new HashSet();
1418: removeAll.add(o);
1419: try {
1420: entrySet.removeAll(removeAll);
1421: throw new AssertionError(
1422: "Should have thrown a ReadOnlyException");
1423: } catch (ReadOnlyException t) {
1424: // Expected
1425: }
1426: }
1427: }
1428:
1429: // Setting up for the ReadOnly test for entry set iterator remove.
1430: void testSetUpEntrySetIteratorRemove(Map map, boolean validate,
1431: int v) {
1432: if (!canTestReadOnly(map)) {
1433: return;
1434: }
1435:
1436: Map toAdd = new HashMap();
1437: addMappings(toAdd, 3, v);
1438:
1439: if (validate) {
1440: assertMappings(toAdd, map);
1441: } else {
1442: synchronized (map) {
1443: map.putAll(toAdd);
1444: }
1445: tryReadOnlyEntrySetIteratorRemove(map);
1446: }
1447:
1448: }
1449:
1450: // tryReadOnlyEntrySetIteratorRemove() goes hand in hand with testSetUpEntrySetIteratorRemove().
1451: private void tryReadOnlyEntrySetIteratorRemove(Map map) {
1452: synchronized (map) {
1453: Iterator iterator = map.entrySet().iterator();
1454: try {
1455: iterator.next();
1456: iterator.remove();
1457: throw new AssertionError(
1458: "Should have thrown a ReadOnlyException");
1459: } catch (ReadOnlyException t) {
1460: // Expected
1461: }
1462: }
1463: }
1464:
1465: // Setting up for the ReadOnly test for entry set setValue.
1466: void testSetUpEntrySet(Map map, boolean validate, int v) {
1467: if (!canTestReadOnly(map)) {
1468: return;
1469: }
1470:
1471: Map toAdd = new HashMap();
1472: addMappings(toAdd, 3, v);
1473:
1474: if (validate) {
1475: assertMappings(toAdd, map);
1476: } else {
1477: synchronized (map) {
1478: map.putAll(toAdd);
1479: }
1480: tryReadOnlyEntrySet(map);
1481: }
1482:
1483: }
1484:
1485: // tryReadOnlyEntrySet() goes hand in hand with testSetUpEntrySet().
1486: private void tryReadOnlyEntrySet(Map map) {
1487: synchronized (map) {
1488: Iterator iterator = map.entrySet().iterator();
1489: try {
1490: Entry entry = (Entry) iterator.next();
1491: entry.setValue("modified value");
1492: throw new AssertionError(
1493: "Should have thrown a ReadOnlyException");
1494: } catch (ReadOnlyException t) {
1495: // Expected
1496: }
1497: }
1498: }
1499:
1500: // Setting up for the ReadOnly test for key set clear.
1501: void testSetUpKeySetClear(Map map, boolean validate, int v) {
1502: if (!canTestReadOnly(map)) {
1503: return;
1504: }
1505:
1506: Map toAdd = new HashMap();
1507: addMappings(toAdd, 3, v);
1508:
1509: if (validate) {
1510: assertMappings(toAdd, map);
1511: } else {
1512: synchronized (map) {
1513: map.putAll(toAdd);
1514: }
1515: tryReadOnlyKeySetClear(map);
1516: }
1517:
1518: }
1519:
1520: // tryReadOnlyKeySetClear() goes hand in hand with testSetUpKeySetClear().
1521: private void tryReadOnlyKeySetClear(Map map) {
1522: synchronized (map) {
1523: Set keySet = map.keySet();
1524: try {
1525: keySet.clear();
1526: throw new AssertionError(
1527: "Should have thrown a ReadOnlyException");
1528: } catch (ReadOnlyException t) {
1529: // Expected
1530: }
1531: }
1532: }
1533:
1534: // Setting up for the ReadOnly test for key set retainAll.
1535: void testSetUpKeySetRetainAll(Map map, boolean validate, int v) {
1536: if (!canTestReadOnly(map)) {
1537: return;
1538: }
1539:
1540: Map toAdd = new HashMap();
1541: addMappings(toAdd, 3, v);
1542:
1543: if (validate) {
1544: assertMappings(toAdd, map);
1545: } else {
1546: synchronized (map) {
1547: map.putAll(toAdd);
1548: }
1549: tryReadOnlyKeySetRetainAll(map);
1550: }
1551:
1552: }
1553:
1554: // tryReadOnlyKeySetRetainAll() goes hand in hand with testSetUpKeySetRetainAll().
1555: private void tryReadOnlyKeySetRetainAll(Map map) {
1556: synchronized (map) {
1557: Set keySet = map.keySet();
1558: Object o = keySet.iterator().next();
1559: Set retainAll = new HashSet();
1560: retainAll.add(o);
1561: try {
1562: keySet.retainAll(retainAll);
1563: throw new AssertionError(
1564: "Should have thrown a ReadOnlyException");
1565: } catch (ReadOnlyException t) {
1566: // Expected
1567: }
1568: }
1569: }
1570:
1571: // Setting up for the ReadOnly test for key set removeAll.
1572: void testSetUpKeySetRemoveAll(Map map, boolean validate, int v) {
1573: if (!canTestReadOnly(map)) {
1574: return;
1575: }
1576:
1577: Map toAdd = new HashMap();
1578: addMappings(toAdd, 3, v);
1579:
1580: if (validate) {
1581: assertMappings(toAdd, map);
1582: } else {
1583: synchronized (map) {
1584: map.putAll(toAdd);
1585: }
1586: tryReadOnlyKeySetRemoveAll(map);
1587: }
1588:
1589: }
1590:
1591: // tryReadOnlyKeySetRemoveAll() goes hand in hand with testSetUpKeySetRemoveAll().
1592: private void tryReadOnlyKeySetRemoveAll(Map map) {
1593: synchronized (map) {
1594: Set keySet = map.keySet();
1595: Object o = keySet.iterator().next();
1596: Set removeAll = new HashSet();
1597: removeAll.add(o);
1598: try {
1599: keySet.removeAll(removeAll);
1600: throw new AssertionError(
1601: "Should have thrown a ReadOnlyException");
1602: } catch (ReadOnlyException t) {
1603: // Expected
1604: }
1605: }
1606: }
1607:
1608: // Setting up for the ReadOnly test for key set iterator remove.
1609: void testSetUpKeySetIteratorRemove(Map map, boolean validate, int v) {
1610: if (!canTestReadOnly(map)) {
1611: return;
1612: }
1613:
1614: Map toAdd = new HashMap();
1615: addMappings(toAdd, 3, v);
1616:
1617: if (validate) {
1618: assertMappings(toAdd, map);
1619: } else {
1620: synchronized (map) {
1621: map.putAll(toAdd);
1622: }
1623: tryReadOnlyKeySetIteratorRemove(map);
1624: }
1625: }
1626:
1627: // tryReadOnlyKeySetIteratorRemove() goes hand in hand with testSetUpKeySetIteratorRemove().
1628: private void tryReadOnlyKeySetIteratorRemove(Map map) {
1629: synchronized (map) {
1630: Iterator iterator = map.keySet().iterator();
1631: try {
1632: iterator.next();
1633: iterator.remove();
1634: throw new AssertionError(
1635: "Should have thrown a ReadOnlyException");
1636: } catch (ReadOnlyException t) {
1637: // Expected
1638: }
1639: }
1640: }
1641:
1642: // Setting up for the ReadOnly test for values clear.
1643: void testSetUpValuesClear(Map map, boolean validate, int v) {
1644: if (!canTestReadOnly(map)) {
1645: return;
1646: }
1647:
1648: Map toAdd = new HashMap();
1649: addMappings(toAdd, 3, v);
1650:
1651: if (validate) {
1652: assertMappings(toAdd, map);
1653: } else {
1654: synchronized (map) {
1655: map.putAll(toAdd);
1656: }
1657: tryReadOnlyValuesClear(map);
1658: }
1659:
1660: }
1661:
1662: // tryReadOnlyValuesClear() goes hand in hand with testSetUpValuesClear().
1663: private void tryReadOnlyValuesClear(Map map) {
1664: synchronized (map) {
1665: Collection values = map.values();
1666: try {
1667: values.clear();
1668: throw new AssertionError(
1669: "Should have thrown a ReadOnlyException");
1670: } catch (ReadOnlyException t) {
1671: // Expected
1672: }
1673: }
1674: }
1675:
1676: // Setting up for the ReadOnly test for values retainAll.
1677: void testSetUpValuesRetainAll(Map map, boolean validate, int v) {
1678: if (!canTestReadOnly(map)) {
1679: return;
1680: }
1681:
1682: Map toAdd = new HashMap();
1683: addMappings(toAdd, 3, v);
1684:
1685: if (validate) {
1686: assertMappings(toAdd, map);
1687: } else {
1688: synchronized (map) {
1689: map.putAll(toAdd);
1690: }
1691: tryReadOnlyValuesRetainAll(map);
1692: }
1693:
1694: }
1695:
1696: // tryReadOnlyKeySetRetainAll() goes hand in hand with testSetUpValuesRetainAll().
1697: private void tryReadOnlyValuesRetainAll(Map map) {
1698: synchronized (map) {
1699: Collection values = map.values();
1700: Object o = values.iterator().next();
1701: Set retainAll = new HashSet();
1702: retainAll.add(o);
1703: try {
1704: values.retainAll(retainAll);
1705: throw new AssertionError(
1706: "Should have thrown a ReadOnlyException");
1707: } catch (ReadOnlyException t) {
1708: // Expected
1709: }
1710: }
1711: }
1712:
1713: // Setting up for the ReadOnly test for values removeAll.
1714: void testSetUpValuesRemoveAll(Map map, boolean validate, int v) {
1715: if (!canTestReadOnly(map)) {
1716: return;
1717: }
1718:
1719: Map toAdd = new HashMap();
1720: addMappings(toAdd, 3, v);
1721:
1722: if (validate) {
1723: assertMappings(toAdd, map);
1724: } else {
1725: synchronized (map) {
1726: map.putAll(toAdd);
1727: }
1728: tryReadOnlyValuesRemoveAll(map);
1729: }
1730:
1731: }
1732:
1733: // tryReadOnlyValuesRemoveAll() goes hand in hand with testSetUpValuesRemoveAll().
1734: private void tryReadOnlyValuesRemoveAll(Map map) {
1735: synchronized (map) {
1736: Collection values = map.values();
1737: Object o = values.iterator().next();
1738: Set removeAll = new HashSet();
1739: removeAll.add(o);
1740: try {
1741: values.removeAll(removeAll);
1742: throw new AssertionError(
1743: "Should have thrown a ReadOnlyException");
1744: } catch (ReadOnlyException t) {
1745: // Expected
1746: }
1747: }
1748: }
1749:
1750: // Setting up for the ReadOnly test for values iterator remove.
1751: void testSetUpValuesIteratorRemove(Map map, boolean validate, int v) {
1752: if (!canTestReadOnly(map)) {
1753: return;
1754: }
1755:
1756: Map toAdd = new HashMap();
1757: addMappings(toAdd, 3, v);
1758:
1759: if (validate) {
1760: assertMappings(toAdd, map);
1761: } else {
1762: synchronized (map) {
1763: map.putAll(toAdd);
1764: }
1765: tryReadOnlyValuesIteratorRemove(map);
1766: }
1767:
1768: }
1769:
1770: // tryReadOnlyValuesIteratorRemove() goes hand in hand with testSetUpValuesIteratorRemove().
1771: private void tryReadOnlyValuesIteratorRemove(Map map) {
1772: synchronized (map) {
1773: Iterator iterator = map.values().iterator();
1774: try {
1775: iterator.next();
1776: iterator.remove();
1777: throw new AssertionError(
1778: "Should have thrown a ReadOnlyException");
1779: } catch (ReadOnlyException t) {
1780: // Expected
1781: }
1782: }
1783: }
1784:
1785: // THashMap specific testing methods.
1786: void testTHashMapRemoveAt(Map map, boolean validate, int v)
1787: throws Exception {
1788: if (!(map instanceof THashMap)) {
1789: return;
1790: }
1791:
1792: Map toAdd = new HashMap();
1793: addMappings(toAdd, 2, v);
1794: if (validate) {
1795: assertMappings(toAdd, map);
1796: } else {
1797: synchronized (map) {
1798: map.putAll(toAdd);
1799: map.put(E("key2", v), "value2");
1800: }
1801: Class mapClass = TObjectHash.class;
1802: Class[] parameterType = new Class[1];
1803:
1804: parameterType[0] = Object.class;
1805: Method m = mapClass.getDeclaredMethod("index",
1806: parameterType);
1807: m.setAccessible(true);
1808: Object indexObj = m.invoke(map,
1809: new Object[] { E("key2", v) });
1810:
1811: synchronized (map) {
1812: parameterType[0] = Integer.TYPE;
1813: m = mapClass.getDeclaredMethod("removeAt",
1814: parameterType);
1815: m.setAccessible(true); // suppressing java access checking since removeRange is
1816: // a protected method.
1817:
1818: m.invoke(map, new Object[] { indexObj });
1819: }
1820:
1821: }
1822: }
1823:
1824: void testTHashMapTransformValues(Map map, boolean validate, int v) {
1825: if (!(map instanceof THashMap)) {
1826: return;
1827: }
1828: THashMap tMap = (THashMap) map;
1829: if (validate) {
1830: for (int i = 0; i < 10; i++) {
1831: Assert.assertEquals(new Integer(i + 1), tMap.get(String
1832: .valueOf(i)));
1833: }
1834: } else {
1835: synchronized (tMap) {
1836: for (int i = 0; i < 10; i++) {
1837: tMap.put(String.valueOf(i), new Integer(i));
1838: }
1839: }
1840: synchronized (tMap) {
1841: tMap.transformValues(new MyObjectFunction());
1842: }
1843: }
1844: }
1845:
1846: void testPutNonPortableObject(Map map, boolean validate, int v) {
1847: if (!canTestNonPortableObject(map)) {
1848: return;
1849: }
1850:
1851: Map toBeAdded = new HashMap();
1852: toBeAdded.put(E("First", v), "First Value");
1853: toBeAdded.put(E("Second", v), "Second Value");
1854: if (validate) {
1855: assertMappings(toBeAdded, map);
1856: } else {
1857: synchronized (map) {
1858: map.putAll(toBeAdded);
1859: }
1860: synchronized (map) {
1861: try {
1862: map.put(E("Non-portable", v),
1863: new MyNonPortableObject());
1864: throw new AssertionError(
1865: "Should have thrown a TCNonPortableObjectError.");
1866: } catch (TCNonPortableObjectError e) {
1867: //
1868: }
1869: }
1870: }
1871: }
1872:
1873: void testLinkedHashMapAccessOrderGet(Map map, boolean validate,
1874: int v) {
1875: if (!isAccessOrderedLinkedHashMap(map)) {
1876: return;
1877: }
1878:
1879: if (validate) {
1880: Map toBeExpected = new LinkedHashMap();
1881: toBeExpected.put(E("First", v), "First Value");
1882: toBeExpected.put(E("Third", v), "Third Value");
1883: toBeExpected.put(E("Second", v), "Second Value"); // access order maps put recently accessed items at the end
1884: assertMappings(toBeExpected, map);
1885: } else {
1886: Map toBeAdded = new LinkedHashMap();
1887: toBeAdded.put(E("First", v), "First Value");
1888: toBeAdded.put(E("Second", v), "Second Value");
1889: toBeAdded.put(E("Third", v), "Third Value");
1890: synchronized (map) {
1891: map.putAll(toBeAdded);
1892: }
1893: synchronized (map) {
1894: Assert.assertEquals("Second Value", map.get(E("Second",
1895: v)));
1896: }
1897: }
1898: }
1899:
1900: void testLinkedHashMapInsertionOrderPut(Map map, boolean validate,
1901: int v) {
1902: if (!(map instanceof LinkedHashMap)) {
1903: return;
1904: }
1905:
1906: // we only want insertion ordered maps in this test
1907: if (isAccessOrderedLinkedHashMap(map)) {
1908: return;
1909: }
1910:
1911: if (validate) {
1912: Map toBeExpected = new LinkedHashMap();
1913: toBeExpected.put(E("First", v), "First Value");
1914: toBeExpected.put(E("Second", v), "New Second Value");
1915: toBeExpected.put(E("Third", v), "Third Value");
1916: assertMappings(toBeExpected, map);
1917: } else {
1918: Map toBeAdded = new LinkedHashMap();
1919: toBeAdded.put(E("First", v), "First Value");
1920: toBeAdded.put(E("Second", v), "Second Value");
1921: toBeAdded.put(E("Third", v), "Third Value");
1922: synchronized (map) {
1923: map.putAll(toBeAdded);
1924: }
1925: synchronized (map) {
1926: // replacing mapping should not affect order (for insertion order maps)
1927: map.put(E("Second", v), "New Second Value");
1928: }
1929: }
1930: }
1931:
1932: void testLinkedHashMapInsertionOrderRemovePut(Map map,
1933: boolean validate, int v) {
1934: if (!(map instanceof LinkedHashMap)) {
1935: return;
1936: }
1937:
1938: // we only want insertion ordered maps in this test
1939: if (isAccessOrderedLinkedHashMap(map)) {
1940: return;
1941: }
1942:
1943: if (validate) {
1944: Map toBeExpected = new LinkedHashMap();
1945: toBeExpected.put(E("First", v), "First Value");
1946: toBeExpected.put(E("Third", v), "Third Value");
1947: toBeExpected.put(E("Second", v), "New Second Value");
1948: assertMappings(toBeExpected, map);
1949: } else {
1950: Map toBeAdded = new LinkedHashMap();
1951: toBeAdded.put(E("First", v), "First Value");
1952: toBeAdded.put(E("Second", v), "Second Value");
1953: toBeAdded.put(E("Third", v), "Third Value");
1954: synchronized (map) {
1955: map.putAll(toBeAdded);
1956: }
1957: synchronized (map) {
1958: map.remove(E("Second", v));
1959: map.put(E("Second", v), "New Second Value");
1960: }
1961: }
1962: }
1963:
1964: void testLinkedHashMapAccessOrderPut(Map map, boolean validate,
1965: int v) {
1966: if (!isAccessOrderedLinkedHashMap(map)) {
1967: return;
1968: }
1969:
1970: if (validate) {
1971: Map toBeExpected = new LinkedHashMap();
1972: toBeExpected.put(E("First", v), "First Value");
1973: toBeExpected.put(E("Third", v), "Third Value");
1974: toBeExpected.put(E("Second", v), "New Second Value"); // access order maps put recently accessed items at the end
1975: assertMappings(toBeExpected, map);
1976: } else {
1977: Map toBeAdded = new LinkedHashMap();
1978: toBeAdded.put(E("First", v), "First Value");
1979: toBeAdded.put(E("Second", v), "Second Value");
1980: toBeAdded.put(E("Third", v), "Third Value");
1981: synchronized (map) {
1982: map.putAll(toBeAdded);
1983: }
1984: synchronized (map) {
1985: // puts count as access on access order linked hash maps
1986: map.put(E("Second", v), "New Second Value");
1987: }
1988: }
1989: }
1990:
1991: private boolean canTestSharedArray(Map map) {
1992: return (!(map instanceof HashMap)
1993: && !(map instanceof LinkedHashMap)
1994: && !(map instanceof Hashtable) && !(map instanceof TimeExpiryMap));
1995: }
1996:
1997: private boolean canTestNonPortableObject(Map map) {
1998: return ((map instanceof HashMap)
1999: || (map instanceof LinkedHashMap) || (map instanceof Hashtable));
2000: }
2001:
2002: private boolean canTestReadOnly(Map map) {
2003: return (!(map instanceof Hashtable)
2004: && !(map instanceof FastHashMap)
2005: && !(map instanceof TimeExpiryMap) && !(isCHM(map)));
2006: }
2007:
2008: /**
2009: * Getting the array for HashMap and LinkedHashMap from a non-shared map temporarily until we fixed HashMap and
2010: * LinkedHashMap.
2011: */
2012: private Object getMySubclassArray(Map map) {
2013: if (map instanceof TimeExpiryMap) {
2014: return nonSharedArrayMap.get("arrayforTimeExpiryMap");
2015: }
2016: if (map instanceof MyLinkedHashMap3) {
2017: return nonSharedArrayMap.get("arrayforMyLinkedHashMap3");
2018: }
2019: if (map instanceof MyLinkedHashMap2) {
2020: return nonSharedArrayMap.get("arrayforMyLinkedHashMap2");
2021: }
2022: if (map instanceof MyLinkedHashMap) {
2023: return nonSharedArrayMap.get("arrayforMyLinkedHashMap");
2024: }
2025: if (map instanceof MyHashMap2) {
2026: return nonSharedArrayMap.get("arrayforMyHashMap2");
2027: }
2028: if (map instanceof MyHashMap) {
2029: return nonSharedArrayMap.get("arrayforMyHashMap");
2030: }
2031: if (map instanceof MyHashMap3) {
2032: return sharedMap.get("arrayforMyHashMap3");
2033: }
2034: if (map instanceof MyTreeMap) {
2035: return sharedMap.get("arrayforMyTreeMap");
2036: }
2037: if (map instanceof MyTreeMap2) {
2038: return sharedMap.get("arrayforMyTreeMap");
2039: }
2040: if (map instanceof MyHashtable2) {
2041: return nonSharedArrayMap.get("arrayforMyHashtable2");
2042: }
2043: if (map instanceof MyHashtable) {
2044: return nonSharedArrayMap.get("arrayforMyHashtable");
2045: }
2046: if (map instanceof MyTHashMap) {
2047: return sharedMap.get("arrayforMyTHashMap");
2048: }
2049: if (map instanceof MyFastHashMap) {
2050: return nonSharedArrayMap.get("arrayforMyFastHashMap");
2051: }
2052: if (map instanceof MyProperties2) {
2053: return nonSharedArrayMap.get("arrayforMyProperties2");
2054: }
2055: if (map instanceof MyProperties3) {
2056: return nonSharedArrayMap.get("arrayforMyProperties3");
2057: }
2058: if (map instanceof MyProperties) {
2059: return nonSharedArrayMap.get("arrayforMyProperties");
2060: }
2061: return null;
2062: }
2063:
2064: /**
2065: * Getting the array for HashMap and LinkedHashMap from a non-shared map temporarily until we fixed HashMap and
2066: * LinkedHashMap.
2067: */
2068: private Object[] getArray(Map map) {
2069: Object o = getMySubclassArray(map);
2070: if (o != null) {
2071: return (Object[]) o;
2072: }
2073:
2074: if (map instanceof Properties) {
2075: return (Object[]) nonSharedArrayMap
2076: .get("arrayforProperties");
2077: }
2078: if (map instanceof FastHashMap) {
2079: if (((FastHashMap) map).getFast()) {
2080: return (Object[]) nonSharedArrayMap
2081: .get("arrayforFastHashMapWithFast");
2082: } else {
2083: return (Object[]) nonSharedArrayMap
2084: .get("arrayforFastHashMap");
2085: }
2086: } else if (map instanceof LinkedHashMap) {
2087: return (Object[]) nonSharedArrayMap
2088: .get("arrayforLinkedHashMap");
2089: } else if (map instanceof HashMap) {
2090: return (Object[]) nonSharedArrayMap.get("arrayforHashMap");
2091: } else if (map instanceof Hashtable) {
2092: return (Object[]) nonSharedArrayMap
2093: .get("arrayforHashtable");
2094: } else if (map instanceof TreeMap) {
2095: return (Object[]) sharedMap.get("arrayforTreeMap");
2096: } else if (map instanceof THashMap) {
2097: return (Object[]) sharedMap.get("arrayforTHashMap");
2098: } else if (isCHM(map)) {
2099: return (Object[]) sharedMap
2100: .get("arrayforConcurrentHashMap");
2101: }
2102:
2103: return null;
2104: }
2105:
2106: private static boolean isCHM(Map map) {
2107: return map.getClass().getName().equals(
2108: "java.util.concurrent.ConcurrentHashMap");
2109: }
2110:
2111: void assertMappingsKeysEqual(Object[] expect, Collection collection) {
2112: Assert.assertEquals(expect.length, collection.size());
2113: for (int i = 0; i < expect.length; i++) {
2114: Assert.assertTrue(collection.contains(expect[i]));
2115: }
2116: }
2117:
2118: void assertMappings(Map expect, Map actual) {
2119: Assert.assertEquals(actual.getClass(), expect.size(), actual
2120: .size());
2121:
2122: Set expectEntries = expect.entrySet();
2123: Set actualEntries = actual.entrySet();
2124: if (actual instanceof LinkedHashMap) {
2125: for (Iterator iExpect = expectEntries.iterator(), iActual = actualEntries
2126: .iterator(); iExpect.hasNext();) {
2127: Assert.assertEquals(actual.getClass(), iExpect.next(),
2128: iActual.next());
2129: }
2130: }
2131:
2132: for (Iterator i = actualEntries.iterator(); i.hasNext();) {
2133: Entry entry = (Entry) i.next();
2134: Assert.assertEquals(actual.getClass(), entry.getValue(),
2135: expect.get(entry.getKey()));
2136: }
2137: }
2138:
2139: void assertMappingsEqual(Object[] expect, Map map) {
2140: Assert.assertEquals(expect.length, map.size());
2141:
2142: if (map instanceof LinkedHashMap) {
2143: Set entries = map.entrySet();
2144: int i = 0;
2145: for (Iterator iActual = entries.iterator(); iActual
2146: .hasNext();) {
2147: Assert.assertEquals(expect[i++], iActual.next());
2148: }
2149: }
2150:
2151: for (int i = 0; i < expect.length; i++) {
2152: Entry entry = (Entry) expect[i];
2153: if (isAccessOrderedLinkedHashMap(map)) {
2154: synchronized (map) {
2155: Assert.assertEquals(entry.getValue(), map.get(entry
2156: .getKey()));
2157: }
2158: } else {
2159: Assert.assertEquals(entry.getValue(), map.get(entry
2160: .getKey()));
2161: }
2162: }
2163: }
2164:
2165: void assertSingleMappingInternal(Map map, final Object key,
2166: final Object value) {
2167: try {
2168: Assert.assertFalse(map.isEmpty());
2169: Assert.assertEquals(1, map.size());
2170: Assert.assertEquals(1, map.entrySet().size());
2171: Assert.assertEquals(1, map.values().size());
2172: Assert.assertEquals(1, map.keySet().size());
2173:
2174: Assert.assertEquals(value, map.get(key));
2175: Assert.assertTrue(map.containsKey(key));
2176: Assert.assertTrue(map.containsValue(value));
2177:
2178: Set entries = map.entrySet();
2179: for (Iterator i = entries.iterator(); i.hasNext();) {
2180: Entry entry = (Entry) i.next();
2181: Assert.assertEquals(key, entry.getKey());
2182: Assert.assertEquals(value, entry.getValue());
2183: }
2184:
2185: for (Iterator i = map.values().iterator(); i.hasNext();) {
2186: Object o = i.next();
2187: Assert.assertEquals(value, o);
2188: }
2189:
2190: for (Iterator i = map.keySet().iterator(); i.hasNext();) {
2191: Object o = i.next();
2192: Assert.assertEquals(key, o);
2193: }
2194: } catch (Throwable t) {
2195: notifyError(new ErrorContext(map.getClass().getName(), t));
2196: }
2197: }
2198:
2199: void assertSingleMapping(Map map, final Object key,
2200: final Object value) {
2201: if (isAccessOrderedLinkedHashMap(map)) {
2202: // MyLinkedHashMap3 has accessOrder set to true; therefore, get() method
2203: // will mutate internal state and thus, require synchronized.
2204: synchronized (map) {
2205: assertSingleMappingInternal(map, key, value);
2206: }
2207: } else {
2208: assertSingleMappingInternal(map, key, value);
2209: }
2210: }
2211:
2212: void assertEmptyMap(Map map) {
2213: try {
2214: Assert.assertTrue(map.isEmpty());
2215: Assert.assertEquals(0, map.size());
2216: Assert.assertEquals(0, map.entrySet().size());
2217: Assert.assertEquals(0, map.values().size());
2218: Assert.assertEquals(0, map.keySet().size());
2219: Assert.assertEquals(Collections.EMPTY_MAP, map);
2220: } catch (Throwable t) {
2221: notifyError(new ErrorContext(map.getClass().getName(), t));
2222: }
2223: }
2224:
2225: private void addMappings(Map map, int count, int v) {
2226: synchronized (map) {
2227: for (int i = 0; i < count; i++) {
2228: map.put(E("key" + i, v), "value" + i);
2229: }
2230: }
2231: }
2232:
2233: private Map getOrderSensitiveMappings(int v) {
2234: Map map = null;
2235: map = new HashMap();
2236: map.put(E("January", v), E("Jan", v));
2237: map.put(E("February", v), E("Feb", v));
2238: map.put(E("March", v), E("Mar", v));
2239: map.put(E("April", v), E("Apr", v));
2240: return map;
2241: }
2242:
2243: private boolean isAccessOrderedLinkedHashMap(Map map) {
2244: if (map instanceof LinkedHashMap) {
2245: try {
2246: Field f = LinkedHashMap.class
2247: .getDeclaredField("accessOrder");
2248: f.setAccessible(true);
2249: return ((Boolean) f.get(map)).booleanValue();
2250: } catch (Throwable t) {
2251: throw new RuntimeException(t);
2252: }
2253: }
2254:
2255: return false;
2256: }
2257:
2258: private static boolean allowsNull(Map map) {
2259: return !(map instanceof Hashtable)
2260: && !(map instanceof TimeExpiryMap) && !(isCHM(map));
2261: }
2262:
2263: private static class Key implements Comparable {
2264: private final String id; // not used in equals()
2265: private final String equals;
2266:
2267: Key(String id, String equals) {
2268: this .id = id;
2269: this .equals = equals;
2270: }
2271:
2272: public int hashCode() {
2273: return equals == null ? 0 : equals.hashCode();
2274: }
2275:
2276: public boolean equals(Object o) {
2277: if (!(o instanceof Key)) {
2278: return false;
2279: }
2280: Key other = (Key) o;
2281: if (this .equals == null && other.equals == null) {
2282: return true;
2283: }
2284: if (this .equals == null && other.equals != null) {
2285: return false;
2286: }
2287: if (other.equals == null && this .equals != null) {
2288: return false;
2289: }
2290: return this .equals.equals(other.equals);
2291: }
2292:
2293: public int compareTo(Object o) {
2294: Key cmp = (Key) o;
2295: return equals.compareTo(cmp.equals);
2296: }
2297:
2298: }
2299:
2300: private static class NullTolerantComparator implements Comparator {
2301:
2302: public int compare(Object o1, Object o2) {
2303: if (o1 == null && o2 == null) {
2304: return 0;
2305: }
2306: if (o1 == null && o2 != null) {
2307: return -1;
2308: }
2309: if (o1 != null && o2 == null) {
2310: return 1;
2311: }
2312: return ((Comparable) o1).compareTo(o2);
2313: }
2314: }
2315:
2316: private static class SimpleEntry implements Map.Entry {
2317: // NOTE: this class more or less copied from Sun's source to java/util/AbstractMap
2318:
2319: private final Object key;
2320: private Object value;
2321:
2322: public SimpleEntry(Object key, Object value) {
2323: this .key = key;
2324: this .value = value;
2325: }
2326:
2327: public SimpleEntry(Map.Entry e) {
2328: this .key = e.getKey();
2329: this .value = e.getValue();
2330: }
2331:
2332: public Object getKey() {
2333: return key;
2334: }
2335:
2336: public Object getValue() {
2337: return value;
2338: }
2339:
2340: public Object setValue(Object value) {
2341: Object oldValue = this .value;
2342: this .value = value;
2343: return oldValue;
2344: }
2345:
2346: public boolean equals(Object o) {
2347: if (!(o instanceof Map.Entry))
2348: return false;
2349: Map.Entry e = (Map.Entry) o;
2350: return eq(key, e.getKey()) && eq(value, e.getValue());
2351: }
2352:
2353: public int hashCode() {
2354: return ((key == null) ? 0 : key.hashCode())
2355: ^ ((value == null) ? 0 : value.hashCode());
2356: }
2357:
2358: public String toString() {
2359: return key + "=" + value;
2360: }
2361:
2362: private static boolean eq(Object o1, Object o2) {
2363: return (o1 == null ? o2 == null : o1.equals(o2));
2364: }
2365: }
2366:
2367: private static class MyHashMap extends HashMap {
2368: protected Object key;
2369: protected Object value;
2370:
2371: public MyHashMap(int k) {
2372: super (11);
2373: }
2374:
2375: public MyHashMap(Map map) {
2376: putAll(map);
2377: }
2378:
2379: public Object put(Object arg0, Object arg1) {
2380: this .key = arg0;
2381: this .value = arg1;
2382: return super .put(arg0, arg1);
2383: }
2384:
2385: public Object getKey() {
2386: return key;
2387: }
2388:
2389: public Object getValue() {
2390: return value;
2391: }
2392:
2393: public Object getObject(Object arg) {
2394: return super .get(arg);
2395: }
2396:
2397: protected void testProtected() {
2398: // do nothing
2399: }
2400:
2401: private void readObject(java.io.ObjectInputStream s)
2402: throws IOException, ClassNotFoundException {
2403: s.defaultReadObject();
2404: }
2405:
2406: private void writeObject(java.io.ObjectOutputStream s)
2407: throws IOException {
2408: s.defaultWriteObject();
2409: }
2410: }
2411:
2412: private static class MyHashMap2 extends MyHashMap {
2413: public MyHashMap2() {
2414: super (12);
2415: }
2416: }
2417:
2418: private static class MyTreeMap extends TreeMap {
2419: public MyTreeMap() {
2420: super ();
2421: }
2422:
2423: public MyTreeMap(Comparator comparator) {
2424: super (comparator);
2425: }
2426: }
2427:
2428: private static class MyHashMap3 extends HashMap {
2429: private int i;
2430:
2431: public MyHashMap3(int i) {
2432: super ();
2433: this .i = i;
2434: }
2435:
2436: public int getI() {
2437: // this method here to silence compiler warning, no other reason
2438: return i;
2439: }
2440: }
2441:
2442: private static class MyTreeMap2 extends MyTreeMap {
2443: private Object key;
2444: private Object value;
2445:
2446: public MyTreeMap2(Comparator comparator) {
2447: super (comparator);
2448: }
2449:
2450: public Object put(Object arg0, Object arg1) {
2451: this .key = arg0;
2452: this .value = arg1;
2453: return super .put(arg0, arg1);
2454: }
2455:
2456: public Object getKey() {
2457: return key;
2458: }
2459:
2460: public Object getValue() {
2461: return value;
2462: }
2463: }
2464:
2465: private static class MyHashtable extends Hashtable {
2466: public MyHashtable() {
2467: super ();
2468: }
2469:
2470: }
2471:
2472: private static class MyHashtable2 extends MyHashtable {
2473: public MyHashtable2() {
2474: super ();
2475: }
2476: }
2477:
2478: private static class MyLinkedHashMap extends LinkedHashMap {
2479: public MyLinkedHashMap() {
2480: super ();
2481: }
2482: }
2483:
2484: private static class MyLinkedHashMap2 extends MyLinkedHashMap {
2485: private Object key;
2486: private Object value;
2487:
2488: public MyLinkedHashMap2() {
2489: super ();
2490: }
2491:
2492: public Object put(Object arg0, Object arg1) {
2493: this .key = arg0;
2494: this .value = arg1;
2495: return super .put(arg0, arg1);
2496: }
2497:
2498: public Object getKey() {
2499: return key;
2500: }
2501:
2502: public Object getValue() {
2503: return value;
2504: }
2505: }
2506:
2507: private static class MyLinkedHashMap3 extends LinkedHashMap {
2508: public MyLinkedHashMap3(boolean accessOrder) {
2509: super (10, 0.75f, accessOrder);
2510: }
2511: }
2512:
2513: private static class MyTHashMap extends THashMap {
2514: public MyTHashMap() {
2515: super ();
2516: }
2517: }
2518:
2519: private static class MyFastHashMap extends FastHashMap {
2520: public MyFastHashMap() {
2521: super ();
2522: }
2523: }
2524:
2525: private static class MyProperties extends Properties {
2526: private Object key;
2527: private Object value;
2528:
2529: public MyProperties() {
2530: super ();
2531: }
2532:
2533: public Object put(Object arg0, Object arg1) {
2534: this .key = arg0;
2535: this .value = arg1;
2536: return super .put(arg0, arg1);
2537: }
2538:
2539: public Object getKey() {
2540: return key;
2541: }
2542:
2543: public Object getValue() {
2544: return value;
2545: }
2546: }
2547:
2548: private static class MyProperties2 extends MyProperties {
2549: private Object lastGetKey;
2550:
2551: public MyProperties2() {
2552: super ();
2553: }
2554:
2555: public synchronized Object get(Object key) {
2556: this .lastGetKey = key;
2557: return super .get(key);
2558: }
2559:
2560: public Object getLastGetKey() {
2561: return lastGetKey;
2562: }
2563: }
2564:
2565: private static class MyProperties3 extends Properties {
2566: public MyProperties3() {
2567: super ();
2568: }
2569:
2570: public void setDefault(Properties newDefaults) {
2571: defaults = newDefaults;
2572: }
2573:
2574: public Object put(Object arg0, Object arg1) {
2575: return super .put(arg0, arg1);
2576: }
2577: }
2578:
2579: private static class MyObjectFunction implements TObjectFunction {
2580:
2581: public Object execute(Object value) {
2582: return new Integer(((Integer) value).intValue() + 1);
2583: }
2584:
2585: }
2586:
2587: private static class MyNonPortableObject {
2588: public MyNonPortableObject() {
2589: super();
2590: }
2591: }
2592:
2593: }
|