Skip to content

Commit e98e184

Browse files
authored
Merge pull request onlyliuxin#574 from dustheart/master
payroll&&junit3
2 parents 703c74a + 96083b1 commit e98e184

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1253
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.coderising.junit;
2+
3+
public class AllTest {
4+
5+
// public static Test suite(){
6+
// TestSuite suite = new TestSuite("All Test");
7+
// suite.addTestSuite(CalculatorTest.class);
8+
// suite.addTestSuite(CalculatorTest.class);
9+
// return suite;
10+
// }
11+
12+
public static Test suite(){
13+
TestSuite suite = new TestSuite("All Test");
14+
suite.addTest(CalculatorSuite.suite());
15+
// suite.addTestSuite(CalculatorTest.class);
16+
suite.addTest(new RepeatedTest(new TestSuite(CalculatorTest.class),3));
17+
return new OverallTestSetup(suite);
18+
}
19+
20+
static class OverallTestSetup extends TestSetUp{
21+
22+
public OverallTestSetup(Test test) {
23+
super(test);
24+
}
25+
26+
@Override
27+
public void setUp(){
28+
System.out.println("this is overall testsetup");
29+
}
30+
31+
@Override
32+
public void tearDown(){
33+
System.out.println("this is overall testteardown");
34+
}
35+
}
36+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.coderising.junit;
2+
3+
public class Assert {
4+
5+
public static void assertEquals(int expected,int actual){
6+
if(expected == actual)
7+
return;
8+
failNotEquals(expected,actual);
9+
}
10+
11+
private static void failNotEquals(int expected, int actual) {
12+
String message = "expected:<" + expected + "> but was:<" + actual + ">";
13+
fail(message);
14+
}
15+
16+
private static void fail(String message) {
17+
throw new AssertionFailedError(message);
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.coderising.junit;
2+
3+
public class AssertionFailedError extends Error {
4+
5+
public AssertionFailedError(){
6+
7+
}
8+
9+
public AssertionFailedError(String message){
10+
super(message);
11+
}
12+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.coderising.junit;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
6+
public abstract class BaseTestRunner implements TestListener {
7+
8+
public static final String SUITE_METHODNAME = "suite";
9+
10+
public Test getTest(String suiteClassName) {
11+
Class<?> testClass = null;
12+
try {
13+
testClass = loadSuiteClass(suiteClassName);
14+
} catch (ClassNotFoundException e) {
15+
16+
e.printStackTrace();
17+
}
18+
19+
Method suiteMethod = null;
20+
try {
21+
suiteMethod = testClass.getMethod(SUITE_METHODNAME, new Class[0]);
22+
} catch (NoSuchMethodException e) {
23+
24+
e.printStackTrace();
25+
} catch (SecurityException e) {
26+
27+
e.printStackTrace();
28+
}
29+
30+
Test test = null;
31+
try {
32+
test = (Test) suiteMethod.invoke(null, new Class[0]);
33+
} catch (IllegalAccessException e) {
34+
35+
e.printStackTrace();
36+
} catch (IllegalArgumentException e) {
37+
38+
e.printStackTrace();
39+
} catch (InvocationTargetException e) {
40+
41+
e.printStackTrace();
42+
}
43+
44+
return test;
45+
}
46+
47+
private Class<?> loadSuiteClass(String suiteClassName) throws ClassNotFoundException {
48+
49+
return Class.forName(suiteClassName);
50+
}
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.coderising.junit;
2+
3+
public class Calculator {
4+
5+
private int result = 0;
6+
7+
public void add(int x){
8+
result += x;
9+
}
10+
11+
public void subtract(int x){
12+
result -= x;
13+
}
14+
15+
public int getResult(){
16+
return result;
17+
}
18+
19+
public static void main(String[] args){
20+
TestSuite ts = new TestSuite(CalculatorTest.class);
21+
TestResult tr = new TestResult();
22+
ts.run(tr);
23+
for(TestFailure testFailure:tr.getFailures()){
24+
System.out.println(testFailure.thrownException());
25+
}
26+
for(TestFailure testFailure:tr.getErrors()){
27+
System.out.println(testFailure.thrownException());
28+
}
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.coderising.junit;
2+
3+
public class CalculatorSuite {
4+
5+
public static Test suite(){
6+
TestSuite suite = new TestSuite("Calculator All Test");
7+
suite.addTestSuite(CalculatorTest.class);
8+
return suite;
9+
}
10+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.coderising.junit;
2+
3+
public class CalculatorTest extends TestCase {
4+
5+
public CalculatorTest(String name) {
6+
super(name);
7+
}
8+
9+
Calculator calculator = null;
10+
11+
public void setUp(){
12+
calculator = new Calculator();
13+
}
14+
15+
public void tearDown(){
16+
calculator = null;
17+
}
18+
19+
public void testAdd(){
20+
calculator.add(10);
21+
Assert.assertEquals(10, calculator.getResult());
22+
}
23+
24+
public void testSubtract(){
25+
calculator.add(10);
26+
calculator.subtract(5);
27+
Assert.assertEquals(10, calculator.getResult());
28+
}
29+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.coderising.junit;
2+
3+
public interface Protectable {
4+
public void protect() throws Throwable;
5+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.coderising.junit;
2+
3+
public class RepeatedTest extends TestDecorator {
4+
5+
private int fTimesRepeat;
6+
7+
public RepeatedTest(Test test,int repeat) {
8+
super(test);
9+
if(repeat < 0)
10+
throw new IllegalArgumentException("repeation count must be > 0");
11+
this.fTimesRepeat = repeat;
12+
}
13+
14+
public int countTestCases(){
15+
return super.countTestCases() * fTimesRepeat;
16+
}
17+
18+
public void run(TestResult testResult){
19+
for(int i = 0;i < fTimesRepeat;i++){
20+
super.run(testResult);
21+
}
22+
}
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.coderising.junit;
2+
3+
public interface Test {
4+
5+
public void run(TestResult tr);
6+
7+
public int countTestCases();
8+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.coderising.junit;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
6+
public abstract class TestCase extends Assert implements Test {
7+
8+
private String name;
9+
10+
public TestCase(String name){
11+
this.name = name;
12+
}
13+
14+
@Override
15+
public void run(TestResult tr){
16+
tr.run(this);
17+
}
18+
19+
public void doRun() throws Throwable{
20+
setUp();
21+
try{
22+
runTest();
23+
}finally{
24+
tearDown();
25+
}
26+
}
27+
28+
protected void runTest() throws Throwable{
29+
Method runMethod = this.getClass().getMethod(name, null);
30+
try {
31+
runMethod.invoke(this, new Class[0]);
32+
} catch (InvocationTargetException e) {
33+
e.fillInStackTrace();
34+
throw e.getTargetException();
35+
}
36+
}
37+
38+
protected void setUp(){
39+
40+
}
41+
42+
protected void tearDown(){
43+
44+
}
45+
46+
@Override
47+
public int countTestCases() {
48+
return 1;
49+
}
50+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.coderising.junit;
2+
3+
public class TestDecorator implements Test {
4+
5+
protected Test test;
6+
7+
public TestDecorator(Test test){
8+
this.test = test;
9+
}
10+
11+
@Override
12+
public void run(TestResult tr) {
13+
basicRun(tr);
14+
}
15+
16+
public void basicRun(TestResult tr) {
17+
this.test.run(tr);
18+
}
19+
20+
@Override
21+
public int countTestCases() {
22+
23+
return test.countTestCases();
24+
}
25+
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.coderising.junit;
2+
3+
public class TestFailure {
4+
5+
private Test failedTest;
6+
private Throwable thrownException;
7+
8+
public TestFailure(Test failedTest, Throwable thrownException) {
9+
super();
10+
this.failedTest = failedTest;
11+
this.thrownException = thrownException;
12+
}
13+
14+
public Test failedTest(){
15+
return this.failedTest;
16+
}
17+
18+
public Throwable thrownException(){
19+
return this.thrownException;
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.coderising.junit;
2+
3+
public interface TestListener {
4+
5+
public void addError(Test test, Throwable t);
6+
7+
public void addFailure(Test test, AssertionFailedError t);
8+
9+
public void startTest(Test test);
10+
11+
public void endTest(Test test);
12+
}

0 commit comments

Comments
 (0)