1 22 package org.jboss.test.aop.callerscope; 23 24 import org.jboss.aop.advice.Scope; 25 import org.jboss.test.aop.AOPTestWithSetup; 26 import org.jboss.util.collection.WeakIdentityHashMap; 27 import junit.framework.Test; 28 import junit.framework.TestSuite; 29 import junit.textui.TestRunner; 30 31 36 public class CallerScopeTestCase extends AOPTestWithSetup 37 { 38 Caller caller = new Caller(); 39 40 public static void main(String [] args) 41 { 42 TestRunner.run(suite()); 43 } 44 45 public static Test suite() 46 { 47 TestSuite suite = new TestSuite("DotInPointcutNameTester"); 48 suite.addTestSuite(CallerScopeTestCase.class); 49 return suite; 50 } 51 52 public CallerScopeTestCase(String name) 53 { 54 super(name); 55 } 56 57 public void testWeakIdentityHashMap() 58 { 59 System.out.println("*** testWeakIdentityHashMap()"); 60 61 75 String a = new String ("a"); 76 String b = new String ("a"); 77 String c = new String ("a"); 78 String d = new String ("a"); 79 Long l1 = new Long (1); 80 Long l2 = new Long (2); 81 Long l3 = new Long (3); 82 Long l4 = new Long (4); 83 84 WeakIdentityHashMap map = new WeakIdentityHashMap(); 85 map.put(a, l1); 86 map.put(b, l2); 87 map.put(c, l3); 88 map.put(d, l4); 89 90 assertEquals(4, map.size()); 91 System.gc(); 92 assertEquals(4, map.size()); 93 94 assertEquals(l1, map.get(a)); 95 assertEquals(l2, map.get(b)); 96 assertEquals(l3, map.get(c)); 97 assertEquals(l4, map.get(d)); 98 99 c = null; 100 d = null; 101 System.gc(); 102 assertEquals(2, map.size()); 103 104 assertEquals(l1, map.get(a)); 105 assertEquals(l2, map.get(b)); 106 107 System.out.println("*** testWeakIdentityHashMap() - successful"); 108 109 } 110 111 public void testPerVm()throws Exception 112 { 113 System.out.println("*** testPerVm"); 114 clearInterceptions(); 115 POJO pojo = new POJO(); 116 checkInterceptions(Scope.PER_VM); 117 118 clearInterceptions(); 119 pojo.perVmMethod(); 120 checkInterceptions(Scope.PER_VM); 121 System.out.println(); 122 } 123 124 public void testPerClass()throws Exception 125 { 126 System.out.println("*** testPerClass"); 127 clearInterceptions(); 128 POJO pojo = new POJO(1); 129 checkInterceptions(Scope.PER_CLASS); 130 131 clearInterceptions(); 132 pojo.perClassMethod(); 133 checkInterceptions(Scope.PER_CLASS); 134 System.out.println(); 135 } 136 137 public void testPerInstance()throws Exception 138 { 139 System.out.println("*** testPerInstance"); 140 clearInterceptions(); 141 POJO pojo = new POJO(); 142 checkInterceptions(Scope.PER_VM); 143 144 clearInterceptions(); 145 pojo.perInstanceMethod(); 146 checkInterceptions(Scope.PER_INSTANCE); 147 148 caller.testPerInstance(pojo); 149 System.out.println(); 150 } 151 152 153 public void testPerJoinpoint()throws Exception 154 { 155 System.out.println("*** testPerJoinpoint"); 156 clearInterceptions(); 157 POJO pojo = new POJO(); 158 checkInterceptions(Scope.PER_VM); 159 160 clearInterceptions(); 161 pojo.perJoinpointMethod(); 162 checkInterceptions(Scope.PER_JOINPOINT); 163 System.out.println(); 164 } 165 166 public void testPerClassJoinpoint()throws Exception 167 { 168 System.out.println("*** testPerClassJoinpoint"); 169 clearInterceptions(); 170 POJO pojo = new POJO(false); 171 checkInterceptions(Scope.PER_CLASS_JOINPOINT); 172 173 clearInterceptions(); 174 pojo.perClassJoinpointMethod(); 175 checkInterceptions(Scope.PER_CLASS_JOINPOINT); 176 System.out.println(); 177 } 178 179 static void clearInterceptions() 180 { 181 PerVmAspect.intercepted = false; 182 PerVmInterceptor.intercepted = false; 183 PerClassAspect.intercepted = false; 184 PerClassInterceptor.intercepted = false; 185 PerInstanceAspect.intercepted = false; 186 PerInstanceInterceptor.intercepted = false; 187 PerJoinpointAspect.intercepted = false; 188 PerJoinpointInterceptor.intercepted = false; 189 PerClassJoinpointAspect.intercepted = false; 190 PerClassJoinpointInterceptor.intercepted = false; 191 } 192 193 194 static void checkInterceptions(Scope scope) 195 { 196 if (scope == Scope.PER_VM) 197 { 198 checkInterceptions(true, true, false, false, false, false, false, false, false, false); 199 } 200 else if (scope == Scope.PER_CLASS) 201 { 202 checkInterceptions(false, false, true, true, false, false, false, false, false, false); 203 } 204 else if (scope == Scope.PER_INSTANCE) 205 { 206 checkInterceptions(false, false, false, false, true, true, false, false, false, false); 207 } 208 else if (scope == Scope.PER_JOINPOINT) 209 { 210 checkInterceptions(false, false, false, false, false, false, true, true, false, false); 211 } 212 else if (scope == Scope.PER_CLASS_JOINPOINT) 213 { 214 checkInterceptions(false, false, false, false, false, false, false, false, true, true); 215 } 216 } 217 218 static void checkInterceptions( 219 boolean perVmAspect, 220 boolean perVmInterceptor, 221 boolean perClassAspect, 222 boolean perClassInterceptor, 223 boolean perInstanceAspect, 224 boolean perInstanceInterceptor, 225 boolean perJoinpointAspect, 226 boolean perJoinpointInterceptor, 227 boolean perClassJoinpointAspect, 228 boolean perClassJoinpointInterceptor) 229 { 230 assertEquals("Wrong intercepted value for PerVMAspect", perVmAspect, PerVmAspect.intercepted); 231 assertEquals("Wrong intercepted value for PerVmInterceptor", perVmInterceptor, PerVmInterceptor.intercepted); 232 assertEquals("Wrong intercepted value for PerClassAspect", perClassAspect, PerClassAspect.intercepted); 233 assertEquals("Wrong intercepted value for PerClassInterceptor", perClassInterceptor, PerClassInterceptor.intercepted); 234 assertEquals("Wrong intercepted value for PerInstanceAspect", perInstanceAspect, PerInstanceAspect.intercepted); 235 assertEquals("Wrong intercepted value for PerInstanceInterceptor", perInstanceInterceptor, PerInstanceInterceptor.intercepted); 236 assertEquals("Wrong intercepted value for PerJoinpointAspect", perJoinpointAspect, PerJoinpointAspect.intercepted); 237 assertEquals("Wrong intercepted value for PerJoinpointInterceptor", perJoinpointInterceptor, PerJoinpointInterceptor.intercepted); 238 assertEquals("Wrong intercepted value for PerClassJoinpointAspect", perClassJoinpointAspect, PerClassJoinpointAspect.intercepted); 239 assertEquals("Wrong intercepted value for PerClassJoinpointInterceptor", perClassJoinpointInterceptor, PerClassJoinpointInterceptor.intercepted); 240 } 241 242 } 243 | Popular Tags |