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