1 4 package com.tctest; 5 6 import com.tc.object.config.ConfigVisitor; 7 import com.tc.object.config.DSOClientConfigHelper; 8 import com.tc.object.config.TransparencyClassSpec; 9 import com.tc.simulator.app.ApplicationConfig; 10 import com.tc.simulator.listener.ListenerProvider; 11 import com.tc.util.Assert; 12 import com.tctest.runner.AbstractTransparentApp; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.IdentityHashMap ; 18 import java.util.LinkedList ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Set ; 22 import java.util.Vector ; 23 24 public class NullReferenceTestApp extends AbstractTransparentApp { 25 26 private final List nodes = new ArrayList (); 27 private Holder holder; 28 29 public NullReferenceTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 30 super(appId, cfg, listenerProvider); 31 } 32 33 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 34 String testClass = NullReferenceTestApp.class.getName(); 35 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 36 spec.addRoot("nodes", "nodesLock"); 37 spec.addRoot("holder", "holderLock"); 38 39 String methodExpression; 40 41 methodExpression = "* " + testClass + ".init()"; 42 config.addWriteAutolock(methodExpression); 43 44 methodExpression = "* " + testClass + ".check()"; 45 config.addWriteAutolock(methodExpression); 46 47 methodExpression = "* " + testClass + "$Holder.*(..)"; 48 config.addWriteAutolock(methodExpression); 49 50 config.addIncludePattern(Holder.class.getName()); 51 } 52 53 public void run() { 54 init(); 55 check(); 56 finish(); 57 } 58 59 private void finish() { 60 holder.mod(); 61 } 62 63 private void check() { 64 holder.check(); 65 66 synchronized (nodes) { 67 nodes.remove(0); 68 nodes.notifyAll(); 69 70 while (nodes.size() > 0) { 71 try { 72 nodes.wait(); 73 } catch (InterruptedException e) { 74 throw new RuntimeException (e); 75 } 76 } 77 } 78 } 79 80 private void init() { 81 synchronized (nodes) { 82 Holder h = new Holder(); 83 this.holder = h; 84 85 if (nodes.size() == 0) { 88 Assert.assertTrue(this.holder == h); 89 } else { 90 Assert.assertTrue(this.holder != h); 91 } 92 93 nodes.add(new Object ()); 94 nodes.notifyAll(); 95 96 while (nodes.size() != getParticipantCount()) { 97 try { 98 nodes.wait(); 99 } catch (InterruptedException e) { 100 throw new RuntimeException (e); 101 } 102 } 103 } 104 105 } 106 107 private static class Holder { 108 Object reference = null; 109 Object [] array = new Object [] { null, new Object (), null }; 110 111 Map map1 = new HashMap (); 112 Map map2 = new IdentityHashMap (); 113 114 List list1 = new LinkedList (); 115 List list2 = new ArrayList (); 116 List list3 = new Vector (); 117 118 Set set1 = new HashSet (); 119 120 private int count = 0; 121 122 Holder() { 123 mod(); 124 } 125 126 public void check() { 127 Assert.assertNull(reference); 128 Assert.assertNull(array[0]); 129 Assert.assertNotNull(array[1]); 130 Assert.assertNull(array[2]); 131 132 Assert.assertEquals(1, list1.size()); 133 Assert.assertEquals(1, list2.size()); 134 Assert.assertEquals(1, list3.size()); 135 Assert.assertTrue(list1.contains(null)); 136 Assert.assertTrue(list2.contains(null)); 137 Assert.assertTrue(list3.contains(null)); 138 139 Assert.assertEquals(2, map1.size()); 140 Assert.assertEquals(2, map2.size()); 141 Assert.assertTrue(map1.containsKey(null)); 142 Assert.assertTrue(map2.containsKey(null)); 143 Assert.assertTrue(map1.containsValue(null)); 144 Assert.assertTrue(map2.containsValue(null)); 145 146 Assert.assertEquals(1, set1.size()); 147 Assert.assertTrue(set1.contains(null)); 148 } 149 150 synchronized void mod() { 151 modSets(new Set [] { set1 }); 152 modLists(new List [] { list1, list2, list3 }); 153 modMaps(new Map [] { map1, map2 }); 154 reference = null; 155 array[0] = null; 156 } 157 158 void modLists(List [] lists) { 159 for (int i = 0; i < lists.length; i++) { 160 lists[i].add(null); 161 } 162 } 163 164 void modSets(Set [] sets) { 165 for (int i = 0; i < sets.length; i++) { 166 sets[i].add(null); 167 } 168 } 169 170 void modMaps(Map [] maps) { 171 for (int i = 0; i < maps.length; i++) { 172 maps[i].put(null, "value for null key"); 173 maps[i].put("key" + count + " for null value", null); 174 } 175 count++; 176 } 177 178 } 179 180 } 181 | Popular Tags |