From e99c8ada61a0206a0ad0d4ab19bf64341ec796e9 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 4 May 2017 13:09:59 -0400 Subject: [PATCH 01/14] Initial Commit --- .gitignore | 13 ++++++++ pom.xml | 25 ++++++++++++++ src/main/java/kim/christopher/App.java | 13 ++++++++ src/test/java/kim/christopher/AppTest.java | 38 ++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/kim/christopher/App.java create mode 100644 src/test/java/kim/christopher/AppTest.java diff --git a/.gitignore b/.gitignore index 32858aa..8c30fc7 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,16 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + + +# Intellij +.idea/ +*.iml +*.iws + +# Mac +.DS_Store + +# Maven +log/ +target/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..6171931 --- /dev/null +++ b/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + + kim.christopher + UnitCornTestRunner + 1.0-SNAPSHOT + jar + + UnitCornTestRunner + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + diff --git a/src/main/java/kim/christopher/App.java b/src/main/java/kim/christopher/App.java new file mode 100644 index 0000000..1686b26 --- /dev/null +++ b/src/main/java/kim/christopher/App.java @@ -0,0 +1,13 @@ +package kim.christopher; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/src/test/java/kim/christopher/AppTest.java b/src/test/java/kim/christopher/AppTest.java new file mode 100644 index 0000000..02c6083 --- /dev/null +++ b/src/test/java/kim/christopher/AppTest.java @@ -0,0 +1,38 @@ +package kim.christopher; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} From 508d345a4d888a36c12431c8de7d1046e024ef46 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 4 May 2017 13:32:29 -0400 Subject: [PATCH 02/14] Created method stubs in ClassInspector --- pom.xml | 1 + src/main/java/kim/christopher/App.java | 13 ------- .../java/kim/christopher/ClassInspector.java | 21 ++++++++++ src/test/java/kim/christopher/AppTest.java | 38 ------------------- .../kim/christopher/ClassInspectorTest.java | 4 ++ 5 files changed, 26 insertions(+), 51 deletions(-) delete mode 100644 src/main/java/kim/christopher/App.java create mode 100644 src/main/java/kim/christopher/ClassInspector.java delete mode 100644 src/test/java/kim/christopher/AppTest.java create mode 100644 src/test/java/kim/christopher/ClassInspectorTest.java diff --git a/pom.xml b/pom.xml index 6171931..290b8bb 100644 --- a/pom.xml +++ b/pom.xml @@ -22,4 +22,5 @@ test + diff --git a/src/main/java/kim/christopher/App.java b/src/main/java/kim/christopher/App.java deleted file mode 100644 index 1686b26..0000000 --- a/src/main/java/kim/christopher/App.java +++ /dev/null @@ -1,13 +0,0 @@ -package kim.christopher; - -/** - * Hello world! - * - */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); - } -} diff --git a/src/main/java/kim/christopher/ClassInspector.java b/src/main/java/kim/christopher/ClassInspector.java new file mode 100644 index 0000000..38a76b7 --- /dev/null +++ b/src/main/java/kim/christopher/ClassInspector.java @@ -0,0 +1,21 @@ +package kim.christopher; + +import java.util.ArrayList; + +public class ClassInspector { + + public boolean classImplementsInterface(Class c, String s){ + return true; + } + + public String listAllMembers(){ + return ""; + } + public String getClassHierarchy(Object obj){ + return ""; + } + + public ArrayList instantiateClassHierarchy(Object obj){ + return new ArrayList(); + } +} diff --git a/src/test/java/kim/christopher/AppTest.java b/src/test/java/kim/christopher/AppTest.java deleted file mode 100644 index 02c6083..0000000 --- a/src/test/java/kim/christopher/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package kim.christopher; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/src/test/java/kim/christopher/ClassInspectorTest.java b/src/test/java/kim/christopher/ClassInspectorTest.java new file mode 100644 index 0000000..7f90336 --- /dev/null +++ b/src/test/java/kim/christopher/ClassInspectorTest.java @@ -0,0 +1,4 @@ +package kim.christopher; + +public class ClassInspectorTest { +} From 82dcd639a4973f666462e1e72f5e763b63305cea Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 4 May 2017 13:43:08 -0400 Subject: [PATCH 03/14] Added comments with method requirements --- pom.xml | 2 +- .../java/kim/christopher/ClassInspector.java | 20 ++++++- .../kim/christopher/ClassInspectorTest.java | 53 +++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 290b8bb..c03b83f 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ junit junit - 3.8.1 + 4.12 test diff --git a/src/main/java/kim/christopher/ClassInspector.java b/src/main/java/kim/christopher/ClassInspector.java index 38a76b7..758b828 100644 --- a/src/main/java/kim/christopher/ClassInspector.java +++ b/src/main/java/kim/christopher/ClassInspector.java @@ -4,17 +4,35 @@ public class ClassInspector { + + //Take two arguments: 1) a class, speicfied by an object, Class object, or class name, and 2) an interface name. + // Return true if the specified class implements the specified interface public boolean classImplementsInterface(Class c, String s){ - return true; + return false; } + //Take an object and list all declared members (Fields, Constructors, and Methods) of its class, as well as those of each superclass, all the way to Object. + //This method should return a string containing each declared member listed on a separate line as follows: + //The member name, preceded with the name of the declaring class, and any modifiers (static, private etc.) + //Within each class, members should be listed in the order: Fields, Constructors, Methods + //Each group of members (fields, constructors, or methods) should be listed alphabetically + //The first line should start with the members declared by the given object type; the last line should be the last method defined by Object + public String listAllMembers(){ return ""; } + + //take an object and produce an indented class hierarchy with one class per line. + // Each line should be indented two spaces more than the previous one. + // The first line should be java.lang.Object public String getClassHierarchy(Object obj){ return ""; } + + //take an object and return a List containing instances of every concrete class in its hierarchy. + // Handle classes without a no-argument constructor gracefully (your program should not crash, but may not be able to instantiate these classes). + // Note: Your test should confirm that each list item is an instance of a different class. public ArrayList instantiateClassHierarchy(Object obj){ return new ArrayList(); } diff --git a/src/test/java/kim/christopher/ClassInspectorTest.java b/src/test/java/kim/christopher/ClassInspectorTest.java index 7f90336..97cab5f 100644 --- a/src/test/java/kim/christopher/ClassInspectorTest.java +++ b/src/test/java/kim/christopher/ClassInspectorTest.java @@ -1,4 +1,57 @@ package kim.christopher; +import org.junit.Before; +import org.junit.Test; +import org.junit.Assert; + public class ClassInspectorTest { + + @Before + public void initialize() { + + } + + @Test + public void classImplementsInterfaceTest(){ + + //: Given + + //: When + + //: Then + + } + + @Test + public void listAllMembersTest(){ + + //: Given + + //: When + + //: Then + + } + + @Test + public void getClassHierarchyTest(){ + + //: Given + + //: When + + //: Then + + } + + @Test + public void instantiateClassHierarchyTest(){ + + //: Given + + //: When + + //: Then + + } } From 85796dfeab055a4bd0cf46d6550a87f7a1efb6a2 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 4 May 2017 14:34:09 -0400 Subject: [PATCH 04/14] wrote tests for and completed classImplementsInterface method --- .../java/kim/christopher/ClassInspector.java | 8 ++++-- .../kim/christopher/ClassInspectorTest.java | 27 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main/java/kim/christopher/ClassInspector.java b/src/main/java/kim/christopher/ClassInspector.java index 758b828..c45f7df 100644 --- a/src/main/java/kim/christopher/ClassInspector.java +++ b/src/main/java/kim/christopher/ClassInspector.java @@ -1,13 +1,18 @@ package kim.christopher; import java.util.ArrayList; +import java.lang.Class; public class ClassInspector { - //Take two arguments: 1) a class, speicfied by an object, Class object, or class name, and 2) an interface name. + //Take two arguments: 1) a class, specified by an object, Class object, or class name, and 2) an interface name. // Return true if the specified class implements the specified interface public boolean classImplementsInterface(Class c, String s){ + Class[] interfaceArray = c.getInterfaces(); + for(Class i: interfaceArray){ + if (i.getSimpleName().equals(s)) return true; + } return false; } @@ -17,7 +22,6 @@ public boolean classImplementsInterface(Class c, String s){ //Within each class, members should be listed in the order: Fields, Constructors, Methods //Each group of members (fields, constructors, or methods) should be listed alphabetically //The first line should start with the members declared by the given object type; the last line should be the last method defined by Object - public String listAllMembers(){ return ""; } diff --git a/src/test/java/kim/christopher/ClassInspectorTest.java b/src/test/java/kim/christopher/ClassInspectorTest.java index 97cab5f..53fc3cc 100644 --- a/src/test/java/kim/christopher/ClassInspectorTest.java +++ b/src/test/java/kim/christopher/ClassInspectorTest.java @@ -2,23 +2,44 @@ import org.junit.Before; import org.junit.Test; -import org.junit.Assert; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; public class ClassInspectorTest { + ClassInspector inspector; + Class c; + + @Before public void initialize() { - + inspector = new ClassInspector(); + c = ArrayList.class; } @Test public void classImplementsInterfaceTest(){ - //: Given + //: When + boolean result = inspector.classImplementsInterface(c, "List"); + + //: Then + assertEquals("ArrayList does implement List", true, result); + + } + + @Test + public void classImplementsInterfaceFailTest(){ //: When + boolean result = inspector.classImplementsInterface(c, "Type"); //: Then + assertEquals("ArrayList does not implement Type", false, result); } From 1ec60dd86acc7fe78922c08ed066577450fb0591 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 5 May 2017 07:54:28 -0400 Subject: [PATCH 05/14] Wrote listAllMembers and getClassHierarchy --- .../java/kim/christopher/ClassInspector.java | 79 +++++++++++++++++-- .../kim/christopher/ClassInspectorTest.java | 55 +++++++++++-- 2 files changed, 120 insertions(+), 14 deletions(-) diff --git a/src/main/java/kim/christopher/ClassInspector.java b/src/main/java/kim/christopher/ClassInspector.java index c45f7df..f53582c 100644 --- a/src/main/java/kim/christopher/ClassInspector.java +++ b/src/main/java/kim/christopher/ClassInspector.java @@ -1,5 +1,9 @@ package kim.christopher; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.lang.Class; @@ -9,7 +13,7 @@ public class ClassInspector { //Take two arguments: 1) a class, specified by an object, Class object, or class name, and 2) an interface name. // Return true if the specified class implements the specified interface public boolean classImplementsInterface(Class c, String s){ - Class[] interfaceArray = c.getInterfaces(); + Class[] interfaceArray = c.getInterfaces(); for(Class i: interfaceArray){ if (i.getSimpleName().equals(s)) return true; } @@ -22,15 +26,72 @@ public boolean classImplementsInterface(Class c, String s){ //Within each class, members should be listed in the order: Fields, Constructors, Methods //Each group of members (fields, constructors, or methods) should be listed alphabetically //The first line should start with the members declared by the given object type; the last line should be the last method defined by Object - public String listAllMembers(){ - return ""; + public String listAllMembers(Object object){ + System.out.println("Hello"); + StringBuilder sb = new StringBuilder(); + sb.append("Fields:\n" + listFields(object) + "\n"); + sb.append("Constructors\n" + listConstructors(object) + "\n"); + sb.append("Methods\n" + listMethods(object)); + return sb.toString(); } - //take an object and produce an indented class hierarchy with one class per line. + public String listFields(Object object){ + StringBuilder sb = new StringBuilder(); + Class classOfObject = object.getClass(); + Field[] fields = classOfObject.getFields(); + for(Field f: fields){ + sb.append(f.getDeclaringClass().getSimpleName()+ ": " + f.toString() + "\n"); + } + return sb.toString(); + } + + public String listConstructors(Object object){ + StringBuilder sb = new StringBuilder(); + Class classOfObject = object.getClass(); + Constructor[] constructors = classOfObject.getConstructors(); + for(Constructor c: constructors){ + sb.append(c.getDeclaringClass().getSimpleName() + ": " + c.toString() + "\n"); + } + return sb.toString(); + } + + public String listMethods(Object object){ + StringBuilder sb = new StringBuilder(); + Class classOfObject = object.getClass(); + Method[] methods = object.getClass().getMethods(); + for(Method m: methods){ + sb.append(m.getDeclaringClass().getSimpleName() + ": " + m.toString() + "\n"); + } + return sb.toString(); + } + + // Take an object and produce an indented class hierarchy with one class per line. // Each line should be indented two spaces more than the previous one. // The first line should be java.lang.Object public String getClassHierarchy(Object obj){ - return ""; + StringBuilder sb = new StringBuilder(); + ArrayList list = new ArrayList(); + Class classOfObject = obj.getClass(); + boolean flag = true; + list.add(classOfObject.getSimpleName()); + while(flag){ + list.add(classOfObject.getSuperclass().getSimpleName()); + classOfObject = classOfObject.getSuperclass(); + if(classOfObject.getSimpleName().equals("Object")) flag = false; + } + //counter for number of spaces to add at the beginning of the line + int count = 1; + + for(int i = list.size() - 1; i >= 0; i--){ + sb.append(list.get(i) + "\n"); + //loop that adds two spaces each time it runs + for(int j = 0; j < count; j++){ + sb.append(" "); + } + count++; + } + + return sb.toString(); } @@ -38,6 +99,12 @@ public String getClassHierarchy(Object obj){ // Handle classes without a no-argument constructor gracefully (your program should not crash, but may not be able to instantiate these classes). // Note: Your test should confirm that each list item is an instance of a different class. public ArrayList instantiateClassHierarchy(Object obj){ - return new ArrayList(); + +// Class c = obj.getClass(); +// boolean flag = true; +// ArrayList +// while(flag){ +// +// } } } diff --git a/src/test/java/kim/christopher/ClassInspectorTest.java b/src/test/java/kim/christopher/ClassInspectorTest.java index 53fc3cc..98ee4f6 100644 --- a/src/test/java/kim/christopher/ClassInspectorTest.java +++ b/src/test/java/kim/christopher/ClassInspectorTest.java @@ -5,19 +5,21 @@ import java.lang.reflect.Array; import java.util.ArrayList; -import java.util.List; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class ClassInspectorTest { ClassInspector inspector; + Integer i; Class c; @Before public void initialize() { inspector = new ClassInspector(); + i = new Integer(0); c = ArrayList.class; } @@ -26,9 +28,11 @@ public void classImplementsInterfaceTest(){ //: When boolean result = inspector.classImplementsInterface(c, "List"); + boolean result2 = inspector.classImplementsInterface(Integer.class, "Comparable"); //: Then assertEquals("ArrayList does implement List", true, result); + assertEquals("Integer does implement Comparable", true, result2); } @@ -46,33 +50,68 @@ public void classImplementsInterfaceFailTest(){ @Test public void listAllMembersTest(){ - //: Given - //: When + String result = inspector.listAllMembers(i); //: Then + assertTrue(result.contains("public static final int java.lang.Integer.MIN_VALUE")); + assertTrue(result.contains("public java.lang.Integer(int)")); + assertTrue(result.contains("public static int java.lang.Integer.remainderUnsigned(int,int)")); } @Test - public void getClassHierarchyTest(){ + public void listFieldsTest(){ - //: Given //: When + String result = inspector.listFields(i); //: Then + assertTrue(result.contains("public static final MAX_VALUE")); } @Test - public void instantiateClassHierarchyTest(){ + public void listConstructorsTest(){ - //: Given + //: When + String result = inspector.listConstructors(i); + + //: Then + assertEquals("Test", "Integer: public java.lang.Integer(int)\n" + + "Integer: public java.lang.Integer(java.lang.String) throws java.lang.NumberFormatException\n", result.toString()); + + } + + @Test + public void listMethodsTest(){ //: When + String result = inspector.listMethods(i); + + //: Then + assertTrue(result.contains("public float java.lang.Integer.floatValue()")); + + } + + @Test + public void getClassHierarchyTest(){ + //: When + String result = inspector.getClassHierarchy(i); //: Then + assertTrue(result.contains("Object") && result.contains("Number")); + + } + @Test + public void instantiateClassHierarchyTest(){ + + //: When + ArrayList result = inspector.instantiateClassHierarchy(i); = inspector.instantiateClassHierarchy(i); + + //: Then + assertTrue(!result.get(0).getClass().equals(result.get(1).getClass())); } } From 7737c3eff9b641df3cb6df2389c4d517e5a9ec46 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 5 May 2017 14:49:38 -0400 Subject: [PATCH 06/14] Finished part one, I think --- .../java/kim/christopher/ClassInspector.java | 31 ++++++++++++++----- .../kim/christopher/ClassInspectorTest.java | 9 ++++-- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/main/java/kim/christopher/ClassInspector.java b/src/main/java/kim/christopher/ClassInspector.java index f53582c..e7276c9 100644 --- a/src/main/java/kim/christopher/ClassInspector.java +++ b/src/main/java/kim/christopher/ClassInspector.java @@ -6,6 +6,8 @@ import java.lang.reflect.Modifier; import java.util.ArrayList; import java.lang.Class; +import java.util.Collections; +import java.util.List; public class ClassInspector { @@ -77,7 +79,8 @@ public String getClassHierarchy(Object obj){ while(flag){ list.add(classOfObject.getSuperclass().getSimpleName()); classOfObject = classOfObject.getSuperclass(); - if(classOfObject.getSimpleName().equals("Object")) flag = false; + if(classOfObject.getSimpleName().equals("Object")) + flag = false; } //counter for number of spaces to add at the beginning of the line int count = 1; @@ -100,11 +103,25 @@ public String getClassHierarchy(Object obj){ // Note: Your test should confirm that each list item is an instance of a different class. public ArrayList instantiateClassHierarchy(Object obj){ -// Class c = obj.getClass(); -// boolean flag = true; -// ArrayList -// while(flag){ -// -// } + Class c = obj.getClass(); + ArrayList list = new ArrayList(); + while (c != null) { + list.add(c); + c = c.getSuperclass(); + } + + Collections.reverse(list); + + ArrayList objList = new ArrayList(); + for(int i = 0; i < list.size(); i++){ + try{ + objList.add(list.get(i).newInstance()); + } catch (InstantiationException e){ + System.out.println(list.get(i).getSimpleName() + " did not have a no-args constructor and could not be instantiated"); + } catch (IllegalAccessException e){ + System.out.println(list.get(i).getSimpleName() + " was not able to be accessed"); + } + } + return objList; } } diff --git a/src/test/java/kim/christopher/ClassInspectorTest.java b/src/test/java/kim/christopher/ClassInspectorTest.java index 98ee4f6..fe2d79f 100644 --- a/src/test/java/kim/christopher/ClassInspectorTest.java +++ b/src/test/java/kim/christopher/ClassInspectorTest.java @@ -3,8 +3,10 @@ import org.junit.Before; import org.junit.Test; -import java.lang.reflect.Array; +import java.io.File; +import java.io.FileWriter; import java.util.ArrayList; +import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -14,13 +16,14 @@ public class ClassInspectorTest { ClassInspector inspector; Integer i; Class c; - + ArrayList s; @Before public void initialize() { inspector = new ClassInspector(); i = new Integer(0); c = ArrayList.class; + s = new ArrayList(); } @Test @@ -109,7 +112,7 @@ public void getClassHierarchyTest(){ public void instantiateClassHierarchyTest(){ //: When - ArrayList result = inspector.instantiateClassHierarchy(i); = inspector.instantiateClassHierarchy(i); + List result = inspector.instantiateClassHierarchy(s); //: Then assertTrue(!result.get(0).getClass().equals(result.get(1).getClass())); From b48fcf45ab36e8fdb5a8eb56fe8f782766f187e9 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 5 May 2017 15:00:01 -0400 Subject: [PATCH 07/14] Procrastinating before starting UnitCorn --- src/main/java/kim/christopher/App.java | 5 +++++ src/main/java/kim/christopher/ClassInspector.java | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 src/main/java/kim/christopher/App.java diff --git a/src/main/java/kim/christopher/App.java b/src/main/java/kim/christopher/App.java new file mode 100644 index 0000000..9f0df38 --- /dev/null +++ b/src/main/java/kim/christopher/App.java @@ -0,0 +1,5 @@ +package kim.christopher; + +public class App { + +} diff --git a/src/main/java/kim/christopher/ClassInspector.java b/src/main/java/kim/christopher/ClassInspector.java index e7276c9..aefb320 100644 --- a/src/main/java/kim/christopher/ClassInspector.java +++ b/src/main/java/kim/christopher/ClassInspector.java @@ -117,7 +117,7 @@ public ArrayList instantiateClassHierarchy(Object obj){ try{ objList.add(list.get(i).newInstance()); } catch (InstantiationException e){ - System.out.println(list.get(i).getSimpleName() + " did not have a no-args constructor and could not be instantiated"); + System.out.println(list.get(i).getSimpleName() + " could not be instantiated"); } catch (IllegalAccessException e){ System.out.println(list.get(i).getSimpleName() + " was not able to be accessed"); } From dcf3787751cba2a9efe285f9ab8f7b38e05526b8 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 5 May 2017 15:22:54 -0400 Subject: [PATCH 08/14] Added stubs for UnitCorn and its test class --- src/main/java/kim/christopher/App.java | 5 ----- src/main/java/kim/christopher/partOne/App.java | 5 +++++ .../java/kim/christopher/{ => partOne}/ClassInspector.java | 2 +- .../java/kim/christopher/unitcorn/UnitCornTestRunner.java | 5 +++++ .../kim/christopher/{ => partOne}/ClassInspectorTest.java | 5 ++--- .../kim/christopher/unitcorn/UnitCornTestRunnerTester.java | 5 +++++ 6 files changed, 18 insertions(+), 9 deletions(-) delete mode 100644 src/main/java/kim/christopher/App.java create mode 100644 src/main/java/kim/christopher/partOne/App.java rename src/main/java/kim/christopher/{ => partOne}/ClassInspector.java (99%) create mode 100644 src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java rename src/test/java/kim/christopher/{ => partOne}/ClassInspectorTest.java (97%) create mode 100644 src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java diff --git a/src/main/java/kim/christopher/App.java b/src/main/java/kim/christopher/App.java deleted file mode 100644 index 9f0df38..0000000 --- a/src/main/java/kim/christopher/App.java +++ /dev/null @@ -1,5 +0,0 @@ -package kim.christopher; - -public class App { - -} diff --git a/src/main/java/kim/christopher/partOne/App.java b/src/main/java/kim/christopher/partOne/App.java new file mode 100644 index 0000000..37fb3d1 --- /dev/null +++ b/src/main/java/kim/christopher/partOne/App.java @@ -0,0 +1,5 @@ +package kim.christopher.partOne; + +public class App { + +} diff --git a/src/main/java/kim/christopher/ClassInspector.java b/src/main/java/kim/christopher/partOne/ClassInspector.java similarity index 99% rename from src/main/java/kim/christopher/ClassInspector.java rename to src/main/java/kim/christopher/partOne/ClassInspector.java index aefb320..fc8a5a0 100644 --- a/src/main/java/kim/christopher/ClassInspector.java +++ b/src/main/java/kim/christopher/partOne/ClassInspector.java @@ -1,4 +1,4 @@ -package kim.christopher; +package kim.christopher.partOne; import java.lang.reflect.Constructor; import java.lang.reflect.Field; diff --git a/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java new file mode 100644 index 0000000..4f696b5 --- /dev/null +++ b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java @@ -0,0 +1,5 @@ +package kim.christopher.unitcorn; + +public class UnitCornTestRunner { + +} diff --git a/src/test/java/kim/christopher/ClassInspectorTest.java b/src/test/java/kim/christopher/partOne/ClassInspectorTest.java similarity index 97% rename from src/test/java/kim/christopher/ClassInspectorTest.java rename to src/test/java/kim/christopher/partOne/ClassInspectorTest.java index fe2d79f..0840f25 100644 --- a/src/test/java/kim/christopher/ClassInspectorTest.java +++ b/src/test/java/kim/christopher/partOne/ClassInspectorTest.java @@ -1,10 +1,9 @@ -package kim.christopher; +package kim.christopher.partOne; +import kim.christopher.partOne.ClassInspector; import org.junit.Before; import org.junit.Test; -import java.io.File; -import java.io.FileWriter; import java.util.ArrayList; import java.util.List; diff --git a/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java b/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java new file mode 100644 index 0000000..e008e26 --- /dev/null +++ b/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java @@ -0,0 +1,5 @@ +package kim.christopher.unitcorn; + +public class UnitCornTestRunnerTester { + +} From 31b0c0902a7cb39d059b0ab9c4ce9c98fe2c5b72 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 5 May 2017 16:43:15 -0400 Subject: [PATCH 09/14] Created mini-method stub and wrote test for getMethod(Class, String) --- pom.xml | 5 ++ .../java/kim/christopher/partOne/App.java | 10 ++++ .../unitcorn/UnitCornTestRunner.java | 16 ++++++ .../unitcorn/UnitCornTestRunnerTester.java | 53 +++++++++++++++++++ 4 files changed, 84 insertions(+) diff --git a/pom.xml b/pom.xml index c03b83f..397a5ff 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,11 @@ 4.12 test + + junit + junit + RELEASE + diff --git a/src/main/java/kim/christopher/partOne/App.java b/src/main/java/kim/christopher/partOne/App.java index 37fb3d1..523f80e 100644 --- a/src/main/java/kim/christopher/partOne/App.java +++ b/src/main/java/kim/christopher/partOne/App.java @@ -1,5 +1,15 @@ package kim.christopher.partOne; +import java.lang.reflect.Method; + public class App { + public static void main(String[] args) { + Method[] methodArray = Double.class.getMethods(); + for(Method m: methodArray){ + System.out.println(m.toString()); + } + } + + } diff --git a/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java index 4f696b5..be8d5aa 100644 --- a/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java +++ b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java @@ -1,5 +1,21 @@ package kim.christopher.unitcorn; +import org.junit.runner.Result; + +import java.lang.reflect.Method; + public class UnitCornTestRunner { + public Result runTest(Class c, String methodName){ + return new Result(); + } + + public Method getMethod(Class c, String methodName){ + return Double.class.getMethods()[1]; + } + + public String runTests(Class c){ + return ""; + } + } diff --git a/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java b/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java index e008e26..9bcf339 100644 --- a/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java +++ b/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java @@ -1,5 +1,58 @@ package kim.christopher.unitcorn; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; + public class UnitCornTestRunnerTester { + Class c; + String methodName; + UnitCornTestRunner runner; + + @Before + public void initialize() { + c = Double.class; + methodName = "isNaN"; + runner = new UnitCornTestRunner(); + } + + @Test + public void getMethodTest(){ + + //: When + Method result = runner.getMethod(c, methodName); + String expectedValue = "isNaN"; + String actualValue = result.getName(); + + //: Then + assertEquals(expectedValue, actualValue); + + } + + @Test + public void runTestTest(){ + + //: Given + + //: When + + //: Then + + } + + @Test + public void runTestsTest(){ + + //: Given + + //: When + + //: Then + + } } From b60119a265c96e72b11c908e4f999835d354ab73 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 5 May 2017 16:59:43 -0400 Subject: [PATCH 10/14] Finished getMethod(Class, String) --- .../java/kim/christopher/unitcorn/UnitCornTestRunner.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java index be8d5aa..e1a3a47 100644 --- a/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java +++ b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java @@ -10,8 +10,14 @@ public Result runTest(Class c, String methodName){ return new Result(); } + //used in runTest public Method getMethod(Class c, String methodName){ - return Double.class.getMethods()[1]; + Method[] methods = c.getMethods(); + for(Method method: methods){ + if(methodName.toString().equals(method.getName())) + return method; + } + return null; } public String runTests(Class c){ From 3e6b247fb242a77196c715c288df65557defceb9 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 5 May 2017 17:14:20 -0400 Subject: [PATCH 11/14] Added a method to instantiate objects --- .../unitcorn/UnitCornTestRunner.java | 12 +++++++++ .../unitcorn/UnitCornTestRunnerTester.java | 26 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java index e1a3a47..b31106f 100644 --- a/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java +++ b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java @@ -20,6 +20,18 @@ public Method getMethod(Class c, String methodName){ return null; } + public Object instantiateClass(Class c){ + try { + Object object = c.newInstance(); + return object; + } catch (InstantiationException e) { + System.out.println(c.getSimpleName() + ": could not instantiate!"); + } catch (IllegalAccessException e) { + System.out.println(e); + } + return null; + } + public String runTests(Class c){ return ""; } diff --git a/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java b/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java index 9bcf339..602f2be 100644 --- a/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java +++ b/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java @@ -34,6 +34,32 @@ public void getMethodTest(){ } + @Test + public void InstantiateClassTest(){ + + //: Given + c = String.class; + + //: When + Object obj = runner.instantiateClass(c); + String expectedValue = "String"; + String actualValue = obj.getClass().getSimpleName(); + + //: Then + assertEquals(expectedValue, actualValue); + + } + + @Test + public void InstantiateClassFailTest(){ + + //: When + Object expectedObject = runner.instantiateClass(c); + + //: Then + assertNull(expectedObject); + } + @Test public void runTestTest(){ From e4a52a0968597a03b62deaa53330c205a813247c Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 7 May 2017 18:01:22 -0400 Subject: [PATCH 12/14] Started working on Result object --- .../java/kim/christopher/unitcorn/Result.java | 30 +++++++++++++++++++ .../unitcorn/UnitCornTestRunner.java | 1 + 2 files changed, 31 insertions(+) create mode 100644 src/main/java/kim/christopher/unitcorn/Result.java diff --git a/src/main/java/kim/christopher/unitcorn/Result.java b/src/main/java/kim/christopher/unitcorn/Result.java new file mode 100644 index 0000000..df81256 --- /dev/null +++ b/src/main/java/kim/christopher/unitcorn/Result.java @@ -0,0 +1,30 @@ +package kim.christopher.unitcorn; + +public class Result { + + boolean wasSuccessful; + String message; + + + public Result(){ + wasSuccessful = false; + message = ""; + } + + public Result(boolean success, String message){ + wasSuccessful = success; + this.message = message; + } + + public void setWasSuccessful(boolean wasSuccessful){ + this.wasSuccessful = wasSuccessful; + } + + public boolean isWasSuccessful() { + return wasSuccessful; + } + + public String getMessage() { + return message; + } +} diff --git a/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java index b31106f..1d305db 100644 --- a/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java +++ b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java @@ -7,6 +7,7 @@ public class UnitCornTestRunner { public Result runTest(Class c, String methodName){ + return new Result(); } From 408d191e206db14a95a457ec465aa02770b96634 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 7 May 2017 20:02:48 -0400 Subject: [PATCH 13/14] Added first version of RunTest tests --- .../java/kim/christopher/unitcorn/Result.java | 13 ++++----- .../unitcorn/UnitCornTestRunner.java | 10 +++---- .../unitcorn/UnitCornTestRunnerTester.java | 27 ++++++++++++++----- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/main/java/kim/christopher/unitcorn/Result.java b/src/main/java/kim/christopher/unitcorn/Result.java index df81256..3b31a86 100644 --- a/src/main/java/kim/christopher/unitcorn/Result.java +++ b/src/main/java/kim/christopher/unitcorn/Result.java @@ -2,26 +2,23 @@ public class Result { - boolean wasSuccessful; + boolean successful; String message; public Result(){ - wasSuccessful = false; + successful = false; message = ""; } public Result(boolean success, String message){ - wasSuccessful = success; + this.successful = success; this.message = message; } - public void setWasSuccessful(boolean wasSuccessful){ - this.wasSuccessful = wasSuccessful; - } - public boolean isWasSuccessful() { - return wasSuccessful; + public boolean isSuccessful() { + return successful; } public String getMessage() { diff --git a/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java index 1d305db..dc64ff9 100644 --- a/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java +++ b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java @@ -1,13 +1,13 @@ package kim.christopher.unitcorn; -import org.junit.runner.Result; - import java.lang.reflect.Method; public class UnitCornTestRunner { public Result runTest(Class c, String methodName){ - + Method m = getMethod(c, methodName); + Object o = instantiateClass(c); + Result result = new Result(); return new Result(); } @@ -23,8 +23,8 @@ public Method getMethod(Class c, String methodName){ public Object instantiateClass(Class c){ try { - Object object = c.newInstance(); - return object; + Object obj = c.newInstance(); + return obj; } catch (InstantiationException e) { System.out.println(c.getSimpleName() + ": could not instantiate!"); } catch (IllegalAccessException e) { diff --git a/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java b/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java index 602f2be..464a0ce 100644 --- a/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java +++ b/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java @@ -5,8 +5,6 @@ import static org.junit.Assert.*; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Arrays; public class UnitCornTestRunnerTester { @@ -35,7 +33,7 @@ public void getMethodTest(){ } @Test - public void InstantiateClassTest(){ + public void instantiateClassTest(){ //: Given c = String.class; @@ -51,7 +49,7 @@ public void InstantiateClassTest(){ } @Test - public void InstantiateClassFailTest(){ + public void instantiateClassFailTest(){ //: When Object expectedObject = runner.instantiateClass(c); @@ -61,14 +59,29 @@ public void InstantiateClassFailTest(){ } @Test - public void runTestTest(){ + public void runTestPassTest(){ + //: When + Result result = runner.runTest(UnitCornTestRunnerTester.class, "public void UnitCornTestRunnerTester.instantiateClassTest()"); + boolean actualPass = result.isSuccessful(); + String actualMessage = result.getMessage(); + String expectedMessage = "PASS"; - //: Given + //: Then + assertTrue(actualPass); + assertEquals(expectedMessage, actualMessage); + } + @Test + public void runTestFailTest(){ //: When + Result result = runner.runTest(UnitCornTestRunnerTester.class, "public void UnitCornTestRunnerTester.instantiateClassFailTest()"); + boolean actualPass = result.isSuccessful(); + String actualMessage = result.getMessage(); + String expectedMessage = "FAIL"; //: Then - + assertTrue(!actualPass); + assertEquals(expectedMessage, actualMessage); } @Test From 4eef4dd5acd69591e895ed4017f374c4146c3924 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 7 May 2017 20:21:49 -0400 Subject: [PATCH 14/14] attempted to write runTest, but wasn't sure how to put it all together --- .../java/kim/christopher/unitcorn/Result.java | 12 ++++++++++-- .../unitcorn/UnitCornTestRunner.java | 17 +++++++++++++++-- .../java/kim/christopher/unitcorn/TestTest.java | 15 +++++++++++++++ .../unitcorn/UnitCornTestRunnerTester.java | 2 +- 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 src/test/java/kim/christopher/unitcorn/TestTest.java diff --git a/src/main/java/kim/christopher/unitcorn/Result.java b/src/main/java/kim/christopher/unitcorn/Result.java index 3b31a86..de7f364 100644 --- a/src/main/java/kim/christopher/unitcorn/Result.java +++ b/src/main/java/kim/christopher/unitcorn/Result.java @@ -2,8 +2,8 @@ public class Result { - boolean successful; - String message; + private boolean successful; + private String message; public Result(){ @@ -21,7 +21,15 @@ public boolean isSuccessful() { return successful; } + public void setSuccessful(boolean b){ + this.successful = b; + } + public String getMessage() { return message; } + + public void setMessage(String message) { + this.message = message; + } } diff --git a/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java index dc64ff9..6001f31 100644 --- a/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java +++ b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java @@ -1,5 +1,6 @@ package kim.christopher.unitcorn; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class UnitCornTestRunner { @@ -7,8 +8,20 @@ public class UnitCornTestRunner { public Result runTest(Class c, String methodName){ Method m = getMethod(c, methodName); Object o = instantiateClass(c); - Result result = new Result(); - return new Result(); + Result r = new Result(); + try{ + Object returnedObject = m.invoke(o); + r.setSuccessful(true); + r.setMessage(returnedObject.getClass().getName()); + } catch (IllegalAccessException e){ + r.setSuccessful(false); + r.setMessage(e.getMessage()); + } catch (InvocationTargetException f){ + r.setSuccessful(false); + r.setMessage(f.getMessage()); + } + + return r; } //used in runTest diff --git a/src/test/java/kim/christopher/unitcorn/TestTest.java b/src/test/java/kim/christopher/unitcorn/TestTest.java new file mode 100644 index 0000000..ea33fd7 --- /dev/null +++ b/src/test/java/kim/christopher/unitcorn/TestTest.java @@ -0,0 +1,15 @@ +package kim.christopher.unitcorn; + +public class TestTest { + + @Test + public void Test(){ + + //: Given + + //: When + + //: Then + + } +} diff --git a/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java b/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java index 464a0ce..a5750ab 100644 --- a/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java +++ b/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java @@ -61,7 +61,7 @@ public void instantiateClassFailTest(){ @Test public void runTestPassTest(){ //: When - Result result = runner.runTest(UnitCornTestRunnerTester.class, "public void UnitCornTestRunnerTester.instantiateClassTest()"); + Result result = runner.runTest(UnitCornTestRunnerTester.class, ""); boolean actualPass = result.isSuccessful(); String actualMessage = result.getMessage(); String expectedMessage = "PASS";