1 22 package org.jboss.test.aop.instanceofannotated; 23 24 import java.lang.reflect.Constructor ; 25 import java.lang.reflect.Field ; 26 import java.lang.reflect.Method ; 27 28 import org.jboss.aop.AspectManager; 29 import org.jboss.aop.ClassAdvisor; 30 import org.jboss.test.aop.AOPTestWithSetup; 31 32 import junit.framework.Test; 33 import junit.framework.TestSuite; 34 35 36 41 public class InstanceOfAnnotatedTester extends AOPTestWithSetup 42 { 43 public static Test suite() 44 { 45 TestSuite suite = new TestSuite("InstanceOfAnnotatedTester"); 46 suite.addTestSuite(InstanceOfAnnotatedTester.class); 47 return suite; 48 } 49 50 public InstanceOfAnnotatedTester(String name) 51 { 52 super(name); 53 } 54 55 public void testInstanceOfAnnotations()throws Exception 56 { 57 POJO pojo = new POJO(); 58 59 CountingInterceptor.count = 0; 60 pojo.annotatedMethod(); 61 if (CountingInterceptor.count != 1) 62 { 63 throw new RuntimeException ("annotatedMethod did not intercept " + CountingInterceptor.count ); 64 } 65 66 CountingInterceptor.count = 0; 67 pojo.otherAnnotatedMethod(); 68 if (CountingInterceptor.count != 1) 69 { 70 throw new RuntimeException ("otherAnnotatedMethod did not intercept " + CountingInterceptor.count ); 71 } 72 73 CountingInterceptor.count = 0; 74 pojo.superClassMethod(); 75 if (CountingInterceptor.count != 1) 76 { 77 throw new RuntimeException ("superClassMethod did not intercept " + CountingInterceptor.count ); 78 } 79 80 AnnotatedSuperClass superClass = new AnnotatedSuperClass(); 81 CountingInterceptor.count = 0; 82 superClass.superClassMethod(); 83 if (CountingInterceptor.count != 1) 84 { 85 throw new RuntimeException ("AnnotatedSuperClass.superClassMethod did not intercept " + CountingInterceptor.count ); 86 } 87 88 try 89 { 90 EmptyInterface ei = (EmptyInterface)pojo; 91 System.out.println("Cast POJO to Empty interface " + ei); 92 } 93 catch (RuntimeException e) 94 { 95 throw new RuntimeException ("POJO does not implement EmptyInterface"); 96 } 97 } 98 99 public void testInstanceOfWildcards()throws Exception 100 { 101 CountingInterceptor.count = 0; 102 WildcardPOJO pojo = new WildcardPOJO(); 103 if (CountingInterceptor.count != 1) 104 { 105 throw new RuntimeException ("Did not intercept constructor" + CountingInterceptor.count ); 106 } 107 108 CountingInterceptor.count = 0; 109 pojo.someMethod(); 110 if (CountingInterceptor.count != 1) 111 { 112 throw new RuntimeException ("Did not intercept someMethod()" + CountingInterceptor.count ); 113 } 114 115 CountingInterceptor.count = 0; 116 pojo.otherMethod(); 117 if (CountingInterceptor.count != 1) 118 { 119 throw new RuntimeException ("Did not intercept otherMethod()" + CountingInterceptor.count ); 120 } 121 122 CountingInterceptor.count = 0; 123 pojo.anotherMethod(); 124 if (CountingInterceptor.count != 1) 125 { 126 throw new RuntimeException ("Did not intercept anotherMethod()" + CountingInterceptor.count ); 127 } 128 } 129 130 public void testReturnTypeAndParams() throws Exception 131 { 132 CountingInterceptor.count = 0; 133 POJO pojo = new POJO(new Type(3), new Type(4), 5); 134 if (CountingInterceptor.count != 2) 135 { 136 throw new RuntimeException ("Did not intercept constructor:" + CountingInterceptor.count ); 137 } 138 139 CountingInterceptor.count = 0; 140 pojo.paramsAndInstanceofReturn(new Type(3), new Type(4), 5); 141 if (CountingInterceptor.count != 1) 142 { 143 throw new RuntimeException ("Did not intercept method with instanceof return:" + CountingInterceptor.count ); 144 } 145 146 CountingInterceptor.count = 0; 147 pojo.paramsAndTypedefReturn(new Type(4), new Type(5), 6); 148 if (CountingInterceptor.count != 2) 149 { 150 throw new RuntimeException ("Did not intercept method with typedef return:" + CountingInterceptor.count ); 151 } 152 } 153 154 public void testFieldTypes()throws Exception 155 { 156 CountingInterceptor.count = 0; 157 POJO pojo = new POJO(); 158 pojo.instanceofField = new Type(5); 159 if (CountingInterceptor.count != 1) 160 { 161 throw new RuntimeException ("Did not intercept instanceof field:" + CountingInterceptor.count ); 162 } 163 164 CountingInterceptor.count = 0; 165 pojo.typedefField = new Type(5); 166 if (CountingInterceptor.count != 1) 167 { 168 throw new RuntimeException ("Did not intercept typedef field:" + CountingInterceptor.count ); 169 } 170 171 } 172 173 public void testTypeExpressions() throws Exception 174 { 175 Class introducedPOJO = IntroducedPOJO.class; 176 ClassAdvisor advisor = AspectManager.instance().getAdvisor(introducedPOJO); 177 178 Introduced introduced = (Introduced) advisor.resolveAnnotation(Introduced.class); 179 assertNotNull("Class did not have the @Introduced annotation", introduced); 180 181 Constructor con = introducedPOJO.getConstructors()[0]; 182 introduced = (Introduced) advisor.resolveAnnotation(con, Introduced.class); 183 assertNotNull("Constructor did not have the @Introduced annotation", introduced); 184 Introduced2 introduced2 = (Introduced2) advisor.resolveAnnotation(con, Introduced2.class); 185 assertNotNull("Constructor did not have the @Introduced2 annotation", introduced2); 186 Introduced3 introduced3 = (Introduced3) advisor.resolveAnnotation(con, Introduced3.class); 187 assertNull("Constructor has the @Introduced3 annotation", introduced3); 188 189 Field field = introducedPOJO.getField("field"); 190 introduced = (Introduced) advisor.resolveAnnotation(field, Introduced.class); 191 assertNotNull("Field did not have the @Introduced annotation", introduced); 192 introduced2 = (Introduced2) advisor.resolveAnnotation(field, Introduced2.class); 193 assertNotNull("Field did not have the @Introduced2 annotation", introduced2); 194 195 Method method = introducedPOJO.getMethod("someMethod", new Class []{Type.class, Type.class}); 196 introduced = (Introduced) advisor.resolveAnnotation(method, Introduced.class); 197 assertNotNull("Method did not have the @Introduced annotation", introduced); 198 introduced2 = (Introduced2) advisor.resolveAnnotation(method, Introduced2.class); 199 assertNotNull("Method did not have the @Introduced2 annotation", introduced2); 200 introduced3 = (Introduced3) advisor.resolveAnnotation(method, Introduced3.class); 201 assertNull("Method has the @Introduced3 annotation", introduced3); 202 } 203 204 } | Popular Tags |