KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > annotationc > AnnotationTester


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.annotationc;
23
24
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27
28 import org.jboss.aop.annotation.AnnotationElement;
29 import org.jboss.aop.annotation.PortableAnnotationElement;
30 import org.jboss.aop.annotation.factory.duplicate.AnnotationCreator;
31 import org.jboss.aop.util.ConstructorComparator;
32 import org.jboss.test.aop.AOPTestWithSetup;
33
34 import java.lang.reflect.Constructor JavaDoc;
35 import java.lang.reflect.Field JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37 import java.util.Arrays JavaDoc;
38
39 /**
40  * Tests an annotated introduction
41  *
42  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
43  * @version $Revision: 57019 $
44  */

45 public class AnnotationTester extends AOPTestWithSetup
46 {
47    public static Test suite()
48    {
49       TestSuite suite = new TestSuite("AnnotationTester");
50       suite.addTestSuite(AnnotationTester.class);
51       return suite;
52    }
53    // Constants ----------------------------------------------------
54
// Attributes ---------------------------------------------------
55

56    // Static -------------------------------------------------------
57

58    // Constructors -------------------------------------------------
59
// * @@org.jboss.test.aop.annotationc.complex(ch='a', string="hello world", flt=5.5, dbl=6.6, shrt=5, lng=6, integer=7, bool=true, annotation=@single("hello"), array={"hello", "world"), clazz=java.lang.String)
60

61    public AnnotationTester(String JavaDoc name)
62    {
63       super(name);
64    }
65
66    public void testCreateAnnotation() throws Exception JavaDoc
67    {
68       String JavaDoc expr = "@org.jboss.test.aop.annotationc.complex (ch='a', string=\"hello world\", flt=5.5, dbl=6.6, shrt=5, lng=6, integer=7, bool=true, annotation=@org.jboss.test.aop.annotationc.single(\"hello\"), array={\"hello\", \"world\"}, clazz=java.lang.String, enumVal=org.jboss.test.aop.annotationc.MyEnum.ONE)";
69       complex c = (complex) AnnotationCreator.createAnnotation(expr, complex.class);
70       if (c == null) throw new RuntimeException JavaDoc("failed to get @complex");
71       if (c.ch() != 'a') throw new RuntimeException JavaDoc("@complex.ch has wrong value");
72       if (!c.string().equals("hello world")) throw new RuntimeException JavaDoc("@complex.string has wrong value");
73       if (c.flt() != 5.5) throw new RuntimeException JavaDoc("@complex.flt has wrong value");
74       if (c.dbl() != 6.6) throw new RuntimeException JavaDoc("@complex.dbl has wrong value");
75       if (c.shrt() != 5) throw new RuntimeException JavaDoc("@complex.shrt has wrong value");
76       if (c.lng() != 6) throw new RuntimeException JavaDoc("@complex.lng has wrong value");
77       if (c.integer() != 7) throw new RuntimeException JavaDoc("@complex.integer has wrong value");
78       if (c.bool() == false) throw new RuntimeException JavaDoc("@complex.bool has wrong value");
79       single s = c.annotation();
80       if (s == null) throw new RuntimeException JavaDoc("@complex.annotation is null");
81       if (!s.value().equals("hello")) throw new RuntimeException JavaDoc("@complex.annotation has wrong value");
82       if (!c.array()[0].equals("hello")) throw new RuntimeException JavaDoc("@complex.array[0] has wrong value");
83       if (!c.array()[1].equals("world")) throw new RuntimeException JavaDoc("@complex.array[1] has wrong value");
84       if (!java.lang.String JavaDoc.class.equals(c.clazz())) throw new RuntimeException JavaDoc("@complex.clazz has wrong value");
85       if (c.enumVal() != MyEnum.ONE) throw new RuntimeException JavaDoc("@complex.enumVal has wrong value");
86    }
87
88    public void testClassAnnotation()
89    {
90       empty e = (empty) AnnotationElement.getAnyAnnotation(BytecodePOJO.class, empty.class);
91       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
92       single s = (single) AnnotationElement.getAnyAnnotation(BytecodePOJO.class, single.class);
93       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
94       if (!s.value().equals("hello world")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
95
96       complex c = (complex) AnnotationElement.getAnyAnnotation(BytecodePOJO.class, complex.class);
97       if (c == null) throw new RuntimeException JavaDoc("failed to get @complex");
98       if (c.ch() != 'a') throw new RuntimeException JavaDoc("@complex.ch has wrong value");
99       if (!c.string().equals("hello world")) throw new RuntimeException JavaDoc("@complex.string has wrong value");
100       if (c.flt() != 5.5) throw new RuntimeException JavaDoc("@complex.flt has wrong value");
101       if (c.dbl() != 6.6) throw new RuntimeException JavaDoc("@complex.dbl has wrong value");
102       if (c.shrt() != 5) throw new RuntimeException JavaDoc("@complex.shrt has wrong value");
103       if (c.lng() != 6) throw new RuntimeException JavaDoc("@complex.lng has wrong value");
104       if (c.integer() != 7) throw new RuntimeException JavaDoc("@complex.integer has wrong value");
105       if (c.bool() == false) throw new RuntimeException JavaDoc("@complex.bool has wrong value");
106       s = c.annotation();
107       if (s == null) throw new RuntimeException JavaDoc("@complex.annotation is null");
108       if (!s.value().equals("hello")) throw new RuntimeException JavaDoc("@complex.annotation has wrong value");
109       if (!c.array()[0].equals("hello")) throw new RuntimeException JavaDoc("@complex.array[0] has wrong value");
110       if (!c.array()[1].equals("world")) throw new RuntimeException JavaDoc("@complex.array[1] has wrong value");
111       if (!java.lang.String JavaDoc.class.equals(c.clazz())) throw new RuntimeException JavaDoc("@complex.clazz has wrong value");
112       if (c.enumVal() != MyEnum.ONE) throw new RuntimeException JavaDoc("@complex.enumVal has wrong value");
113    }
114
115    public void testClassGetVisibleAnnotations()
116    {
117       empty e = null;
118       single s = null;
119       complex c = null;
120       try
121       {
122          Object JavaDoc[] annotations = PortableAnnotationElement.getVisibleAnnotations(BytecodePOJO.class);
123          for (int i = 0 ; i < annotations.length ; i++)
124          {
125             if (complex.class.isAssignableFrom(annotations[i].getClass()))
126             {
127                c = (complex)annotations[i];
128             }
129             else if (empty.class.isAssignableFrom(annotations[i].getClass()))
130             {
131                e = (empty)annotations[i];
132             }
133             else if (single.class.isAssignableFrom(annotations[i].getClass()))
134             {
135                s = (single)annotations[i];
136             }
137          }
138       }
139       catch (Exception JavaDoc e1)
140       {
141          // AutoGenerated
142
throw new RuntimeException JavaDoc(e1);
143       }
144
145       assertNotNull(c);
146       assertNotNull(e);
147       assertNotNull(s);
148
149       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
150       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
151       if (!s.value().equals("hello world")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
152
153       if (c == null) throw new RuntimeException JavaDoc("failed to get @complex");
154       if (c.ch() != 'a') throw new RuntimeException JavaDoc("@complex.ch has wrong value");
155       if (!c.string().equals("hello world")) throw new RuntimeException JavaDoc("@complex.string has wrong value");
156       if (c.flt() != 5.5) throw new RuntimeException JavaDoc("@complex.flt has wrong value");
157       if (c.dbl() != 6.6) throw new RuntimeException JavaDoc("@complex.dbl has wrong value");
158       if (c.shrt() != 5) throw new RuntimeException JavaDoc("@complex.shrt has wrong value");
159       if (c.lng() != 6) throw new RuntimeException JavaDoc("@complex.lng has wrong value");
160       if (c.integer() != 7) throw new RuntimeException JavaDoc("@complex.integer has wrong value");
161       if (c.bool() == false) throw new RuntimeException JavaDoc("@complex.bool has wrong value");
162       s = c.annotation();
163       if (s == null) throw new RuntimeException JavaDoc("@complex.annotation is null");
164       if (!s.value().equals("hello")) throw new RuntimeException JavaDoc("@complex.annotation has wrong value");
165       if (!c.array()[0].equals("hello")) throw new RuntimeException JavaDoc("@complex.array[0] has wrong value");
166       if (!c.array()[1].equals("world")) throw new RuntimeException JavaDoc("@complex.array[1] has wrong value");
167       if (!java.lang.String JavaDoc.class.equals(c.clazz())) throw new RuntimeException JavaDoc("@complex.clazz has wrong value");
168       if (c.enumVal() != MyEnum.ONE) throw new RuntimeException JavaDoc("@complex.enumVal has wrong value");
169       
170    }
171
172    
173    public void testConstructorAnnotation()
174    {
175       Constructor JavaDoc con = BytecodePOJO.class.getConstructors()[0];
176       empty e = (empty) AnnotationElement.getAnyAnnotation(con, empty.class);
177       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
178       single s = (single) AnnotationElement.getAnyAnnotation(con, single.class);
179       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
180       if (!s.value().equals("hello world")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
181
182       complex c = (complex) AnnotationElement.getAnyAnnotation(con, complex.class);
183       if (c == null) throw new RuntimeException JavaDoc("failed to get @complex");
184       if (c.ch() != 'a') throw new RuntimeException JavaDoc("@complex.ch has wrong value");
185       if (!c.string().equals("hello world")) throw new RuntimeException JavaDoc("@complex.string has wrong value");
186       if (c.flt() != 5.5) throw new RuntimeException JavaDoc("@complex.flt has wrong value");
187       if (c.dbl() != 6.6) throw new RuntimeException JavaDoc("@complex.dbl has wrong value");
188       if (c.shrt() != 5) throw new RuntimeException JavaDoc("@complex.shrt has wrong value");
189       if (c.lng() != 6) throw new RuntimeException JavaDoc("@complex.lng has wrong value");
190       if (c.integer() != 7) throw new RuntimeException JavaDoc("@complex.integer has wrong value");
191       if (c.bool() == false) throw new RuntimeException JavaDoc("@complex.bool has wrong value");
192       s = c.annotation();
193       if (s == null) throw new RuntimeException JavaDoc("@complex.annotation is null");
194       if (!s.value().equals("hello")) throw new RuntimeException JavaDoc("@complex.annotation has wrong value");
195       if (!c.array()[0].equals("hello")) throw new RuntimeException JavaDoc("@complex.array[0] has wrong value");
196       if (!c.array()[1].equals("world")) throw new RuntimeException JavaDoc("@complex.array[1] has wrong value");
197       if (!java.lang.String JavaDoc.class.equals(c.clazz())) throw new RuntimeException JavaDoc("@complex.clazz has wrong value");
198       if (c.enumVal() != MyEnum.TWO) throw new RuntimeException JavaDoc("@complex.enumVal has wrong value");
199    }
200
201    public void testConstructorGetVisibleAnnotations() throws Exception JavaDoc
202    {
203       Constructor JavaDoc con = BytecodePOJO.class.getConstructors()[0];
204       empty e = null;
205       complex c = null;
206       single s = null;
207       
208       try
209       {
210          Object JavaDoc[] annotations = PortableAnnotationElement.getVisibleAnnotations(con);
211          for (int i = 0 ; i < annotations.length ; i++)
212          {
213             if (complex.class.isAssignableFrom(annotations[i].getClass()))
214             {
215                c = (complex)annotations[i];
216             }
217             else if (empty.class.isAssignableFrom(annotations[i].getClass()))
218             {
219                e = (empty)annotations[i];
220             }
221             else if (single.class.isAssignableFrom(annotations[i].getClass()))
222             {
223                s = (single)annotations[i];
224             }
225          }
226       }
227       catch (Exception JavaDoc e1)
228       {
229          throw new RuntimeException JavaDoc(e1);
230       }
231       assertNotNull(c);
232       assertNotNull(e);
233       assertNotNull(s);
234
235       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
236       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
237       if (!s.value().equals("hello world")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
238
239       if (c == null) throw new RuntimeException JavaDoc("failed to get @complex");
240       if (c.ch() != 'a') throw new RuntimeException JavaDoc("@complex.ch has wrong value");
241       if (!c.string().equals("hello world")) throw new RuntimeException JavaDoc("@complex.string has wrong value");
242       if (c.flt() != 5.5) throw new RuntimeException JavaDoc("@complex.flt has wrong value");
243       if (c.dbl() != 6.6) throw new RuntimeException JavaDoc("@complex.dbl has wrong value");
244       if (c.shrt() != 5) throw new RuntimeException JavaDoc("@complex.shrt has wrong value");
245       if (c.lng() != 6) throw new RuntimeException JavaDoc("@complex.lng has wrong value");
246       if (c.integer() != 7) throw new RuntimeException JavaDoc("@complex.integer has wrong value");
247       if (c.bool() == false) throw new RuntimeException JavaDoc("@complex.bool has wrong value");
248       s = c.annotation();
249       if (s == null) throw new RuntimeException JavaDoc("@complex.annotation is null");
250       if (!s.value().equals("hello")) throw new RuntimeException JavaDoc("@complex.annotation has wrong value");
251       if (!c.array()[0].equals("hello")) throw new RuntimeException JavaDoc("@complex.array[0] has wrong value");
252       if (!c.array()[1].equals("world")) throw new RuntimeException JavaDoc("@complex.array[1] has wrong value");
253       if (!java.lang.String JavaDoc.class.equals(c.clazz())) throw new RuntimeException JavaDoc("@complex.clazz has wrong value");
254       if (c.enumVal() != MyEnum.TWO) throw new RuntimeException JavaDoc("@complex.enumVal has wrong value");
255       
256    }
257    
258    public void testMethodAnnotation() throws Exception JavaDoc
259    {
260       Method JavaDoc con = BytecodePOJO.class.getMethod("method", new Class JavaDoc[0]);
261       empty e = (empty) AnnotationElement.getAnyAnnotation(con, empty.class);
262       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
263       single s = (single) AnnotationElement.getAnyAnnotation(con, single.class);
264       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
265       if (!s.value().equals("hello world")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
266
267       complex c = (complex) AnnotationElement.getAnyAnnotation(con, complex.class);
268       if (c == null) throw new RuntimeException JavaDoc("failed to get @complex");
269       if (c.ch() != 'a') throw new RuntimeException JavaDoc("@complex.ch has wrong value");
270       if (!c.string().equals("hello world")) throw new RuntimeException JavaDoc("@complex.string has wrong value");
271       if (c.flt() != 5.5) throw new RuntimeException JavaDoc("@complex.flt has wrong value");
272       if (c.dbl() != 6.6) throw new RuntimeException JavaDoc("@complex.dbl has wrong value");
273       if (c.shrt() != 5) throw new RuntimeException JavaDoc("@complex.shrt has wrong value");
274       if (c.lng() != 6) throw new RuntimeException JavaDoc("@complex.lng has wrong value");
275       if (c.integer() != 7) throw new RuntimeException JavaDoc("@complex.integer has wrong value");
276       if (c.bool() == false) throw new RuntimeException JavaDoc("@complex.bool has wrong value");
277       s = c.annotation();
278       if (s == null) throw new RuntimeException JavaDoc("@complex.annotation is null");
279       if (!s.value().equals("hello")) throw new RuntimeException JavaDoc("@complex.annotation has wrong value");
280       if (!c.array()[0].equals("hello")) throw new RuntimeException JavaDoc("@complex.array[0] has wrong value");
281       if (!c.array()[1].equals("world")) throw new RuntimeException JavaDoc("@complex.array[1] has wrong value");
282       if (!java.lang.String JavaDoc.class.equals(c.clazz())) throw new RuntimeException JavaDoc("@complex.clazz has wrong value");
283       if (c.enumVal() != MyEnum.TWO) throw new RuntimeException JavaDoc("@complex.enumVal has wrong value");
284    }
285
286    public void testMethodGetVisibleAnnotations() throws Exception JavaDoc
287    {
288       Method JavaDoc con = BytecodePOJO.class.getMethod("method", new Class JavaDoc[0]);
289       empty e = null;
290       complex c = null;
291       single s = null;
292       
293       try
294       {
295          Object JavaDoc[] annotations = PortableAnnotationElement.getVisibleAnnotations(con);
296          for (int i = 0 ; i < annotations.length ; i++)
297          {
298             if (complex.class.isAssignableFrom(annotations[i].getClass()))
299             {
300                c = (complex)annotations[i];
301             }
302             else if (empty.class.isAssignableFrom(annotations[i].getClass()))
303             {
304                e = (empty)annotations[i];
305             }
306             else if (single.class.isAssignableFrom(annotations[i].getClass()))
307             {
308                s = (single)annotations[i];
309             }
310          }
311       }
312       catch (Exception JavaDoc e1)
313       {
314          throw new RuntimeException JavaDoc(e1);
315       }
316       assertNotNull(c);
317       assertNotNull(e);
318       assertNotNull(s);
319
320       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
321       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
322       if (!s.value().equals("hello world")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
323
324       if (c == null) throw new RuntimeException JavaDoc("failed to get @complex");
325       if (c.ch() != 'a') throw new RuntimeException JavaDoc("@complex.ch has wrong value");
326       if (!c.string().equals("hello world")) throw new RuntimeException JavaDoc("@complex.string has wrong value");
327       if (c.flt() != 5.5) throw new RuntimeException JavaDoc("@complex.flt has wrong value");
328       if (c.dbl() != 6.6) throw new RuntimeException JavaDoc("@complex.dbl has wrong value");
329       if (c.shrt() != 5) throw new RuntimeException JavaDoc("@complex.shrt has wrong value");
330       if (c.lng() != 6) throw new RuntimeException JavaDoc("@complex.lng has wrong value");
331       if (c.integer() != 7) throw new RuntimeException JavaDoc("@complex.integer has wrong value");
332       if (c.bool() == false) throw new RuntimeException JavaDoc("@complex.bool has wrong value");
333       s = c.annotation();
334       if (s == null) throw new RuntimeException JavaDoc("@complex.annotation is null");
335       if (!s.value().equals("hello")) throw new RuntimeException JavaDoc("@complex.annotation has wrong value");
336       if (!c.array()[0].equals("hello")) throw new RuntimeException JavaDoc("@complex.array[0] has wrong value");
337       if (!c.array()[1].equals("world")) throw new RuntimeException JavaDoc("@complex.array[1] has wrong value");
338       if (!java.lang.String JavaDoc.class.equals(c.clazz())) throw new RuntimeException JavaDoc("@complex.clazz has wrong value");
339       if (c.enumVal() != MyEnum.TWO) throw new RuntimeException JavaDoc("@complex.enumVal has wrong value");
340    }
341
342    public void testPrimitiveClassAnnotationCreation() throws Exception JavaDoc
343    {
344       testPrimitiveClassCreate("void.class", void.class);
345       testPrimitiveClassCreate("int.class", int.class);
346       testPrimitiveClassCreate("short.class", short.class);
347       testPrimitiveClassCreate("long.class", long.class);
348       testPrimitiveClassCreate("double.class", double.class);
349       testPrimitiveClassCreate("float.class", float.class);
350       testPrimitiveClassCreate("char.class", char.class);
351       testPrimitiveClassCreate("boolean.class", boolean.class);
352    }
353
354    private void testPrimitiveClassCreate(String JavaDoc sPrimitive, Class JavaDoc primitive)
355    throws Exception JavaDoc
356    {
357       String JavaDoc expr = "@org.jboss.test.aop.annotationc.ClassAnnotation (" + sPrimitive + ")";
358       ClassAnnotation c = (ClassAnnotation) AnnotationCreator.createAnnotation(expr, ClassAnnotation.class);
359       if (!c.value().equals(primitive)) throw new RuntimeException JavaDoc("failed to match " + primitive.getName());
360    }
361
362    public void testPrimitiveClassMemberAnnotation() throws Exception JavaDoc
363    {
364       Class JavaDoc clazz = BytecodePOJO.class;
365       Class JavaDoc[] types = {};
366       primitive(clazz.getMethod("someVoid", types), void.class);
367       primitive(clazz.getMethod("someInt", types), int.class);
368       primitive(clazz.getMethod("someShort", types), short.class);
369       primitive(clazz.getMethod("someLong", types), long.class);
370       primitive(clazz.getMethod("someDouble", types), double.class);
371       primitive(clazz.getMethod("someFloat", types), float.class);
372       primitive(clazz.getMethod("someChar", types), char.class);
373       primitive(clazz.getMethod("someBoolean", types), boolean.class);
374    }
375
376    private void primitive(Method JavaDoc method, Class JavaDoc primitive)
377    {
378       ClassAnnotation ca = (ClassAnnotation) AnnotationElement.getAnyAnnotation(method, ClassAnnotation.class);
379       if (ca == null) throw new RuntimeException JavaDoc("Failed to get ClassAnnotation for: " + method);
380       if (!ca.value().equals(primitive)) throw new RuntimeException JavaDoc("failed to match " + primitive.getName() + " for method: " + method);
381    }
382
383    public void testFieldAnnotation() throws Exception JavaDoc
384    {
385       Field JavaDoc con = BytecodePOJO.class.getField("field");
386       empty e = (empty) AnnotationElement.getAnyAnnotation(con, empty.class);
387       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
388       single s = (single) AnnotationElement.getAnyAnnotation(con, single.class);
389       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
390       if (!s.value().equals("hello world")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
391
392       complex c = (complex) AnnotationElement.getAnyAnnotation(con, complex.class);
393       if (c == null) throw new RuntimeException JavaDoc("failed to get @complex");
394       if (c.ch() != 'a') throw new RuntimeException JavaDoc("@complex.ch has wrong value");
395       if (!c.string().equals("hello world")) throw new RuntimeException JavaDoc("@complex.string has wrong value");
396       if (c.flt() != 5.5) throw new RuntimeException JavaDoc("@complex.flt has wrong value");
397       if (c.dbl() != 6.6) throw new RuntimeException JavaDoc("@complex.dbl has wrong value");
398       if (c.shrt() != 5) throw new RuntimeException JavaDoc("@complex.shrt has wrong value");
399       if (c.lng() != 6) throw new RuntimeException JavaDoc("@complex.lng has wrong value");
400       if (c.integer() != 7) throw new RuntimeException JavaDoc("@complex.integer has wrong value");
401       if (c.bool() == false) throw new RuntimeException JavaDoc("@complex.bool has wrong value");
402       s = c.annotation();
403       if (s == null) throw new RuntimeException JavaDoc("@complex.annotation is null");
404       if (!s.value().equals("hello")) throw new RuntimeException JavaDoc("@complex.annotation has wrong value");
405       if (!c.array()[0].equals("hello")) throw new RuntimeException JavaDoc("@complex.array[0] has wrong value");
406       if (!c.array()[1].equals("world")) throw new RuntimeException JavaDoc("@complex.array[1] has wrong value");
407       if (!java.lang.String JavaDoc.class.equals(c.clazz())) throw new RuntimeException JavaDoc("@complex.clazz has wrong value");
408       if (c.enumVal() != MyEnum.ONE) throw new RuntimeException JavaDoc("@complex.enumVal has wrong value");
409    }
410    
411    public void testFieldGetVisibleAnnotations() throws Exception JavaDoc
412    {
413       Field JavaDoc con = BytecodePOJO.class.getField("field");
414       empty e = null;
415       complex c = null;
416       single s = null;
417       
418       try
419       {
420          Object JavaDoc[] annotations = PortableAnnotationElement.getVisibleAnnotations(con);
421          for (int i = 0 ; i < annotations.length ; i++)
422          {
423             if (complex.class.isAssignableFrom(annotations[i].getClass()))
424             {
425                c = (complex)annotations[i];
426             }
427             else if (empty.class.isAssignableFrom(annotations[i].getClass()))
428             {
429                e = (empty)annotations[i];
430             }
431             else if (single.class.isAssignableFrom(annotations[i].getClass()))
432             {
433                s = (single)annotations[i];
434             }
435          }
436       }
437       catch (Exception JavaDoc e1)
438       {
439          throw new RuntimeException JavaDoc(e1);
440       }
441       assertNotNull(c);
442       assertNotNull(e);
443       assertNotNull(s);
444
445       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
446       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
447       if (!s.value().equals("hello world")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
448
449       if (c == null) throw new RuntimeException JavaDoc("failed to get @complex");
450       if (c.ch() != 'a') throw new RuntimeException JavaDoc("@complex.ch has wrong value");
451       if (!c.string().equals("hello world")) throw new RuntimeException JavaDoc("@complex.string has wrong value");
452       if (c.flt() != 5.5) throw new RuntimeException JavaDoc("@complex.flt has wrong value");
453       if (c.dbl() != 6.6) throw new RuntimeException JavaDoc("@complex.dbl has wrong value");
454       if (c.shrt() != 5) throw new RuntimeException JavaDoc("@complex.shrt has wrong value");
455       if (c.lng() != 6) throw new RuntimeException JavaDoc("@complex.lng has wrong value");
456       if (c.integer() != 7) throw new RuntimeException JavaDoc("@complex.integer has wrong value");
457       if (c.bool() == false) throw new RuntimeException JavaDoc("@complex.bool has wrong value");
458       s = c.annotation();
459       if (s == null) throw new RuntimeException JavaDoc("@complex.annotation is null");
460       if (!s.value().equals("hello")) throw new RuntimeException JavaDoc("@complex.annotation has wrong value");
461       if (!c.array()[0].equals("hello")) throw new RuntimeException JavaDoc("@complex.array[0] has wrong value");
462       if (!c.array()[1].equals("world")) throw new RuntimeException JavaDoc("@complex.array[1] has wrong value");
463       if (!java.lang.String JavaDoc.class.equals(c.clazz())) throw new RuntimeException JavaDoc("@complex.clazz has wrong value");
464       if (c.enumVal() != MyEnum.ONE) throw new RuntimeException JavaDoc("@complex.enumVal has wrong value");
465    }
466    
467    public void testInnerClass() throws Exception JavaDoc
468    {
469       //Check class
470
empty e = (empty)AnnotationElement.getAnyAnnotation(BytecodePOJO.InnerClass.class, empty.class);
471       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
472       single s = (single)AnnotationElement.getAnyAnnotation(BytecodePOJO.InnerClass.class, single.class);
473       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
474       if (!s.value().equals("inner")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
475       
476       //Non static inner classes get an extra ctor param containing the outer class, do sorting here to get right order between
477
//default and other ctor
478
Constructor JavaDoc[] ctors = BytecodePOJO.InnerClass.class.getDeclaredConstructors();
479       Arrays.sort(ctors, ConstructorComparator.INSTANCE);
480
481       System.out.println(ctors[0]);
482       System.out.println(ctors[1]);
483       //Check default ctor
484
Constructor JavaDoc def = ctors[0];
485       e = (empty)AnnotationElement.getAnyAnnotation(def, empty.class);
486       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
487       s = (single)AnnotationElement.getAnyAnnotation(def, single.class);
488       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
489       if (!s.value().equals("inner ctor")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
490       
491       //Check ctor
492
Constructor JavaDoc con = ctors[1];
493       e = (empty)AnnotationElement.getAnyAnnotation(con, empty.class);
494       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
495       s = (single)AnnotationElement.getAnyAnnotation(con, single.class);
496       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
497       if (!s.value().equals("inner ctor 2")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
498       
499       //Check method
500
Method JavaDoc m = BytecodePOJO.InnerClass.class.getDeclaredMethod("method", new Class JavaDoc[] {Integer.TYPE});
501       e = (empty)AnnotationElement.getAnyAnnotation(m, empty.class);
502       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
503       s = (single)AnnotationElement.getAnyAnnotation(m, single.class);
504       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
505       if (!s.value().equals("inner method")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
506
507       //check field
508
Field JavaDoc f = BytecodePOJO.InnerClass.class.getDeclaredField("field");
509       e = (empty)AnnotationElement.getAnyAnnotation(f, empty.class);
510       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
511       s = (single)AnnotationElement.getAnyAnnotation(f, single.class);
512       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
513       if (!s.value().equals("inner field")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
514    }
515
516    public void testStaticInnerClass() throws Exception JavaDoc
517    {
518       //Check class
519
empty e = (empty)AnnotationElement.getAnyAnnotation(BytecodePOJO.StaticInnerClass.class, empty.class);
520       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
521       single s = (single)AnnotationElement.getAnyAnnotation(BytecodePOJO.StaticInnerClass.class, single.class);
522       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
523       if (!s.value().equals("static inner")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
524       
525       //Check default ctor
526
Constructor JavaDoc def = BytecodePOJO.StaticInnerClass.class.getDeclaredConstructor(new Class JavaDoc[0]);
527       e = (empty)AnnotationElement.getAnyAnnotation(def, empty.class);
528       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
529       s = (single)AnnotationElement.getAnyAnnotation(def, single.class);
530       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
531       if (!s.value().equals("static inner ctor")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
532       
533       //Check ctor
534
Constructor JavaDoc con = BytecodePOJO.StaticInnerClass.class.getDeclaredConstructor(new Class JavaDoc[] {Integer.TYPE});
535       e = (empty)AnnotationElement.getAnyAnnotation(con, empty.class);
536       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
537       s = (single)AnnotationElement.getAnyAnnotation(con, single.class);
538       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
539       if (!s.value().equals("static inner ctor 2")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
540       
541       //Check method
542
Method JavaDoc m = BytecodePOJO.StaticInnerClass.class.getDeclaredMethod("method", new Class JavaDoc[] {Integer.TYPE});
543       e = (empty)AnnotationElement.getAnyAnnotation(m, empty.class);
544       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
545       s = (single)AnnotationElement.getAnyAnnotation(m, single.class);
546       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
547       if (!s.value().equals("static inner method")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
548
549       //check field
550
Field JavaDoc f = BytecodePOJO.StaticInnerClass.class.getDeclaredField("field");
551       e = (empty)AnnotationElement.getAnyAnnotation(f, empty.class);
552       if (e == null) throw new RuntimeException JavaDoc("failed to get @empty");
553       s = (single)AnnotationElement.getAnyAnnotation(f, single.class);
554       if (s == null) throw new RuntimeException JavaDoc("failed to get @single");
555       if (!s.value().equals("static inner field")) throw new RuntimeException JavaDoc("@single.value mismatch: " + s.value());
556    }
557 }
558
559
Popular Tags