KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > instanceofannotated > InstanceOfAnnotatedTester


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.aop.instanceofannotated;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.Field JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
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 /**
37  *
38  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
39  * @version $Revision: 45977 $
40  */

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 JavaDoc name)
51    {
52       super(name);
53    }
54
55    public void testInstanceOfAnnotations()throws Exception JavaDoc
56    {
57       POJO pojo = new POJO();
58       
59       CountingInterceptor.count = 0;
60       pojo.annotatedMethod();
61       if (CountingInterceptor.count != 1)
62       {
63          throw new RuntimeException JavaDoc("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 JavaDoc("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 JavaDoc("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 JavaDoc("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 JavaDoc e)
94       {
95          throw new RuntimeException JavaDoc("POJO does not implement EmptyInterface");
96       }
97    }
98
99    public void testInstanceOfWildcards()throws Exception JavaDoc
100    {
101       CountingInterceptor.count = 0;
102       WildcardPOJO pojo = new WildcardPOJO();
103       if (CountingInterceptor.count != 1)
104       {
105          throw new RuntimeException JavaDoc("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 JavaDoc("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 JavaDoc("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 JavaDoc("Did not intercept anotherMethod()" + CountingInterceptor.count );
127       }
128    }
129
130    public void testReturnTypeAndParams() throws Exception JavaDoc
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 JavaDoc("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 JavaDoc("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 JavaDoc("Did not intercept method with typedef return:" + CountingInterceptor.count );
151       }
152   }
153    
154   public void testFieldTypes()throws Exception JavaDoc
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 JavaDoc("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 JavaDoc("Did not intercept typedef field:" + CountingInterceptor.count );
169      }
170      
171   }
172
173   public void testTypeExpressions() throws Exception JavaDoc
174   {
175      Class JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc method = introducedPOJO.getMethod("someMethod", new Class JavaDoc[]{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