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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.apache.lucene.util;
21 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.text.NumberFormat;
26 import java.util.logging.LogManager;
28 import junit.framework.AssertionFailedError;
29 import junit.framework.Test;
31 import org.apache.lucene.store.LockReleaseFailedException;
32 import org.apache.lucene.store.NativeFSLockFactory;
33 import org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter;
34 import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;
35 import org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner;
36 import org.apache.tools.ant.util.FileUtils;
37 import org.apache.tools.ant.util.StringUtils;
38 import org.junit.Ignore;
41 * Just like BriefJUnitResultFormatter "brief" bundled with ant,
42 * except all formatted text is buffered until the test suite is finished.
43 * At this point, the output is written at once in synchronized fashion.
44 * This way tests can run in parallel without interleaving output.
46 public class LuceneJUnitResultFormatter implements JUnitResultFormatter {
47 private static final double ONE_SECOND = 1000.0;
49 private static final NativeFSLockFactory lockFactory;
51 /** Where to write the log to. */
52 private OutputStream out;
54 /** Formatter for timings. */
55 private NumberFormat numberFormat = NumberFormat.getInstance();
57 /** Output suite has written to System.out */
58 private String systemOutput = null;
60 /** Output suite has written to System.err */
61 private String systemError = null;
63 /** Buffer output until the end of the test */
64 private ByteArrayOutputStream sb; // use a BOS for our mostly ascii-output
66 private static final org.apache.lucene.store.Lock lock;
69 File lockDir = new File(System.getProperty("java.io.tmpdir"),
72 if (!lockDir.exists()) {
73 throw new RuntimeException("Could not make Lock directory:" + lockDir);
76 lockFactory = new NativeFSLockFactory(lockDir);
77 lock = lockFactory.makeLock("junit_lock");
78 } catch (IOException e) {
79 throw new RuntimeException(e);
83 /** Constructor for LuceneJUnitResultFormatter. */
84 public LuceneJUnitResultFormatter() {
88 * Sets the stream the formatter is supposed to write its results to.
89 * @param out the output stream to write to
91 public void setOutput(OutputStream out) {
96 * @see JUnitResultFormatter#setSystemOutput(String)
99 public void setSystemOutput(String out) {
104 * @see JUnitResultFormatter#setSystemError(String)
106 /** {@inheritDoc}. */
107 public void setSystemError(String err) {
113 * The whole testsuite started.
114 * @param suite the test suite
116 public synchronized void startTestSuite(JUnitTest suite) {
118 return; // Quick return - no output do nothing.
120 sb = new ByteArrayOutputStream(); // don't reuse, so its gc'ed
122 LogManager.getLogManager().readConfiguration();
123 } catch (Exception e) {}
124 append("Testsuite: ");
125 append(suite.getName());
126 append(StringUtils.LINE_SEP);
130 * The whole testsuite ended.
131 * @param suite the test suite
133 public synchronized void endTestSuite(JUnitTest suite) {
134 append("Tests run: ");
135 append(suite.runCount());
136 append(", Failures: ");
137 append(suite.failureCount());
138 append(", Errors: ");
139 append(suite.errorCount());
140 append(", Time elapsed: ");
141 append(numberFormat.format(suite.getRunTime() / ONE_SECOND));
143 append(StringUtils.LINE_SEP);
144 append(StringUtils.LINE_SEP);
146 // append the err and output streams to the log
147 if (systemOutput != null && systemOutput.length() > 0) {
148 append("------------- Standard Output ---------------")
149 .append(StringUtils.LINE_SEP)
150 .append(systemOutput)
151 .append("------------- ---------------- ---------------")
152 .append(StringUtils.LINE_SEP);
155 // HACK: junit gives us no way to do this in LuceneTestCase
157 Class<?> clazz = Class.forName(suite.getName());
158 Ignore ignore = clazz.getAnnotation(Ignore.class);
159 if (ignore != null) {
160 if (systemError == null) systemError = "";
161 systemError += "NOTE: Ignoring test class '" + clazz.getSimpleName() + "': "
162 + ignore.value() + StringUtils.LINE_SEP;
164 } catch (ClassNotFoundException e) { /* no problem */ }
167 if (systemError != null && systemError.length() > 0) {
168 append("------------- Standard Error -----------------")
169 .append(StringUtils.LINE_SEP)
171 .append("------------- ---------------- ---------------")
172 .append(StringUtils.LINE_SEP);
184 } catch(LockReleaseFailedException e) {
185 // well lets pretend its released anyway
188 } catch (IOException e) {
189 throw new RuntimeException("unable to write results", e);
191 if (out != System.out && out != System.err) {
192 FileUtils.close(out);
202 public void startTest(Test test) {
209 public void endTest(Test test) {
213 * Interface TestListener for JUnit <= 3.4.
217 * @param t the exception thrown by the test
219 public void addFailure(Test test, Throwable t) {
220 formatError("\tFAILED", test, t);
224 * Interface TestListener for JUnit > 3.4.
228 * @param t the assertion failed by the test
230 public void addFailure(Test test, AssertionFailedError t) {
231 addFailure(test, (Throwable) t);
235 * A test caused an error.
237 * @param error the error thrown by the test
239 public void addError(Test test, Throwable error) {
240 formatError("\tCaused an ERROR", test, error);
244 * Format the test for printing..
246 * @return the formatted testname
248 protected String formatTest(Test test) {
250 return "Null Test: ";
252 return "Testcase: " + test.toString() + ":";
257 * Format an error and print it.
258 * @param type the type of error
259 * @param test the test that failed
260 * @param error the exception that the test threw
262 protected synchronized void formatError(String type, Test test,
268 append(formatTest(test) + type);
269 append(StringUtils.LINE_SEP);
270 append(error.getMessage());
271 append(StringUtils.LINE_SEP);
272 String strace = JUnitTestRunner.getFilteredTrace(error);
274 append(StringUtils.LINE_SEP);
275 append(StringUtils.LINE_SEP);
278 public LuceneJUnitResultFormatter append(String s) {
282 sb.write(s.getBytes()); // intentionally use default charset, its a console.
283 } catch (IOException e) {
284 throw new RuntimeException(e);
289 public LuceneJUnitResultFormatter append(long l) {
290 return append(Long.toString(l));