unit test for reflection utils
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Thu, 29 Apr 2010 22:13:48 +0000 (22:13 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Thu, 29 Apr 2010 22:13:48 +0000 (22:13 +0000)
support/general/src/test/java/org/wamblee/reflection/ReflectionUtilsTest.java [new file with mode: 0644]

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 (file)
index 0000000..1cc6e4b
--- /dev/null
@@ -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<Method> res = ReflectionUtils.getAllMethods(X2.class, Object.class);
+        assertEquals(2, res.size());
+    }
+
+    @Test
+    public void testInheritanceAdditionalMethod() {
+        List<Method> res = ReflectionUtils.getAllMethods(X3.class, Object.class);
+        assertEquals(3, res.size());
+    }
+
+    @Test
+    public void testInheritanceOverriddenMethod() {
+        List<Method> 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<Field> fields = ReflectionUtils.getAllFields(X2.class);
+        assertEquals(1, fields.size());
+        assertEquals("bla", fields.get(0).getName());
+    }
+    
+    public void testFieldsWithInheritance() { 
+        List<Field> fields = ReflectionUtils.getAllFields(X5.class);
+        assertEquals(3, fields.size());
+    }
+}