KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > annotation > DefaultValueTest


1 /**************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package test.annotation;
9
10 import junit.framework.TestCase;
11 import org.codehaus.aspectwerkz.reflect.MethodInfo;
12 import org.codehaus.aspectwerkz.reflect.impl.java.JavaMethodInfo;
13 import org.codehaus.aspectwerkz.annotation.AnnotationInfo;
14
15 import java.lang.reflect.Method JavaDoc;
16
17 /**
18  * TODO change sout in asserts
19  *
20  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
21  */

22 public class DefaultValueTest extends TestCase {
23
24     @DefaultedAnnotation
25     void annotatedMethod() {
26     }
27
28     public MethodInfo getAnnotatedMethod() {
29         try {
30             Method JavaDoc m = DefaultValueTest.class.getDeclaredMethod("annotatedMethod", new Class JavaDoc[0]);
31             MethodInfo mi = JavaMethodInfo.getMethodInfo(m);
32             if (mi.getAnnotations().size() <= 0) {
33                 throw new Error JavaDoc("test corrupted");
34             }
35             return mi;
36         } catch (Throwable JavaDoc t) {
37             throw new Error JavaDoc("test corrupted");
38         }
39     }
40
41     public void testDefaultedAnnotation() {
42         AnnotationInfo annI = (AnnotationInfo) getAnnotatedMethod().getAnnotations().get(0);
43         DefaultedAnnotation ann = (DefaultedAnnotation) annI.getAnnotation();
44
45         // string
46
assertEquals("default", ann.s());
47
48         // primitive array
49
int[] is = ann.is();
50         int[] defaultIs = {1, 2};
51         for (int i = 0; i < is.length; i++) {
52             assertEquals(defaultIs[i], is[i]);
53         }
54
55         // nested annotation which has a default itself but whose assigned default differs
56
DefaultedAnnotation.NestedDefaultedAnnotation nested = ann.nested();
57         assertEquals("default_const", nested.s());
58
59         // nested annotation which is using the default itself
60
DefaultedAnnotation.NestedDefaultedAnnotation nested2 = ann.nested2();
61         assertEquals("default_nested", nested2.s());
62
63         //-- so far we should not have triggered any ReferencedClass loading
64
System.out.println("----");
65         // class
66
assertEquals(ReferencedClass.class, ann.klass());
67
68         // class
69
assertEquals(ReferencedClass[].class.getName(), ann.klass2()[0].getName());
70         assertEquals(ReferencedClass.class, ann.klass2()[1]);
71     }
72
73     public void testToString() throws Throwable JavaDoc {
74         AnnotationInfo annI = (AnnotationInfo) getAnnotatedMethod().getAnnotations().get(0);
75         DefaultedAnnotation ann = (DefaultedAnnotation) annI.getAnnotation();
76
77         System.out.println("");
78         System.out.println(ann.toString());
79         System.out.println(ann.annotationType());
80
81
82         Method JavaDoc m = DefaultValueTest.class.getDeclaredMethod("annotatedMethod", new Class JavaDoc[0]);
83         System.out.println(m);
84         java.lang.annotation.Annotation JavaDoc[] anns = m.getAnnotations();
85         for (int i = 0; i < anns.length; i++) {
86             java.lang.annotation.Annotation JavaDoc annotation = anns[i];
87             System.out.println(annotation);
88             System.out.println(annotation.annotationType());
89         }
90
91
92     }
93
94     public static void main(String JavaDoc[] args) {
95         junit.textui.TestRunner.run(suite());
96     }
97
98     public static junit.framework.Test suite() {
99         return new junit.framework.TestSuite(DefaultValueTest.class);
100     }
101
102
103
104 }
105
Popular Tags