View | Details | Raw Unified | Return to bug 41534
Collapse All | Expand All

(-)src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java (+1 lines)
Lines 721-726 Link Here
721
721
722
        private final String[] splitClasses = {
722
        private final String[] splitClasses = {
723
            "BriefJUnitResultFormatter",
723
            "BriefJUnitResultFormatter",
724
            "QuietJUnitResultFormatter",
724
            "JUnitResultFormatter",
725
            "JUnitResultFormatter",
725
            "JUnitTaskMirrorImpl",
726
            "JUnitTaskMirrorImpl",
726
            "JUnitTestRunner",
727
            "JUnitTestRunner",
(-)src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java (-5 / +16 lines)
Lines 46-51 Link Here
46
 * @see JUnitTask
46
 * @see JUnitTask
47
 * @see XMLJUnitResultFormatter
47
 * @see XMLJUnitResultFormatter
48
 * @see BriefJUnitResultFormatter
48
 * @see BriefJUnitResultFormatter
49
 * @see QuietJUnitResultFormatter
49
 * @see PlainJUnitResultFormatter
50
 * @see PlainJUnitResultFormatter
50
 * @see JUnitResultFormatter
51
 * @see JUnitResultFormatter
51
 */
52
 */
Lines 65-70 Link Here
65
    /** brief formatter class */
66
    /** brief formatter class */
66
    public static final String BRIEF_FORMATTER_CLASS_NAME =
67
    public static final String BRIEF_FORMATTER_CLASS_NAME =
67
        "org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter";
68
        "org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter";
69
    /** quiet formatter class */
70
    public static final String QUIET_FORMATTER_CLASS_NAME =
71
        "org.apache.tools.ant.taskdefs.optional.junit.QuietJUnitResultFormatter";
68
    /** plain formatter class */
72
    /** plain formatter class */
69
    public static final String PLAIN_FORMATTER_CLASS_NAME =
73
    public static final String PLAIN_FORMATTER_CLASS_NAME =
70
        "org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter";
74
        "org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter";
Lines 76-81 Link Here
76
     * <ul>
80
     * <ul>
77
     * <li> The <code>xml</code> type uses a <code>XMLJUnitResultFormatter</code>.
81
     * <li> The <code>xml</code> type uses a <code>XMLJUnitResultFormatter</code>.
78
     * <li> The <code>brief</code> type uses a <code>BriefJUnitResultFormatter</code>.
82
     * <li> The <code>brief</code> type uses a <code>BriefJUnitResultFormatter</code>.
83
     * <li> The <code>quiet</code> type uses a <code>QuietJUnitResultFormatter</code>.
79
     * <li> The <code>plain</code> type (the default) uses a <code>PlainJUnitResultFormatter</code>.
84
     * <li> The <code>plain</code> type (the default) uses a <code>PlainJUnitResultFormatter</code>.
80
     * </ul>
85
     * </ul>
81
     *
86
     *
Lines 89-96 Link Here
89
        } else {
94
        } else {
90
            if ("brief".equals(type.getValue())) {
95
            if ("brief".equals(type.getValue())) {
91
                setClassname(BRIEF_FORMATTER_CLASS_NAME);
96
                setClassname(BRIEF_FORMATTER_CLASS_NAME);
92
            } else { // must be plain, ensured by TypeAttribute
97
            } else {
93
                setClassname(PLAIN_FORMATTER_CLASS_NAME);
98
		if ("quiet".equals(type.getValue())) {
99
		    setClassname(QUIET_FORMATTER_CLASS_NAME);
100
		} else { // must be plain, ensured by TypeAttribute
101
		    setClassname(PLAIN_FORMATTER_CLASS_NAME);
102
		}
94
            }
103
            }
95
        }
104
        }
96
    }
105
    }
Lines 109-115 Link Here
109
           setExtension(".txt");
118
           setExtension(".txt");
110
        } else if (BRIEF_FORMATTER_CLASS_NAME.equals(classname)) {
119
        } else if (BRIEF_FORMATTER_CLASS_NAME.equals(classname)) {
111
           setExtension(".txt");
120
           setExtension(".txt");
112
        }
121
        } else if (QUIET_FORMATTER_CLASS_NAME.equals(classname)) {
122
           setExtension(".txt");
123
	}
