KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > junit > JUnitTaskMirrorImpl


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.junit;
20
21 import java.io.OutputStream JavaDoc;
22 import junit.framework.AssertionFailedError;
23 import junit.framework.TestCase;
24 import junit.framework.TestResult;
25 import org.apache.tools.ant.AntClassLoader;
26
27 /**
28  * Implementation of the part of the junit task which can directly refer to junit.* classes.
29  * Public only to permit use of reflection; do not use directly.
30  * @see JUnitTaskMirror
31  * @see "bug #38799"
32  * @since 1.7
33  */

34 public final class JUnitTaskMirrorImpl implements JUnitTaskMirror {
35
36     private final JUnitTask task;
37
38     /**
39      * Constructor.
40      * @param task the junittask that uses this mirror.
41      */

42     public JUnitTaskMirrorImpl(JUnitTask task) {
43         this.task = task;
44     }
45
46     /** {@inheritDoc}. */
47     public void addVmExit(JUnitTest test, JUnitTaskMirror.JUnitResultFormatterMirror aFormatter,
48             OutputStream JavaDoc out, String JavaDoc message, String JavaDoc testCase) {
49         JUnitResultFormatter formatter = (JUnitResultFormatter) aFormatter;
50         formatter.setOutput(out);
51         formatter.startTestSuite(test);
52         //the trick to integrating test output to the formatter, is to
53
//create a special test class that asserts an error
54
//and tell the formatter that it raised.
55
TestCase t = new VmExitErrorTest(message, test, testCase);
56         formatter.startTest(t);
57         formatter.addError(t, new AssertionFailedError(message));
58         formatter.endTestSuite(test);
59     }
60
61     /** {@inheritDoc}. */
62     public JUnitTaskMirror.JUnitTestRunnerMirror newJUnitTestRunner(JUnitTest test,
63             boolean haltOnError, boolean filterTrace, boolean haltOnFailure,
64             boolean showOutput, boolean logTestListenerEvents, AntClassLoader classLoader) {
65         return new JUnitTestRunner(test, haltOnError, filterTrace, haltOnFailure,
66                 showOutput, logTestListenerEvents, classLoader);
67     }
68
69     /** {@inheritDoc}. */
70     public JUnitTaskMirror.SummaryJUnitResultFormatterMirror newSummaryJUnitResultFormatter() {
71         return new SummaryJUnitResultFormatter();
72     }
73
74     static class VmExitErrorTest extends TestCase {
75
76         private String JavaDoc message;
77         private JUnitTest test;
78         private String JavaDoc testCase;
79
80         VmExitErrorTest(String JavaDoc aMessage, JUnitTest anOriginalTest, String JavaDoc aTestCase) {
81             message = aMessage;
82             test = anOriginalTest;
83             testCase = aTestCase;
84         }
85
86         public int countTestCases() {
87             return 1;
88         }
89
90         public void run(TestResult r) {
91             throw new AssertionFailedError(message);
92         }
93
94         public String JavaDoc getName() {
95             return testCase;
96         }
97
98         String JavaDoc getClassName() {
99             return test.getName();
100         }
101
102         public String JavaDoc toString() {
103             return test.getName() + ":" + testCase;
104         }
105     }
106 }
107
Popular Tags