1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package jp.liq.container.util;
17
18 import static java.lang.reflect.Modifier.*;
19 import static org.junit.Assert.*;
20 import static jp.liq.container.ContainerVocabulary.*;
21
22 import java.util.HashSet;
23 import java.util.Set;
24
25 import jp.liq.container.reflect.ConstructorWrapper;
26 import jp.liq.container.reflect.FieldWrapper;
27 import jp.liq.container.reflect.ReflectException;
28 import jp.liq.container.reflect.MethodWrapper;
29
30 import org.junit.Test;
31
32
33
34
35
36 public class IntrospectorTest {
37
38 @Test
39 public void getConstructor() throws Exception {
40 ConstructorWrapper<?> cw = ctor.of(Ctor1.class);
41 Object obj = cw.createObject(new Object[]{});
42 assertTrue(obj instanceof Ctor1);
43 }
44
45 public static class Ctor1 {
46
47 }
48
49 @Test
50 public void getConstructorWithArgs() throws Exception {
51 ConstructorWrapper<Ctor2> cw = ctor.of(Ctor2.class, String.class);
52 Ctor2 obj = cw.createObject(new Object[]{"hoge"});
53 assertEquals("hoge", obj.arg1);
54 }
55
56
57 public static class Ctor2 {
58 String arg1;
59 public Ctor2(String arg1) {
60 this.arg1 = arg1;
61 }
62 }
63
64 @Test
65 public void ctorInvocationErrorException() throws Exception {
66 ConstructorWrapper<?> cw = ctor.of(Ctor3.class);
67 try {
68 cw.createObject(new Object[]{});
69 fail();
70 } catch (RuntimeException e) {
71 }
72
73 }
74
75 public static class Ctor3 {
76 public Ctor3() {
77 throw new RuntimeException("ctor3");
78 }
79 }
80
81 public class Method1 {
82 public void m1(){}
83 public void m2(){}
84 public void m3(){}
85 }
86
87 @Test
88 public void findAllMethods() {
89 Set<String> s = new HashSet<String>();
90 s.add("m1");
91 s.add("m2");
92 s.add("m3");
93 for(MethodWrapper m : methods.of(Method1.class)){
94 s.remove(m.name());
95 }
96 assertEquals(0, s.size());
97 }
98
99 public static void method1() {
100 }
101
102 public static void method2() {
103 }
104
105 @Ann1
106 public void method3() {
107 }
108
109 @Ann1
110 public void method4() {
111 }
112
113 public void p1Foo() {
114 }
115
116 public void p1Bar() {
117 }
118
119 @Ann2
120 public void p2Foo() {
121 }
122
123 @Ann2
124 public void p2Bar() {
125
126 }
127
128 public void p2FooBar() {
129
130 }
131
132 @Test
133 public void findMethodsByModifier() throws Exception{
134 int cnt = 0;
135 Set<String> s = new HashSet<String>();
136 s.add("method1");
137 s.add("method2");
138 for(MethodWrapper m : methods.of(this.getClass()).withModifier(STATIC)){
139 cnt++;
140 assertTrue(s.contains(m.name()));
141 }
142
143 assertEquals(s.size(), cnt);
144 }
145
146 @Test
147 public void findMethodsByAnnotation() throws Exception{
148 int cnt = 0;
149 Set<String> s = new HashSet<String>();
150 s.add("method3");
151 s.add("method4");
152 for(MethodWrapper m : methods.of(this.getClass()).withAnnotation(Ann1.class)){
153 cnt++;
154 assertTrue(s.contains(m.name()));
155 }
156
157 assertEquals(s.size(), cnt);
158 }
159
160 @Test
161 public void findMethodsByPrefix() throws Exception{
162 int cnt = 0;
163 Set<String> s = new HashSet<String>();
164 s.add("p1Foo");
165 s.add("p1Bar");
166 for(MethodWrapper m : methods.of(this.getClass()).withPrefix("p1")){
167 cnt++;
168 assertTrue(s.contains(m.name()));
169 }
170
171 assertEquals(s.size(), cnt);
172 }
173
174 @Test
175 public void findMethodsByAnnotationAndPrefix() throws Exception{
176 int cnt = 0;
177 Set<String> s = new HashSet<String>();
178 s.add("p2Foo");
179 s.add("p2Bar");
180 for(MethodWrapper m : methods.of(this.getClass()).withPrefix("p2").withAnnotation(Ann2.class)){
181 cnt++;
182 assertTrue(s.contains(m.name()));
183 }
184
185 assertEquals(s.size(), cnt);
186 }
187
188 @Test
189 public void methodInvocationError() {
190 for(MethodWrapper m : methods.of(this.getClass()).withPrefix("throw")) {
191 try {
192 m.invoke(this, new Object[]{});
193 fail();
194 } catch (ReflectException e) {
195 }
196 }
197 }
198
199 public void throwException() {
200 throw new RuntimeException();
201 }
202
203 public class Fields {
204 public String f1;
205 @Ann1
206 public String f2;
207 @Ann1
208 public String f3;
209 }
210
211 @Test
212 public void findFieldsByModifier() {
213 int cnt = 0;
214
215 Set<String> s = new HashSet<String>();
216 s.add("f2");
217 s.add("f3");
218
219 for(FieldWrapper f : fields.of(Fields.class).withAnnotation(Ann1.class)){
220 cnt++;
221 assertTrue(s.contains(f.name()));
222 }
223
224 assertEquals(s.size(), cnt);
225 }
226
227
228 }