Skip to content

Commit 0a6ebbc

Browse files
author
'1299310140'
committed
TestListener
1 parent b79097c commit 0a6ebbc

File tree

8 files changed

+175
-0
lines changed

8 files changed

+175
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}
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: 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.coderising.junit;
22

33
public interface Test {
4+
45
public void run(TestResult tr);
6+
7+
public int countTestCases();
58
}

students/1299310140/src/com/coderising/junit/TestCase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ protected void setUp(){
4242
protected void tearDown(){
4343

4444
}
45+
46+
@Override
47+
public int countTestCases() {
48+
return 1;
49+
}
4550
}
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+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.coderising.junit;
2+
3+
import java.io.PrintStream;
4+
5+
public class TestRunner extends BaseTestRunner {
6+
7+
PrintStream write = System.out;
8+
int column = 0;
9+
10+
@Override
11+
public void addError(Test test, Throwable t) {
12+
write.print("E");
13+
}
14+
15+
@Override
16+
public void addFailure(Test test, AssertionFailedError t) {
17+
write.print("F");
18+
}
19+
20+
@Override
21+
public void startTest(Test test) {
22+
write.print(".");
23+
if(column++ >= 40){
24+
write.println();
25+
column = 0;
26+
}
27+
}
28+
29+
@Override
30+
public void endTest(Test test) {
31+
32+
}
33+
34+
public static void main(String[] args) {
35+
TestRunner testRunner = new TestRunner();
36+
try {
37+
TestResult testResult = testRunner.start(args);
38+
} catch (Exception e) {
39+
40+
e.printStackTrace();
41+
}
42+
}
43+
44+
private TestResult start(String[] args) throws Exception {
45+
if(args.length == 0){
46+
throw new Exception("Usage : TestRunner TestCaseName");
47+
}
48+
49+
String testCase = args[0];
50+
Test suite = getTest(testCase);
51+
52+
return doRun(suite);
53+
}
54+
55+
private TestResult doRun(Test suite) {
56+
TestResult testResult = new TestResult();
57+
testResult.addListener(this);
58+
suite.run(testResult);
59+
return testResult;
60+
}
61+
62+
}

students/1299310140/src/com/coderising/junit/TestSuite.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class TestSuite implements Test {
1111

1212
private List<Test> tests = new ArrayList<Test>();
1313

14+
private String name = "";
15+
1416
@Override
1517
public void run(TestResult tr) {
1618
for(Iterator<Test> iterator = tests.iterator();iterator.hasNext();){
@@ -23,11 +25,20 @@ public void addTest(Test test){
2325
tests.add(test);
2426
}
2527

28+
public void addTestSuite(Class<?> theClass){
29+
addTest(new TestSuite(theClass));
30+
}
31+
2632
public TestSuite(){
2733

2834
}
2935

36+
public TestSuite(String name){
37+
this.name = name;
38+
}
39+
3040
public TestSuite(final Class<?> theClass){
41+
this.name = theClass.getName();
3142
Constructor<?> constructor = null;
3243
try {
3344
constructor = theClass.getConstructor(String.class);
@@ -72,4 +83,14 @@ private boolean isPublicTestMethod(Method method) {
7283
return Modifier.isPublic(method.getModifiers()) && name.startsWith("test")
7384
&& parameters.length == 0 && returnType.equals(Void.TYPE);
7485
}
86+
87+
@Override
88+
public int countTestCases() {
89+
int count = 0;
90+
for(Iterator<Test> iterator = tests.iterator();iterator.hasNext();){
91+
Test test = iterator.next();
92+
count += test.countTestCases();
93+
}
94+
return count;
95+
}
7596
}

0 commit comments

Comments
 (0)