From 500816242439e85797929c15c6d0130ec3e295e4 Mon Sep 17 00:00:00 2001 From: erik Date: Thu, 29 Apr 2010 22:13:48 +0000 Subject: [PATCH] unit test for reflection utils --- .../reflection/ReflectionUtilsTest.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 support/general/src/test/java/org/wamblee/reflection/ReflectionUtilsTest.java 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()); + } +} -- 2.31.1