(no commit message)
[utils] / support / general / src / test / java / org / wamblee / reflection / ReflectionUtilsTest.java
1 package org.wamblee.reflection;
2
3 import static junit.framework.Assert.*;
4
5 import java.lang.reflect.Field;
6 import java.lang.reflect.Method;
7 import java.util.List;
8
9 import org.junit.Test;
10
11 public class ReflectionUtilsTest {
12
13     public static interface X {
14         void x();
15     }
16
17     public static class X2 {
18         private int bla;
19
20         public void x() {
21
22         }
23
24         private void y() {
25
26         }
27     }
28
29     public static class X3 extends X2 {
30         public void z() {
31
32         }
33     }
34
35     public static class X4 extends X2 {
36         public void x() {
37
38         }
39     }
40
41     public static class X5 extends X2 {
42         private String field;
43         private int g;
44     }
45
46     @Test(expected = IllegalArgumentException.class)
47     public void testGetAllMethodsInterface() {
48         ReflectionUtils.getAllMethods(X.class);
49     }
50
51     @Test
52     public void testBasicClass() {
53         List<Method> res = ReflectionUtils
54             .getAllMethods(X2.class, Object.class);
55         assertEquals(2, res.size());
56     }
57
58     @Test
59     public void testInheritanceAdditionalMethod() {
60         List<Method> res = ReflectionUtils
61             .getAllMethods(X3.class, Object.class);
62         assertEquals(3, res.size());
63     }
64
65     @Test
66     public void testInheritanceOverriddenMethod() {
67         List<Method> res = ReflectionUtils
68             .getAllMethods(X4.class, Object.class);
69         assertEquals(2, res.size());
70         for (Method method : res) {
71             if (method.getName().equals("x")) {
72                 assertEquals(X4.class, method.getDeclaringClass());
73             }
74         }
75     }
76
77     @Test(expected = IllegalArgumentException.class)
78     public void testGetAllFieldsInterface() {
79         ReflectionUtils.getAllFields(X.class);
80     }
81
82     @Test
83     public void testFieldsFromBasicClass() {
84         List<Field> fields = ReflectionUtils.getAllFields(X2.class);
85         assertEquals(1, fields.size());
86         assertEquals("bla", fields.get(0).getName());
87     }
88
89     public void testFieldsWithInheritance() {
90         List<Field> fields = ReflectionUtils.getAllFields(X5.class);
91         assertEquals(3, fields.size());
92     }
93 }