1 | /*
|
---|
2 | * Copyright 2000-2002,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 |
|
---|
18 | package org.apache.tools.ant.taskdefs.optional.junit;
|
---|
19 |
|
---|
20 | import java.io.IOException;
|
---|
21 | import java.io.OutputStream;
|
---|
22 | import java.text.NumberFormat;
|
---|
23 | import junit.framework.AssertionFailedError;
|
---|
24 | import junit.framework.Test;
|
---|
25 | import org.apache.tools.ant.BuildException;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Prints short summary output of the test to Ant's logging system.
|
---|
29 | *
|
---|
30 | */
|
---|
31 |
|
---|
32 | public class SummaryJUnitResultFormatter implements JUnitResultFormatter {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Formatter for timings.
|
---|
36 | */
|
---|
37 | private NumberFormat nf = NumberFormat.getInstance();
|
---|
38 | /**
|
---|
39 | * OutputStream to write to.
|
---|
40 | */
|
---|
41 | private OutputStream out;
|
---|
42 |
|
---|
43 | private boolean withOutAndErr = false;
|
---|
44 | private String systemOutput = null;
|
---|
45 | private String systemError = null;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Empty
|
---|
49 | */
|
---|
50 | public SummaryJUnitResultFormatter() {
|
---|
51 | }
|
---|
52 | /**
|
---|
53 | * Empty
|
---|
54 | */
|
---|
55 | public void startTestSuite(JUnitTest suite) {
|
---|
56 | }
|
---|
57 | /**
|
---|
58 | * Empty
|
---|
59 | */
|
---|
60 | public void startTest(Test t) {
|
---|
61 | }
|
---|
62 | /**
|
---|
63 | * Empty
|
---|
64 | */
|
---|
65 | public void endTest(Test test) {
|
---|
66 | }
|
---|
67 | /**
|
---|
68 | * Empty
|
---|
69 | */
|
---|
70 | public void addFailure(Test test, Throwable t) {
|
---|
71 | }
|
---|
72 | /**
|
---|
73 | * Interface TestListener for JUnit > 3.4.
|
---|
74 | *
|
---|
75 | * <p>A Test failed.
|
---|
76 | */
|
---|
77 | public void addFailure(Test test, AssertionFailedError t) {
|
---|
78 | addFailure(test, (Throwable) t);
|
---|
79 | }
|
---|
80 | /**
|
---|
81 | * Empty
|
---|
82 | */
|
---|
83 | public void addError(Test test, Throwable t) {
|
---|
84 | }
|
---|
85 |
|
---|
86 | public void setOutput(OutputStream out) {
|
---|
87 | this.out = out;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void setSystemOutput(String out) {
|
---|
91 | systemOutput = out;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void setSystemError(String err) {
|
---|
95 | systemError = err;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Should the output to System.out and System.err be written to
|
---|
100 | * the summary.
|
---|
101 | */
|
---|
102 | public void setWithOutAndErr(boolean value) {
|
---|
103 | withOutAndErr = value;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * The whole testsuite ended.
|
---|
108 | */
|
---|
109 | public void endTestSuite(JUnitTest suite) throws BuildException {
|
---|
110 | String newLine = System.getProperty("line.separator");
|
---|
111 | StringBuffer sb = new StringBuffer("Tests run: ");
|
---|
112 | sb.append(suite.runCount());
|
---|
113 | sb.append(", Failures: ");
|
---|
114 | sb.append(suite.failureCount());
|
---|
115 | sb.append(", Errors: ");
|
---|
116 | sb.append(suite.errorCount());
|
---|
117 | sb.append(", Time elapsed: ");
|
---|
118 | sb.append(nf.format(suite.getRunTime() / 1000.0));
|
---|
119 | sb.append(" sec");
|
---|
120 | sb.append(newLine);
|
---|
121 |
|
---|
122 | if (withOutAndErr) {
|
---|
123 | if (systemOutput != null && systemOutput.length() > 0) {
|
---|
124 | sb.append("Output:").append(newLine).append(systemOutput)
|
---|
125 | .append(newLine);
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (systemError != null && systemError.length() > 0) {
|
---|
129 | sb.append("Error: ").append(newLine).append(systemError)
|
---|
130 | .append(newLine);
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | try {
|
---|
135 | out.write(sb.toString().getBytes());
|
---|
136 | out.flush();
|
---|
137 | } catch (IOException ioex) {
|
---|
138 | throw new BuildException("Unable to write summary output", ioex);
|
---|
139 | } finally {
|
---|
140 | if (out != System.out && out != System.err) {
|
---|
141 | try {
|
---|
142 | out.close();
|
---|
143 | } catch (IOException e) {
|
---|
144 | // ignore
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|