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..397a5ff --- /dev/null +++ b/pom.xml @@ -0,0 +1,31 @@ + + 4.0.0 + + kim.christopher + UnitCornTestRunner + 1.0-SNAPSHOT + jar + + UnitCornTestRunner + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 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 new file mode 100644 index 0000000..523f80e --- /dev/null +++ b/src/main/java/kim/christopher/partOne/App.java @@ -0,0 +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/partOne/ClassInspector.java b/src/main/java/kim/christopher/partOne/ClassInspector.java new file mode 100644 index 0000000..fc8a5a0 --- /dev/null +++ b/src/main/java/kim/christopher/partOne/ClassInspector.java @@ -0,0 +1,127 @@ +package kim.christopher.partOne; + +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; +import java.util.Collections; +import java.util.List; + +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(); + for(Class i: interfaceArray){ + if (i.getSimpleName().equals(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(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(); + } + + 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){ + 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(); + } + + + //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){ + + 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() + " 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/main/java/kim/christopher/unitcorn/Result.java b/src/main/java/kim/christopher/unitcorn/Result.java new file mode 100644 index 0000000..de7f364 --- /dev/null +++ b/src/main/java/kim/christopher/unitcorn/Result.java @@ -0,0 +1,35 @@ +package kim.christopher.unitcorn; + +public class Result { + + private boolean successful; + private String message; + + + public Result(){ + successful = false; + message = ""; + } + + public Result(boolean success, String message){ + this.successful = success; + this.message = message; + } + + + 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 new file mode 100644 index 0000000..6001f31 --- /dev/null +++ b/src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java @@ -0,0 +1,53 @@ +package kim.christopher.unitcorn; + +import java.lang.reflect.InvocationTargetException; +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 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 + public Method getMethod(Class c, String methodName){ + Method[] methods = c.getMethods(); + for(Method method: methods){ + if(methodName.toString().equals(method.getName())) + return method; + } + return null; + } + + public Object instantiateClass(Class c){ + try { + Object obj = c.newInstance(); + return obj; + } 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/partOne/ClassInspectorTest.java b/src/test/java/kim/christopher/partOne/ClassInspectorTest.java new file mode 100644 index 0000000..0840f25 --- /dev/null +++ b/src/test/java/kim/christopher/partOne/ClassInspectorTest.java @@ -0,0 +1,119 @@ +package kim.christopher.partOne; + +import kim.christopher.partOne.ClassInspector; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +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 + 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); + + } + + @Test + public void classImplementsInterfaceFailTest(){ + + //: When + boolean result = inspector.classImplementsInterface(c, "Type"); + + //: Then + assertEquals("ArrayList does not implement Type", false, result); + + } + + @Test + public void listAllMembersTest(){ + + //: 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 listFieldsTest(){ + + + //: When + String result = inspector.listFields(i); + + //: Then + assertTrue(result.contains("public static final MAX_VALUE")); + + } + + @Test + public void listConstructorsTest(){ + + //: 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 + List result = inspector.instantiateClassHierarchy(s); + + //: Then + assertTrue(!result.get(0).getClass().equals(result.get(1).getClass())); + } +} 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 new file mode 100644 index 0000000..a5750ab --- /dev/null +++ b/src/test/java/kim/christopher/unitcorn/UnitCornTestRunnerTester.java @@ -0,0 +1,97 @@ +package kim.christopher.unitcorn; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +import java.lang.reflect.Method; + +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 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 runTestPassTest(){ + //: When + Result result = runner.runTest(UnitCornTestRunnerTester.class, ""); + boolean actualPass = result.isSuccessful(); + String actualMessage = result.getMessage(); + String expectedMessage = "PASS"; + + //: 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 + public void runTestsTest(){ + + //: Given + + //: When + + //: Then + + } +}