113
    }
124
    }
114
125
115
    /**
126
    /**
Lines 270-283 Link Here
270
    }
281
    }
271
282
272
    /**
283
    /**
273
     * <p> Enumerated attribute with the values "plain", "xml" and "brief".
284
     * <p> Enumerated attribute with the values "plain", "xml", "brief", and "quiet".
274
     *
285
     *
275
     * <p> Use to enumerate options for <code>type</code> attribute.
286
     * <p> Use to enumerate options for <code>type</code> attribute.
276
     */
287
     */
277
    public static class TypeAttribute extends EnumeratedAttribute {
288
    public static class TypeAttribute extends EnumeratedAttribute {
278
        /** {@inheritDoc}. */
289
        /** {@inheritDoc}. */
279
        public String[] getValues() {
290
        public String[] getValues() {
280
            return new String[] {"plain", "xml", "brief"};
291
            return new String[] {"plain", "xml", "brief", "quiet"};
281
        }
292
        }
282
    }
293
    }
283
}
294
}
(-)src/main/org/apache/tools/ant/taskdefs/optional/junit/QuietJUnitResultFormatter.java (+262 lines)
Line 0 Link Here
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
 *  NOTE:
18
 *  This file was added by Mathias Ricken to include support for a
19
 *  "quiet" JUnit formatter.
20
 *  For more information, please go to
21
 *
22
 *      http://www.superscalar.org/files/apache-ant-1.7.0-quiet/
23
 *
24
 *  Thank you.
25
 */
26
27
package org.apache.tools.ant.taskdefs.optional.junit;
28
29
import java.io.OutputStream;
30
import java.io.PrintWriter;
31
import java.io.StringWriter;
32
import java.text.NumberFormat;
33
34
import junit.framework.AssertionFailedError;
35
import junit.framework.Test;
36
37
import org.apache.tools.ant.util.FileUtils;
38
import org.apache.tools.ant.util.StringUtils;
39
40
/**
41
 * Only prints out errors, no summaries.
42
 *
43
 * @see FormatterElement
44
 * @see PlainJUnitResultFormatter
45
 */
