001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import org.apache.commons.collections.FastHashMap;
008:
009: import com.tc.object.bytecode.Manageable;
010: import com.tc.object.config.ConfigVisitor;
011: import com.tc.object.config.DSOClientConfigHelper;
012: import com.tc.object.tx.UnlockedSharedObjectException;
013: import com.tc.simulator.app.ApplicationConfig;
014: import com.tc.simulator.listener.ListenerProvider;
015: import com.tc.util.Assert;
016: import com.tc.util.TIMUtil;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.HashMap;
024: import java.util.Hashtable;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028: import java.util.Properties;
029: import java.util.Set;
030: import java.util.Map.Entry;
031:
032: public class AutoLockMapTestApp extends GenericTestApp {
033:
034: public AutoLockMapTestApp(String appId, ApplicationConfig cfg,
035: ListenerProvider listenerProvider) {
036: super (appId, cfg, listenerProvider, Map.class);
037: }
038:
039: protected Object getTestObject(String test) {
040: List maps = (List) sharedMap.get("maps");
041:
042: return maps.iterator();
043: }
044:
045: @SuppressWarnings({"unchecked","unchecked"})
046: protected void setupTestObject(String test) {
047: List maps = new ArrayList();
048: maps.add(new HashMap());
049: maps.add(new Hashtable());
050: maps.add(new FastHashMap());
051: FastHashMap fm = new FastHashMap();
052: fm.setFast(true);
053: maps.add(fm);
054: maps.add(new Properties());
055:
056: sharedMap.put("maps", maps);
057: sharedMap.put("arrayforHashtable", new Object[4]);
058: sharedMap.put("arrayforFastHashMap", new Object[4]);
059: sharedMap.put("arrayforFastHashMapWithFast", new Object[4]);
060: sharedMap.put("arrayforProperties", new Object[4]);
061: }
062:
063: private void initialize(Map map) {
064: map.putAll(getInitialData());
065: }
066:
067: private Hashtable getInitialData() {
068: Hashtable table = new Hashtable();
069: table.put("January", "Jan");
070: table.put("February", "Feb");
071: table.put("March", "Mar");
072: table.put("April", "Apr");
073: return table;
074: }
075:
076: void testDisableAutoLocks(Map map, boolean validate)
077: throws Exception {
078: if (!(map instanceof Hashtable) || (map instanceof HashMap)) {
079: return;
080: }
081:
082: if (validate) {
083: return;
084: }
085:
086: Hashtable ht = (Hashtable) map;
087: ((Manageable) ht).__tc_managed().disableAutoLocking();
088: try {
089: ht.put("saravan", "smells");
090: throw new AssertionError("put() did not fail");
091: } catch (UnlockedSharedObjectException use) {
092: // expected
093: }
094: }
095:
096: void testPut(Map map, boolean validate) throws Exception {
097: if (map instanceof HashMap) {
098: return;
099: }
100:
101: if (validate) {
102: assertMappings(getInitialData(), map);
103: } else {
104: initialize(map);
105: }
106: }
107:
108: void testEntrySetRemove(Map map, boolean validate) throws Exception {
109: if (map instanceof HashMap) {
110: return;
111: }
112:
113: if (validate) {
114: Hashtable expect = getInitialData();
115: expect.remove("February");
116: assertMappings(expect, map);
117: } else {
118: initialize(map);
119:
120: Set entrySet = map.entrySet();
121: entrySet.remove(new SimpleEntry("February", "Feb"));
122: }
123: }
124:
125: void testEntrySetClear(Map map, boolean validate) throws Exception {
126: if (map instanceof HashMap) {
127: return;
128: }
129:
130: if (validate) {
131: Assert.assertEquals(0, map.size());
132: } else {
133: initialize(map);
134: Set entrySet = map.entrySet();
135: entrySet.clear();
136: }
137: }
138:
139: void testEntrySetRetainAll(Map map, boolean validate)
140: throws Exception {
141: if (map instanceof HashMap) {
142: return;
143: }
144:
145: if (validate) {
146: Hashtable expect = getInitialData();
147: expect.remove("January");
148: expect.remove("April");
149: assertMappings(expect, map);
150: } else {
151: initialize(map);
152: Set entrySet = map.entrySet();
153:
154: Collection toRetain = new ArrayList();
155: toRetain.add(new SimpleEntry("February", "Feb"));
156: toRetain.add(new SimpleEntry("March", "Mar"));
157:
158: entrySet.retainAll(toRetain);
159: }
160: }
161:
162: void testEntrySetRemoveAll(Map map, boolean validate)
163: throws Exception {
164: if (map instanceof HashMap) {
165: return;
166: }
167:
168: if (validate) {
169: Hashtable expect = getInitialData();
170: expect.remove("February");
171: expect.remove("March");
172: assertMappings(expect, map);
173: } else {
174: initialize(map);
175: Set entrySet = map.entrySet();
176:
177: Collection toRemove = new ArrayList();
178: toRemove.add(new SimpleEntry("February", "Feb"));
179: toRemove.add(new SimpleEntry("March", "Mar"));
180:
181: entrySet.removeAll(toRemove);
182: }
183: }
184:
185: /**
186: * EntryWrapper is not portable yet, so the putting the entries to a shared array by calling the entrySet().toArray()
187: * method will not work and will throw a NonPortableException at this point.
188: */
189: /*
190: * void testEntrySetToArray(Map map, boolean validate) { Object[] array = getArray(map);
191: *
192: * if (validate) { assertArray(array, map); } else { initialize(map);
193: *
194: * synchronized (array) { Object[] returnArray = map.entrySet().toArray(array); Assert.assertTrue(returnArray ==
195: * array); } } }
196: */
197:
198: void testEntrySetIterator(Map map, boolean validate)
199: throws Exception {
200: if ((map instanceof FastHashMap)
201: && (!((FastHashMap) map).getFast())) {
202: return;
203: }
204: if (map instanceof HashMap) {
205: return;
206: }
207:
208: if (validate) {
209: Hashtable expect = getInitialData();
210: expect.remove("February");
211:
212: assertMappings(expect, map);
213: } else {
214: initialize(map);
215: Set entrySet = map.entrySet();
216: for (Iterator iterator = entrySet.iterator(); iterator
217: .hasNext();) {
218: Entry entry = (Entry) iterator.next();
219: if ("February".equals(entry.getKey())) {
220: iterator.remove();
221: break;
222: }
223: }
224: }
225: }
226:
227: void testKeySetRemove(Map map, boolean validate) throws Exception {
228: if (map instanceof HashMap) {
229: return;
230: }
231:
232: if (validate) {
233: Hashtable expect = getInitialData();
234: expect.remove("February");
235: assertMappings(expect, map);
236: } else {
237: initialize(map);
238: Set keySet = map.keySet();
239: keySet.remove("February");
240: }
241: }
242:
243: void testKeySetClear(Map map, boolean validate) throws Exception {
244: if (map instanceof HashMap) {
245: return;
246: }
247:
248: if (validate) {
249: Assert.assertEquals(0, map.size());
250: } else {
251: initialize(map);
252: Set keySet = map.keySet();
253: keySet.clear();
254: }
255: }
256:
257: void testKeySetIterator(Map map, boolean validate) throws Exception {
258: if ((map instanceof FastHashMap)
259: && (!((FastHashMap) map).getFast())) {
260: return;
261: }
262: if (map instanceof HashMap) {
263: return;
264: }
265:
266: if (validate) {
267: Hashtable expect = getInitialData();
268: expect.remove("February");
269: assertMappings(expect, map);
270: } else {
271: initialize(map);
272: Set keySet = map.keySet();
273: for (Iterator iterator = keySet.iterator(); iterator
274: .hasNext();) {
275: String key = (String) iterator.next();
276: if ("February".equals(key)) {
277: iterator.remove();
278: break;
279: }
280: }
281: }
282: }
283:
284: @SuppressWarnings("unchecked")
285: void testKeySetRetainAll(Map map, boolean validate)
286: throws Exception {
287: if (map instanceof HashMap) {
288: return;
289: }
290:
291: if (validate) {
292: Hashtable expect = getInitialData();
293: expect.remove("January");
294: expect.remove("April");
295: assertMappings(expect, map);
296: } else {
297: initialize(map);
298: Set keySet = map.keySet();
299:
300: Collection toRetain = new ArrayList();
301: toRetain.add("February");
302: toRetain.add("March");
303:
304: keySet.retainAll(toRetain);
305: }
306: }
307:
308: void testKeySetRemoveAll(Map map, boolean validate)
309: throws Exception {
310: if (map instanceof HashMap) {
311: return;
312: }
313:
314: if (validate) {
315: Hashtable expect = getInitialData();
316: expect.remove("February");
317: expect.remove("March");
318: assertMappings(expect, map);
319: } else {
320: initialize(map);
321: Set keySet = map.keySet();
322:
323: Collection toRemove = new ArrayList();
324: toRemove.add("February");
325: toRemove.add("March");
326:
327: keySet.removeAll(toRemove);
328: }
329: }
330:
331: void testKeySetToArray(Map map, boolean validate) {
332: if (map instanceof HashMap) {
333: return;
334: }
335:
336: Object[] array = getArray(map);
337:
338: if (validate) {
339: assertArray(array, map.keySet());
340: } else {
341: initialize(map);
342:
343: synchronized (array) {
344: Object[] returnArray = map.keySet().toArray(array);
345: Assert.assertTrue(returnArray == array);
346: }
347: }
348: }
349:
350: void testValuesRemove(Map map, boolean validate) throws Exception {
351: if (map instanceof HashMap) {
352: return;
353: }
354:
355: if (validate) {
356: Hashtable expect = getInitialData();
357: expect.remove("February");
358: assertMappings(expect, map);
359: } else {
360: initialize(map);
361: Collection values = map.values();
362: values.remove("Feb");
363: }
364: }
365:
366: void testValuesClear(Map map, boolean validate) throws Exception {
367: if (map instanceof HashMap) {
368: return;
369: }
370:
371: if (validate) {
372: Assert.assertEquals(0, map.size());
373: } else {
374: initialize(map);
375: Collection values = map.values();
376: values.clear();
377: }
378: }
379:
380: void testValuesIterator(Map map, boolean validate) throws Exception {
381: if ((map instanceof FastHashMap)
382: && (!((FastHashMap) map).getFast())) {
383: return;
384: }
385: if (map instanceof HashMap) {
386: return;
387: }
388:
389: if (validate) {
390: Hashtable expect = getInitialData();
391: expect.remove("February");
392: assertMappings(expect, map);
393: } else {
394: initialize(map);
395:
396: Collection values = map.values();
397: for (Iterator iterator = values.iterator(); iterator
398: .hasNext();) {
399: String value = (String) iterator.next();
400: if ("Feb".equals(value)) {
401: iterator.remove();
402: break;
403: }
404: }
405: }
406: }
407:
408: void testValuesRetainAll(Map map, boolean validate)
409: throws Exception {
410: if (map instanceof HashMap) {
411: return;
412: }
413:
414: if (validate) {
415: Hashtable expect = getInitialData();
416: expect.remove("January");
417: expect.remove("April");
418: assertMappings(expect, map);
419: } else {
420: initialize(map);
421: Collection values = map.values();
422:
423: Collection toRetain = new ArrayList();
424: toRetain.add("Feb");
425: toRetain.add("Mar");
426:
427: values.retainAll(toRetain);
428: }
429: }
430:
431: void testValuesRemoveAll(Map map, boolean validate)
432: throws Exception {
433: if (map instanceof HashMap) {
434: return;
435: }
436:
437: if (validate) {
438: Hashtable expect = getInitialData();
439: expect.remove("February");
440: expect.remove("March");
441: assertMappings(expect, map);
442: } else {
443: initialize(map);
444: Collection values = map.values();
445:
446: Collection toRemove = new ArrayList();
447: toRemove.add("Feb");
448: toRemove.add("Mar");
449:
450: values.removeAll(toRemove);
451: }
452: }
453:
454: void testValuesToArray(Map map, boolean validate) {
455: if (map instanceof HashMap) {
456: return;
457: }
458:
459: Object[] array = getArray(map);
460:
461: if (validate) {
462: assertArray(array, map.values());
463: } else {
464: initialize(map);
465:
466: synchronized (array) {
467: Object[] returnArray = map.values().toArray(array);
468: Assert.assertTrue(returnArray == array);
469: }
470: }
471: }
472:
473: void testBasicSetProperty(Map map, boolean validate) {
474: if (map instanceof HashMap) {
475: return;
476: }
477: if (!(map instanceof Properties)) {
478: return;
479: }
480:
481: if (validate) {
482: assertMappings(getInitialData(), map);
483: } else {
484: ((Properties) map).setProperty("January", "Jan");
485: ((Properties) map).setProperty("February", "Feb");
486: ((Properties) map).setProperty("March", "Mar");
487: ((Properties) map).setProperty("April", "Apr");
488: }
489: }
490:
491: void testBasicGetProperty(Map map, boolean validate) {
492: if (map instanceof HashMap) {
493: return;
494: }
495: if (!(map instanceof Properties)) {
496: return;
497: }
498:
499: if (validate) {
500: Assert.assertEquals("value", ((Properties) map)
501: .getProperty("key"));
502: Assert.assertEquals("defaultValue", ((Properties) map)
503: .getProperty("nonsense", "defaultValue"));
504: Assert.assertEquals("value", ((Properties) map)
505: .getProperty("key", "defaultValue"));
506: } else {
507: ((Properties) map).setProperty("key", "value");
508: }
509: }
510:
511: void testBasicLoad(Map map, boolean validate) {
512: if (map instanceof HashMap) {
513: return;
514: }
515: if (!(map instanceof Properties)) {
516: return;
517: }
518:
519: if (validate) {
520: Map expectedMap = new Properties();
521: expectedMap.put("key1", "val1");
522: expectedMap.put("key2", "val2");
523: expectedMap.put("key3", "val3");
524: assertMappings(expectedMap, map);
525: } else {
526: Properties data = new Properties();
527: data.setProperty("key1", "val1");
528: data.setProperty("key2", "val2");
529: data.setProperty("key3", "val3");
530: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
531: try {
532: data.store(outputStream, null);
533: } catch (IOException ioe) {
534: Assert.fail();
535: }
536: ByteArrayInputStream inputStream = new ByteArrayInputStream(
537: outputStream.toByteArray());
538: try {
539: ((Properties) map).load(inputStream);
540: } catch (IOException ioe) {
541: Assert.fail();
542: }
543: }
544: }
545:
546: void testHashMapPut(Map map, boolean validate) {
547: if (!(map instanceof HashMap) || (map instanceof FastHashMap)) {
548: return;
549: }
550:
551: if (validate) {
552: Assert.assertEquals(0, map.size());
553: } else {
554: try {
555: map.put("key1", "value1");
556: throw new AssertionError(
557: "Should have thrown an UnlockedSharedObjectException.");
558: } catch (UnlockedSharedObjectException e) {
559: // ignore
560: }
561: }
562: }
563:
564: void assertArray(Object[] expect, Collection collection) {
565: Assert.assertEquals(expect.length, collection.size());
566: for (int i = 0; i < expect.length; i++) {
567: String val = (String) expect[i];
568: Assert.assertTrue(collection.contains(val));
569: }
570: }
571:
572: void assertArray(Object[] expect, Map map) {
573: Assert.assertEquals(expect.length, map.size());
574: for (int i = 0; i < expect.length; i++) {
575: Entry entry = (Entry) expect[i];
576: Object val = map.get(entry.getKey());
577: Assert.assertEquals(entry.getValue(), val);
578: }
579: }
580:
581: void assertMappings(Map expect, Map actual) {
582: Assert.assertEquals(expect.size(), actual.size());
583:
584: Set expectEntries = expect.entrySet();
585: Set actualEntries = actual.entrySet();
586:
587: for (Iterator i = expectEntries.iterator(); i.hasNext();) {
588: Entry entry = (Entry) i.next();
589: Assert.assertEquals(entry.getValue(), actual.get(entry
590: .getKey()));
591: }
592:
593: for (Iterator i = actualEntries.iterator(); i.hasNext();) {
594: Entry entry = (Entry) i.next();
595: Assert.assertEquals(entry.getValue(), expect.get(entry
596: .getKey()));
597: }
598: }
599:
600: private Object[] getArray(Map map) {
601: if (map instanceof Properties) {
602: return (Object[]) sharedMap.get("arrayforProperties");
603: }
604: if (map instanceof FastHashMap) {
605: if (((FastHashMap) map).getFast()) {
606: return (Object[]) sharedMap
607: .get("arrayforFastHashMapWithFast");
608: } else {
609: return (Object[]) sharedMap.get("arrayforFastHashMap");
610: }
611: } else if (map instanceof Hashtable) {
612: return (Object[]) sharedMap.get("arrayforHashtable");
613: }
614:
615: return null;
616: }
617:
618: public static void visitL1DSOConfig(ConfigVisitor visitor,
619: DSOClientConfigHelper config) {
620: config.addModule(TIMUtil.COMMONS_COLLECTIONS_3_1, TIMUtil
621: .getVersion(TIMUtil.COMMONS_COLLECTIONS_3_1));
622:
623: String testClass = AutoLockMapTestApp.class.getName();
624: config.getOrCreateSpec(testClass);
625: String readOnlyMethodExpression = "* " + testClass
626: + "*.*ReadOnly*(..)";
627: config.addReadAutolock(readOnlyMethodExpression);
628: String methodExpression = "* " + testClass + "*.*(..)";
629: config.addWriteAutolock(methodExpression);
630: }
631:
632: private static class SimpleEntry implements Map.Entry {
633:
634: private final Object key;
635: private Object value;
636:
637: public SimpleEntry(Object key, Object value) {
638: this .key = key;
639: this .value = value;
640: }
641:
642: public SimpleEntry(Map.Entry e) {
643: this .key = e.getKey();
644: this .value = e.getValue();
645: }
646:
647: public Object getKey() {
648: return key;
649: }
650:
651: public Object getValue() {
652: return value;
653: }
654:
655: public Object setValue(Object value) {
656: Object oldValue = this .value;
657: this .value = value;
658: return oldValue;
659: }
660:
661: public boolean equals(Object o) {
662: if (!(o instanceof Map.Entry))
663: return false;
664: Map.Entry e = (Map.Entry) o;
665: return eq(key, e.getKey()) && eq(value, e.getValue());
666: }
667:
668: public int hashCode() {
669: return ((key == null) ? 0 : key.hashCode())
670: ^ ((value == null) ? 0 : value.hashCode());
671: }
672:
673: public String toString() {
674: return key + "=" + value;
675: }
676:
677: private static boolean eq(Object o1, Object o2) {
678: return (o1 == null ? o2 == null : o1.equals(o2));
679: }
680: }
681: }
|