KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > AutoLockMapTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest;
5
6 import org.apache.commons.collections.FastHashMap;
7
8 import com.tc.object.bytecode.Manageable;
9 import com.tc.object.config.ConfigVisitor;
10 import com.tc.object.config.DSOClientConfigHelper;
11 import com.tc.object.tx.UnlockedSharedObjectException;
12 import com.tc.simulator.app.ApplicationConfig;
13 import com.tc.simulator.listener.ListenerProvider;
14 import com.tc.util.Assert;
15
16 import java.io.ByteArrayInputStream JavaDoc;
17 import java.io.ByteArrayOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.Map.Entry;
29
30 public class AutoLockMapTestApp extends GenericTestApp {
31
32   public AutoLockMapTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
33     super(appId, cfg, listenerProvider, Map.class);
34   }
35
36   protected Object JavaDoc getTestObject(String JavaDoc test) {
37     List JavaDoc maps = (List JavaDoc) sharedMap.get("maps");
38
39     return maps.iterator();
40   }
41
42   protected void setupTestObject(String JavaDoc test) {
43     List JavaDoc maps = new ArrayList JavaDoc();
44     maps.add(new HashMap());
45     maps.add(new Hashtable JavaDoc());
46     maps.add(new FastHashMap());
47     FastHashMap fm = new FastHashMap();
48     fm.setFast(true);
49     maps.add(fm);
50     maps.add(new Properties JavaDoc());
51
52     sharedMap.put("maps", maps);
53     sharedMap.put("arrayforHashtable", new Object JavaDoc[4]);
54     sharedMap.put("arrayforFastHashMap", new Object JavaDoc[4]);
55     sharedMap.put("arrayforFastHashMapWithFast", new Object JavaDoc[4]);
56     sharedMap.put("arrayforProperties", new Object JavaDoc[4]);
57   }
58
59   private void initialize(Map map) {
60     map.putAll(getInitialData());
61   }
62
63   private Hashtable JavaDoc getInitialData() {
64     Hashtable JavaDoc table = new Hashtable JavaDoc();
65     table.put("January", "Jan");
66     table.put("February", "Feb");
67     table.put("March", "Mar");
68     table.put("April", "Apr");
69     return table;
70   }
71
72   void testDisableAutoLocks(Map map, boolean validate) throws Exception JavaDoc {
73     if (! (map instanceof Hashtable JavaDoc) || (map instanceof HashMap)) {
74       return;
75     }
76
77     if (validate) {
78       return;
79     }
80
81     Hashtable JavaDoc ht = (Hashtable JavaDoc)map;
82     ((Manageable)ht).__tc_managed().disableAutoLocking();
83     try {
84       ht.put("saravan", "smells");
85       throw new AssertionError JavaDoc("put() did not fail");
86     } catch (UnlockedSharedObjectException use) {
87       // expected
88
}
89   }
90
91   void testPut(Map map, boolean validate) throws Exception JavaDoc {
92     if (map instanceof HashMap) { return; }
93
94     if (validate) {
95       assertMappings(getInitialData(), map);
96     } else {
97       initialize(map);
98     }
99   }
100
101   void testEntrySetRemove(Map map, boolean validate) throws Exception JavaDoc {
102     if (map instanceof HashMap) { return; }
103
104     if (validate) {
105       Hashtable JavaDoc expect = getInitialData();
106       expect.remove("February");
107       assertMappings(expect, map);
108     } else {
109       initialize(map);
110
111       Set JavaDoc entrySet = map.entrySet();
112       entrySet.remove(new SimpleEntry("February", "Feb"));
113     }
114   }
115
116   void testEntrySetClear(Map map, boolean validate) throws Exception JavaDoc {
117     if (map instanceof HashMap) { return; }
118
119     if (validate) {
120       Assert.assertEquals(0, map.size());
121     } else {
122       initialize(map);
123       Set JavaDoc entrySet = map.entrySet();
124       entrySet.clear();
125     }
126   }
127
128   void testEntrySetRetainAll(Map map, boolean validate) throws Exception JavaDoc {
129     if (map instanceof HashMap) { return; }
130
131     if (validate) {
132       Hashtable JavaDoc expect = getInitialData();
133       expect.remove("January");
134       expect.remove("April");
135       assertMappings(expect, map);
136     } else {
137       initialize(map);
138       Set JavaDoc entrySet = map.entrySet();
139
140       Collection JavaDoc toRetain = new ArrayList JavaDoc();
141       toRetain.add(new SimpleEntry("February", "Feb"));
142       toRetain.add(new SimpleEntry("March", "Mar"));
143
144       entrySet.retainAll(toRetain);
145     }
146   }
147
148   void testEntrySetRemoveAll(Map map, boolean validate) throws Exception JavaDoc {
149     if (map instanceof HashMap) { return; }
150
151     if (validate) {
152       Hashtable JavaDoc expect = getInitialData();
153       expect.remove("February");
154       expect.remove("March");
155       assertMappings(expect, map);
156     } else {
157       initialize(map);
158       Set JavaDoc entrySet = map.entrySet();
159
160       Collection JavaDoc toRemove = new ArrayList JavaDoc();
161       toRemove.add(new SimpleEntry("February", "Feb"));
162       toRemove.add(new SimpleEntry("March", "Mar"));
163
164       entrySet.removeAll(toRemove);
165     }
166   }
167
168   /**
169    * EntryWrapper is not portable yet, so the putting the entries to a shared array
170    * by calling the entrySet().toArray() method will not work and will throw a NonPortableException
171    * at this point.
172    */

