1 | /*
|
---|
2 | * Copyright 2001,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.optional.junit;
|
---|
18 |
|
---|
19 | import java.io.*;
|
---|
20 | import junit.framework.*;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Small testcase for the runner, tests are very very very basics.
|
---|
25 | * They must be enhanced with time.
|
---|
26 | *
|
---|
27 | */
|
---|
28 | public class JUnitTestRunnerTest extends TestCase {
|
---|
29 |
|
---|
30 | // mandatory constructor
|
---|
31 | public JUnitTestRunnerTest(String name){
|
---|
32 | super(name);
|
---|
33 | }
|
---|
34 |
|
---|
35 | // check that having no suite generates no errors
|
---|
36 | public void testNoSuite(){
|
---|
37 | TestRunner runner = createRunner(NoSuiteTestCase.class);
|
---|
38 | runner.run();
|
---|
39 | assertEquals(runner.getFormatter().getError(), JUnitTestRunner.SUCCESS, runner.getRetCode());
|
---|
40 | }
|
---|
41 |
|
---|
42 | // check that a suite generates no errors
|
---|
43 | public void testSuite(){
|
---|
44 | TestRunner runner = createRunner(SuiteTestCase.class);
|
---|
45 | runner.run();
|
---|
46 | assertEquals(runner.getFormatter().getError(), JUnitTestRunner.SUCCESS, runner.getRetCode());
|
---|
47 | }
|
---|
48 |
|
---|
49 | // check that an invalid suite generates an error.
|
---|
50 | public void testInvalidSuite(){
|
---|
51 | TestRunner runner = createRunner(InvalidSuiteTestCase.class);
|
---|
52 | runner.run();
|
---|
53 | String error = runner.getFormatter().getError();
|
---|
54 | assertEquals(error, JUnitTestRunner.ERRORS, runner.getRetCode());
|
---|
55 | assertTrue(error, error.indexOf("thrown on purpose") != -1);
|
---|
56 | }
|
---|
57 |
|
---|
58 | // check that something which is not a testcase generates no errors
|
---|
59 | // at first even though this is incorrect.
|
---|
60 | public void testNoTestCase(){
|
---|
61 | TestRunner runner = createRunner(NoTestCase.class);
|
---|
62 | runner.run();
|
---|
63 | assertEquals(runner.getFormatter().getError(), JUnitTestRunner.FAILURES, runner.getRetCode());
|
---|
64 | }
|
---|
65 |
|
---|
66 | // check that an exception in the constructor is noticed
|
---|
67 | public void testInvalidTestCase(){
|
---|
68 | TestRunner runner = createRunner(InvalidTestCase.class);
|
---|
69 | runner.run();
|
---|
70 | String error = runner.getFormatter().getError();
|
---|
71 | assertEquals(error, JUnitTestRunner.FAILURES, runner.getRetCode());
|
---|
72 | //@fixme as of now does not report the original stacktrace.
|
---|
73 | //assertTrue(error, error.indexOf("thrown on purpose") != -1);
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected TestRunner createRunner(Class clazz){
|
---|
77 | return new TestRunner(new JUnitTest(clazz.getName()), true, true, true);
|
---|
78 | }
|
---|
79 |
|
---|
80 | // the test runner that wrap the dummy formatter that interests us
|
---|
81 | private final static class TestRunner extends JUnitTestRunner {
|
---|
82 | private ResultFormatter formatter = new ResultFormatter();
|
---|
83 | TestRunner(JUnitTest test, boolean haltonerror, boolean filtertrace, boolean haltonfailure){
|
---|
84 | super(test, haltonerror, filtertrace, haltonfailure, TestRunner.class.getClassLoader());
|
---|
85 | // use the classloader that loaded this class otherwise
|
---|
86 | // it will not be able to run inner classes if this test
|
---|
87 | // is ran in non-forked mode.
|
---|
88 | addFormatter(formatter);
|
---|
89 | }
|
---|
90 | ResultFormatter getFormatter(){
|
---|
91 | return formatter;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | // dummy formatter just to catch the error
|
---|
96 | private final static class ResultFormatter implements JUnitResultFormatter {
|
---|
97 | private Throwable error;
|
---|
98 | public void setSystemOutput(String output){}
|
---|
99 | public void setSystemError(String output){}
|
---|
100 | public void startTestSuite(JUnitTest suite) throws BuildException{}
|
---|
101 | public void endTestSuite(JUnitTest suite) throws BuildException{}
|
---|
102 | public void setOutput(java.io.OutputStream out){}
|
---|
103 | public void startTest(Test t) {}
|
---|
104 | public void endTest(Test test) {}
|
---|
105 | public void addFailure(Test test, Throwable t) { }
|
---|
106 | public void addFailure(Test test, AssertionFailedError t) { }
|
---|
107 | public void addError(Test test, Throwable t) {
|
---|
108 | error = t;
|
---|
109 | }
|
---|
110 | String getError(){
|
---|
111 | if (error == null){
|
---|
112 | return "";
|
---|
113 | }
|
---|
114 | StringWriter sw = new StringWriter();
|
---|
115 | error.printStackTrace(new PrintWriter(sw));
|
---|
116 | return sw.toString();
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | public static class NoTestCase {
|
---|
121 | }
|
---|
122 |
|
---|
123 | public static class InvalidTestCase extends TestCase {
|
---|
124 | public InvalidTestCase(String name){
|
---|
125 | super(name);
|
---|
126 | throw new NullPointerException("thrown on purpose");
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | public static class NoSuiteTestCase extends TestCase {
|
---|
131 | public NoSuiteTestCase(String name){ super(name); }
|
---|
132 | public void testA(){}
|
---|
133 | }
|
---|
134 |
|
---|
135 | public static class SuiteTestCase extends NoSuiteTestCase {
|
---|
136 | public SuiteTestCase(String name){ super(name); }
|
---|
137 | public static Test suite(){
|
---|
138 | return new TestSuite(SuiteTestCase.class);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | public static class InvalidSuiteTestCase extends NoSuiteTestCase {
|
---|
143 | public InvalidSuiteTestCase(String name){ super(name); }
|
---|
144 | public static Test suite(){
|
---|
145 | throw new NullPointerException("thrown on purpose");
|
---|
146 | }
|
---|
147 | }
|
---|
148 | public static void main(String[] args){
|
---|
149 | junit.textui.TestRunner.run(JUnitTestRunnerTest.class);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|