46
public class QuietJUnitResultFormatter implements JUnitResultFormatter {
47
48
    /**
49
     * Where to write the log to.
50
     */
51
    private OutputStream out;
52
53
    /**
54
     * Used for writing the results.
55
     */
56
    private PrintWriter output;
57
58
    /**
59
     * Used as part of formatting the results.
60
     */
61
    private StringWriter results;
62
63
    /**
64
     * Used for writing formatted results to.
65
     */
66
    private PrintWriter resultWriter;
67
68
    /**
69
     * Formatter for timings.
70
     */
71
    private NumberFormat numberFormat = NumberFormat.getInstance();
72
73
    /**
74
     * Output suite has written to System.out
75
     */
76
    private String systemOutput = null;
77
78
    /**
79
     * Output suite has written to System.err
80
     */
81
    private String systemError = null;
82
83
    /**
84
     * Constructor for QuietJUnitResultFormatter.
85
     */
86
    public QuietJUnitResultFormatter() {
87
        results = new StringWriter();
88
        resultWriter = new PrintWriter(results);
89
    }
90
91
    /**
92
     * Sets the stream the formatter is supposed to write its results to.
93
     * @param out the output stream to write to
94
     */
95
    public void setOutput(OutputStream out) {
96
        this.out = out;
97
        output = new PrintWriter(out);
98
    }
99
100
    /**
101
     * @see JUnitResultFormatter#setSystemOutput(String)
102
     */
103
    /** {@inheritDoc}. */
104
    public void setSystemOutput(String out) {
105
        systemOutput = out;
106
    }
107
108
    /**
109
     * @see JUnitResultFormatter#setSystemError(String)
110
     */
111
    /** {@inheritDoc}. */
112
    public void setSystemError(String err) {
113
        systemError = err;
114
    }
115
116
    /**
117
     * String generated when the testsuite was started.
118
     */
119
    private String _startTestSuiteOutput = "";
120
121
    /**
122
     * The whole testsuite started.
123
     * @param suite the test suite
124
     */
125
    public void startTestSuite(JUnitTest suite) {
126
        if (output == null) {
127
            return; // Quick return - no output do nothing.
128
        }
129
        StringBuffer sb = new StringBuffer("Testsuite: ");
130
        sb.append(suite.getName());
131
        sb.append(StringUtils.LINE_SEP);
132
	_startTestSuiteOutput = sb.toString();
133
    }
134
135
    /**
136
     * The whole testsuite ended.
137
     * @param suite the test suite
138
     */
139
    public void endTestSuite(JUnitTest suite) {
140
        StringBuffer sb = new StringBuffer("Tests run: ");
141
        sb.append(suite.runCount());
142
        sb.append(", Failures: ");
143
        sb.append(suite.failureCount());
144
        sb.append(", Errors: ");
145
        sb.append(suite.errorCount());
146
        sb.append(", Time elapsed: ");
147
        sb.append(numberFormat.format(suite.getRunTime() / 1000.0));
148
        sb.append(" sec");
149
        sb.append(StringUtils.LINE_SEP);
150
        sb.append(StringUtils.LINE_SEP);
151
152
        // append the err and output streams to the log
153
        if (systemOutput != null && systemOutput.length() > 0) {
154
            sb.append("------------- Standard Output ---------------")
155
                    .append(StringUtils.LINE_SEP)
156
                    .append(systemOutput)
157
                    .append("------------- ---------------- ---------------")
158
                    .append(StringUtils.LINE_SEP);
159
        }
160
161
        if (systemError != null && systemError.length() > 0) {
162
            sb.append("------------- Standard Error -----------------")
163
                    .append(StringUtils.LINE_SEP)
164
                    .append(systemError)
165
                    .append("------------- ---------------- ---------------")
166
                    .append(StringUtils.LINE_SEP);
167
        }
168
169
        if (output != null) {
170
	    if ((suite.failureCount()!=0) || (suite.errorCount()!=0)) {
171
		try {
172
		    output.write(_startTestSuiteOutput);
173
		    output.write(sb.toString());
174
		    resultWriter.close();
175
		    output.write(results.toString());
176
		    output.flush();
177
		} finally {
178
		    if (out != System.out && out != System.err) {
179
			FileUtils.close(out);
180
		    }
181
		}
182
            }
183
        }
184
    }
185
186
    /**
187
     * A test started.
188
     * @param test a test
189
     */
190
    public void startTest(Test test) {
191
    }
192
193
    /**
194
     * A test ended.
195
     * @param test a test
196
     */
197
    public void endTest(Test test) {
198
    }
199
200
    /**
201
     * Interface TestListener for JUnit &lt;= 3.4.
202
     *
203
     * <p>A Test failed.
204
     * @param test a test
205
     * @param t    the exception thrown by the test
206
     */
207
    public void addFailure(Test test, Throwable t) {
208
        formatError("\tFAILED", test, t);
209
    }
210
211
    /**
212
     * Interface TestListener for JUnit &gt; 3.4.
213
     *
214
     * <p>A Test failed.
215
     * @param test a test
216
     * @param t    the assertion failed by the test
217
     */
218
    public void addFailure(Test test, AssertionFailedError t) {
219
        addFailure(test, (Throwable) t);
220
    }
221
222
    /**
223
     * A test caused an error.
224
     * @param test  a test
225
     * @param error the error thrown by the test
226
     */
227
    public void addError(Test test, Throwable error) {
228
        formatError("\tCaused an ERROR", test, error);
229
    }
230
231
    /**
232
     * Format the test for printing..
233
     * @param test a test
234
     * @return the formatted testname
235
     */
236
    protected String formatTest(Test test) {
237
        if (test == null) {
238
            return "Null Test: ";
239
        } else {
240
            return "Testcase: " + test.toString() + ":";
241
        }
242
    }
243
244
    /**
245
     * Format an error and print it.
246
     * @param type the type of error
247
     * @param test the test that failed
248
     * @param error the exception that the test threw
249
     */
250
    protected synchronized void formatError(String type, Test test,
251
                                            Throwable error) {
252
        if (test != null) {
253
            endTest(test);
254
        }
255
256
        resultWriter.println(formatTest(test) + type);
257
        resultWriter.println(error.getMessage());
258
        String strace = JUnitTestRunner.getFilteredTrace(error);
259
        resultWriter.println(strace);
260
        resultWriter.println();
261
    }
262
}

Return to bug 41534