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:
019: package org.apache.tools.ant.taskdefs.optional.junit;
020:
021: import java.io.OutputStream;
022: import junit.framework.AssertionFailedError;
023: import junit.framework.TestCase;
024: import junit.framework.TestResult;
025: import org.apache.tools.ant.AntClassLoader;
026:
027: /**
028: * Implementation of the part of the junit task which can directly refer to junit.* classes.
029: * Public only to permit use of reflection; do not use directly.
030: * @see JUnitTaskMirror
031: * @see "bug #38799"
032: * @since 1.7
033: */
034: public final class JUnitTaskMirrorImpl implements JUnitTaskMirror {
035:
036: private final JUnitTask task;
037:
038: /**
039: * Constructor.
040: * @param task the junittask that uses this mirror.
041: */
042: public JUnitTaskMirrorImpl(JUnitTask task) {
043: this .task = task;
044: }
045:
046: /** {@inheritDoc}. */
047: public void addVmExit(JUnitTest test,
048: JUnitTaskMirror.JUnitResultFormatterMirror aFormatter,
049: OutputStream out, String message, String testCase) {
050: JUnitResultFormatter formatter = (JUnitResultFormatter) aFormatter;
051: formatter.setOutput(out);
052: formatter.startTestSuite(test);
053: //the trick to integrating test output to the formatter, is to
054: //create a special test class that asserts an error
055: //and tell the formatter that it raised.
056: TestCase t = new VmExitErrorTest(message, test, testCase);
057: formatter.startTest(t);
058: formatter.addError(t, new AssertionFailedError(message));
059: formatter.endTestSuite(test);
060: }
061:
062: /** {@inheritDoc}. */
063: public JUnitTaskMirror.JUnitTestRunnerMirror newJUnitTestRunner(
064: JUnitTest test, boolean haltOnError, boolean filterTrace,
065: boolean haltOnFailure, boolean showOutput,
066: boolean logTestListenerEvents, AntClassLoader classLoader) {
067: return new JUnitTestRunner(test, haltOnError, filterTrace,
068: haltOnFailure, showOutput, logTestListenerEvents,
069: classLoader);
070: }
071:
072: /** {@inheritDoc}. */
073: public JUnitTaskMirror.SummaryJUnitResultFormatterMirror newSummaryJUnitResultFormatter() {
074: return new SummaryJUnitResultFormatter();
075: }
076:
077: static class VmExitErrorTest extends TestCase {
078:
079: private String message;
080: private JUnitTest test;
081: private String testCase;
082:
083: VmExitErrorTest(String aMessage, JUnitTest anOriginalTest,
084: String aTestCase) {
085: message = aMessage;
086: test = anOriginalTest;
087: testCase = aTestCase;
088: }
089:
090: public int countTestCases() {
091: return 1;
092: }
093:
094: public void run(TestResult r) {
095: throw new AssertionFailedError(message);
096: }
097:
098: public String getName() {
099: return testCase;
100: }
101:
102: String getClassName() {
103: return test.getName();
104: }
105:
106: public String toString() {
107: return test.getName() + ":" + testCase;
108: }
109: }
110: }
|