1 package org.wamblee.reflection;
3 import static junit.framework.Assert.*;
5 import java.lang.reflect.Field;
6 import java.lang.reflect.Method;
11 public class ReflectionUtilsTest {
13 public static interface X {
17 public static class X2 {
29 public static class X3 extends X2 {
35 public static class X4 extends X2 {
41 public static class X5 extends X2 {
46 @Test(expected = IllegalArgumentException.class)
47 public void testGetAllMethodsInterface() {
48 ReflectionUtils.getAllMethods(X.class);
52 public void testBasicClass() {
53 List<Method> res = ReflectionUtils.getAllMethods(X2.class, Object.class);
54 assertEquals(2, res.size());
58 public void testInheritanceAdditionalMethod() {
59 List<Method> res = ReflectionUtils.getAllMethods(X3.class, Object.class);
60 assertEquals(3, res.size());
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());
74 @Test(expected = IllegalArgumentException.class)
75 public void testGetAllFieldsInterface() {
76 ReflectionUtils.getAllFields(X.class);
80 public void testFieldsFromBasicClass() {
81 List<Field> fields = ReflectionUtils.getAllFields(X2.class);
82 assertEquals(1, fields.size());
83 assertEquals("bla", fields.get(0).getName());
86 public void testFieldsWithInheritance() {
87 List<Field> fields = ReflectionUtils.getAllFields(X5.class);
88 assertEquals(3, fields.size());