1 22 package org.jboss.test.aop.scoped; 23 24 29 public class ScopedTester implements ScopedTesterMBean 30 { 31 int expectedInterceptorValue; 32 int expectedAspectValue; 33 int metadataSuffix; 34 35 String ctorPlainMetadata; 36 String methodPlainMetadata; 37 String customMetadata; 38 39 public void setExpectedInterceptorValue(int i) 40 { 41 expectedInterceptorValue = i; 42 } 43 44 public void setExpectedAspectValue(int i) 45 { 46 expectedAspectValue = i; 47 } 48 49 public int getExpectedInterceptorValue() 50 { 51 return expectedInterceptorValue; 52 } 53 54 public int getExpectedAspectValue() 55 { 56 return expectedAspectValue; 57 } 58 59 public void setMetadataSuffix(int i) 60 { 61 metadataSuffix = i; 62 ctorPlainMetadata = "ctor" + metadataSuffix; 63 methodPlainMetadata = "method" + metadataSuffix; 64 customMetadata = "custom" + metadataSuffix; 65 } 66 67 public void testExpectedValues() 68 { 69 if (ScopedInterceptor.value != expectedInterceptorValue) 70 { 71 throw new RuntimeException ("Expected Interceptor value " + expectedInterceptorValue + ", was " + ScopedInterceptor.value); 72 } 73 if (ScopedAspect.value != expectedAspectValue) 74 { 75 throw new RuntimeException ("Expected Aspect value " + expectedAspectValue + ", was " + ScopedAspect.value); 76 } 77 78 } 79 80 public void testScoped() throws Exception 81 { 82 System.out.println("--------------------------- TESTING SCOPED ------------------"); 83 System.out.println("MY CLASSLOADER " + getClass().getClassLoader()); 84 System.out.println("SCOPED INTERCEPTOR CLASSLOADER " + ScopedInterceptor.class.getClassLoader()); 85 86 87 System.out.println("------- CTOR"); 88 ScopedAspect.intercepted = 0; 89 ScopedFactoryAspect.intercepted = 0; 90 ScopedFactoryAspect.metadata = null; 91 ScopedFactoryAspect.customMetadata = null; 92 93 POJO pojo = new POJO(); 94 if (ScopedAspect.intercepted != 1) 95 { 96 throw new RuntimeException ("Expected ScopedAspect 1 for POJO constructor, was " + ScopedAspect.intercepted); 97 } 98 if (ScopedFactoryAspect.intercepted != 1) 99 { 100 throw new RuntimeException ("Expected ScopedFactoryAspect 1 for POJO constructor, was " + ScopedFactoryAspect.intercepted); 101 } 102 if (!ctorPlainMetadata.equals(ScopedFactoryAspect.metadata)) 103 { 104 throw new RuntimeException ("Expected ctor metadata " + ctorPlainMetadata + ", was " + ScopedFactoryAspect.metadata); 105 } 106 if (!customMetadata.equals(ScopedFactoryAspect.customMetadata)) 107 { 108 throw new RuntimeException ("Expected ctor customm metadata " + customMetadata + ", was " + ScopedFactoryAspect.customMetadata); 109 } 110 111 System.out.println("------- METHOD"); 112 ScopedInterceptor.intercepted = 0; 113 ScopedAspect.intercepted = 0; 114 ScopedFactoryAspect.intercepted = 0; 115 ScopedFactoryAspect.metadata = null; 116 ScopedFactoryAspect.customMetadata = null; 117 118 pojo.method(); 119 if (ScopedInterceptor.intercepted != 1) 120 { 121 throw new RuntimeException ("Expected ScopedInterceptor 1 for POJO method, was " + ScopedInterceptor.intercepted); 122 } 123 if (ScopedAspect.intercepted != 1) 124 { 125 throw new RuntimeException ("Expected ScopedAspect 1 for POJO method, was " + ScopedAspect.intercepted); 126 } 127 if (ScopedFactoryAspect.intercepted != 1) 128 { 129 throw new RuntimeException ("Expected ScopedFactoryAspect 1 for POJO method, was " + ScopedFactoryAspect.intercepted); 130 } 131 if (!methodPlainMetadata.equals(ScopedFactoryAspect.metadata)) 132 { 133 throw new RuntimeException ("Expected method metadata '" + methodPlainMetadata + ", was " + ScopedFactoryAspect.metadata); 134 } 135 if (!customMetadata.equals(ScopedFactoryAspect.customMetadata)) 136 { 137 throw new RuntimeException ("Expected method customm metadata " + customMetadata + ", was " + ScopedFactoryAspect.customMetadata); 138 } 139 } 140 141 public void testAnnotatedScopedAnnotationsDeployed() 142 { 143 AnnotatedInterceptor.invoked = false; 144 POJO pojo = new POJO(); 145 if (!AnnotatedInterceptor.invoked) throw new RuntimeException ("AnnotatedInterceptor should have been invoked"); 146 } 147 148 public void testAnnotatedScopedAnnotationsNotDeployed() 149 { 150 try 151 { 152 AnnotatedInterceptor.invoked = false; 153 throw new RuntimeException ("AnnotatedInterceptor should not be available in this deployment"); 154 } 155 catch (NoClassDefFoundError expected) 156 { 157 } 158 } 159 160 public void testIntroduction1() 161 { 162 IntroducedPOJO pojo = new IntroducedPOJO(); 163 Scope1Interface iface = (Scope1Interface)pojo; 164 165 ScopedInterceptor.intercepted = 0; 166 iface.testMethod(); 167 if (ScopedInterceptor.intercepted != 1) 168 { 169 throw new RuntimeException ("Expected ScopedInterceptor 1 for introduction " + ScopedInterceptor.intercepted); 170 } 171 172 try 173 { 174 Scope2Interface iface2 = (Scope2Interface)pojo; 175 throw new RuntimeException ("Should not be implementing Scope2Interface"); 176 } 177 catch(ClassCastException expected) 178 { 179 } 180 } 181 182 public void testIntroduction2() 183 { 184 IntroducedPOJO pojo = new IntroducedPOJO(); 185 Scope2Interface iface = (Scope2Interface)pojo; 186 187 ScopedInterceptor.intercepted = 0; 188 iface.testMethod(); 189 if (ScopedInterceptor.intercepted != 1) 190 { 191 throw new RuntimeException ("Expected ScopedInterceptor 1 for introduction " + ScopedInterceptor.intercepted); 192 } 193 194 try 195 { 196 Scope1Interface iface2 = (Scope1Interface)pojo; 197 throw new RuntimeException ("Should not be implementing Scope1Interface"); 198 } 199 catch(ClassCastException expected) 200 { 201 } 202 } 203 204 } 205 | Popular Tags |