View Javadoc

1   /*
2    * $Id: AnnotationsScannerTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.util.scan.annotations;
11  
12  import java.io.IOException;
13  import java.util.List;
14  
15  import junit.framework.TestCase;
16  import org.objectweb.asm.ClassReader;
17  
18  public class AnnotationsScannerTestCase extends TestCase
19  {
20      protected AnnotationsScanner scanner;
21  
22      protected void setUp() throws IOException
23      {
24          ClassReader r = new ClosableClassReader(SampleClassWithAnnotations.class.getName());
25          scanner = new AnnotationsScanner();
26  
27          r.accept(scanner, 0);
28      }
29  
30      public void testParamAnnotations() throws Exception
31      {
32          final List<AnnotationInfo> paramAnnotations = scanner.getParamAnnotations();
33  
34          System.out.println("Parameter annotations: " + paramAnnotations);
35  
36          assertNotNull(paramAnnotations);
37          assertEquals(2, paramAnnotations.size());
38  
39          // @Marker("ParamLevel")
40          AnnotationInfo ann = paramAnnotations.get(0);
41          assertEquals(Marker.class.getName(), ann.getClassName());
42          List<AnnotationInfo.NameValue> annValues = ann.getParams();
43          assertNotNull(annValues);
44          assertEquals(1, annValues.size());
45          assertEquals(new AnnotationInfo.NameValue("value", "ParamLevel"), annValues.get(0));
46  
47          // @MultiMarker(value = "ParamLevel", param1 = "12", param2 = "abc")
48          ann = paramAnnotations.get(1);
49          assertEquals(MultiMarker.class.getName(), ann.getClassName());
50          annValues = ann.getParams();
51          assertNotNull(annValues);
52          assertEquals(3, annValues.size());
53          assertEquals(new AnnotationInfo.NameValue("value", "ParamLevel"), annValues.get(0));
54          assertEquals(new AnnotationInfo.NameValue("param1", "12"), annValues.get(1));
55          assertEquals(new AnnotationInfo.NameValue("param2", "abc"), annValues.get(2));
56      }
57  
58      public void testFieldAnnotations() throws Exception
59      {
60          final List<AnnotationInfo> fieldAnnotations = scanner.getFieldAnnotations();
61  
62          System.out.println("Field annotations: " + fieldAnnotations);
63  
64          assertNotNull(fieldAnnotations);
65          assertEquals(1, fieldAnnotations.size());
66  
67          // @Marker("FieldLevel")
68          AnnotationInfo ann = fieldAnnotations.get(0);
69          assertEquals(Marker.class.getName(), ann.getClassName());
70          final List<AnnotationInfo.NameValue> annValues = ann.getParams();
71          assertNotNull(annValues);
72          assertEquals(1, annValues.size());
73          assertEquals(new AnnotationInfo.NameValue("value", "FieldLevel"), annValues.get(0));
74      }
75  
76      public void testClassAnnotations() throws Exception
77      {
78          final List<AnnotationInfo> classAnnotations = scanner.getClassAnnotations();
79  
80          System.out.println("Class annotations: " + classAnnotations);
81  
82          assertNotNull(classAnnotations);
83          assertEquals(1, classAnnotations.size());
84  
85          // @Marker("ClassLevel")
86          AnnotationInfo ann = classAnnotations.get(0);
87          assertEquals(Marker.class.getName(), ann.getClassName());
88          final List<AnnotationInfo.NameValue> annValues = ann.getParams();
89          assertNotNull(annValues);
90          assertEquals(1, annValues.size());
91          assertEquals(new AnnotationInfo.NameValue("value", "ClassLevel"), annValues.get(0));
92      }
93  
94      public void testMethodAnnotations() throws Exception
95      {
96          final List<AnnotationInfo> methodAnnotations = scanner.getMethodAnnotations();
97  
98          System.out.println("Method annotations: " + methodAnnotations);
99  
100         assertNotNull(methodAnnotations);
101         assertEquals(2, methodAnnotations.size());
102 
103         // @Marker("MethodLevel / Main")
104         AnnotationInfo ann = methodAnnotations.get(0);
105         assertEquals(Marker.class.getName(), ann.getClassName());
106         List<AnnotationInfo.NameValue> annValues = ann.getParams();
107         assertNotNull(annValues);
108         assertEquals(1, annValues.size());
109         assertEquals(new AnnotationInfo.NameValue("value", "MethodLevel / Main"), annValues.get(0));
110 
111         // @Marker("MethodLevel / toString")
112         ann = methodAnnotations.get(1);
113         assertEquals(Marker.class.getName(), ann.getClassName());
114         annValues = ann.getParams();
115         assertNotNull(annValues);
116         assertEquals(1, annValues.size());
117         assertEquals(new AnnotationInfo.NameValue("value", "MethodLevel / toString"), annValues.get(0));
118 
119 
120     }
121 
122 }