001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.junit;
019:
020: import java.io.*;
021: import junit.framework.*;
022: import org.apache.tools.ant.BuildException;
023:
024: /**
025: * Small testcase for the runner, tests are very very very basics.
026: * They must be enhanced with time.
027: *
028: */
029: public class JUnitTestRunnerTest extends TestCase {
030:
031: // mandatory constructor
032: public JUnitTestRunnerTest(String name) {
033: super (name);
034: }
035:
036: // check that having no suite generates no errors
037: public void testNoSuite() {
038: TestRunner runner = createRunner(NoSuiteTestCase.class);
039: runner.run();
040: assertEquals(runner.getFormatter().getError(),
041: JUnitTestRunner.SUCCESS, runner.getRetCode());
042: }
043:
044: // check that a suite generates no errors
045: public void testSuite() {
046: TestRunner runner = createRunner(SuiteTestCase.class);
047: runner.run();
048: assertEquals(runner.getFormatter().getError(),
049: JUnitTestRunner.SUCCESS, runner.getRetCode());
050: }
051:
052: // check that an invalid suite generates an error.
053: public void testInvalidSuite() {
054: TestRunner runner = createRunner(InvalidSuiteTestCase.class);
055: runner.run();
056: String error = runner.getFormatter().getError();
057: assertEquals(error, JUnitTestRunner.ERRORS, runner.getRetCode());
058: assertTrue(error, error.indexOf("thrown on purpose") != -1);
059: }
060:
061: // check that something which is not a testcase generates no errors
062: // at first even though this is incorrect.
063: public void testNoTestCase() {
064: TestRunner runner = createRunner(NoTestCase.class);
065: runner.run();
066: // On junit3 this is a FAILURE, on junit4 this is an ERROR
067: int ret = runner.getRetCode();
068:
069: if (ret != JUnitTestRunner.FAILURES
070: && ret != JUnitTestRunner.ERRORS) {
071: fail("Unexpected result " + ret + " from junit runner");
072: }
073: // JUnit3 test
074: //assertEquals(runner.getFormatter().getError(), JUnitTestRunner.FAILURES, runner.getRetCode());
075: }
076:
077: // check that an exception in the constructor is noticed
078: public void testInvalidTestCase() {
079: TestRunner runner = createRunner(InvalidTestCase.class);
080: runner.run();
081: // On junit3 this is a FAILURE, on junit4 this is an ERROR
082: int ret = runner.getRetCode();
083: if (ret != JUnitTestRunner.FAILURES
084: && ret != JUnitTestRunner.ERRORS) {
085: fail("Unexpected result " + ret + " from junit runner");
086: }
087: // JUNIT3 test
088: //assertEquals(error, JUnitTestRunner.FAILURES, runner.getRetCode());
089: //@fixme as of now does not report the original stacktrace.
090: //assertTrue(error, error.indexOf("thrown on purpose") != -1);
091: }
092:
093: protected TestRunner createRunner(Class clazz) {
094: return new TestRunner(new JUnitTest(clazz.getName()), true,
095: true, true);
096: }
097:
098: // the test runner that wrap the dummy formatter that interests us
099: private final static class TestRunner extends JUnitTestRunner {
100: private ResultFormatter formatter = new ResultFormatter();
101:
102: TestRunner(JUnitTest test, boolean haltonerror,
103: boolean filtertrace, boolean haltonfailure) {
104: super (test, haltonerror, filtertrace, haltonfailure,
105: TestRunner.class.getClassLoader());
106: // use the classloader that loaded this class otherwise
107: // it will not be able to run inner classes if this test
108: // is ran in non-forked mode.
109: addFormatter(formatter);
110: }
111:
112: ResultFormatter getFormatter() {
113: return formatter;
114: }
115: }
116:
117: // dummy formatter just to catch the error
118: private final static class ResultFormatter implements
119: JUnitResultFormatter {
120: private Throwable error;
121:
122: public void setSystemOutput(String output) {
123: }
124:
125: public void setSystemError(String output) {
126: }
127:
128: public void startTestSuite(JUnitTest suite)
129: throws BuildException {
130: }
131:
132: public void endTestSuite(JUnitTest suite) throws BuildException {
133: }
134:
135: public void setOutput(java.io.OutputStream out) {
136: }
137:
138: public void startTest(Test t) {
139: }
140:
141: public void endTest(Test test) {
142: }
143:
144: public void addFailure(Test test, Throwable t) {
145: }
146:
147: public void addFailure(Test test, AssertionFailedError t) {
148: }
149:
150: public void addError(Test test, Throwable t) {
151: error = t;
152: }
153:
154: String getError() {
155: if (error == null) {
156: return "";
157: }
158: StringWriter sw = new StringWriter();
159: error.printStackTrace(new PrintWriter(sw));
160: return sw.toString();
161: }
162: }
163:
164: public static class NoTestCase {
165: }
166:
167: public static class InvalidTestCase extends TestCase {
168: public InvalidTestCase(String name) {
169: super (name);
170: throw new NullPointerException("thrown on purpose");
171: }
172: }
173:
174: public static class NoSuiteTestCase extends TestCase {
175: public NoSuiteTestCase(String name) {
176: super (name);
177: }
178:
179: public void testA() {
180: }
181: }
182:
183: public static class SuiteTestCase extends NoSuiteTestCase {
184: public SuiteTestCase(String name) {
185: super (name);
186: }
187:
188: public static Test suite() {
189: return new TestSuite(SuiteTestCase.class);
190: }
191: }
192:
193: public static class InvalidSuiteTestCase extends NoSuiteTestCase {
194: public InvalidSuiteTestCase(String name) {
195: super (name);
196: }
197:
198: public static Test suite() {
199: throw new NullPointerException("thrown on purpose");
200: }
201: }
202:
203: public static void main(String[] args) {
204: junit.textui.TestRunner.run(JUnitTestRunnerTest.class);
205: }
206: }
|