001: /**************************************************************************************
002: * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test.annotation;
008:
009: import junit.framework.TestCase;
010: import org.codehaus.aspectwerkz.annotation.Annotations;
011: import org.codehaus.aspectwerkz.annotation.UntypedAnnotation;
012:
013: import java.util.List;
014: import java.lang.reflect.Method;
015:
016: /**
017: * Note: when using untyped annotation, then the first space character(s) in the value part will be
018: * resumed to only one space (untyped type -> untyped type), due to QDox doclet handling.
019: *
020: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
021: * @BeforeAction some untype that starts with Before
022: * @BeforeAction (other untyped)
023: * @BeforeAction("yet another untyped")
024: * @packaged.BeforeAction
025: * @Void
026: * @Void()
027: * @Simple()
028: * @Simple(val="foo", s="foo")
029: * @DefaultString("hello")
030: * @packaged.DefaultString("hello")
031: * @Complex(i=3, ls={1l,2l,6L}, klass=java.lang.String.class)
032: * @Untyped
033: * @Untyped "hello"
034: * @Untyped ("hello2")
035: * @Untyped "(hello) - see the space here !"
036: * @Untyped("preserved hello")
037: * @ComplexNested(nesteds={@Simple(val="foo"), @Simple(val="bar")})
038: */
039: public class AnnotationCTest extends TestCase {
040:
041: public void testClassAnnotation() {
042: Class me = AnnotationCTest.class;
043:
044: List voids = Annotations.getAnnotations("Void", me);
045: assertEquals(2, voids.size());
046:
047: List simples = Annotations.getAnnotations("Simple", me);
048: assertEquals(2, simples.size());
049:
050: StringBuffer all = new StringBuffer();
051: for (int i = 0; i < simples.size(); i++) {
052: all.append("[").append(
053: ((AnnotationParserTest.Simple) simples.get(i)).s())
054: .append("]");
055: }
056: String[] lookFor = new String[] { "[null]", "[foo]" };
057: for (int i = 0; i < lookFor.length; i++) {
058: String s = lookFor[i];
059: if (all.toString().indexOf(s) < 0) {
060: fail("could not find " + lookFor[i] + " in "
061: + all.toString());
062: }
063: }
064:
065: List beforeActions = Annotations.getAnnotations("BeforeAction",
066: me);
067: assertEquals(3, beforeActions.size());
068: all = new StringBuffer();
069: for (int i = 0; i < beforeActions.size(); i++) {
070: all.append("[").append(
071: ((UntypedAnnotation) beforeActions.get(i)).value())
072: .append("]");
073: }
074: lookFor = new String[] {
075: "[some untype that starts with Before]",
076: "[other untyped]", "[yet another untyped]",
077:
078: };
079: for (int i = 0; i < lookFor.length; i++) {
080: String s = lookFor[i];
081: if (all.toString().indexOf(s) < 0) {
082: fail("could not find " + lookFor[i] + " in "
083: + all.toString());
084: }
085: }
086:
087: assertEquals("hello",
088: ((AnnotationParserTest.DefaultString) Annotations
089: .getAnnotation("DefaultString", me)).value());
090:
091: assertEquals(String.class,
092: ((AnnotationParserTest.Complex) Annotations
093: .getAnnotation("Complex", me)).klass());
094:
095: List untypeds = Annotations.getAnnotations("Untyped", me);
096: assertEquals(5, untypeds.size());
097: all = new StringBuffer();
098: for (int i = 0; i < untypeds.size(); i++) {
099: all.append("[").append(
100: ((AnnotationParserTest.Untyped) untypeds.get(i))
101: .value()).append("]");
102: }
103: lookFor = new String[] { "[]", "[hello]",
104: "[(hello) - see the space here !]", "[hello2]",
105: "[preserved hello]" };
106: for (int i = 0; i < lookFor.length; i++) {
107: String s = lookFor[i];
108: if (all.toString().indexOf(s) < 0) {
109: fail("could not find " + lookFor[i] + " in "
110: + 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",
130: new Class[0]);
131:
132: //QDOX bug..
133: // * @Around execution(* test.customproceed.CustomProceedTest.setInt(int)) && args(i)
134: // *
135: // * @Around("execution(* test.customproceed.CustomProceedTest.setInt(int)) && args(i)")
136: // List around = Annotations.getAnnotations(Around.class, m);
137: // assertEquals(2, around.size());
138: // assertEquals(((Around)around.get(0)).value(), "execution(* test.customproceed.CustomProceedTest.setInt(int)) && args(i)");
139: // assertEquals(((Around)around.get(1)).value(), "execution(* test.customproceed.CustomProceedTest.setInt(int)) && args(i)");
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(
149: ((AnnotationParserTest.Simple) simples.get(i)).s())
150: .append("]");
151: }
152: String[] lookFor = new String[] { "[null]", "[foo]" };
153: for (int i = 0; i < lookFor.length; i++) {
154: String s = lookFor[i];
155: if (all.toString().indexOf(s) < 0) {
156: fail("could not find " + lookFor[i] + " in "
157: + all.toString());
158: }
159: }
160:
161: assertEquals("hello",
162: ((AnnotationParserTest.DefaultString) Annotations
163: .getAnnotation("DefaultString", me)).value());
164:
165: assertEquals(String.class,
166: ((AnnotationParserTest.Complex) Annotations
167: .getAnnotation("Complex", me)).klass());
168:
169: List untypeds = Annotations.getAnnotations("Untyped", m);
170: assertEquals(4, untypeds.size());
171: all = new StringBuffer();
172: for (int i = 0; i < untypeds.size(); i++) {
173: all.append("[").append(
174: ((AnnotationParserTest.Untyped) untypeds.get(i))
175: .value()).append("]");
176: }
177: lookFor = new String[] { "[]", "[hello]",
178: "[(hello) - see the space here !]" };
179: for (int i = 0; i < lookFor.length; i++) {
180: String s = lookFor[i];
181: if (all.toString().indexOf(s) < 0) {
182: fail("could not find " + lookFor[i] + " in "
183: + all.toString());
184: }
185: }
186: }
187:
188: public void testNestedAnnotation() throws Throwable {
189: Class me = AnnotationCTest.class;
190: AnnotationParserTest.ComplexNested ann = (AnnotationParserTest.ComplexNested) Annotations
191: .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: ;//ok
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: }
|