173   /*void testEntrySetToArray(Map map, boolean validate) {
174     Object[] array = getArray(map);
175
176     if (validate) {
177       assertArray(array, map);
178     } else {
179       initialize(map);
180
181       synchronized (array) {
182         Object[] returnArray = map.entrySet().toArray(array);
183         Assert.assertTrue(returnArray == array);
184       }
185     }
186   }*/

187
188   void testEntrySetIterator(Map map, boolean validate) throws Exception JavaDoc {
189     if ((map instanceof FastHashMap) && (!((FastHashMap) map).getFast())) { return; }
190     if (map instanceof HashMap) { return; }
191
192     if (validate) {
193       Hashtable JavaDoc expect = getInitialData();
194       expect.remove("February");
195
196       assertMappings(expect, map);
197     } else {
198       initialize(map);
199       Set JavaDoc entrySet = map.entrySet();
200       for (Iterator JavaDoc iterator = entrySet.iterator(); iterator.hasNext();) {
201         Entry entry = (Entry) iterator.next();
202         if ("February".equals(entry.getKey())) {
203           iterator.remove();
204           break;
205         }
206       }
207     }
208   }
209
210   void testKeySetRemove(Map map, boolean validate) throws Exception JavaDoc {
211     if (map instanceof HashMap) { return; }
212
213     if (validate) {
214       Hashtable JavaDoc expect = getInitialData();
215       expect.remove("February");
216       assertMappings(expect, map);
217     } else {
218       initialize(map);
219       Set JavaDoc keySet = map.keySet();
220       keySet.remove("February");
221     }
222   }
223
224   void testKeySetClear(Map map, boolean validate) throws Exception JavaDoc {
225     if (map instanceof HashMap) { return; }
226
227     if (validate) {
228       Assert.assertEquals(0, map.size());
229     } else {
230       initialize(map);
231       Set JavaDoc keySet = map.keySet();
232       keySet.clear();
233     }
234   }
235
236   void testKeySetIterator(Map map, boolean validate) throws Exception JavaDoc {
237     if ((map instanceof FastHashMap) && (!((FastHashMap) map).getFast())) { return; }
238     if (map instanceof HashMap) { return; }
239
240     if (validate) {
241       Hashtable JavaDoc expect = getInitialData();
242       expect.remove("February");
243       assertMappings(expect, map);
244     } else {
245       initialize(map);
246       Set JavaDoc keySet = map.keySet();
247       for (Iterator JavaDoc iterator = keySet.iterator(); iterator.hasNext();) {
248         String JavaDoc key = (String JavaDoc) iterator.next();
249         if ("February".equals(key)) {
250           iterator.remove();
251           break;
252         }
253       }
254     }
255   }
256
257   void testKeySetRetainAll(Map map, boolean validate) throws Exception JavaDoc {
258     if (map instanceof HashMap) { return; }
259
260     if (validate) {
261       Hashtable JavaDoc expect = getInitialData();
262       expect.remove("January");
263       expect.remove("April");
264       assertMappings(expect, map);
265     } else {
266       initialize(map);
267       Set JavaDoc keySet = map.keySet();
268
269       Collection JavaDoc toRetain = new ArrayList JavaDoc();
270       toRetain.add("February");
271       toRetain.add("March");
272
273       keySet.retainAll(toRetain);
274     }
275   }
276
277   void testKeySetRemoveAll(Map map, boolean validate) throws Exception JavaDoc {
278     if (map instanceof HashMap) { return; }
279
280     if (validate) {
281       Hashtable JavaDoc expect = getInitialData();
282       expect.remove("February");
283       expect.remove("March");
284       assertMappings(expect, map);
285     } else {
286       initialize(map);
287       Set JavaDoc keySet = map.keySet();
288
289       Collection JavaDoc toRemove = new ArrayList JavaDoc();
290       toRemove.add("February");
291       toRemove.add("March");
292
293       keySet.removeAll(toRemove);
294     }
295   }
296
297   void testKeySetToArray(Map map, boolean validate) {
298     if (map instanceof HashMap) { return; }
299
300     Object JavaDoc[] array = getArray(map);
301
302     if (validate) {
303       assertArray(array, map.keySet());
304     } else {
305       initialize(map);
306
307       synchronized (array) {
308         Object JavaDoc[] returnArray = map.keySet().toArray(array);
309         Assert.assertTrue(returnArray == array);
310       }
311     }
312   }
313
314   void testValuesRemove(Map map, boolean validate) throws Exception JavaDoc {
315     if (map instanceof HashMap) { return; }
316
317     if (validate) {
318       Hashtable JavaDoc expect = getInitialData();
319       expect.remove("February");
320       assertMappings(expect, map);
321     } else {
322       initialize(map);
323       Collection JavaDoc values = map.values();
324       values.remove("Feb");
325     }
326   }
327
328   void testValuesClear(Map map, boolean validate) throws Exception JavaDoc {
329     if (map instanceof HashMap) { return; }
330
331     if (validate) {
332       Assert.assertEquals(0, map.size());
333     } else {
334       initialize(map);
335       Collection JavaDoc values = map.values();
336       values.clear();
337     }
338   }
339
340   void testValuesIterator(Map map, boolean validate) throws Exception JavaDoc {
341     if ((map instanceof FastHashMap) && (!((FastHashMap) map).getFast())) { return; }
342     if (map instanceof HashMap) { return; }
343
344     if (validate) {
345       Hashtable JavaDoc expect = getInitialData();
346       expect.remove("February");
347       assertMappings(expect, map);
348     } else {
349       initialize(map);
350
351       Collection JavaDoc values = map.values();
352       for (Iterator JavaDoc iterator = values.iterator(); iterator.hasNext();) {
353         String JavaDoc value = (String JavaDoc) iterator.next();
354         if ("Feb".equals(value)) {
355           iterator.remove();
356           break;
357         }
358       }
359     }
360   }
361
362   void testValuesRetainAll(Map map, boolean validate) throws Exception JavaDoc {
363     if (map instanceof HashMap) { return; }
364
365     if (validate) {
366       Hashtable JavaDoc expect = getInitialData();
367       expect.remove("January");
368       expect.remove("April");
369       assertMappings(expect, map);
370     } else {
371       initialize(map);
372       Collection JavaDoc values = map.values();
373
374       Collection JavaDoc toRetain = new ArrayList JavaDoc();
375       toRetain.add("Feb");
376       toRetain.add("Mar");
377
378       values.retainAll(toRetain);
379     }
380   }
381
382   void testValuesRemoveAll(Map map, boolean validate) throws Exception JavaDoc {
383     if (map instanceof HashMap) { return; }
384
385     if (validate) {
386       Hashtable JavaDoc expect = getInitialData();
387       expect.remove("February");
388       expect.remove("March");
389       assertMappings(expect, map);
390     } else {
391       initialize(map);
392       Collection JavaDoc values = map.values();
393
394       Collection JavaDoc toRemove = new ArrayList JavaDoc();
395       toRemove.add("Feb");
396       toRemove.add("Mar");
397
398       values.removeAll(toRemove);
399     }
400   }
401
402   void testValuesToArray(Map map, boolean validate) {
403     if (map instanceof HashMap) { return; }
404
405     Object JavaDoc[] array = getArray(map);
406
407     if (validate) {
408       assertArray(array, map.values());
409     } else {
410       initialize(map);
411
412       synchronized (array) {
413         Object JavaDoc[] returnArray = map.values().toArray(array);
414         Assert.assertTrue(returnArray == array);
415       }
416     }
417   }
418
419   void testBasicSetProperty(Map map, boolean validate) {
420     if (map instanceof HashMap) { return; }
421     if (!(map instanceof Properties JavaDoc)) {
422       return;
423     }
424
425     if(validate) {
426       assertMappings(getInitialData(), map);
427     } else {
428       ((Properties JavaDoc)map).setProperty("January", "Jan");
429       ((Properties JavaDoc)map).setProperty("February", "Feb");
430       ((Properties JavaDoc)map).setProperty("March", "Mar");
431       ((Properties JavaDoc)map).setProperty("April", "Apr");
432     }
433   }
434
435   void testBasicGetProperty(Map map, boolean validate) {
436     if (map instanceof HashMap) { return; }
437     if (!(map instanceof Properties JavaDoc)) {
438       return;
439     }
440
441     if(validate) {
442       Assert.assertEquals("value", ((Properties JavaDoc)map).getProperty("key"));
443       Assert.assertEquals("defaultValue", ((Properties JavaDoc)map).getProperty("nonsense", "defaultValue"));
444       Assert.assertEquals("value", ((Properties JavaDoc)map).getProperty("key", "defaultValue"));
445     } else {
446       ((Properties JavaDoc)map).setProperty("key", "value");
447     }
448   }
449
450   void testBasicLoad(Map map, boolean validate) {
451     if (map instanceof HashMap) { return; }
452     if (!(map instanceof Properties JavaDoc)) {
453       return;
454     }
455
456     if(validate) {
457       Map expectedMap = new Properties JavaDoc();
458       expectedMap.put("key1", "val1");
459       expectedMap.put("key2", "val2");
460       expectedMap.put("key3", "val3");
461       assertMappings(expectedMap, map);
462     } else {
463       Properties JavaDoc data = new Properties JavaDoc();
464       data.setProperty("key1", "val1");
465       data.setProperty("key2", "val2");
466       data.setProperty("key3", "val3");
467       ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
468       try {
469         data.store(outputStream, null);
470       } catch (IOException JavaDoc ioe) {
471         Assert.fail();
472       }
473       ByteArrayInputStream JavaDoc inputStream = new ByteArrayInputStream JavaDoc(outputStream.toByteArray());
474       try {
475         ((Properties JavaDoc)map).load(inputStream);
476       } catch (IOException JavaDoc ioe) {
477         Assert.fail();
478       }
479     }
480   }
481
482   void testHashMapPut(Map map, boolean validate) {
483     if (!(map instanceof HashMap) || (map instanceof FastHashMap)) { return; }
484
485     if (validate) {
486       Assert.assertEquals(0, map.size());
487     } else {
488       try {
489         map.put("key1", "value1");
490         throw new AssertionError JavaDoc("Should have thrown an UnlockedSharedObjectException.");
491       } catch (UnlockedSharedObjectException e) {
492         // ignore
493
}
494     }
495   }
496
497   void assertArray(Object JavaDoc[] expect, Collection JavaDoc collection) {
498     Assert.assertEquals(expect.length, collection.size());
499     for (int i = 0; i < expect.length; i++) {
500       String JavaDoc val = (String JavaDoc)expect[i];
501       Assert.assertTrue(collection.contains(val));
502     }
503   }
504
505   void assertArray(Object JavaDoc[] expect, Map map) {
506     Assert.assertEquals(expect.length, map.size());
507     for (int i = 0; i < expect.length; i++) {
508       Entry entry = (Entry) expect[i];
509       Object JavaDoc val = map.get(entry.getKey());
510       Assert.assertEquals(entry.getValue(), val);
511     }
512   }
513
514   void assertMappings(Map expect, Map actual) {
515     Assert.assertEquals(expect.size(), actual.size());
516
517     Set JavaDoc expectEntries = expect.entrySet();
518     Set JavaDoc actualEntries = actual.entrySet();
519
520     for (Iterator JavaDoc i = expectEntries.iterator(); i.hasNext();) {
521       Entry entry = (Entry) i.next();
522       Assert.assertEquals(entry.getValue(), actual.get(entry.getKey()));
523     }
524
525     for (Iterator JavaDoc i = actualEntries.iterator(); i.hasNext();) {
526       Entry entry = (Entry) i.next();
527       Assert.assertEquals(entry.getValue(), expect.get(entry.getKey()));
528     }
529   }
530
531   private Object JavaDoc[] getArray(Map map) {
532     if (map instanceof Properties JavaDoc) { return (Object JavaDoc[]) sharedMap.get("arrayforProperties"); }
533     if (map instanceof FastHashMap) {
534       if (((FastHashMap) map).getFast()) {
535         return (Object JavaDoc[]) sharedMap.get("arrayforFastHashMapWithFast");
536       } else {
537         return (Object JavaDoc[]) sharedMap.get("arrayforFastHashMap");
538       }
539     } else if (map instanceof Hashtable JavaDoc) { return (Object JavaDoc[]) sharedMap.get("arrayforHashtable"); }
540
541     return null;
542   }
543
544   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
545     config.addNewModule("clustered-commons-collections-3.1", "1.0.0");
546
547     String JavaDoc testClass = AutoLockMapTestApp.class.getName();
548     config.getOrCreateSpec(testClass);
549     String JavaDoc readOnlyMethodExpression = "* " + testClass + "*.*ReadOnly*(..)";
550     config.addReadAutolock(readOnlyMethodExpression);
551     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
552     config.addWriteAutolock(methodExpression);
553   }
554
555   private static class SimpleEntry implements Map.Entry {
556
557     private final Object JavaDoc key;
558     private Object JavaDoc value;
559
560     public SimpleEntry(Object JavaDoc key, Object JavaDoc value) {
561       this.key = key;
562       this.value = value;
563     }
564
565     public SimpleEntry(Map.Entry e) {
566       this.key = e.getKey();
567       this.value = e.getValue();
568     }
569
570     public Object JavaDoc getKey() {
571       return key;
572     }
573
574     public Object JavaDoc getValue() {
575       return value;
576     }
577
578     public Object JavaDoc setValue(Object JavaDoc value) {
579       Object JavaDoc oldValue = this.value;
580       this.value = value;
581       return oldValue;
582     }
583
584     public boolean equals(Object JavaDoc o) {
585       if (!(o instanceof Map.Entry)) return false;
586       Map.Entry e = (Map.Entry) o;
587       return eq(key, e.getKey()) && eq(value, e.getValue());
588     }
589
590     public int hashCode() {
591       return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
592     }
593
594     public String JavaDoc toString() {
595       return key + "=" + value;
596     }
597
598     private static boolean eq(Object JavaDoc o1, Object JavaDoc o2) {
599       return (o1 == null ? o2 == null : o1.equals(o2));
600     }
601   }
602 }
603
Popular Tags