From e06e7b56a60283a3b95dd5fadb0102a708965e2f Mon Sep 17 00:00:00 2001 From: n9Mtq4 Date: Thu, 28 Jan 2016 20:52:23 -0500 Subject: [PATCH] unit tests --- build.gradle | 1 + .../n9mtq4/reflection/ReflectionHelper.java | 5 +- .../reflection/ReflectionProtector.java | 2 +- .../n9mtq4/reflection/EnhancedProxyTest.java | 61 ++ .../reflection/ReflectionHelperTest.java | 864 ++++++++++++++++++ .../reflection/ReflectionProtectorTest.java | 27 + .../reflection/toreflect/TestInterface.java | 18 + .../TestInterfaceImplementation.java | 53 ++ 8 files changed, 1028 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/n9mtq4/reflection/EnhancedProxyTest.java create mode 100644 src/test/java/com/n9mtq4/reflection/ReflectionHelperTest.java create mode 100644 src/test/java/com/n9mtq4/reflection/ReflectionProtectorTest.java create mode 100644 src/test/java/com/n9mtq4/reflection/toreflect/TestInterface.java create mode 100644 src/test/java/com/n9mtq4/reflection/toreflect/TestInterfaceImplementation.java diff --git a/build.gradle b/build.gradle index 6399bfe..89b86e7 100644 --- a/build.gradle +++ b/build.gradle @@ -9,6 +9,7 @@ repositories { } dependencies { + testCompile group: 'junit', name: 'junit', version: '4.11' } gradle.taskGraph.afterTask { Task task, TaskState state -> diff --git a/src/main/java/com/n9mtq4/reflection/ReflectionHelper.java b/src/main/java/com/n9mtq4/reflection/ReflectionHelper.java index 32d92c4..ac382c1 100644 --- a/src/main/java/com/n9mtq4/reflection/ReflectionHelper.java +++ b/src/main/java/com/n9mtq4/reflection/ReflectionHelper.java @@ -41,7 +41,7 @@ * Class to simplify reflection calls.
*/ @SuppressWarnings("unused") -public class ReflectionHelper { +public final class ReflectionHelper { /** * Gets the instance of Unsafe. @@ -2660,7 +2660,8 @@ public static boolean canBePrimitive(Class clazz) { * @see ReflectionHelper#canBePrimitive(Class) * */ public static boolean isPrimitive(Class clazz) { - return getObjectClass(clazz) != null; + return clazz.isPrimitive(); +// return getObjectClass(clazz) != null; } /** diff --git a/src/main/java/com/n9mtq4/reflection/ReflectionProtector.java b/src/main/java/com/n9mtq4/reflection/ReflectionProtector.java index 10440e6..0482b86 100644 --- a/src/main/java/com/n9mtq4/reflection/ReflectionProtector.java +++ b/src/main/java/com/n9mtq4/reflection/ReflectionProtector.java @@ -11,7 +11,7 @@ * @author Will "n9Mtq4" Bresnahan */ @SuppressWarnings("unused") -public class ReflectionProtector { +public final class ReflectionProtector { private static final int DEFAULT_INDEX = 3; diff --git a/src/test/java/com/n9mtq4/reflection/EnhancedProxyTest.java b/src/test/java/com/n9mtq4/reflection/EnhancedProxyTest.java new file mode 100644 index 0000000..365dca5 --- /dev/null +++ b/src/test/java/com/n9mtq4/reflection/EnhancedProxyTest.java @@ -0,0 +1,61 @@ +package com.n9mtq4.reflection; + +import com.n9mtq4.reflection.toreflect.TestInterface; +import com.n9mtq4.reflection.toreflect.TestInterfaceImplementation; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.Method; + +import static org.junit.Assert.*; + +/** + * Created by will on 1/28/16 at 8:24 PM. + * + * @author Will "n9Mtq4" Bresnahan + */ +public class EnhancedProxyTest { + + private int methodCalls = 0; + + private TestInterfaceImplementation testInterfaceImplementation = new TestInterfaceImplementation(); + private TestInterface ti; + + @Before + public void setUp() throws Exception { + + testInterfaceImplementation = new TestInterfaceImplementation(); + ti = EnhancedProxy.newInstance(testInterfaceImplementation, new EnhancedProxy.EnhancedInvocationHandler() { + @Override + public Object invoke(Object obj, Object proxy, Method method, Object[] args) throws Throwable { + +// does the same thing, but increments methodCalls by one + methodCalls++; + return EnhancedProxy.callChild(obj, method, args); + + } + }); + + } + + @Test + public void testEnhancedProxy() throws Exception { + + System.out.println("Testing Enhanced Proxy"); + assertTrue(methodCalls == 0); + ti.voidMethod(); + assertTrue(methodCalls == 1); + assertTrue(ti.booleanMethod()); + assertTrue(methodCalls == 2); + assertTrue(ti.charMethod() == 'a'); + assertTrue(methodCalls == 3); + assertTrue(ti.stringMethod().equals("Hello World")); + assertTrue(methodCalls == 4); + assertTrue(ti.intMethod() == 1); + assertTrue(methodCalls == 5); + assertTrue(ti.doubleMethod() == 1.1); + assertTrue(methodCalls == 6); + + } + +} diff --git a/src/test/java/com/n9mtq4/reflection/ReflectionHelperTest.java b/src/test/java/com/n9mtq4/reflection/ReflectionHelperTest.java new file mode 100644 index 0000000..46dee76 --- /dev/null +++ b/src/test/java/com/n9mtq4/reflection/ReflectionHelperTest.java @@ -0,0 +1,864 @@ +package com.n9mtq4.reflection; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static com.n9mtq4.reflection.ReflectionHelper.*; + +/** + * Created by will on 1/28/16 at 8:22 PM. + * + * @author Will "n9Mtq4" Bresnahan + */ +public class ReflectionHelperTest { + + private void dontUnImport() { + assertFalse(false); + callVoidMethod("", null, null, null, null); + } + + @Test + public void testGetUnsafe() throws Exception { + + } + + @Test + public void testGetInt() throws Exception { + + } + + @Test + public void testGetInt1() throws Exception { + + } + + @Test + public void testSetInt() throws Exception { + + } + + @Test + public void testSetInt1() throws Exception { + + } + + @Test + public void testGetInt2() throws Exception { + + } + + @Test + public void testSetInt2() throws Exception { + + } + + @Test + public void testGetStaticInt() throws Exception { + + } + + @Test + public void testGetStaticInt1() throws Exception { + + } + + @Test + public void testSetStaticInt() throws Exception { + + } + + @Test + public void testSetStaticInt1() throws Exception { + + } + + @Test + public void testGetByte() throws Exception { + + } + + @Test + public void testGetByte1() throws Exception { + + } + + @Test + public void testSetByte() throws Exception { + + } + + @Test + public void testSetByte1() throws Exception { + + } + + @Test + public void testGetByte2() throws Exception { + + } + + @Test + public void testSetByte2() throws Exception { + + } + + @Test + public void testGetStaticByte() throws Exception { + + } + + @Test + public void testGetStaticByte1() throws Exception { + + } + + @Test + public void testSetStaticByte() throws Exception { + + } + + @Test + public void testSetStaticByte1() throws Exception { + + } + + @Test + public void testGetBoolean() throws Exception { + + } + + @Test + public void testGetBoolean1() throws Exception { + + } + + @Test + public void testSetBoolean() throws Exception { + + } + + @Test + public void testSetBoolean1() throws Exception { + + } + + @Test + public void testGetBoolean2() throws Exception { + + } + + @Test + public void testSetBoolean2() throws Exception { + + } + + @Test + public void testGetStaticBoolean() throws Exception { + + } + + @Test + public void testGetStaticBoolean1() throws Exception { + + } + + @Test + public void testSetStaticBoolean() throws Exception { + + } + + @Test + public void testSetStaticBoolean1() throws Exception { + + } + + @Test + public void testGetChar() throws Exception { + + } + + @Test + public void testGetChar1() throws Exception { + + } + + @Test + public void testSetChar() throws Exception { + + } + + @Test + public void testSetChar1() throws Exception { + + } + + @Test + public void testGetChar2() throws Exception { + + } + + @Test + public void testSetChar2() throws Exception { + + } + + @Test + public void testGetStaticChar() throws Exception { + + } + + @Test + public void testGetStaticChar1() throws Exception { + + } + + @Test + public void testSetStaticChar() throws Exception { + + } + + @Test + public void testSetStaticChar1() throws Exception { + + } + + @Test + public void testGetFloat() throws Exception { + + } + + @Test + public void testGetFloat1() throws Exception { + + } + + @Test + public void testSetFloat() throws Exception { + + } + + @Test + public void testSetFloat1() throws Exception { + + } + + @Test + public void testGetFloat2() throws Exception { + + } + + @Test + public void testSetFloat2() throws Exception { + + } + + @Test + public void testGetStaticFloat() throws Exception { + + } + + @Test + public void testGetStaticFloat1() throws Exception { + + } + + @Test + public void testSetStaticFloat() throws Exception { + + } + + @Test + public void testSetStaticFloat1() throws Exception { + + } + + @Test + public void testGetDouble() throws Exception { + + } + + @Test + public void testGetDouble1() throws Exception { + + } + + @Test + public void testSetDouble() throws Exception { + + } + + @Test + public void testSetDouble1() throws Exception { + + } + + @Test + public void testGetDouble2() throws Exception { + + } + + @Test + public void testSetDouble2() throws Exception { + + } + + @Test + public void testGetStaticDouble() throws Exception { + + } + + @Test + public void testGetStaticDouble1() throws Exception { + + } + + @Test + public void testSetStaticDouble() throws Exception { + + } + + @Test + public void testSetStaticDouble1() throws Exception { + + } + + @Test + public void testGetLong() throws Exception { + + } + + @Test + public void testGetLong1() throws Exception { + + } + + @Test + public void testSetLong() throws Exception { + + } + + @Test + public void testSetLong1() throws Exception { + + } + + @Test + public void testGetLong2() throws Exception { + + } + + @Test + public void testSetLong2() throws Exception { + + } + + @Test + public void testGetStaticLong() throws Exception { + + } + + @Test + public void testGetStaticLong1() throws Exception { + + } + + @Test + public void testSetStaticLong() throws Exception { + + } + + @Test + public void testSetStaticLong1() throws Exception { + + } + + @Test + public void testGetShort() throws Exception { + + } + + @Test + public void testGetShort1() throws Exception { + + } + + @Test + public void testSetShort() throws Exception { + + } + + @Test + public void testSetShort1() throws Exception { + + } + + @Test + public void testGetShort2() throws Exception { + + } + + @Test + public void testSetShort2() throws Exception { + + } + + @Test + public void testGetStaticShort() throws Exception { + + } + + @Test + public void testGetStaticShort1() throws Exception { + + } + + @Test + public void testSetStaticShort() throws Exception { + + } + + @Test + public void testSetStaticShort1() throws Exception { + + } + + @Test + public void testGetObject() throws Exception { + + } + + @Test + public void testGetObject1() throws Exception { + + } + + @Test + public void testSetObject() throws Exception { + + } + + @Test + public void testSetObject1() throws Exception { + + } + + @Test + public void testGetObject2() throws Exception { + + } + + @Test + public void testSetObject2() throws Exception { + + } + + @Test + public void testGetStaticObject() throws Exception { + + } + + @Test + public void testGetStaticObject1() throws Exception { + + } + + @Test + public void testSetStaticObject() throws Exception { + + } + + @Test + public void testSetStaticObject1() throws Exception { + + } + + @Test + public void testCallConstructor() throws Exception { + + } + + @Test + public void testCallConstructor1() throws Exception { + + } + + @Test + public void testCallObjectMethod() throws Exception { + + } + + @Test + public void testCallObjectMethod1() throws Exception { + + } + + @Test + public void testCallObjectMethod2() throws Exception { + + } + + @Test + public void testCallObjectMethod3() throws Exception { + + } + + @Test + public void testCallObjectMethod4() throws Exception { + + } + + @Test + public void testCallStaticObjectMethod() throws Exception { + + } + + @Test + public void testCallStaticObjectMethod1() throws Exception { + + } + + @Test + public void testCallStaticObjectMethod2() throws Exception { + + } + + @Test + public void testCallVoidMethod() throws Exception { + + } + + @Test + public void testCallVoidMethod1() throws Exception { + + } + + @Test + public void testCallVoidMethod2() throws Exception { + + } + + @Test + public void testCallVoidMethod3() throws Exception { + + } + + @Test + public void testCallStaticVoidMethod() throws Exception { + + } + + @Test + public void testCallStaticVoidMethod1() throws Exception { + + } + + @Test + public void testCallIntMethod() throws Exception { + + } + + @Test + public void testCallIntMethod1() throws Exception { + + } + + @Test + public void testCallIntMethod2() throws Exception { + + } + + @Test + public void testCallIntMethod3() throws Exception { + + } + + @Test + public void testCallStaticIntMethod() throws Exception { + + } + + @Test + public void testCallStaticIntMethod1() throws Exception { + + } + + @Test + public void testCallByteMethod() throws Exception { + + } + + @Test + public void testCallByteMethod1() throws Exception { + + } + + @Test + public void testCallByteMethod2() throws Exception { + + } + + @Test + public void testCallByteMethod3() throws Exception { + + } + + @Test + public void testCallStaticByteMethod() throws Exception { + + } + + @Test + public void testCallStaticByteMethod1() throws Exception { + + } + + @Test + public void testCallBooleanMethod() throws Exception { + + } + + @Test + public void testCallBooleanMethod1() throws Exception { + + } + + @Test + public void testCallBooleanMethod2() throws Exception { + + } + + @Test + public void testCallBooleanMethod3() throws Exception { + + } + + @Test + public void testCallStaticBooleanMethod() throws Exception { + + } + + @Test + public void testCallStaticBooleanMethod1() throws Exception { + + } + + @Test + public void testCallCharMethod() throws Exception { + + } + + @Test + public void testCallCharMethod1() throws Exception { + + } + + @Test + public void testCallCharMethod2() throws Exception { + + } + + @Test + public void testCallCharMethod3() throws Exception { + + } + + @Test + public void testCallStaticCharMethod() throws Exception { + + } + + @Test + public void testCallStaticCharMethod1() throws Exception { + + } + + @Test + public void testCallFloatMethod() throws Exception { + + } + + @Test + public void testCallFloatMethod1() throws Exception { + + } + + @Test + public void testCallFloatMethod2() throws Exception { + + } + + @Test + public void testCallFloatMethod3() throws Exception { + + } + + @Test + public void testCallStaticFloatMethod() throws Exception { + + } + + @Test + public void testCallStaticFloatMethod1() throws Exception { + + } + + @Test + public void testCallDoubleMethod() throws Exception { + + } + + @Test + public void testCallDoubleMethod1() throws Exception { + + } + + @Test + public void testCallDoubleMethod2() throws Exception { + + } + + @Test + public void testCallDoubleMethod3() throws Exception { + + } + + @Test + public void testCallStaticDoubleMethod() throws Exception { + + } + + @Test + public void testCallStaticDoubleMethod1() throws Exception { + + } + + @Test + public void testCallLongMethod() throws Exception { + + } + + @Test + public void testCallLongMethod1() throws Exception { + + } + + @Test + public void testCallLongMethod2() throws Exception { + + } + + @Test + public void testCallLongMethod3() throws Exception { + + } + + @Test + public void testCallStaticLongMethod() throws Exception { + + } + + @Test + public void testCallStaticLongMethod1() throws Exception { + + } + + @Test + public void testGetClassesForPackage() throws Exception { + + } + + @Test + public void testFindPackageNamesStartingWith() throws Exception { + + } + + @Test + public void testGetClassBySimpleName() throws Exception { + + } + + @Test + public void testGetClassByFullName() throws Exception { + + } + + @Test + public void testGetClass() throws Exception { + + } + + @Test + public void testGetAllDeclaredFields() throws Exception { + + } + + @Test + public void testGetAllDeclaredField() throws Exception { + + } + + @Test + public void testGetAllDeclaredMethods() throws Exception { + + } + + @Test + public void testGetAllDeclaredMethod() throws Exception { + + } + + @Test + public void testFindAllDeclaredMethodByParams() throws Exception { + + } + + @Test + public void testGetClassParams() throws Exception { + + } + + @Test + public void testCanBePrimitive() throws Exception { + assertTrue(canBePrimitive(Double.class)); + assertTrue(canBePrimitive(Integer.class)); + assertTrue(canBePrimitive(Boolean.class)); + assertFalse(canBePrimitive(String.class)); + assertFalse(canBePrimitive(ReflectionHelperTest.class)); + } + + @Test + public void testIsPrimitive() throws Exception { + assertTrue(isPrimitive(double.class)); + assertTrue(isPrimitive(int.class)); + assertTrue(isPrimitive(boolean.class)); + assertFalse(isPrimitive(Double.class)); + assertFalse(isPrimitive(Integer.class)); + assertFalse(isPrimitive(Boolean.class)); + assertFalse(isPrimitive(String.class)); + assertFalse(isPrimitive(ReflectionHelperTest.class)); + } + + @Test + public void testGetPrimitiveClass() throws Exception { +// TODO: add all + assertTrue(getPrimitiveClass(Boolean.class) == boolean.class); + assertTrue(getPrimitiveClass(Double.class) == double.class); + assertTrue(getPrimitiveClass(String.class) == null); + } + + @Test + public void testGetObjectClass() throws Exception { + + } + + @Test + public void testForceGetPrimitiveClass() throws Exception { + assertTrue(forceGetPrimitiveClass(Double.class) == double.class); + assertTrue(forceGetPrimitiveClass(String.class) == String.class); + } + + @Test + public void testForceGetObjectClass() throws Exception { + + } +} diff --git a/src/test/java/com/n9mtq4/reflection/ReflectionProtectorTest.java b/src/test/java/com/n9mtq4/reflection/ReflectionProtectorTest.java new file mode 100644 index 0000000..e206718 --- /dev/null +++ b/src/test/java/com/n9mtq4/reflection/ReflectionProtectorTest.java @@ -0,0 +1,27 @@ +package com.n9mtq4.reflection; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by will on 1/28/16 at 8:13 PM. + * + * @author Will "n9Mtq4" Bresnahan + */ +public class ReflectionProtectorTest { + + @Test + public void testUsedReflection() throws Exception { + + System.out.println("Testing reflection protector"); + assertFalse("no reflection", usedReflectionToCallMe()); + assertTrue("with reflection", ReflectionHelper.callStaticBooleanMethod("usedReflectionToCallMe", ReflectionProtectorTest.class)); + + } + + public static boolean usedReflectionToCallMe() { + return ReflectionProtector.usedReflection(); + } + +} diff --git a/src/test/java/com/n9mtq4/reflection/toreflect/TestInterface.java b/src/test/java/com/n9mtq4/reflection/toreflect/TestInterface.java new file mode 100644 index 0000000..c295ce7 --- /dev/null +++ b/src/test/java/com/n9mtq4/reflection/toreflect/TestInterface.java @@ -0,0 +1,18 @@ +package com.n9mtq4.reflection.toreflect; + +/** + * Created by will on 1/28/16 at 8:26 PM. + * + * @author Will "n9Mtq4" Bresnahan + */ +public interface TestInterface { + +// not all primitives, but a lot. + void voidMethod(); + boolean booleanMethod(); + char charMethod(); + String stringMethod(); // I know String isn't primitive + int intMethod(); + double doubleMethod(); + +} diff --git a/src/test/java/com/n9mtq4/reflection/toreflect/TestInterfaceImplementation.java b/src/test/java/com/n9mtq4/reflection/toreflect/TestInterfaceImplementation.java new file mode 100644 index 0000000..27f3800 --- /dev/null +++ b/src/test/java/com/n9mtq4/reflection/toreflect/TestInterfaceImplementation.java @@ -0,0 +1,53 @@ +package com.n9mtq4.reflection.toreflect; + +/** + * Created by will on 1/28/16 at 8:27 PM. + * + * @author Will "n9Mtq4" Bresnahan + */ +public class TestInterfaceImplementation implements TestInterface { + + public boolean voidMethod = false; + public boolean booleanMethod = false; + public boolean charMethod = false; + public boolean stringMethod = false; + public boolean intMethod = false; + public boolean doubleMethod = false; + + @Override + public void voidMethod() { + voidMethod = true; + System.out.println(TestInterfaceImplementation.class.getName() + ".voidMethod() was called"); + } + + @Override + public boolean booleanMethod() { + booleanMethod = true; + return true; + } + + @Override + public char charMethod() { + charMethod = true; + return 'a'; + } + + @Override + public String stringMethod() { + stringMethod = true; + return "Hello World"; + } + + @Override + public int intMethod() { + intMethod = true; + return 1; + } + + @Override + public double doubleMethod() { + doubleMethod = true; + return 1.1; + } + +}