Type information is now available.
[utils] / support / general / src / test / java / org / wamblee / reflection / AnnotationUtilsTest.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.reflection;
17
18 import java.lang.annotation.ElementType;
19 import java.lang.annotation.Retention;
20 import java.lang.annotation.RetentionPolicy;
21 import java.lang.annotation.Target;
22 import java.util.List;
23
24 import org.junit.Test;
25
26 import static junit.framework.TestCase.*; 
27
28 public class AnnotationUtilsTest {
29     
30     @Retention(RetentionPolicy.RUNTIME)
31     @Target({ElementType.FIELD, ElementType.METHOD})
32     public static @interface MyAnnotation { 
33         
34     }
35     
36     public static class X1 { 
37         @MyAnnotation
38         public int x; 
39     }
40     
41     public static class X2 { 
42         @MyAnnotation
43         private int x;
44         
45     }
46
47     public static class X3 { 
48         private int x;
49         
50         public void setX(int aX) {
51             x = aX;
52         }
53         
54         @MyAnnotation
55         public int getX() {
56             return x;
57         }
58     }
59     
60     public static class X4 { 
61         private int x;
62         
63         private void setX(int aX) {
64             x = aX;
65         }
66         
67         @MyAnnotation
68         private int getX() {
69             return x;
70         }
71     }
72
73     public static class X5 { 
74         private int x;
75         
76         @MyAnnotation
77         private int y; 
78         
79         private void setX(int aX) {
80             x = aX;
81         }
82         
83         @MyAnnotation
84         private int getX() {
85             return x;
86         }
87     }
88
89
90     
91     @Test
92     public void testPublicField() {
93         List<Accessor> accessors = AnnotationUtils.analyse(X1.class, MyAnnotation.class);
94         assertEquals(1, accessors.size());
95         assertTrue(accessors.get(0) instanceof FieldAccessor);
96         assertEquals(int.class, accessors.get(0).getType());
97
98         X1 obj = new X1();
99         assertEquals(0, obj.x);
100         accessors.get(0).set(obj, 100);
101         assertEquals(100,obj.x);
102     }
103     
104     @Test
105     public void testPrivateField() { 
106         List<Accessor> accessors = AnnotationUtils.analyse(X2.class, MyAnnotation.class);
107         assertEquals(1, accessors.size());
108         assertTrue(accessors.get(0) instanceof FieldAccessor);
109         X2 obj = new X2();
110         assertEquals(0, obj.x);
111         accessors.get(0).set(obj, 100);
112         assertEquals(100, obj.x);
113     }
114     
115     @Test
116     public void testPublicProperty() { 
117         List<Accessor> accessors = AnnotationUtils.analyse(X3.class, MyAnnotation.class);
118         assertEquals(1, accessors.size());
119         assertTrue(accessors.get(0) instanceof PropertyAccessor);
120         assertEquals(int.class, accessors.get(0).getType());
121         X3 obj = new X3();
122         assertEquals(0, obj.x);
123         accessors.get(0).set(obj, 100);
124         assertEquals(100, obj.x);
125     }
126     
127     @Test
128     public void testPrivateProperty() { 
129         List<Accessor> accessors = AnnotationUtils.analyse(X4.class, MyAnnotation.class);
130         assertEquals(1, accessors.size());
131         assertTrue(accessors.get(0) instanceof PropertyAccessor);
132         X4 obj = new X4();
133         assertEquals(0, obj.x);
134         accessors.get(0).set(obj, 100);
135         assertEquals(100, obj.x);
136     }
137     
138     @Test
139     public void testMultipleMatches() { 
140         List<Accessor> accessors = AnnotationUtils.analyse(X5.class, MyAnnotation.class);
141         assertEquals(2, accessors.size());
142         X5 obj = new X5();
143         assertEquals(0, obj.x);
144         assertEquals(0, obj.y);
145         for (Accessor accessor: accessors) { 
146             accessor.set(obj, 100);
147         }
148         assertEquals(100, obj.x);
149         assertEquals(100, obj.y);
150     }
151 }