From 2e26989819463767ab186d38559fafb5bff34406 Mon Sep 17 00:00:00 2001 From: vidyachandasekhar Date: Sun, 7 May 2017 16:00:19 -0400 Subject: [PATCH 1/5] initial implemented Reflection class which will list all the methods, fields and constructors > created test cases --- pom.xml | 30 ++++++++++ .../typeinformation/ReflectionClass.java | 48 +++++++++++++++ .../java/com/zipcode/typeinformation/Dog.java | 16 +++++ .../java/com/zipcode/typeinformation/Pet.java | 8 +++ .../typeinformation/ReflectionClassTest.java | 58 +++++++++++++++++++ .../com/zipcode/typeinformation/Vehicle.java | 7 +++ 6 files changed, 167 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/zipcode/typeinformation/ReflectionClass.java create mode 100644 src/test/java/com/zipcode/typeinformation/Dog.java create mode 100644 src/test/java/com/zipcode/typeinformation/Pet.java create mode 100644 src/test/java/com/zipcode/typeinformation/ReflectionClassTest.java create mode 100644 src/test/java/com/zipcode/typeinformation/Vehicle.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e4b497f --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + com.zipocde.app + typeinformation + jar + 1.0-SNAPSHOT + typeinformation-lab + http://maven.apache.org + + + junit + junit + 4.12 + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + \ No newline at end of file diff --git a/src/main/java/com/zipcode/typeinformation/ReflectionClass.java b/src/main/java/com/zipcode/typeinformation/ReflectionClass.java new file mode 100644 index 0000000..3d740be --- /dev/null +++ b/src/main/java/com/zipcode/typeinformation/ReflectionClass.java @@ -0,0 +1,48 @@ +package com.zipcode.typeinformation; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * Created by vidyachandasekhar on 5/7/17. + */ +public class ReflectionClass { + + + public Boolean classImplementsInterface(Object object , String interfaceName){ + Class[] interfaces = object.getClass().getInterfaces(); + + for (Class interfaceType : interfaces ){ + if ( interfaceType.getName().equals(interfaceName)){ + return true; + } + } + return false; + } + + public String listAllMembers(Object object) { + + Field[] fields = object.getClass().getFields(); + StringBuilder sb = new StringBuilder(); + + for (Field field : fields) { + sb.append("Field Name :" + field.getName() + " Declared class Name : " + field.getDeclaringClass().getName() + " Field modifier :" + field.getDeclaringClass().getModifiers()+"\n"); + } + + Constructor[] constructors = object.getClass().getConstructors(); + + for (Constructor constructor : constructors) { + sb.append("Constructor Name :" + constructor.getName()+"\n"); + } + + + Method[] methods =object.getClass().getDeclaredMethods(); + for (Method method : methods) { + sb.append("Method Name :" + method.getName()+"\n"); + } + + return sb.toString(); + } + +} diff --git a/src/test/java/com/zipcode/typeinformation/Dog.java b/src/test/java/com/zipcode/typeinformation/Dog.java new file mode 100644 index 0000000..6b8d086 --- /dev/null +++ b/src/test/java/com/zipcode/typeinformation/Dog.java @@ -0,0 +1,16 @@ +package com.zipcode.typeinformation; + +/** + * Created by vidyachandasekhar on 5/7/17. + */ +public class Dog implements Pet { + public String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/test/java/com/zipcode/typeinformation/Pet.java b/src/test/java/com/zipcode/typeinformation/Pet.java new file mode 100644 index 0000000..617e200 --- /dev/null +++ b/src/test/java/com/zipcode/typeinformation/Pet.java @@ -0,0 +1,8 @@ +package com.zipcode.typeinformation; + +/** + * Created by vidyachandasekhar on 5/7/17. + */ +public interface Pet { + +} diff --git a/src/test/java/com/zipcode/typeinformation/ReflectionClassTest.java b/src/test/java/com/zipcode/typeinformation/ReflectionClassTest.java new file mode 100644 index 0000000..ae6ad6c --- /dev/null +++ b/src/test/java/com/zipcode/typeinformation/ReflectionClassTest.java @@ -0,0 +1,58 @@ +package com.zipcode.typeinformation; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Created by vidyachandasekhar on 5/7/17. + */ +public class ReflectionClassTest { + @Test + public void listAllMembers() throws Exception { + //given + + ReflectionClass reflectionClass = new ReflectionClass(); + Dog dog = new Dog(); + //when + String result = reflectionClass.listAllMembers(dog); + + + String exptected ="Field Name :name Declared class Name : com.zipcode.typeinformation.Dog Field modifier :1\n" + + "Constructor Name :com.zipcode.typeinformation.Dog\n" + + "Method Name :getName\n" + + "Method Name :setName\n"; + //then + assertEquals(exptected,result); + + } + + @Test + public void classImplementsInterfacePositiveCondition() throws Exception { + //given + + ReflectionClass reflectionClass = new ReflectionClass(); + Dog dog = new Dog(); + //when + Boolean result = reflectionClass.classImplementsInterface(dog, "com.zipcode.typeinformation.Pet"); + + + //then + assertTrue(result); + } + @Test + public void classImplementsInterfaceNegative() throws Exception { + //given + + ReflectionClass reflectionClass = new ReflectionClass(); + Dog dog = new Dog(); + //when + Boolean result = reflectionClass.classImplementsInterface(dog, "com.zipcode.typeinformation.Vehicle"); + + + //then + assertFalse(result); + } +} \ No newline at end of file diff --git a/src/test/java/com/zipcode/typeinformation/Vehicle.java b/src/test/java/com/zipcode/typeinformation/Vehicle.java new file mode 100644 index 0000000..9defb5b --- /dev/null +++ b/src/test/java/com/zipcode/typeinformation/Vehicle.java @@ -0,0 +1,7 @@ +package com.zipcode.typeinformation; + +/** + * Created by vidyachandasekhar on 5/7/17. + */ +public interface Vehicle { +} From 4dcf856f2a809e2f99eced50c0f2ba225491eae3 Mon Sep 17 00:00:00 2001 From: vidyachandasekhar Date: Sun, 7 May 2017 16:08:16 -0400 Subject: [PATCH 2/5] initial implemented Reflection class which will list all the methods, fields and constructors > created test cases --- .idea/.name | 1 + .idea/compiler.xml | 16 +++ .idea/kotlinc.xml | 7 + .idea/libraries/Maven__junit_junit_4_12.xml | 13 ++ .../Maven__org_hamcrest_hamcrest_core_1_3.xml | 13 ++ .idea/misc.xml | 62 +++++++++ .idea/modules.xml | 8 ++ .idea/uiDesigner.xml | 124 ++++++++++++++++++ .idea/vcs.xml | 6 + typeinformation.iml | 16 +++ 10 files changed, 266 insertions(+) create mode 100644 .idea/.name create mode 100644 .idea/compiler.xml create mode 100644 .idea/kotlinc.xml create mode 100644 .idea/libraries/Maven__junit_junit_4_12.xml create mode 100644 .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 typeinformation.iml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..6668c41 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +typeinformation \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..aef09a0 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..1c24f9a --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml new file mode 100644 index 0000000..d411041 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_12.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..f58bbc1 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b2aa25a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.8 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..42e27ba --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/typeinformation.iml b/typeinformation.iml new file mode 100644 index 0000000..4e3316b --- /dev/null +++ b/typeinformation.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file From 54bcafc5e53634772bb0d596bc0a32ff4a650dc7 Mon Sep 17 00:00:00 2001 From: vidyachandasekhar Date: Sun, 7 May 2017 17:19:08 -0400 Subject: [PATCH 3/5] initial implemented Reflection class which will list all the methods, fields and constructors > created test cases --- src/main/java/com/zipcode/typeinformation/ReflectionClass.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/zipcode/typeinformation/ReflectionClass.java b/src/main/java/com/zipcode/typeinformation/ReflectionClass.java index 3d740be..d1a2592 100644 --- a/src/main/java/com/zipcode/typeinformation/ReflectionClass.java +++ b/src/main/java/com/zipcode/typeinformation/ReflectionClass.java @@ -21,6 +21,7 @@ public Boolean classImplementsInterface(Object object , String interfaceName){ return false; } + public String listAllMembers(Object object) { Field[] fields = object.getClass().getFields(); From a37b3765c699eef016bf7bb50a871d0560318ea5 Mon Sep 17 00:00:00 2001 From: vidyachandasekhar Date: Sun, 7 May 2017 18:16:59 -0400 Subject: [PATCH 4/5] created Method to print Class Hierarchy test cases --- .../typeinformation/ReflectionClass.java | 56 ++++++++++++------- .../typeinformation/ReflectionClassTest.java | 21 +++++++ 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/zipcode/typeinformation/ReflectionClass.java b/src/main/java/com/zipcode/typeinformation/ReflectionClass.java index d1a2592..a90aba9 100644 --- a/src/main/java/com/zipcode/typeinformation/ReflectionClass.java +++ b/src/main/java/com/zipcode/typeinformation/ReflectionClass.java @@ -2,7 +2,9 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.List; /** * Created by vidyachandasekhar on 5/7/17. @@ -10,11 +12,11 @@ public class ReflectionClass { - public Boolean classImplementsInterface(Object object , String interfaceName){ - Class[] interfaces = object.getClass().getInterfaces(); + public Boolean classImplementsInterface(Object object, String interfaceName) { + Class[] interfaces = object.getClass().getInterfaces(); - for (Class interfaceType : interfaces ){ - if ( interfaceType.getName().equals(interfaceName)){ + for (Class interfaceType : interfaces) { + if (interfaceType.getName().equals(interfaceName)) { return true; } } @@ -22,28 +24,42 @@ public Boolean classImplementsInterface(Object object , String interfaceName){ } - public String listAllMembers(Object object) { + public String listAllMembers(Object object) { - Field[] fields = object.getClass().getFields(); - StringBuilder sb = new StringBuilder(); + Field[] fields = object.getClass().getFields(); + StringBuilder sb = new StringBuilder(); - for (Field field : fields) { - sb.append("Field Name :" + field.getName() + " Declared class Name : " + field.getDeclaringClass().getName() + " Field modifier :" + field.getDeclaringClass().getModifiers()+"\n"); - } + for (Field field : fields) { + sb.append("Field Name :" + field.getName() + " Declared class Name : " + field.getDeclaringClass().getName() + " Field modifier :" + field.getDeclaringClass().getModifiers() + "\n"); + } - Constructor[] constructors = object.getClass().getConstructors(); + Constructor[] constructors = object.getClass().getConstructors(); - for (Constructor constructor : constructors) { - sb.append("Constructor Name :" + constructor.getName()+"\n"); - } + for (Constructor constructor : constructors) { + sb.append("Constructor Name :" + constructor.getName() + "\n"); + } - Method[] methods =object.getClass().getDeclaredMethods(); - for (Method method : methods) { - sb.append("Method Name :" + method.getName()+"\n"); - } + Method[] methods = object.getClass().getDeclaredMethods(); + for (Method method : methods) { + sb.append("Method Name :" + method.getName() + "\n"); + } + + return sb.toString(); + } + + public String getClassHierarchy(Object object) { + StringBuilder sb = new StringBuilder(); + sb.append(object.getClass().getName() ); + Class superclass = object.getClass().getSuperclass(); + while (superclass != null) { + sb.append("\n"+superclass.getName()); + superclass = superclass.getSuperclass(); + + } + return sb.toString(); + } + - return sb.toString(); - } } diff --git a/src/test/java/com/zipcode/typeinformation/ReflectionClassTest.java b/src/test/java/com/zipcode/typeinformation/ReflectionClassTest.java index ae6ad6c..2d04618 100644 --- a/src/test/java/com/zipcode/typeinformation/ReflectionClassTest.java +++ b/src/test/java/com/zipcode/typeinformation/ReflectionClassTest.java @@ -2,6 +2,8 @@ import org.junit.Test; +import java.util.ArrayList; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -10,6 +12,25 @@ * Created by vidyachandasekhar on 5/7/17. */ public class ReflectionClassTest { + @Test + public void getClassHierarchy() throws Exception { + //given + + ReflectionClass reflectionClass = new ReflectionClass(); + Object alist = new ArrayList(); + + //when + String result = reflectionClass.getClassHierarchy(alist); + //then + String exptected ="java.util.ArrayList\n" + + "java.util.AbstractList\n" + + "java.util.AbstractCollection\n" + + "java.lang.Object"; + assertEquals(exptected ,result) ; + + + } + @Test public void listAllMembers() throws Exception { //given From b1fde24f868be6b94682a021d6656b08d806d0ab Mon Sep 17 00:00:00 2001 From: vidyachandasekhar Date: Sun, 7 May 2017 20:25:45 -0400 Subject: [PATCH 5/5] >added UnitCorn test >junit test for the Class --- src/main/java/Result.java | 34 ++++++++++++++++++ src/main/java/UnitCornTestRunner.java | 35 +++++++++++++++++++ src/test/java/UnitCornTestRunnerTest.java | 22 ++++++++++++ .../typeinformation/DummyTestClass.java | 22 ++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 src/main/java/Result.java create mode 100644 src/main/java/UnitCornTestRunner.java create mode 100644 src/test/java/UnitCornTestRunnerTest.java create mode 100644 src/test/java/com/zipcode/typeinformation/DummyTestClass.java diff --git a/src/main/java/Result.java b/src/main/java/Result.java new file mode 100644 index 0000000..89b32c8 --- /dev/null +++ b/src/main/java/Result.java @@ -0,0 +1,34 @@ +/** + * Created by vidyachandasekhar on 5/7/17. + */ +public class Result { + private String failureTestDetails; + + private String successTestsDetails; + + public String getSuccessTestsDetails() { + return successTestsDetails; + } + + public void setSuccessTestsDetails(String successTestsDetails) { + this.successTestsDetails = successTestsDetails; + } + + public String getFailureTestDetails() { + return failureTestDetails; + } + + public void setFailureTestDetails(String failureTestDetails) { + this.failureTestDetails = failureTestDetails; + } + + @Override + public String toString() { + if (failureTestDetails != null) { + return failureTestDetails ; + } else { + return successTestsDetails; + } + + } +} diff --git a/src/main/java/UnitCornTestRunner.java b/src/main/java/UnitCornTestRunner.java new file mode 100644 index 0000000..7b76b38 --- /dev/null +++ b/src/main/java/UnitCornTestRunner.java @@ -0,0 +1,35 @@ +import org.junit.Test; + +import java.lang.reflect.Method; + +/** + * Created by vidyachandasekhar on 5/7/17. + */ +public class UnitCornTestRunner { + + public Result runTest(Class c, Method methodName) { + Result result = new Result(); + + try{ + methodName.invoke(c.newInstance()); + result.setSuccessTestsDetails("Test on method "+ methodName + " passed"); + } catch (Throwable t){ + result.setFailureTestDetails("Test on method "+ methodName + " failed" + " cause: "+ t.getCause().getMessage()); + } + + return result; + } + + public String runTests(Class c) { + Method[] methods = c.getDeclaredMethods(); + StringBuilder sb = new StringBuilder(); + for (Method method : methods) { + if (method.isAnnotationPresent(Test.class)) { + Result testResultReport = runTest(c, method); + sb.append(testResultReport.toString()); + } + } + return sb.toString(); + } +} + diff --git a/src/test/java/UnitCornTestRunnerTest.java b/src/test/java/UnitCornTestRunnerTest.java new file mode 100644 index 0000000..ab8fc08 --- /dev/null +++ b/src/test/java/UnitCornTestRunnerTest.java @@ -0,0 +1,22 @@ +import com.zipcode.typeinformation.DummyTestClass; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by vidyachandasekhar on 5/7/17. + */ +public class UnitCornTestRunnerTest { + @Test + public void runTests() throws Exception { + //Given + UnitCornTestRunner unitCornTestRunner = new UnitCornTestRunner(); + + //when + String result = unitCornTestRunner.runTests(DummyTestClass.class); + + //then + assertNotNull(result); + } + +} \ No newline at end of file diff --git a/src/test/java/com/zipcode/typeinformation/DummyTestClass.java b/src/test/java/com/zipcode/typeinformation/DummyTestClass.java new file mode 100644 index 0000000..2b281b6 --- /dev/null +++ b/src/test/java/com/zipcode/typeinformation/DummyTestClass.java @@ -0,0 +1,22 @@ +package com.zipcode.typeinformation; + +import org.junit.Test; + +/** + * Created by vidyachandasekhar on 5/7/17. + */ +public class DummyTestClass { + + @Test + public void allwaysSuccessMethod() { + System.out.println("this method will always be successful"); + } + + @Test + public void allwaysFailMethod() throws Exception { + System.out.println("this method will always throw exception "); + throw new Exception(); + } +} + +