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.annotation.Annotations;
12  import org.codehaus.aspectwerkz.annotation.UntypedAnnotationProxy;
13  
14  import java.util.List;
15  import java.lang.reflect.Method;
16  
17  /***
18   * Note: when using untyped annotation, then the first space character(s) in the value part will be
19   * resumed to only one space (untyped     type -> untyped type), due to QDox doclet handling.
20   *
21   * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
22   * @BeforeAction some untype that starts with Before
23   * @BeforeAction(other   untyped)
24   * @BeforeAction("yet another untyped")
25   * @packaged.BeforeAction
26   * @Void
27   * @Void()
28   * @Simple()
29   * @Simple(val="foo", s="foo")
30   * @DefaultString("hello")
31   * @packaged.DefaultString("hello")
32   * @Complex(i=3, ls={1l,2l,6L},  klass=java.lang.String.class)
33   * @Untyped
34   * @Untyped hello
35   * @Untyped "hello"
36   * @Untyped ("hello") - see the space here !
37   * @Untyped (hello) - see the space here !
38   */
39  public class AnnotationCTest extends TestCase {
40  
41      public void testClassAnnotation() {
42          Class me = AnnotationCTest.class;
43  
44          List voids = Annotations.getAnnotations("Void", me);
45          assertEquals(2, voids.size());
46  
47          List simples = Annotations.getAnnotations("Simple", me);
48          assertEquals(2, simples.size());
49          StringBuffer all = new StringBuffer();
50          for (int i = 0; i < simples.size(); i++) {
51              all.append("[").append(((AnnotationParserTest.Simple) simples.get(i)).s()).append("]");
52          }
53          String[] lookFor = new String[]{
54              "[null]",
55              "[foo]"
56          };
57          for (int i = 0; i < lookFor.length; i++) {
58              String s = lookFor[i];
59              if (all.indexOf(s) < 0) {
60                  fail("could not find " + lookFor[i] + " in " + all.toString());
61              }
62          }
63  
64          List beforeActions = Annotations.getAnnotations("BeforeAction", me);
65          assertEquals(3, beforeActions.size());
66          all = new StringBuffer();
67          for (int i = 0; i < beforeActions.size(); i++) {
68              all.append("[").append(((UntypedAnnotationProxy)beforeActions.get(i)).getValue()).append("]");
69          }
70          lookFor = new String[]{
71              "[some untype that starts with Before]",
72              "[other untyped]",// some space chars have been lost
73              "[\"yet another untyped\"]",
74  
75          };
76          for (int i = 0; i < lookFor.length; i++) {
77              String s = lookFor[i];
78              if (all.indexOf(s) < 0) {
79                  fail("could not find " + lookFor[i] + " in " + all.toString());
80              }
81          }
82  
83          assertEquals(
84                  "hello",
85                  ((AnnotationParserTest.DefaultString) Annotations.getAnnotation("DefaultString", me)).getValue()
86          );
87  
88          assertEquals(
89                  String.class, ((AnnotationParserTest.Complex) Annotations.getAnnotation("Complex", me)).getKlass()
90          );
91  
92          List untypeds = Annotations.getAnnotations("Untyped", me);
93          assertEquals(5, untypeds.size());
94          all = new StringBuffer();
95          for (int i = 0; i < untypeds.size(); i++) {
96              all.append("[").append(((AnnotationParserTest.Untyped) untypeds.get(i)).getValue()).append("]");
97          }
98          lookFor = new String[]{
99              "[]",
100             "hello",
101             "\"hello\"",
102             "(\"hello\") - see the space here !",
103             "(hello)"
104         };
105         for (int i = 0; i < lookFor.length; i++) {
106             String s = lookFor[i];
107             if (all.indexOf(s) < 0) {
108                 fail("could not find " + lookFor[i] + " in " + all.toString());
109             }
110         }
111     }
112 
113     /***
114      * @Void
115      * @Void()
116      * @Simple()
117      * @Simple(val="foo", s="foo")
118      * @DefaultString("hello")
119      * @Complex(i=3, ls={1l,2l,6L},  klass=java.lang.String.class)
120      * @Untyped
121      * @Untyped hello
122      * @Untyped "hello"
123      * @Untyped ("hello") - see the space here !
124      * @Untyped (hello) - see the space here !
125      */
126     public void testMethodAnnotation() throws Throwable {
127         Class me = test.annotation.AnnotationCTest.class;
128         Method m = me.getDeclaredMethod("testMethodAnnotation", new Class[0]);
129 
130         List voids = Annotations.getAnnotations("Void", me);
131         assertEquals(2, voids.size());
132 
133         List simples = Annotations.getAnnotations("Simple", me);
134         assertEquals(2, simples.size());
135         StringBuffer all = new StringBuffer();
136         for (int i = 0; i < simples.size(); i++) {
137             all.append("[").append(((AnnotationParserTest.Simple) simples.get(i)).s()).append("]");
138         }
139         String[] lookFor = new String[]{
140             "[null]",
141             "[foo]"
142         };
143         for (int i = 0; i < lookFor.length; i++) {
144             String s = lookFor[i];
145             if (all.indexOf(s) < 0) {
146                 fail("could not find " + lookFor[i] + " in " + all.toString());
147             }
148         }
149 
150         assertEquals(
151                 "hello",
152                 ((AnnotationParserTest.DefaultString) Annotations.getAnnotation("DefaultString", me)).getValue()
153         );
154 
155         assertEquals(
156                 String.class, ((AnnotationParserTest.Complex) Annotations.getAnnotation("Complex", me)).getKlass()
157         );
158 
159         List untypeds = Annotations.getAnnotations("Untyped", me);
160         assertEquals(5, untypeds.size());
161         all = new StringBuffer();
162         for (int i = 0; i < untypeds.size(); i++) {
163             all.append("[").append(((AnnotationParserTest.Untyped) untypeds.get(i)).getValue()).append("]");
164         }
165         lookFor = new String[]{
166             "[]",
167             "hello",
168             "\"hello\"",
169             "(\"hello\") - see the space here !",
170             "(hello)"
171         };
172         for (int i = 0; i < lookFor.length; i++) {
173             String s = lookFor[i];
174             if (all.indexOf(s) < 0) {
175                 fail("could not find " + lookFor[i] + " in " + all.toString());
176             }
177         }
178     }
179 
180     public static void main(String[] args) {
181         junit.textui.TestRunner.run(suite());
182     }
183 
184     public static junit.framework.Test suite() {
185         return new junit.framework.TestSuite(AnnotationCTest.class);
186     }
187 }