1 /***************************************************************************************
2
3 * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
4
5 * http://aspectwerkz.codehaus.org *
6
7 * ---------------------------------------------------------------------------------- *
8
9 * The software in this package is published under the terms of the LGPL license *
10
11 * a copy of which has been included with this distribution in the license.txt file. *
12
13 **************************************************************************************/
14
15 package test.annotation;
16
17
18
19 import junit.framework.TestCase;
20
21 import org.codehaus.aspectwerkz.annotation.Annotations;
22
23 import org.codehaus.aspectwerkz.annotation.UntypedAnnotationProxy;
24
25
26
27 import java.util.List;
28
29 import java.lang.reflect.Method;
30
31
32
33 /***
34
35 * Note: when using untyped annotation, then the first space character(s) in the value part will be
36
37 * resumed to only one space (untyped type -> untyped type), due to QDox doclet handling.
38
39 *
40
41 * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
42
43 * @BeforeAction some untype that starts with Before
44
45 * @BeforeAction(other untyped)
46
47 * @BeforeAction("yet another untyped")
48
49 * @packaged.BeforeAction
50
51 * @Void
52
53 * @Void()
54
55 * @Simple()
56
57 * @Simple(val="foo", s="foo")
58
59 * @DefaultString("hello")
60
61 * @packaged.DefaultString("hello")
62
63 * @Complex(i=3, ls={1l,2l,6L}, klass=java.lang.String.class)
64
65 * @Untyped
66
67 * @Untyped hello
68
69 * @Untyped "hello"
70
71 * @Untyped ("hello") - see the space here !
72
73 * @Untyped (hello) - see the space here !
74
75 */
76
77 public class AnnotationCTest extends TestCase {
78
79
80
81 public void testClassAnnotation() {
82
83 Class me = AnnotationCTest.class;
84
85
86
87 List voids = Annotations.getAnnotations("Void", me);
88
89 assertEquals(2, voids.size());
90
91
92
93 List simples = Annotations.getAnnotations("Simple", me);
94
95 assertEquals(2, simples.size());
96
97 StringBuffer all = new StringBuffer();
98
99 for (int i = 0; i < simples.size(); i++) {
100
101 all.append("[").append(((AnnotationParserTest.Simple) simples.get(i)).s()).append("]");
102
103 }
104
105 String[] lookFor = new String[]{
106
107 "[null]",
108
109 "[foo]"
110
111 };
112
113 for (int i = 0; i < lookFor.length; i++) {
114
115 String s = lookFor[i];
116
117 if (all.indexOf(s) < 0) {
118
119 fail("could not find " + lookFor[i] + " in " + all.toString());
120
121 }
122
123 }
124
125
126
127 List beforeActions = Annotations.getAnnotations("BeforeAction", me);
128
129 assertEquals(3, beforeActions.size());
130
131 all = new StringBuffer();
132
133 for (int i = 0; i < beforeActions.size(); i++) {
134
135 all.append("[").append(((UntypedAnnotationProxy)beforeActions.get(i)).getValue()).append("]");
136
137 }
138
139 lookFor = new String[]{
140
141 "[some untype that starts with Before]",
142
143 "[other untyped]",
144
145 "[\"yet another untyped\"]",
146
147
148
149 };
150
151 for (int i = 0; i < lookFor.length; i++) {
152
153 String s = lookFor[i];
154
155 if (all.indexOf(s) < 0) {
156
157 fail("could not find " + lookFor[i] + " in " + all.toString());
158
159 }
160
161 }
162
163
164
165 assertEquals(
166
167 "hello",
168
169 ((AnnotationParserTest.DefaultString) Annotations.getAnnotation("DefaultString", me)).getValue()
170
171 );
172
173
174
175 assertEquals(
176
177 String.class, ((AnnotationParserTest.Complex) Annotations.getAnnotation("Complex", me)).getKlass()
178
179 );
180
181
182
183 List untypeds = Annotations.getAnnotations("Untyped", me);
184
185 assertEquals(5, untypeds.size());
186
187 all = new StringBuffer();
188
189 for (int i = 0; i < untypeds.size(); i++) {
190
191 all.append("[").append(((AnnotationParserTest.Untyped) untypeds.get(i)).getValue()).append("]");
192
193 }
194
195 lookFor = new String[]{
196
197 "[]",
198
199 "hello",
200
201 "\"hello\"",
202
203 "(\"hello\") - see the space here !",
204
205 "(hello)"
206
207 };
208
209 for (int i = 0; i < lookFor.length; i++) {
210
211 String s = lookFor[i];
212
213 if (all.indexOf(s) < 0) {
214
215 fail("could not find " + lookFor[i] + " in " + all.toString());
216
217 }
218
219 }
220
221 }
222
223
224
225 /***
226
227 * @Void
228
229 * @Void()
230
231 * @Simple()
232
233 * @Simple(val="foo", s="foo")
234
235 * @DefaultString("hello")
236
237 * @Complex(i=3, ls={1l,2l,6L}, klass=java.lang.String.class)
238
239 * @Untyped
240
241 * @Untyped hello
242
243 * @Untyped "hello"
244
245 * @Untyped ("hello") - see the space here !
246
247 * @Untyped (hello) - see the space here !
248
249 */
250
251 public void testMethodAnnotation() throws Throwable {
252
253 Class me = test.annotation.AnnotationCTest.class;
254
255 Method m = me.getDeclaredMethod("testMethodAnnotation", new Class[0]);
256
257
258
259 List voids = Annotations.getAnnotations("Void", me);
260
261 assertEquals(2, voids.size());
262
263
264
265 List simples = Annotations.getAnnotations("Simple", me);
266
267 assertEquals(2, simples.size());
268
269 StringBuffer all = new StringBuffer();
270
271 for (int i = 0; i < simples.size(); i++) {
272
273 all.append("[").append(((AnnotationParserTest.Simple) simples.get(i)).s()).append("]");
274
275 }
276
277 String[] lookFor = new String[]{
278
279 "[null]",
280
281 "[foo]"
282
283 };
284
285 for (int i = 0; i < lookFor.length; i++) {
286
287 String s = lookFor[i];
288
289 if (all.indexOf(s) < 0) {
290
291 fail("could not find " + lookFor[i] + " in " + all.toString());
292
293 }
294
295 }
296
297
298
299 assertEquals(
300
301 "hello",
302
303 ((AnnotationParserTest.DefaultString) Annotations.getAnnotation("DefaultString", me)).getValue()
304
305 );
306
307
308
309 assertEquals(
310
311 String.class, ((AnnotationParserTest.Complex) Annotations.getAnnotation("Complex", me)).getKlass()
312
313 );
314
315
316
317 List untypeds = Annotations.getAnnotations("Untyped", me);
318
319 assertEquals(5, untypeds.size());
320
321 all = new StringBuffer();
322
323 for (int i = 0; i < untypeds.size(); i++) {
324
325 all.append("[").append(((AnnotationParserTest.Untyped) untypeds.get(i)).getValue()).append("]");
326
327 }
328
329 lookFor = new String[]{
330
331 "[]",
332
333 "hello",
334
335 "\"hello\"",
336
337 "(\"hello\") - see the space here !",
338
339 "(hello)"
340
341 };
342
343 for (int i = 0; i < lookFor.length; i++) {
344
345 String s = lookFor[i];
346
347 if (all.indexOf(s) < 0) {
348
349 fail("could not find " + lookFor[i] + " in " + all.toString());
350
351 }
352
353 }
354
355 }
356
357
358
359 public static void main(String[] args) {
360
361 junit.textui.TestRunner.run(suite());
362
363 }
364
365
366
367 public static junit.framework.Test suite() {
368
369 return new junit.framework.TestSuite(AnnotationCTest.class);
370
371 }
372
373 }
374