From fe56008d7beb03fca744daa95d6f4d60192864f5 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Sat, 19 Aug 2006 19:13:42 +0000 Subject: [PATCH] factor out some code that may come handy when implementing the XML listener git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk@432863 13f79535-47bb-0310-9956-ffa450edef68 --- .../ant/antunit/BaseAntUnitListener.java | 171 ++++++++++++++++++ .../ant/antunit/PlainAntUnitListener.java | 112 +----------- 2 files changed, 180 insertions(+), 103 deletions(-) create mode 100644 src/main/org/apache/ant/antunit/BaseAntUnitListener.java diff --git a/src/main/org/apache/ant/antunit/BaseAntUnitListener.java b/src/main/org/apache/ant/antunit/BaseAntUnitListener.java new file mode 100644 index 0000000..ad7a0fb --- /dev/null +++ b/src/main/org/apache/ant/antunit/BaseAntUnitListener.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.ant.antunit; + +import java.io.IOException; +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.text.NumberFormat; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.ProjectComponent; +import org.apache.tools.ant.taskdefs.LogOutputStream; +import org.apache.tools.ant.types.EnumeratedAttribute; +import org.apache.tools.ant.util.FileUtils; +import org.apache.tools.ant.util.TeeOutputStream; + +/** + * A test listener for <antunit> modeled aftern the Plain JUnit + * test listener that is part of Ant. + */ +public abstract class BaseAntUnitListener extends ProjectComponent + implements AntUnitListener { + + protected BaseAntUnitListener(SendLogTo defaultReportTarget) { + logTo = defaultReportTarget; + } + + /** + * Formatter for timings. + */ + protected static final NumberFormat nf = NumberFormat.getInstance(); + + /** + * Directory to write reports to. + */ + private File toDir; + + /** + * Directory to write reports to. + */ + protected final File getToDir() { + return toDir; + } + + /** + * Sets the directory to write test reports to. + */ + public void setToDir(File f) { + toDir = f; + } + + /** + * Where to send log. + */ + private SendLogTo logTo; + + /** + * keeps track of the numer of executed targets, the failures an errors. + */ + protected int runCount, failureCount, errorCount; + /** + * time for the starts of the current test-suite and test-target. + */ + protected long start, testStart; + + /** + * Where to send the test report. + */ + protected void setSendLogTo(SendLogTo logTo) { + this.logTo = logTo; + } + + public void startTestSuite(Project testProject, String buildFile) { + start = System.currentTimeMillis(); + runCount = failureCount = errorCount = 0; + } + + protected final void close(OutputStream out) { + if (out != System.out && out != System.err) { + FileUtils.close(out); + } + } + + public void startTest(String target) { + testStart = System.currentTimeMillis(); + runCount++; + } + public void addFailure(String target, AssertionFailedException ae) { + failureCount++; + } + public void addError(String target, Throwable ae) { + errorCount++; + } + + protected final OutputStream getOut(String buildFile) { + OutputStream l, f; + l = f = null; + if (logTo.getValue().equals(SendLogTo.ANT_LOG) + || logTo.getValue().equals(SendLogTo.BOTH)) { + l = new LogOutputStream(this, Project.MSG_INFO); + if (logTo.getValue().equals(SendLogTo.ANT_LOG)) { + return l; + } + } + if (logTo.getValue().equals(SendLogTo.FILE) + || logTo.getValue().equals(SendLogTo.BOTH)) { + + buildFile = FileUtils.getFileUtils() + .removeLeadingPath(getProject().getBaseDir(), + new File(buildFile)); + if (buildFile.length() > 0 + && buildFile.charAt(0) == File.separatorChar) { + buildFile = buildFile.substring(1); + } + + String fileName = "TEST-" + + buildFile.replace(File.separatorChar, '.').replace(':', '.') + + ".txt"; + File file = toDir == null + ? getProject().resolveFile(fileName) + : new File(toDir, fileName); + try { + f = new FileOutputStream(file); + } catch (IOException e) { + throw new BuildException(e); + } + if (logTo.getValue().equals(SendLogTo.FILE)) { + return f; + } + } + return new TeeOutputStream(l, f); + } + + public static class SendLogTo extends EnumeratedAttribute { + public static final String ANT_LOG = "ant"; + public static final String FILE = "file"; + public static final String BOTH = "both"; + + public SendLogTo() {} + + public SendLogTo(String s) { + setValue(s); + } + + public String[] getValues() { + return new String[] {ANT_LOG, FILE, BOTH}; + } + } +} \ No newline at end of file diff --git a/src/main/org/apache/ant/antunit/PlainAntUnitListener.java b/src/main/org/apache/ant/antunit/PlainAntUnitListener.java index 4f7d3bb..fb7bf1c 100644 --- a/src/main/org/apache/ant/antunit/PlainAntUnitListener.java +++ b/src/main/org/apache/ant/antunit/PlainAntUnitListener.java @@ -40,14 +40,7 @@ import org.apache.tools.ant.util.TeeOutputStream; * A test listener for <antunit> modeled aftern the Plain JUnit * test listener that is part of Ant. */ -public class PlainAntUnitListener extends ProjectComponent - implements AntUnitListener { - - /** - * Formatter for timings. - */ - private NumberFormat nf = NumberFormat.getInstance(); - +public class PlainAntUnitListener extends BaseAntUnitListener { private OutputStream out = null; /** * Helper to store intermediate output. @@ -58,43 +51,21 @@ public class PlainAntUnitListener extends ProjectComponent */ private PrintWriter wri; - /** - * Directory to write reports to. - */ - private File toDir; - - /** - * Where to send log. - */ - private SendLogTo logTo = new SendLogTo(SendLogTo.ANT_LOG); - - /** - * keeps track of the numer of executed targets, the failures an errors. - */ - private int runCount, failureCount, errorCount; - /** - * time for the starts of the current test-suite and test-target. - */ - private long start, testStart; - - /** - * Sets the directory to write test reports to. - */ - public void setToDir(File f) { - toDir = f; + public PlainAntUnitListener() { + super(new BaseAntUnitListener.SendLogTo(SendLogTo.ANT_LOG)); } /** * Where to send the test report. */ - public void setSendLogTo(SendLogTo logTo) { - this.logTo = logTo; + public void setSendLogTo(BaseAntUnitListener.SendLogTo logTo) { + super.setSendLogTo(logTo); } public void startTestSuite(Project testProject, String buildFile) { + super.startTestSuite(testProject, buildFile); inner = new StringWriter(); wri = new PrintWriter(inner); - runCount = failureCount = errorCount; out = getOut(buildFile); String newLine = System.getProperty("line.separator"); StringBuffer sb = new StringBuffer("Build File: "); @@ -106,7 +77,6 @@ public class PlainAntUnitListener extends ProjectComponent } catch (IOException ex) { throw new BuildException("Unable to write output", ex); } - start = System.currentTimeMillis(); } public void endTestSuite(Project testProject, String buildFile) { @@ -132,21 +102,11 @@ public class PlainAntUnitListener extends ProjectComponent } catch (IOException ioex) { throw new BuildException("Unable to write output", ioex); } finally { - if (out != System.out && out != System.err) { - try { - out.close(); - } catch (IOException e) { - // ignore - } - } + close(out); } } } - public void startTest(String target) { - testStart = System.currentTimeMillis(); - runCount++; - } public void endTest(String target) { wri.print("Target: " + target); double seconds = (System.currentTimeMillis() - testStart) / 1000.0; @@ -154,11 +114,11 @@ public class PlainAntUnitListener extends ProjectComponent } public void addFailure(String target, AssertionFailedException ae) { - failureCount++; + super.addFailure(target, ae); formatError("\tFAILED", ae); } public void addError(String target, Throwable ae) { - errorCount++; + super.addError(target, ae); formatError("\tCaused an ERROR", ae); } @@ -167,58 +127,4 @@ public class PlainAntUnitListener extends ProjectComponent wri.println(t.getMessage()); } - private OutputStream getOut(String buildFile) { - OutputStream l, f; - l = f = null; - if (logTo.getValue().equals(SendLogTo.ANT_LOG) - || logTo.getValue().equals(SendLogTo.BOTH)) { - l = new LogOutputStream(this, Project.MSG_INFO); - if (logTo.getValue().equals(SendLogTo.ANT_LOG)) { - return l; - } - } - if (logTo.getValue().equals(SendLogTo.FILE) - || logTo.getValue().equals(SendLogTo.BOTH)) { - - buildFile = FileUtils.getFileUtils() - .removeLeadingPath(getProject().getBaseDir(), - new File(buildFile)); - if (buildFile.length() > 0 - && buildFile.charAt(0) == File.separatorChar) { - buildFile = buildFile.substring(1); - } - - String fileName = "TEST-" + - buildFile.replace(File.separatorChar, '.').replace(':', '.') - + ".txt"; - File file = toDir == null - ? getProject().resolveFile(fileName) - : new File(toDir, fileName); - try { - f = new FileOutputStream(file); - } catch (IOException e) { - throw new BuildException(e); - } - if (logTo.getValue().equals(SendLogTo.FILE)) { - return f; - } - } - return new TeeOutputStream(l, f); - } - - public static class SendLogTo extends EnumeratedAttribute { - public static final String ANT_LOG = "ant"; - public static final String FILE = "file"; - public static final String BOTH = "both"; - - public SendLogTo() {} - - public SendLogTo(String s) { - setValue(s); - } - - public String[] getValues() { - return new String[] {ANT_LOG, FILE, BOTH}; - } - } } \ No newline at end of file