File tree Expand file tree Collapse file tree 8 files changed +175
-0
lines changed
students/1299310140/src/com/coderising/junit Expand file tree Collapse file tree 8 files changed +175
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
package com .coderising .junit ;
2
2
3
3
public interface Test {
4
+
4
5
public void run (TestResult tr );
6
+
7
+ public int countTestCases ();
5
8
}
Original file line number Diff line number Diff line change @@ -42,4 +42,9 @@ protected void setUp(){
42
42
protected void tearDown (){
43
43
44
44
}
45
+
46
+ @ Override
47
+ public int countTestCases () {
48
+ return 1 ;
49
+ }
45
50
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -11,6 +11,8 @@ public class TestSuite implements Test {
11
11
12
12
private List <Test > tests = new ArrayList <Test >();
13
13
14
+ private String name = "" ;
15
+
14
16
@ Override
15
17
public void run (TestResult tr ) {
16
18
for (Iterator <Test > iterator = tests .iterator ();iterator .hasNext ();){
@@ -23,11 +25,20 @@ public void addTest(Test test){
23
25
tests .add (test );
24
26
}
25
27
28
+ public void addTestSuite (Class <?> theClass ){
29
+ addTest (new TestSuite (theClass ));
30
+ }
31
+
26
32
public TestSuite (){
27
33
28
34
}
29
35
36
+ public TestSuite (String name ){
37
+ this .name = name ;
38
+ }
39
+
30
40
public TestSuite (final Class <?> theClass ){
41
+ this .name = theClass .getName ();
31
42
Constructor <?> constructor = null ;
32
43
try {
33
44
constructor = theClass .getConstructor (String .class );
@@ -72,4 +83,14 @@ private boolean isPublicTestMethod(Method method) {
72
83
return Modifier .isPublic (method .getModifiers ()) && name .startsWith ("test" )
73
84
&& parameters .length == 0 && returnType .equals (Void .TYPE );
74
85
}
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
+ }
75
96
}
You can’t perform that action at this time.
0 commit comments