From b8fd9344d95d9386c18f6a3fd46dadb0120b1e71 Mon Sep 17 00:00:00 2001
From: onlyliuxin <14703250@qq.com>
Date: Sun, 13 Aug 2017 21:57:17 +0800
Subject: [PATCH] lite-junit v1
---
.../main/java/org/litejunit/v1/Assert.java | 225 ++++++++++++++++++
.../litejunit/v1/AssertionFailedError.java | 13 +
.../java/org/litejunit/v1/Calculator.java | 22 ++
.../java/org/litejunit/v1/CalculatorTest.java | 65 +++++
.../src/main/java/org/litejunit/v1/Test.java | 6 +
.../main/java/org/litejunit/v1/TestCase.java | 63 +++++
.../java/org/litejunit/v1/TestFailure.java | 39 +++
.../java/org/litejunit/v1/TestResult.java | 95 ++++++++
.../main/java/org/litejunit/v1/TestSuite.java | 130 ++++++++++
9 files changed, 658 insertions(+)
create mode 100644 liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/Assert.java
create mode 100644 liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/AssertionFailedError.java
create mode 100644 liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/Calculator.java
create mode 100644 liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/CalculatorTest.java
create mode 100644 liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/Test.java
create mode 100644 liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestCase.java
create mode 100644 liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestFailure.java
create mode 100644 liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestResult.java
create mode 100644 liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestSuite.java
diff --git a/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/Assert.java b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/Assert.java
new file mode 100644
index 0000000000..07a3c1a22b
--- /dev/null
+++ b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/Assert.java
@@ -0,0 +1,225 @@
+package org.litejunit.v1;
+
+/**
+ * A set of assert methods.
+ */
+
+public class Assert {
+ /**
+ * Protect constructor since it is a static only class
+ */
+ protected Assert() {
+ }
+
+ /**
+ * Asserts that a condition is true. If it isn't it throws
+ * an AssertionFailedError with the given message.
+ */
+ static public void assertTrue(String message, boolean condition) {
+ if (!condition)
+ fail(message);
+ }
+ /**
+ * Asserts that a condition is true. If it isn't it throws
+ * an AssertionFailedError.
+ */
+ static public void assertTrue(boolean condition) {
+ assertTrue(null, condition);
+ }
+ /**
+ * Fails a test with the given message.
+ */
+ static public void fail(String message) {
+ throw new AssertionFailedError(message);
+ }
+ /**
+ * Fails a test with no message.
+ */
+ static public void fail() {
+ fail(null);
+ }
+ /**
+ * Asserts that two objects are equal. If they are not
+ * an AssertionFailedError is thrown.
+ */
+ static public void assertEquals(String message, Object expected, Object actual) {
+ if (expected == null && actual == null)
+ return;
+ if (expected != null && expected.equals(actual))
+ return;
+ failNotEquals(message, expected, actual);
+ }
+ /**
+ * Asserts that two objects are equal. If they are not
+ * an AssertionFailedError is thrown.
+ */
+ static public void assertEquals(Object expected, Object actual) {
+ assertEquals(null, expected, actual);
+ }
+ /**
+ * Asserts that two doubles are equal concerning a delta. If the expected
+ * value is infinity then the delta value is ignored.
+ */
+ static public void assertEquals(String message, double expected, double actual, double delta) {
+ // handle infinity specially since subtracting to infinite values gives NaN and the
+ // the following test fails
+ if (Double.isInfinite(expected)) {
+ if (!(expected == actual))
+ failNotEquals(message, new Double(expected), new Double(actual));
+ } else if (!(Math.abs(expected-actual) <= delta)) // Because comparison with NaN always returns false
+ failNotEquals(message, new Double(expected), new Double(actual));
+ }
+ /**
+ * Asserts that two doubles are equal concerning a delta. If the expected
+ * value is infinity then the delta value is ignored.
+ */
+ static public void assertEquals(double expected, double actual, double delta) {
+ assertEquals(null, expected, actual, delta);
+ }
+ /**
+ * Asserts that two floats are equal concerning a delta. If the expected
+ * value is infinity then the delta value is ignored.
+ */
+ static public void assertEquals(String message, float expected, float actual, float delta) {
+ // handle infinity specially since subtracting to infinite values gives NaN and the
+ // the following test fails
+ if (Float.isInfinite(expected)) {
+ if (!(expected == actual))
+ failNotEquals(message, new Float(expected), new Float(actual));
+ } else if (!(Math.abs(expected-actual) <= delta))
+ failNotEquals(message, new Float(expected), new Float(actual));
+ }
+ /**
+ * Asserts that two floats are equal concerning a delta. If the expected
+ * value is infinity then the delta value is ignored.
+ */
+ static public void assertEquals(float expected, float actual, float delta) {
+ assertEquals(null, expected, actual, delta);
+ }
+ /**
+ * Asserts that two longs are equal.
+ */
+ static public void assertEquals(String message, long expected, long actual) {
+ assertEquals(message, new Long(expected), new Long(actual));
+ }
+ /**
+ * Asserts that two longs are equal.
+ */
+ static public void assertEquals(long expected, long actual) {
+ assertEquals(null, expected, actual);
+ }
+ /**
+ * Asserts that two booleans are equal.
+ */
+ static public void assertEquals(String message, boolean expected, boolean actual) {
+ assertEquals(message, new Boolean(expected), new Boolean(actual));
+ }
+ /**
+ * Asserts that two booleans are equal.
+ */
+ static public void assertEquals(boolean expected, boolean actual) {
+ assertEquals(null, expected, actual);
+ }
+ /**
+ * Asserts that two bytes are equal.
+ */
+ static public void assertEquals(String message, byte expected, byte actual) {
+ assertEquals(message, new Byte(expected), new Byte(actual));
+ }
+ /**
+ * Asserts that two bytes are equal.
+ */
+ static public void assertEquals(byte expected, byte actual) {
+ assertEquals(null, expected, actual);
+ }
+ /**
+ * Asserts that two chars are equal.
+ */
+ static public void assertEquals(String message, char expected, char actual) {
+ assertEquals(message, new Character(expected), new Character(actual));
+ }
+ /**
+ * Asserts that two chars are equal.
+ */
+ static public void assertEquals(char expected, char actual) {
+ assertEquals(null, expected, actual);
+ }
+ /**
+ * Asserts that two shorts are equal.
+ */
+ static public void assertEquals(String message, short expected, short actual) {
+ assertEquals(message, new Short(expected), new Short(actual));
+ }
+ /**
+ * Asserts that two shorts are equal.
+ */
+ static public void assertEquals(short expected, short actual) {
+ assertEquals(null, expected, actual);
+ }
+ /**
+ * Asserts that two ints are equal.
+ */
+ static public void assertEquals(String message, int expected, int actual) {
+ assertEquals(message, new Integer(expected), new Integer(actual));
+ }
+ /**
+ * Asserts that two ints are equal.
+ */
+ static public void assertEquals(int expected, int actual) {
+ assertEquals(null, expected, actual);
+ }
+ /**
+ * Asserts that an object isn't null.
+ */
+ static public void assertNotNull(Object object) {
+ assertNotNull(null, object);
+ }
+ /**
+ * Asserts that an object isn't null.
+ */
+ static public void assertNotNull(String message, Object object) {
+ assertTrue(message, object != null);
+ }
+ /**
+ * Asserts that an object is null.
+ */
+ static public void assertNull(Object object) {
+ assertNull(null, object);
+ }
+ /**
+ * Asserts that an object is null.
+ */
+ static public void assertNull(String message, Object object) {
+ assertTrue(message, object == null);
+ }
+ /**
+ * Asserts that two objects refer to the same object. If they are not
+ * an AssertionFailedError is thrown.
+ */
+ static public void assertSame(String message, Object expected, Object actual) {
+ if (expected == actual)
+ return;
+ failNotSame(message, expected, actual);
+ }
+ /**
+ * Asserts that two objects refer to the same object. If they are not
+ * the same an AssertionFailedError is thrown.
+ */
+ static public void assertSame(Object expected, Object actual) {
+ assertSame(null, expected, actual);
+ }
+
+ static private void failNotEquals(String message, Object expected, Object actual) {
+ String formatted= "";
+ if (message != null)
+ formatted= message+" ";
+ fail(formatted+"expected:<"+expected+"> but was:<"+actual+">");
+ }
+
+ static private void failNotSame(String message, Object expected, Object actual) {
+ String formatted= "";
+ if (message != null)
+ formatted= message+" ";
+ fail(formatted+"expected same");
+ }
+}
\ No newline at end of file
diff --git a/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/AssertionFailedError.java b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/AssertionFailedError.java
new file mode 100644
index 0000000000..05e786ea47
--- /dev/null
+++ b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/AssertionFailedError.java
@@ -0,0 +1,13 @@
+package org.litejunit.v1;
+
+/**
+ * Thrown when an assertion failed.
+ */
+public class AssertionFailedError extends Error {
+
+ public AssertionFailedError () {
+ }
+ public AssertionFailedError (String message) {
+ super (message);
+ }
+}
\ No newline at end of file
diff --git a/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/Calculator.java b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/Calculator.java
new file mode 100644
index 0000000000..f1245ff3a6
--- /dev/null
+++ b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/Calculator.java
@@ -0,0 +1,22 @@
+package org.litejunit.v1;
+
+public class Calculator {
+
+ private int result = 0;
+ public void add(int x){
+ result += x;
+ }
+ public void subtract(int x){
+ result -=x;
+ }
+
+ public int getResult(){
+ return this.result;
+ }
+ public static void main(String[] args){
+ Calculator calculator = new Calculator();
+ calculator.add(10);
+ calculator.subtract(5);
+ System.out.println(calculator.getResult());
+ }
+}
diff --git a/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/CalculatorTest.java b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/CalculatorTest.java
new file mode 100644
index 0000000000..3460528043
--- /dev/null
+++ b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/CalculatorTest.java
@@ -0,0 +1,65 @@
+package org.litejunit.v1;
+
+
+public class CalculatorTest extends TestCase {
+ public CalculatorTest(String name) {
+ super(name);
+
+ }
+ Calculator calculator =null;
+ public void setUp(){
+ calculator = new Calculator();
+ }
+ public void tearDown(){
+ calculator = null;
+ }
+ public void testAdd(){
+
+ calculator.add(10);
+ assertEquals(10,calculator.getResult());
+ }
+ public void testSubtract(){
+ calculator.add(10);
+ calculator.subtract(5);
+ assertEquals(4,calculator.getResult());
+ }
+
+ public static void main(String[] args){
+ TestSuite ts = new TestSuite(CalculatorTest.class);
+ TestResult tr = new TestResult();
+ ts.run(tr);
+ System.out.println(tr.wasSuccessful());
+ for(TestFailure failure : tr.failures){
+ System.err.println(failure);
+ }
+
+ /*{
+ TestCase tc1 = new CalculatorTest("testAdd"){
+ protected void runTest() {
+ testAdd();
+ }
+ };
+
+ TestCase tc2 = new CalculatorTest("testSubtract"){
+ protected void runTest() {
+ testSubtract();
+ }
+ };
+ tc1.run();
+ tc2.run();
+ }
+
+
+ TestSuite ts = new TestSuite();
+ ts.addTest(new CalculatorTest("testAdd"));
+ ts.addTest(new CalculatorTest("testSubtract"));
+
+
+ {
+ TestCase tc1 = new CalculatorTest("test1");
+ TestCase tc2 = new CalculatorTest("test2");
+ tc1.run();
+ tc2.run();
+ }*/
+ }
+}
diff --git a/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/Test.java b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/Test.java
new file mode 100644
index 0000000000..3d870cf637
--- /dev/null
+++ b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/Test.java
@@ -0,0 +1,6 @@
+package org.litejunit.v1;
+
+public interface Test {
+ public abstract int countTestCases();
+ public void run(TestResult tr);
+}
diff --git a/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestCase.java b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestCase.java
new file mode 100644
index 0000000000..d9cebd263b
--- /dev/null
+++ b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestCase.java
@@ -0,0 +1,63 @@
+package org.litejunit.v1;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+
+
+public abstract class TestCase extends Assert implements Test {
+ private String name;
+
+
+ public TestCase(String name) {
+ this.name = name;
+ }
+
+ public int countTestCases() {
+ return 1;
+ }
+
+ protected void runTest() throws Throwable{
+ Method runMethod= null;
+ try {
+ runMethod= getClass().getMethod(name, null);
+ } catch (NoSuchMethodException e) {
+ fail("Method \""+name+"\" not found");
+ }
+ if (!Modifier.isPublic(runMethod.getModifiers())) {
+ fail("Method \""+name+"\" should be public");
+ }
+
+ try {
+ runMethod.invoke(this, new Class[0]);
+ }
+ catch (InvocationTargetException e) {
+ e.fillInStackTrace();
+ throw e.getTargetException();
+ }
+ catch (IllegalAccessException e) {
+ e.fillInStackTrace();
+ throw e;
+ }
+ }
+
+ protected void setUp() {
+ }
+
+ protected void tearDown() {
+ }
+
+ public void run(TestResult tr) {
+ tr.run(this);
+ }
+ public void doRun() throws Throwable{
+ setUp();
+ try{
+ runTest();
+ }
+ finally{
+ tearDown();
+ }
+ }
+}
diff --git a/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestFailure.java b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestFailure.java
new file mode 100644
index 0000000000..1ac44256a4
--- /dev/null
+++ b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestFailure.java
@@ -0,0 +1,39 @@
+package org.litejunit.v1;
+
+/**
+ * A TestFailure
collects a failed test together with
+ * the caught exception.
+ * @see TestResult
+ */
+public class TestFailure {
+ protected Test failedTest;
+ protected Throwable thrownException;
+
+ /**
+ * Constructs a TestFailure with the given test and exception.
+ */
+ public TestFailure(Test failedTest, Throwable thrownException) {
+ this.failedTest= failedTest;
+ this.thrownException= thrownException;
+ }
+ /**
+ * Gets the failed test.
+ */
+ public Test failedTest() {
+ return failedTest;
+ }
+ /**
+ * Gets the thrown exception.
+ */
+ public Throwable thrownException() {
+ return thrownException;
+ }
+ /**
+ * Returns a short description of the failure.
+ */
+ public String toString() {
+ StringBuffer buffer= new StringBuffer();
+ buffer.append(failedTest+": "+thrownException.getMessage());
+ return buffer.toString();
+ }
+}
\ No newline at end of file
diff --git a/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestResult.java b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestResult.java
new file mode 100644
index 0000000000..a9e8c2b439
--- /dev/null
+++ b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestResult.java
@@ -0,0 +1,95 @@
+package org.litejunit.v1;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Vector;
+
+
+
+
+public class TestResult extends Object {
+ protected List failures;
+ protected List errors;
+
+ protected int testCount;
+ private boolean stop;
+
+ public TestResult() {
+ failures= new ArrayList<>();
+ errors= new ArrayList<>();
+
+ testCount= 0;
+ stop= false;
+ }
+
+ public void addError(Test test, Throwable t) {
+ errors.add(new TestFailure(test, t));
+ }
+
+ public void addFailure(Test test, AssertionFailedError t) {
+ failures.add(new TestFailure(test, t));
+ }
+
+ public void startTest(Test test) {
+ int count= test.countTestCases();
+ testCount+= count;
+ }
+ public void endTest(Test test) {
+ }
+
+ /**
+ * Runs a TestCase.
+ */
+ protected void run(final TestCase test) {
+ startTest(test);
+ try {
+ test.doRun();
+ }
+ catch (AssertionFailedError e) {
+ addFailure(test, e);
+ }
+ catch (Throwable e) {
+ addError(test, e);
+ }
+
+ endTest(test);
+ }
+ /**
+ * Gets the number of run tests.
+ */
+ public int runCount() {
+ return testCount;
+ }
+
+
+ public boolean shouldStop() {
+ return stop;
+ }
+
+ public void stop() {
+ stop= true;
+ }
+
+ public int errorCount() {
+ return errors.size();
+ }
+
+ public Iterator errors() {
+ return errors.iterator();
+ }
+
+ public int failureCount() {
+ return failures.size();
+ }
+
+ public Iterator failures() {
+ return failures.iterator();
+ }
+ /**
+ * Returns whether the entire test was successful or not.
+ */
+ public boolean wasSuccessful() {
+ return this.failureCount() == 0 && this.errorCount() == 0;
+ }
+}
\ No newline at end of file
diff --git a/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestSuite.java b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestSuite.java
new file mode 100644
index 0000000000..ad0af20ffd
--- /dev/null
+++ b/liuxin/ood/ood-assignment/src/main/java/org/litejunit/v1/TestSuite.java
@@ -0,0 +1,130 @@
+package org.litejunit.v1;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Vector;
+
+
+
+
+public class TestSuite extends Assert implements Test {
+ private List tests= new ArrayList<>(10);
+ private String name;
+ public TestSuite(){
+
+ }
+ public TestSuite(final Class> theClass) {
+ this.name= theClass.getName();
+ Constructor> constructor= null;
+ try {
+ constructor= getConstructor(theClass);
+ } catch (NoSuchMethodException e) {
+ addTest(warning("Class "+theClass.getName()+" has no public constructor TestCase(String name)"));
+ return;
+ }
+
+ if (!Modifier.isPublic(theClass.getModifiers())) {
+ addTest(warning("Class "+theClass.getName()+" is not public"));
+ return;
+ }
+
+ Vector names= new Vector<>();
+ Method[] methods= theClass.getDeclaredMethods();
+ for (int i= 0; i < methods.length; i++) {
+ addTestMethod(methods[i], names, constructor);
+ }
+
+ if (tests.size() == 0)
+ addTest(warning("No tests found in "+theClass.getName()));
+ }
+
+ private Constructor> getConstructor(Class> theClass) throws NoSuchMethodException {
+ Class>[] args= { String.class };
+ return theClass.getConstructor(args);
+ }
+ private void addTestMethod(Method m, Vector names, Constructor> constructor) {
+ String name= m.getName();
+ if (names.contains(name))
+ return;
+ if (isPublicTestMethod(m)) {
+ names.addElement(name);
+
+ Object[] args= new Object[]{name};
+ try {
+ addTest((Test)constructor.newInstance(args));
+ } catch (InstantiationException e) {
+ addTest(warning("Cannot instantiate test case: "+name+" ("+exceptionToString(e)+")"));
+ } catch (InvocationTargetException e) {
+ addTest(warning("Exception in constructor: "+name+" ("+exceptionToString(e.getTargetException())+")"));
+ } catch (IllegalAccessException e) {
+ addTest(warning("Cannot access test case: "+name+" ("+exceptionToString(e)+")"));
+ }
+
+ } else { // almost a test method
+ if (isTestMethod(m))
+ addTest(warning("Test method isn't public: "+m.getName()));
+ }
+ }
+ private boolean isPublicTestMethod(Method m) {
+ return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
+ }
+ private boolean isTestMethod(Method m) {
+ String name= m.getName();
+ Class>[] parameters= m.getParameterTypes();
+ Class> returnType= m.getReturnType();
+ return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE);
+ }
+ public void addTest(Test test) {
+ tests.add(test);
+ }
+
+ private Test warning(final String message) {
+ return new TestCase("warning") {
+ public void doRun() {
+ fail(message);
+ }
+ };
+ }
+ private String exceptionToString(Throwable t) {
+ StringWriter stringWriter= new StringWriter();
+ PrintWriter writer= new PrintWriter(stringWriter);
+ t.printStackTrace(writer);
+ return stringWriter.toString();
+
+ }
+
+
+
+ @Override
+ public void run(TestResult result) {
+ for (Iterator e= tests(); e.hasNext(); ) {
+ if (result.shouldStop() ){
+ break;
+ }
+ Test test= (Test)e.next();
+ test.run(result);
+ }
+
+ }
+
+ public int countTestCases() {
+ int count= 0;
+
+ for (Iterator e= tests(); e.hasNext(); ) {
+ Test test= e.next();
+ count= count + test.countTestCases();
+ }
+ return count;
+ }
+ public Iterator tests() {
+ return tests.iterator();
+ }
+}