diff --git a/.gitignore b/.gitignore index 32858aa..8fea83e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ *.jar *.war *.ear +.DS_Store # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* 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..61bbb77 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_beust_jcommander_1_64.xml b/.idea/libraries/Maven__com_beust_jcommander_1_64.xml new file mode 100644 index 0000000..e72647c --- /dev/null +++ b/.idea/libraries/Maven__com_beust_jcommander_1_64.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ 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/libraries/Maven__org_testng_testng_6_11.xml b/.idea/libraries/Maven__org_testng_testng_6_11.xml new file mode 100644 index 0000000..5105222 --- /dev/null +++ b/.idea/libraries/Maven__org_testng_testng_6_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_yaml_snakeyaml_1_17.xml b/.idea/libraries/Maven__org_yaml_snakeyaml_1_17.xml new file mode 100644 index 0000000..20e2920 --- /dev/null +++ b/.idea/libraries/Maven__org_yaml_snakeyaml_1_17.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..38d97d5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,19 @@ + + + + false + + false + false + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1ee38f5 --- /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..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..56ef2ab --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,982 @@ + + + + + + + + + + + + + + + + + garcia.luis.typeinformation.* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1493914264146 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.8 + + + + + + + + TypeInformation + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/TypeInformation.iml b/TypeInformation.iml new file mode 100644 index 0000000..3b72213 --- /dev/null +++ b/TypeInformation.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5fa5bb2 --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + garcia.luis + typeinformation + 1.0-SNAPSHOT + + + junit + junit + 4.12 + + + junit + junit + RELEASE + + + org.testng + testng + RELEASE + + + + + + \ No newline at end of file diff --git a/src/main/java/garcia/luis/TypeInformation.java b/src/main/java/garcia/luis/TypeInformation.java new file mode 100644 index 0000000..1a48766 --- /dev/null +++ b/src/main/java/garcia/luis/TypeInformation.java @@ -0,0 +1,154 @@ +package garcia.luis; + +import javax.sound.midi.Soundbank; +import java.lang.reflect.*; +import java.util.ArrayList; + +/** + * Created by luisgarcia on 5/4/17. + */ +public class TypeInformation +{ + + public boolean classImplementsInterface(Object obj, String theInterface) + { + Class classObject = obj.getClass(); + Class [] interfaces = classObject.getInterfaces(); + for(int i = 0; i < interfaces.length; i++) + { + if(theInterface.equals(interfaces[i].getName())) + { + return true; + } + } + return false; + } + + public boolean classImplementsInterface(Class obj, String theInterface) + { + Class [] interfaces = obj.getInterfaces(); + for(int i = 0; i < interfaces.length; i++) + { + if(theInterface.equals(interfaces[i].getName())) + { + return true; + } + } + return false; + } + + public boolean classImplementsInterface(String obj, String theInterface) throws ClassNotFoundException + { + Class cl = Class.forName(obj); + Class [] interfaces = cl.getInterfaces(); + for(int i = 0; i < interfaces.length; i++) + { + if(theInterface.equals(interfaces[i].getName())) + { + return true; + } + } + return false; + + } + + public void listAllMembers(Object obj) + { + Class cl = obj.getClass(); + ArrayList myList = getSuperClass(obj); + System.out.println(myList.size()); + for(int i=0; i mySuperList = getSuperClass(obj.getClass()); + + for(int i = mySuperList.size()-1;i >= 0; i--) + { + System.out.println(tab + mySuperList.get(i)); + tab= tab + " "; + + } + + + + } + + public ArrayList getSuperClass(Object obj) + { + ArrayList superClassList = new ArrayList(); + String superClass = Object.class.getName(); + int i =0; + + + do{ + superClassList.add(obj.getClass().getSuperclass().getName()); + obj = obj.getClass().getSuperclass(); + + } + while (!superClass.equals(superClassList.get(i++))); + + return superClassList; + + } + + public ArrayList instantiateClassHierarchy(Object obj) + { + ArrayList myList = getSuperClass(obj); + ArrayList instance = new ArrayList(); + for(int i = 0; i < myList.size(); i++) + { + if(obj.getClass().isInstance(myList.get(i))) + { + instance.add(obj); + } + } + return instance; + } + + public void printAllMembers(Object obj) + { + Constructor[] constructors = obj.getClass().getDeclaredConstructors(); + Field [] fields = obj.getClass().getDeclaredFields(); + Method [] methods = obj.getClass().getDeclaredMethods(); + + System.out.println("Constructors"); + for(int i = 0; i < constructors.length; i++) + { + System.out.println(constructors[i]); + } + System.out.println("Fields"); + for(int i = 0; i < fields.length; i++) + { + System.out.println(fields[i]); + } + System.out.println("Methods"); + for(int i = 0; i < methods.length; i++) + { + System.out.println(methods[i]); + } + System.out.println(); + } + + public static void main(String[] args) + { + TypeInformation test = new TypeInformation(); + String myString = "Paco"; + Double myDouble = 2.33; + ArrayList myList = test.instantiateClassHierarchy(myString); + System.out.println(myList); + + + } + + +} diff --git a/src/main/java/unitcorn/Result.java b/src/main/java/unitcorn/Result.java new file mode 100644 index 0000000..ba75c17 --- /dev/null +++ b/src/main/java/unitcorn/Result.java @@ -0,0 +1,32 @@ +package unitcorn; + +/** + * Created by luisgarcia on 5/5/17. + */ +public class Result +{ + private boolean pass; + private String method; + private String theClass; + + public Result(boolean pass, String method, String theClass) + { + this.pass = pass; + this.method = method; + this.theClass = theClass; + } + + public boolean getPass() + { + return pass; + } + + public void printResutl() + { + System.out.println("Class Tested: " + theClass); + System.out.println("Method Tested: " + method); + System.out.println("Pass: " + pass); + } + + +} diff --git a/src/main/java/unitcorn/UnitCornTestRunner.java b/src/main/java/unitcorn/UnitCornTestRunner.java new file mode 100644 index 0000000..a4f2076 --- /dev/null +++ b/src/main/java/unitcorn/UnitCornTestRunner.java @@ -0,0 +1,79 @@ +package unitcorn; + +import garcia.luis.TypeInformation; +import org.junit.Assert; +import org.junit.Test; + +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; + + +/** + * Created by luisgarcia on 5/5/17. + */ +public class UnitCornTestRunner +{ + public Result runTest(Class c, String methodName) + { + + boolean pass = false; + try { + Method m = c.getMethod(methodName); + Object obj = c.newInstance(); + pass = methodPass(m, obj); + + }catch (Exception e){ + System.err.println(e.getMessage()); + } + Result myResult = new Result(pass,methodName, c.getName()); + return myResult; + } + + public String runTests(Class c) + { + Method[] methods = c.getDeclaredMethods(); + for(int i = 0; i < methods.length; i++) + { + if(methods[i].isAnnotationPresent(Test.class)) + { + runTest(c, methods[i].getName()); + return printAnnotations(methods[i]); + } + } + + return "No Results"; + } + + private String printAnnotations(Method method) { + Annotation[] methodAnnotations = method.getDeclaredAnnotations(); + String methodName= method.getName(); + if(methodAnnotations.length < 1) + { + return "There were no annotations associated with the method " + methodName; + } + for(Annotation annotation : methodAnnotations) + { + String annotationName = annotation.getClass().getName(); + return annotationName; + } + return "Fuck"; + } + + public boolean methodPass(Method method, Object obj) + { + boolean pass = false; + try { + method.invoke(obj); + pass = true; + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return pass; + } + + +} diff --git a/src/test/java/TestTypeInformation.java b/src/test/java/TestTypeInformation.java new file mode 100644 index 0000000..ee71f97 --- /dev/null +++ b/src/test/java/TestTypeInformation.java @@ -0,0 +1,65 @@ + +import garcia.luis.TypeInformation; +import org.junit.Assert; +import org.junit.Test; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Comparator; + +/** + * Created by luisgarcia on 5/4/17. + */ +public class TestTypeInformation +{ + TypeInformation test = new TypeInformation(); + String strObj = "hello"; + Double dbl = 3.2; + Class cl = dbl.getClass(); + ArrayList myList; + + + @Test + public void testImplementsInterface() throws ClassNotFoundException { + //given + boolean expected = false; + + //when + boolean actual = test.classImplementsInterface(strObj.getClass(), "Comparable"); + + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testListAllMembers() + { + //when + Method[] methods = strObj.getClass().getDeclaredMethods(); + int expected = 77; + + //when + int actual = strObj.getClass().getDeclaredMethods().length; + + //then + Assert.assertEquals(expected, actual, 0); + + } + + @Test + public void testHierarchy() + { + //given + myList = test.getSuperClass(cl); + String expected = "java.lang.Object"; + + //when + String actual = myList.get(0); + + //then + Assert.assertEquals(expected, actual); + + + } + +} diff --git a/src/test/java/TestUnitCornTestRunner.java b/src/test/java/TestUnitCornTestRunner.java new file mode 100644 index 0000000..979ffe0 --- /dev/null +++ b/src/test/java/TestUnitCornTestRunner.java @@ -0,0 +1,61 @@ +import org.junit.Assert; +import org.junit.Test; +import unitcorn.Result; +import unitcorn.UnitCornTestRunner; + +/** + * Created by luisgarcia on 5/5/17. + */ +public class TestUnitCornTestRunner +{ + UnitCornTestRunner tester = new UnitCornTestRunner(); + Double aDouble = 3.333; + Class aClass = aDouble.getClass(); + + + @Test + public void testRunTests() + { + TestUnitCornTestRunner paco = new TestUnitCornTestRunner(); + //given + String expected = "com.sun.proxy.$Proxy1"; + + //when + String actual = tester.runTests(paco.getClass()); + + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testRunTestNoResults() + { + //given + String expected = "No Results"; + + //when + String actual = tester.runTests(aClass); + + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testRunTest() + { + TestUnitCornTestRunner paco = new TestUnitCornTestRunner(); + String myMethod = tester.runTests(paco.getClass()); + + //given + Result myResult = tester.runTest(paco.getClass(), myMethod); + boolean expected = false; + + //when + boolean actual = myResult.getPass(); + + //then + Assert.assertEquals(expected, actual); + } + + +}