From: Erik Brakkee Date: Thu, 29 Apr 2010 22:13:48 +0000 (+0000) Subject: unit test for reflection utils X-Git-Tag: wamblee-utils-0.7~508 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=38e780cf8832444a6a25416dfc33fda9688f9bfb;p=utils unit test for reflection utils --- diff --git a/support/general/src/test/java/org/wamblee/reflection/ReflectionUtilsTest.java b/support/general/src/test/java/org/wamblee/reflection/ReflectionUtilsTest.java new file mode 100644 index 00000000..1cc6e4b0 --- /dev/null +++ b/support/general/src/test/java/org/wamblee/reflection/ReflectionUtilsTest.java @@ -0,0 +1,90 @@ +package org.wamblee.reflection; + +import static junit.framework.Assert.*; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.List; + +import org.junit.Test; + +public class ReflectionUtilsTest { + + public static interface X { + void x(); + } + + public static class X2 { + private int bla; + + public void x() { + + } + + private void y() { + + } + } + + public static class X3 extends X2 { + public void z() { + + } + } + + public static class X4 extends X2 { + public void x() { + + } + } + + public static class X5 extends X2 { + private String field; + private int g; + } + + @Test(expected = IllegalArgumentException.class) + public void testGetAllMethodsInterface() { + ReflectionUtils.getAllMethods(X.class); + } + + @Test + public void testBasicClass() { + List res = ReflectionUtils.getAllMethods(X2.class, Object.class); + assertEquals(2, res.size()); + } + + @Test + public void testInheritanceAdditionalMethod() { + List res = ReflectionUtils.getAllMethods(X3.class, Object.class); + assertEquals(3, res.size()); + } + + @Test + public void testInheritanceOverriddenMethod() { + List res = ReflectionUtils.getAllMethods(X4.class, Object.class); + assertEquals(2, res.size()); + for (Method method : res) { + if (method.getName().equals("x")) { + assertEquals(X4.class, method.getDeclaringClass()); + } + } + } + + @Test(expected = IllegalArgumentException.class) + public void testGetAllFieldsInterface() { + ReflectionUtils.getAllFields(X.class); + } + + @Test + public void testFieldsFromBasicClass() { + List fields = ReflectionUtils.getAllFields(X2.class); + assertEquals(1, fields.size()); + assertEquals("bla", fields.get(0).getName()); + } + + public void testFieldsWithInheritance() { + List fields = ReflectionUtils.getAllFields(X5.class); + assertEquals(3, fields.size()); + } +}