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.UntypedAnnotation;
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 ("hello2")
36 * @Untyped "(hello) - see the space here !"
37 * @Untyped("preserved hello")
38 * @ComplexNested(nesteds={@Simple(val="foo"), @Simple(val="bar")})
39 */
40 public class AnnotationCTest extends TestCase {
41
42 public void testClassAnnotation() {
43 Class me = AnnotationCTest.class;
44
45 List voids = Annotations.getAnnotations("Void", me);
46 assertEquals(2, voids.size());
47
48 List simples = Annotations.getAnnotations("Simple", me);
49 assertEquals(2, simples.size());
50
51 StringBuffer all = new StringBuffer();
52 for (int i = 0; i < simples.size(); i++) {
53 all.append("[").append(((AnnotationParserTest.Simple) simples.get(i)).s()).append("]");
54 }
55 String[] lookFor = new String[]{
56 "[null]",
57 "[foo]"
58 };
59 for (int i = 0; i < lookFor.length; i++) {
60 String s = lookFor[i];
61 if (all.toString().indexOf(s) < 0) {
62 fail("could not find " + lookFor[i] + " in " + all.toString());
63 }
64 }
65
66 List beforeActions = Annotations.getAnnotations("BeforeAction", me);
67 assertEquals(3, beforeActions.size());
68 all = new StringBuffer();
69 for (int i = 0; i < beforeActions.size(); i++) {
70 all.append("[").append(((UntypedAnnotation)beforeActions.get(i)).value()).append("]");
71 }
72 lookFor = new String[]{
73 "[some untype that starts with Before]",
74 "[other untyped]",
75 "[yet another untyped]",
76
77 };
78 for (int i = 0; i < lookFor.length; i++) {
79 String s = lookFor[i];
80 if (all.toString().indexOf(s) < 0) {
81 fail("could not find " + lookFor[i] + " in " + all.toString());
82 }
83 }
84
85 assertEquals(
86 "hello",
87 ((AnnotationParserTest.DefaultString) Annotations.getAnnotation("DefaultString", me)).value()
88 );
89
90 assertEquals(
91 String.class, ((AnnotationParserTest.Complex) Annotations.getAnnotation("Complex", me)).klass()
92 );
93
94 List untypeds = Annotations.getAnnotations("Untyped", me);
95 assertEquals(5, untypeds.size());
96 all = new StringBuffer();
97 for (int i = 0; i < untypeds.size(); i++) {
98 all.append("[").append(((AnnotationParserTest.Untyped) untypeds.get(i)).value()).append("]");
99 }
100 lookFor = new String[]{
101 "[]",
102 "[hello]",
103 "[(hello) - see the space here !]",
104 "[hello2]",
105 "[preserved hello]"
106 };
107 for (int i = 0; i < lookFor.length; i++) {
108 String s = lookFor[i];
109 if (all.toString().indexOf(s) < 0) {
110 fail("could not find " + lookFor[i] + " in " + all.toString());
111 }
112 }
113 }
114
115 /***
116 * @Void
117 * @Void()
118 * @Simple()
119 * @Simple(val="foo", s="foo")
120 * @DefaultString("hello")
121 * @Complex(i=3, ls={1l,2l,6L}, klass=java.lang.String.class)
122 * @Untyped
123 * @Untyped "hello"
124 * @Untyped "hello"
125 * @Untyped "(hello) - see the space here !"
126 */
127 public void testMethodAnnotation() throws Throwable {
128 Class me = test.annotation.AnnotationCTest.class;
129 Method m = me.getDeclaredMethod("testMethodAnnotation", new Class[0]);
130
131
132
133
134
135
136
137
138
139
140
141 List voids = Annotations.getAnnotations("Void", me);
142 assertEquals(2, voids.size());
143
144 List simples = Annotations.getAnnotations("Simple", me);
145 assertEquals(2, simples.size());
146 StringBuffer all = new StringBuffer();
147 for (int i = 0; i < simples.size(); i++) {
148 all.append("[").append(((AnnotationParserTest.Simple) simples.get(i)).s()).append("]");
149 }
150 String[] lookFor = new String[]{
151 "[null]",
152 "[foo]"
153 };
154 for (int i = 0; i < lookFor.length; i++) {
155 String s = lookFor[i];
156 if (all.toString().indexOf(s) < 0) {
157 fail("could not find " + lookFor[i] + " in " + all.toString());
158 }
159 }
160
161 assertEquals(
162 "hello",
163 ((AnnotationParserTest.DefaultString) Annotations.getAnnotation("DefaultString", me)).value()
164 );
165
166 assertEquals(
167 String.class, ((AnnotationParserTest.Complex) Annotations.getAnnotation("Complex", me)).klass()
168 );
169
170 List untypeds = Annotations.getAnnotations("Untyped", m);
171 assertEquals(4, untypeds.size());
172 all = new StringBuffer();
173 for (int i = 0; i < untypeds.size(); i++) {
174 all.append("[").append(((AnnotationParserTest.Untyped) untypeds.get(i)).value()).append("]");
175 }
176 lookFor = new String[]{
177 "[]",
178 "[hello]",
179 "[(hello) - see the space here !]"
180 };
181 for (int i = 0; i < lookFor.length; i++) {
182 String s = lookFor[i];
183 if (all.toString().indexOf(s) < 0) {
184 fail("could not find " + lookFor[i] + " in " + all.toString());
185 }
186 }
187 }
188
189 public void testNestedAnnotation() throws Throwable {
190 Class me = AnnotationCTest.class;
191 AnnotationParserTest.ComplexNested ann = (AnnotationParserTest.ComplexNested) Annotations.getAnnotation("ComplexNested", me);
192 AnnotationParserTest.Simple ann1 = ann.nesteds()[0];
193 AnnotationParserTest.Simple ann2 = ann.nesteds()[1];
194 String ann12 = ann1.val()+"."+ann2.val();
195 if (ann12.equals("foo.bar") || ann12.equals("bar.foo")) {
196 ;
197 } else {
198 fail("Annotation is not correct " + ann.toString());
199 }
200 }
201
202 public static void main(String[] args) {
203 junit.textui.TestRunner.run(suite());
204 }
205
206 public static junit.framework.Test suite() {
207 return new junit.framework.TestSuite(AnnotationCTest.class);
208 }
209 }