1cc6e4b0cdb30e872af8ac047d4d5e886ce57259
[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.getAllMethods(X2.class, Object.class);
54         assertEquals(2, res.size());
55     }
56
57     @Test
58     public void testInheritanceAdditionalMethod() {
59         List<Method> res = ReflectionUtils.getAllMethods(X3.class, Object.class);
60         assertEquals(3, res.size());
61     }
62
63     @Test
64     public void testInheritanceOverriddenMethod() {
65         List<Method> res = ReflectionUtils.getAllMethods(X4.class, Object.class);
66         assertEquals(2, res.size());
67         for (Method method : res) {
68             if (method.getName().equals("x")) {
69                 assertEquals(X4.class, method.getDeclaringClass());
70             }
71         }
72     }
73     
74     @Test(expected = IllegalArgumentException.class)
75     public void testGetAllFieldsInterface() {
76         ReflectionUtils.getAllFields(X.class);
77     }
78
79     @Test
80     public void testFieldsFromBasicClass() { 
81         List<Field> fields = ReflectionUtils.getAllFields(X2.class);
82         assertEquals(1, fields.size());
83         assertEquals("bla", fields.get(0).getName());
84     }
85     
86     public void testFieldsWithInheritance() { 
87         List<Field> fields = ReflectionUtils.getAllFields(X5.class);
88         assertEquals(3, fields.size());
89     }
90 }