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 org.codehaus.aspectwerkz.annotation.expression.ast.AnnotationParser;
11  import org.codehaus.aspectwerkz.annotation.expression.AnnotationVisitor;
12  import org.codehaus.aspectwerkz.annotation.TypedAnnotationProxy;
13  import org.codehaus.aspectwerkz.annotation.UntypedAnnotationProxy;
14  import junit.framework.TestCase;
15  
16  /***
17   * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
18   */
19  public class AnnotationParserTest extends TestCase {
20  
21      protected static final AnnotationParser s_parser = Helper.getAnnotationParser();
22  
23      public void testSimple() {
24          try {
25              Simple annotation = new Simple();
26              AnnotationVisitor.parse(annotation, s_parser.parse("@Single(val=\"foo\")"));
27              assertEquals("foo", annotation.getVal());
28              AnnotationVisitor.parse(annotation, s_parser.parse("@Single(val=\"foo bar\")"));
29              AnnotationVisitor.parse(annotation, s_parser.parse("@Single (val=\"foo bar\")"));
30              AnnotationVisitor.parse(annotation, s_parser.parse("@Single(val=\"foo bar\"       )"));
31  
32              AnnotationVisitor.parse(annotation, s_parser.parse("@Single(s=\"foo\")"));
33              assertEquals("foo", annotation.s());
34              AnnotationVisitor.parse(annotation, s_parser.parse("@Single(s=\"foo bar\")"));
35              AnnotationVisitor.parse(annotation, s_parser.parse("@Single (s=\"foo bar\")"));
36              AnnotationVisitor.parse(annotation, s_parser.parse("@Single(s=\"foo bar\"       )"));
37  
38              VoidTyped annotation2 = new VoidTyped();
39              AnnotationVisitor.parse(annotation2, s_parser.parse("@Void()"));
40              AnnotationVisitor.parse(annotation2, s_parser.parse("@Void"));
41          } catch (Throwable t) {
42              fail(t.toString());
43          }
44      }
45  
46      public void testDefault() {
47          try {
48              DefaultString annotation = new DefaultString();
49              AnnotationVisitor.parse(annotation, s_parser.parse("@DefaultString(\"foo\")"));
50              assertEquals("foo", annotation.getValue());
51  
52              DefaultInt annotationInt = new DefaultInt();
53              AnnotationVisitor.parse(annotationInt, s_parser.parse("@DefaultInt(3)"));
54              assertEquals(3, annotationInt.getValue());
55  
56              DefaultString annotation2 = new DefaultString();
57              AnnotationVisitor.parse(annotation2, s_parser.parse("@packaged.DefaultString(\"foo\")"));
58              assertEquals("foo", annotation2.getValue());
59          } catch (Throwable t) {
60              fail(t.toString());
61          }
62      }
63  
64      // note that for correct long type handling, it is mandatory to have the l or L suffix
65      public void testComplex() {
66          try {
67              Complex annotation = new Complex();
68              AnnotationVisitor.parse(annotation, s_parser.parse("@Complex(i=3  ls={1l,2l,6L}  klass=java.lang.String.class)"));
69              assertEquals(String.class, annotation.getKlass());
70              AnnotationVisitor.parse(annotation, s_parser.parse("@Complex(i=3, ls={1l,2l,6L},  klass=java.lang.String.class)"));
71              assertEquals(String.class, annotation.getKlass());
72              AnnotationVisitor.parse(annotation, s_parser.parse("@Complex(i=3 ls={1l,2l,6L} klass=java.lang.String.class)"));
73              assertEquals(String.class, annotation.getKlass());
74          } catch (Throwable t) {
75              fail(t.toString());
76          }
77      }
78  
79      public void testStringArray() {
80          try {
81              StringArray annotation = new StringArray();
82              AnnotationVisitor.parse(annotation, s_parser.parse("@StringArray(i=3  ss={\"hello\", \"foo\"})"));
83              assertEquals("foo", annotation.ss()[1]);
84              AnnotationVisitor.parse(annotation, s_parser.parse("@StringArray(i=3, ss={\"hello\", \"foo\"})"));
85              assertEquals("foo", annotation.ss()[1]);
86          } catch (Throwable t) {
87              fail(t.toString());
88          }
89      }
90  
91      public static class Helper extends TypedAnnotationProxy {
92          public static AnnotationParser getAnnotationParser() {
93              return PARSER;
94          }
95      }
96  
97      public static class VoidTyped extends TypedAnnotationProxy {
98      }
99  
100     public static class Simple extends TypedAnnotationProxy {
101         String s;
102         public void setVal(String s) {this.s = s;}
103         public String getVal() {return this.s;}
104         public void sets(String s) {this.s = s;}
105         public String s() {return this.s;}
106     }
107 
108     public static class DefaultString extends TypedAnnotationProxy {
109         String s;
110         public void setValue(String s) {this.s = s;}
111         public String getValue() {return this.s;}
112     }
113 
114     public static class DefaultInt extends TypedAnnotationProxy {
115         int i;
116         public void setValue(int i) {this.i = i;}
117         public int getValue() {return this.i;}
118     }
119 
120 
121     public static class Complex extends TypedAnnotationProxy {
122         int i;
123         long[] ls;
124         Class klass;
125         public void setI(int i) {this.i = i;}
126         public int getI() {return this.i;}
127         public void setLs(long[] ls) {this.ls = ls;}
128         public long[] getLs() {return this.ls;}
129         public void setKlass(Class k) {this.klass = k;}
130         public Class getKlass() {return this.klass;}
131     }
132 
133     public static class StringArray extends TypedAnnotationProxy {
134         int i;
135         String[] ss;
136         public int i() {return i;}
137         public void setI(int i) {this.i = i;}
138         public String[] ss() {return ss;}
139         public void setSs(String[] ss) {this.ss = ss;}
140     }
141 
142     public static class Untyped extends UntypedAnnotationProxy {
143     }
144 }