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 org.codehaus.aspectwerkz.annotation.expression.ast.AnnotationParser;
010: import org.codehaus.aspectwerkz.annotation.expression.AnnotationVisitor;
011: import org.codehaus.aspectwerkz.annotation.Java5AnnotationInvocationHandler;
012: import org.codehaus.aspectwerkz.annotation.AnnotationElement;
013: import junit.framework.TestCase;
014:
015: import java.util.Map;
016: import java.util.HashMap;
017:
018: /**
019: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
020: * @author <a href="mailto:the_mindstorm@evolva.ro">Alex Popescu</a>
021: */
022: public class AnnotationParserTest extends TestCase {
023:
024: protected static final AnnotationParser s_parser = Helper
025: .getAnnotationParser();
026:
027: private Object getElementValue(Object o) {
028: AnnotationElement element = (AnnotationElement) o;
029: return element
030: .resolveValueHolderFrom(AnnotationParserTest.class
031: .getClassLoader());
032:
033: }
034:
035: private void check(Map elements, String key, Object expected) {
036: Object o = elements.get(key);
037: if (o == null) {
038: fail("No such element - " + key);
039: } else {
040: assertEquals(expected, getElementValue(o));
041: }
042: }
043:
044: public void testSimple() {
045: try {
046: Map elements = new HashMap();
047: AnnotationVisitor.parse(elements, "@Simple(val=\"foo\")",
048: Simple.class);
049: check(elements, "val", "foo");
050: AnnotationVisitor.parse(elements,
051: "@Simple(val=\"foo bar\")", Simple.class);
052: AnnotationVisitor.parse(elements,
053: "@Simple (val=\"foo bar\")", Simple.class);
054: AnnotationVisitor.parse(elements,
055: "@Simple(val=\"foo bar\" )", Simple.class);
056:
057: elements = new HashMap();
058: AnnotationVisitor.parse(elements, "@Simple(s=\"foo\")",
059: Simple.class);
060: check(elements, "s", "foo");
061:
062: AnnotationVisitor.parse(elements,
063: "@Simple(val=\"hello \\\" alex\")", Simple.class);
064: check(elements, "val", "hello \" alex");
065:
066: AnnotationVisitor.parse(elements, "@Simple(s=\"foo bar\")",
067: Simple.class);
068: AnnotationVisitor.parse(elements,
069: "@Simple (s=\"foo bar\")", Simple.class);
070: AnnotationVisitor.parse(elements,
071: "@Simple(s=\"foo bar\" )", Simple.class);
072:
073: elements = new HashMap();
074: AnnotationVisitor.parse(elements,
075: "@Simple(s=\"foo\", val=\"bar\")", Simple.class);
076: check(elements, "s", "foo");
077: check(elements, "val", "bar");
078:
079: elements = new HashMap();
080: AnnotationVisitor.parse(elements, "@Void()",
081: VoidTyped.class);
082: assertEquals(0, elements.size());
083: AnnotationVisitor.parse(elements, "@Void", VoidTyped.class);
084: assertEquals(0, elements.size());
085: } catch (Throwable t) {
086: fail(t.toString());
087: }
088: }
089:
090: public void testSimpleNested() {
091: try {
092: Map elements = new HashMap();
093: AnnotationVisitor.parse(elements,
094: "@SimpleNested(nested=@Simple(val=\"foo\"))",
095: SimpleNested.class);
096: Simple nested = (Simple) getElementValue(elements
097: .get("nested"));
098: assertEquals("foo", nested.val());
099:
100: elements = new HashMap();
101: AnnotationVisitor
102: .parse(
103: elements,
104: "@SimpleNested(nested=@Simple( s=\"bar\", val=\"foo\"))",
105: SimpleNested.class);
106: nested = (Simple) getElementValue(elements.get("nested"));
107: assertEquals("bar", nested.s());
108: assertEquals("foo", nested.val());
109:
110: elements = new HashMap();
111: AnnotationVisitor
112: .parse(
113: elements,
114: "@SimpleStringArrayNested(nested=@StringArray(i=42, ss={\"one\", \"two\", \"three\"}))",
115: SimpleStringArrayNested.class);
116: StringArray sarr = (StringArray) getElementValue(elements
117: .get("nested"));
118: assertEquals(42, sarr.i());
119: assertEquals("one", sarr.ss()[0]);
120: assertEquals("two", sarr.ss()[1]);
121: assertEquals("three", sarr.ss()[2]);
122: } catch (Throwable t) {
123: fail(t.toString());
124: }
125: }
126:
127: public void testComplexNested() {
128: try {
129: Map elements = new HashMap();
130: AnnotationVisitor
131: .parse(
132: elements,
133: "@ComplexNested(nesteds={@Simple(val=\"foo\"), @Simple(val=\"bar\")})",
134: ComplexNested.class);
135: Simple[] nesteds = (Simple[]) getElementValue(elements
136: .get("nesteds"));
137: assertEquals("foo", nesteds[0].val());
138: assertEquals("bar", nesteds[1].val());
139:
140: elements = new HashMap();
141: AnnotationVisitor
142: .parse(
143: elements,
144: "@ComplexNested(nesteds={@Simple(s=\"bar\", val=\"foo\"), @Simple(val=\"bar\")})",
145: ComplexNested.class);
146: nesteds = (Simple[]) getElementValue(elements
147: .get("nesteds"));
148: assertEquals("foo", nesteds[0].val());
149: assertEquals("bar", nesteds[0].s());
150: assertEquals("bar", nesteds[1].val());
151:
152: } catch (Throwable t) {
153: fail(t.toString());
154: }
155: }
156:
157: public void testDefault() {
158: try {
159: Map elements = new HashMap();
160: AnnotationVisitor.parse(elements,
161: "@DefaultString(\"foo\")", DefaultString.class);
162: check(elements, "value", "foo");
163:
164: elements = new HashMap();
165: AnnotationVisitor.parse(elements, "@DefaultInt(3)",
166: DefaultInt.class);
167: check(elements, "value", new Integer(3));
168:
169: elements = new HashMap();
170: AnnotationVisitor
171: .parse(
172: elements,
173: "@SimpleDefaultNested(nested=@DefaultString(\"bar\"))",
174: SimpleDefaultNested.class);
175: DefaultString ds = (DefaultString) getElementValue(elements
176: .get("nested"));
177: assertEquals("bar", ds.value());
178:
179: elements = new HashMap();
180: AnnotationVisitor
181: .parse(
182: elements,
183: "@SimpleValueDefaultNested(@DefaultString(\"bar\"))",
184: SimpleValueDefaultNested.class);
185: check(elements, "value", "bar");
186: } catch (Throwable t) {
187: fail(t.toString());
188: }
189: }
190:
191: // note that for correct long type handling, it is mandatory to have the l or L suffix
192: public void testComplex() {
193: try {
194: Map elements = new HashMap();
195: AnnotationVisitor
196: .parse(
197: elements,
198: "@Complex(i=3, ls={1l,2l,6L}, klass=java.lang.String.class)",
199: Complex.class);
200: check(elements, "i", new Integer(3));
201: long[] ls = new long[] { 1L, 2L, 6L };
202: long[] lsGet = (long[]) getElementValue(elements.get("ls"));
203: for (int i = 0; i < ls.length; i++) {
204: assertEquals(ls[i], lsGet[i]);
205: }
206: // TODO change when support for LazyClass is there
207: check(elements, "klass", String.class);
208: } catch (Throwable t) {
209: fail(t.toString());
210: }
211: }
212:
213: public void testStringArray() {
214: try {
215: Map elements = new HashMap();
216: AnnotationVisitor.parse(elements,
217: "@StringArray(i=3, ss={\"hello\", \"foo\"})",
218: StringArray.class);
219: check(elements, "i", new Integer(3));
220: String[] ss = new String[] { "hello", "foo" };
221: String[] ssGet = (String[]) getElementValue(elements
222: .get("ss"));
223: for (int i = 0; i < ss.length; i++) {
224: assertEquals(ss[i], ssGet[i]);
225: }
226: } catch (Throwable t) {
227: fail(t.toString());
228: }
229: }
230:
231: public static class Helper extends AnnotationVisitor {
232: public Helper(final Map annotationValues,
233: final Class annotationClass) {
234: super (annotationValues, annotationClass);
235: }
236:
237: public static AnnotationParser getAnnotationParser() {
238: return PARSER;
239: }
240: }
241:
242: public static interface VoidTyped {
243: }
244:
245: public static interface Simple {
246:
247: public String val();
248:
249: public String s();
250: }
251:
252: public static interface SimpleNested {
253:
254: public Simple nested();
255: }
256:
257: public static interface SimpleDefaultNested {
258: public DefaultString nested();
259: }
260:
261: public static interface SimpleValueDefaultNested {
262: public DefaultString value();
263: }
264:
265: public static interface SimpleStringArrayNested {
266: public StringArray nested();
267: }
268:
269: public static interface ComplexNested {
270:
271: public Simple[] nesteds();
272: }
273:
274: public static interface DefaultString {
275:
276: public String value();
277: }
278:
279: public static interface DefaultInt {
280:
281: public int value();
282: }
283:
284: public static interface Complex {
285:
286: public int i();
287:
288: public long[] ls();
289:
290: public Class klass();
291:
292: public Class[] klass2();
293:
294: public int field();
295:
296: }
297:
298: public static interface StringArray {
299: public int i();
300:
301: public String[] ss();
302: }
303:
304: public static interface Untyped {
305: public String value();
306: }
307: }
|