- Refactored some code and moved protected fields to private git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270745 13f79535-47bb-0310-9956-ffa450edef68master
@@ -53,20 +53,18 @@ | |||
*/ | |||
package org.apache.tools.ant.taskdefs.optional.junit; | |||
import java.io.File; | |||
import java.util.Enumeration; | |||
import java.util.Vector; | |||
import java.util.Hashtable; | |||
import java.util.zip.ZipFile; | |||
import java.util.zip.ZipEntry; | |||
import java.io.File; | |||
import java.util.Vector; | |||
import junit.runner.TestCollector; | |||
import org.apache.tools.ant.types.PatternSet; | |||
import org.apache.tools.ant.types.Path; | |||
import org.apache.tools.ant.ProjectComponent; | |||
import org.apache.tools.ant.DirectoryScanner; | |||
import org.apache.tools.ant.Project; | |||
import org.apache.tools.ant.ProjectComponent; | |||
import org.apache.tools.ant.types.Path; | |||
import org.apache.tools.ant.types.PatternSet; | |||
/** | |||
* A rough implementation of a test collector that will collect tests | |||
@@ -78,7 +76,7 @@ import org.apache.tools.ant.Project; | |||
public class ClasspathTestCollector extends ProjectComponent | |||
implements TestCollector { | |||
private final static int SUFFIX_LENGTH= ".class".length(); | |||
private final static int SUFFIX_LENGTH = ".class".length(); | |||
private PatternSet patterns = new PatternSet(); | |||
@@ -90,21 +88,21 @@ public class ClasspathTestCollector extends ProjectComponent | |||
// override last one in case there are duplicates. | |||
// ie mimic classpath behavior. | |||
String[] paths = path.list(); | |||
for (int i = paths.length; i >= 0; i--){ | |||
for (int i = paths.length; i >= 0; i--) { | |||
File f = new File(paths[i]); | |||
Vector included = null; | |||
if ( f.isDirectory() ){ | |||
if (f.isDirectory()) { | |||
included = gatherFromDirectory(f); | |||
} else if ( f.getName().endsWith(".zip") | |||
|| f.getName().endsWith(".jar") ) { | |||
} else if (f.getName().endsWith(".zip") | |||
|| f.getName().endsWith(".jar")) { | |||
included = gatherFromArchive(f); | |||
} else { | |||
continue; | |||
} | |||
// add tests to the already collected one | |||
final int includedCount = included.size(); | |||
for (int j = 0; j < includedCount; j++){ | |||
String testname = (String)included.elementAt(i); | |||
for (int j = 0; j < includedCount; j++) { | |||
String testname = (String) included.elementAt(i); | |||
collected.put(testname, ""); | |||
} | |||
} | |||
@@ -112,7 +110,7 @@ public class ClasspathTestCollector extends ProjectComponent | |||
} | |||
protected Vector gatherFromDirectory(File dir){ | |||
protected Vector gatherFromDirectory(File dir) { | |||
Project project = getProject(); | |||
DirectoryScanner ds = new DirectoryScanner(); | |||
ds.setBasedir(dir); | |||
@@ -123,7 +121,7 @@ public class ClasspathTestCollector extends ProjectComponent | |||
return testClassNameFromFile(included); | |||
} | |||
protected Vector gatherFromArchive(File zip){ | |||
protected Vector gatherFromArchive(File zip) { | |||
ZipScanner zs = new ZipScanner(); | |||
zs.setBasedir(zip); | |||
zs.setIncludes(patterns.getIncludePatterns(project)); | |||
@@ -133,11 +131,11 @@ public class ClasspathTestCollector extends ProjectComponent | |||
return testClassNameFromFile(included); | |||
} | |||
protected Vector testClassNameFromFile(String[] classFileNames){ | |||
protected Vector testClassNameFromFile(String[] classFileNames) { | |||
Vector tests = new Vector(classFileNames.length); | |||
for (int i = 0; i < classFileNames.length; i++){ | |||
for (int i = 0; i < classFileNames.length; i++) { | |||
String file = classFileNames[i]; | |||
if ( isTestClass(file) ){ | |||
if (isTestClass(file)) { | |||
String classname = classNameFromFile(file); | |||
tests.addElement(classname); | |||
} | |||
@@ -146,30 +144,30 @@ public class ClasspathTestCollector extends ProjectComponent | |||
} | |||
protected boolean isTestClass(String classFileName) { | |||
return classFileName.endsWith(".class"); | |||
} | |||
protected String classNameFromFile(String classFileName) { | |||
// convert /a/b.class to a.b | |||
String s= classFileName.substring(0, classFileName.length()-SUFFIX_LENGTH); | |||
String s2= s.replace(File.separatorChar, '.'); | |||
if ( s2.startsWith(".") ){ | |||
s2 = s2.substring(1); | |||
return classFileName.endsWith(".class"); | |||
} | |||
protected String classNameFromFile(String classFileName) { | |||
// convert /a/b.class to a.b | |||
String s = classFileName.substring(0, classFileName.length() - SUFFIX_LENGTH); | |||
String s2 = s.replace(File.separatorChar, '.'); | |||
if (s2.startsWith(".")) { | |||
s2 = s2.substring(1); | |||
} | |||
return s2; | |||
} | |||
return s2; | |||
} | |||
// Ant bean accessors | |||
public void setPath(Path path){ | |||
public void setPath(Path path) { | |||
this.path = path; | |||
} | |||
public PatternSet.NameEntry createInclude(){ | |||
public PatternSet.NameEntry createInclude() { | |||
return patterns.createInclude(); | |||
} | |||
public PatternSet.NameEntry createExclude(){ | |||
public PatternSet.NameEntry createExclude() { | |||
return patterns.createExclude(); | |||
} | |||
@@ -105,7 +105,7 @@ public class FilterElement { | |||
try { | |||
Class clazz = Class.forName(classname); | |||
if (!FilterFormatter.class.isAssignableFrom(clazz)) { | |||
throw new BuildException( clazz + " must be a FilterFormatter."); | |||
throw new BuildException(clazz + " must be a FilterFormatter."); | |||
} | |||
Constructor ctor = clazz.getConstructor(new Class[]{Formatter.class}); | |||
return (Formatter) ctor.newInstance(new Object[]{f}); | |||
@@ -53,14 +53,13 @@ | |||
*/ | |||
package org.apache.tools.ant.taskdefs.optional.junit; | |||
import java.lang.reflect.Method; | |||
import java.io.File; | |||
import java.lang.reflect.Method; | |||
import java.net.URL; | |||
import junit.framework.Test; | |||
import junit.framework.TestSuite; | |||
import org.apache.tools.ant.Project; | |||
import org.apache.tools.ant.types.Path; | |||
/** | |||
@@ -78,12 +77,12 @@ public final class JUnitHelper { | |||
* <tt>name(classname)</tt> | |||
* @return an array with the elements in the order name, classname. | |||
*/ | |||
public static String[] parseTestString(String testname){ | |||
public static String[] parseTestString(String testname) { | |||
int p1 = testname.indexOf('('); | |||
int p2 = testname.indexOf(')', p1); | |||
return new String[]{ | |||
testname.substring(0, p1), | |||
testname.substring(p1 + 1, p2) }; | |||
testname.substring(p1 + 1, p2)}; | |||
} | |||
/** | |||
@@ -110,10 +109,10 @@ public final class JUnitHelper { | |||
public static Test getTest(Class clazz) { | |||
try { | |||
Object obj = clazz.newInstance(); | |||
if (obj instanceof TestSuite){ | |||
return (TestSuite) obj; | |||
if (obj instanceof TestSuite) { | |||
return (TestSuite) obj; | |||
} | |||
} catch (Exception e){ | |||
} catch (Exception e) { | |||
} | |||
try { | |||
// check if there is a suite method | |||
@@ -125,7 +124,7 @@ public final class JUnitHelper { | |||
// this will generate warnings if the class is no suitable Test | |||
try { | |||
return new TestSuite(clazz); | |||
} catch (Exception e){ | |||
} catch (Exception e) { | |||
} | |||
return null; | |||
} | |||
@@ -141,7 +140,7 @@ public final class JUnitHelper { | |||
* @return the file or directory containing the resource or | |||
* <tt>null</tt> if it does not know how to handle it. | |||
*/ | |||
public static File getResourceEntry(String resource){ | |||
public static File getResourceEntry(String resource) { | |||
URL url = JUnitHelper.class.getResource(resource); | |||
if (url != null) { | |||
// can't find the resource... | |||
@@ -169,7 +168,7 @@ public final class JUnitHelper { | |||
* @param resource the resource to look for. | |||
* @see #getResourceEntry(String) | |||
*/ | |||
public static void addClasspathEntry(Path path, String resource){ | |||
public static void addClasspathEntry(Path path, String resource) { | |||
File f = getResourceEntry(resource); | |||
if (f != null) { | |||
path.createPathElement().setLocation(f); | |||
@@ -58,18 +58,18 @@ import java.io.File; | |||
import java.io.FileOutputStream; | |||
import java.io.IOException; | |||
import java.io.OutputStream; | |||
import java.util.Enumeration; | |||
import java.util.Properties; | |||
import java.util.Vector; | |||
import java.util.Enumeration; | |||
import junit.runner.TestCollector; | |||
import org.apache.tools.ant.BuildException; | |||
import org.apache.tools.ant.Project; | |||
import org.apache.tools.ant.Task; | |||
import org.apache.tools.ant.taskdefs.optional.junit.formatter.Formatter; | |||
import org.apache.tools.ant.taskdefs.Execute; | |||
import org.apache.tools.ant.taskdefs.LogStreamHandler; | |||
import org.apache.tools.ant.taskdefs.optional.junit.formatter.Formatter; | |||
import org.apache.tools.ant.types.Commandline; | |||
import org.apache.tools.ant.types.CommandlineJava; | |||
import org.apache.tools.ant.types.Path; | |||
@@ -136,8 +136,8 @@ public class JUnitTask extends Task { | |||
// get all test classes to run... | |||
StringBuffer buf = new StringBuffer(10240); | |||
Enumeration classnames = collectTests(); | |||
while ( classnames.hasMoreElements() ){ | |||
String classname = (String)classnames.nextElement(); | |||
while (classnames.hasMoreElements()) { | |||
String classname = (String) classnames.nextElement(); | |||
buf.append(classname).append(" "); | |||
} | |||
props.setProperty("classnames", buf.toString()); | |||
@@ -170,10 +170,10 @@ public class JUnitTask extends Task { | |||
/** | |||
* @return all collected tests specified with test elements. | |||
*/ | |||
protected Enumeration collectTests(){ | |||
protected Enumeration collectTests() { | |||
Enumeration[] tests = new Enumeration[testCollectors.size()]; | |||
for (int i = 0; i < testCollectors.size(); i++){ | |||
TestCollector te = (TestCollector)testCollectors.elementAt(i); | |||
for (int i = 0; i < testCollectors.size(); i++) { | |||
TestCollector te = (TestCollector) testCollectors.elementAt(i); | |||
tests[i] = te.collectTests(); | |||
} | |||
return Enumerations.fromCompound(tests); | |||
@@ -54,8 +54,8 @@ | |||
package org.apache.tools.ant.taskdefs.optional.junit; | |||
import java.io.FilterOutputStream; | |||
import java.io.OutputStream; | |||
import java.io.IOException; | |||
import java.io.OutputStream; | |||
/** | |||
* Class that can be used to wrap <tt>System.out</tt> and <tt>System.err</tt> | |||
@@ -70,12 +70,12 @@ public class TestElement implements TestCollector { | |||
//@fixme, a path is needed for a test. | |||
public Enumeration collectTests() { | |||
return Enumerations.fromArray( new String[]{ name } ); | |||
return Enumerations.fromArray(new String[]{name}); | |||
} | |||
// Ant bean setters | |||
public String setName(String value){ | |||
public void setName(String value) { | |||
this.name = value; | |||
} | |||
} |
@@ -69,7 +69,7 @@ import junit.framework.TestResult; | |||
public class WatchdogTest extends TestDecorator { | |||
/** the time out delay in msecs */ | |||
protected long timeOut; | |||
private long timeOut; | |||
/** | |||
* Create a new watchdog. | |||
@@ -55,14 +55,12 @@ package org.apache.tools.ant.taskdefs.optional.junit; | |||
import java.io.File; | |||
import java.io.IOException; | |||
import java.util.Vector; | |||
import java.util.Enumeration; | |||
import java.util.zip.ZipFile; | |||
import java.util.Vector; | |||
import java.util.zip.ZipEntry; | |||
import java.util.zip.ZipFile; | |||
import org.apache.tools.ant.DirectoryScanner; | |||
import org.apache.tools.ant.FileScanner; | |||
import org.apache.tools.ant.BuildException; | |||
/** | |||
* Provide a way to scan entries in a zip file. Note that it extends | |||
@@ -93,10 +91,10 @@ public class ZipScanner extends DirectoryScanner { | |||
* normalize a set of paths so that it uses / otherwise matching will | |||
* fail beautifully since archives use / to denote a path. | |||
*/ | |||
protected void normalize(String[] files){ | |||
if (files != null){ | |||
for (int i = 0; i < files.length; i++){ | |||
files[i] = files[i].replace('\\','/'); | |||
protected void normalize(String[] files) { | |||
if (files != null) { | |||
for (int i = 0; i < files.length; i++) { | |||
files[i] = files[i].replace('\\', '/'); | |||
} | |||
} | |||
} | |||
@@ -113,11 +111,11 @@ public class ZipScanner extends DirectoryScanner { | |||
} | |||
if (!basedir.exists()) { | |||
throw new IllegalStateException("zipfile " + basedir | |||
+ " does not exist"); | |||
+ " does not exist"); | |||
} | |||
if (basedir.isDirectory()) { | |||
throw new IllegalStateException("zipfile " + basedir | |||
+ " is not a file"); | |||
+ " is not a file"); | |||
} | |||
if (includes == null) { | |||
@@ -129,12 +127,12 @@ public class ZipScanner extends DirectoryScanner { | |||
excludes = new String[0]; | |||
} | |||
filesIncluded = new Vector(); | |||
filesIncluded = new Vector(); | |||
filesNotIncluded = new Vector(); | |||
filesExcluded = new Vector(); | |||
dirsIncluded = new Vector(); | |||
dirsNotIncluded = new Vector(); | |||
dirsExcluded = new Vector(); | |||
filesExcluded = new Vector(); | |||
dirsIncluded = new Vector(); | |||
dirsNotIncluded = new Vector(); | |||
dirsExcluded = new Vector(); | |||
if (isIncluded("")) { | |||
if (!isExcluded("")) { | |||
@@ -152,13 +150,13 @@ public class ZipScanner extends DirectoryScanner { | |||
ZipFile zip = null; | |||
try { | |||
zip = new ZipFile(file); | |||
} catch (IOException e){ | |||
} catch (IOException e) { | |||
throw new IllegalStateException(e.getMessage()); | |||
} | |||
Enumeration entries = zip.entries(); | |||
while ( entries.hasMoreElements() ) { | |||
ZipEntry entry = (ZipEntry)entries.nextElement(); | |||
while (entries.hasMoreElements()) { | |||
ZipEntry entry = (ZipEntry) entries.nextElement(); | |||
String name = entry.getName(); | |||
// @fixme do we need to strip out entries that starts | |||
// with . or ./ ? | |||
@@ -67,23 +67,22 @@ import java.util.Properties; | |||
*/ | |||
public abstract class BaseFormatter implements Formatter { | |||
protected OutputStream out; | |||
/** writer to output the data to */ | |||
private PrintWriter writer; | |||
protected PrintWriter writer; | |||
/** number of errors */ | |||
private int errorCount; | |||
protected int errorCount; | |||
/** number of failures */ | |||
private int failureCount; | |||
protected int failureCount; | |||
protected int runCount; | |||
protected Properties props; | |||
/** number of runs (success + failure + error) */ | |||
private int runCount; | |||
public void setOutput(OutputStream value) { | |||
out = value; | |||
try { | |||
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, "UTF8")), true); | |||
} catch (IOException e){ | |||
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(value, "UTF8")), true); | |||
} catch (IOException e) { | |||
// should not happen | |||
throw new IllegalStateException(e.getMessage()); | |||
} | |||
@@ -139,22 +138,33 @@ public abstract class BaseFormatter implements Formatter { | |||
close(); | |||
} | |||
/** helper method to flush and close all streams */ | |||
/** | |||
* @return the writer used to print data. | |||
*/ | |||
protected final PrintWriter getWriter() { | |||
return writer; | |||
} | |||
/** @return the number of errors */ | |||
protected final int getErrorCount() { | |||
return errorCount; | |||
} | |||
/** @return the number of failures */ | |||
protected final int getFailureCount() { | |||
return failureCount; | |||
} | |||
/** @return the number of runs */ | |||
protected final int getRunCount() { | |||
return runCount; | |||
} | |||
/** helper method to flush and close the stream */ | |||
protected void close() { | |||
try { | |||
if (writer != null) { | |||
writer.flush(); | |||
writer = null; | |||
} | |||
} finally { | |||
// make sure we're not closing System.out or System.err... | |||
if (out != null && out != System.err && out != System.out) { | |||
try { | |||
out.close(); | |||
out = null; | |||
} catch (IOException e) { | |||
} | |||
} | |||
if (writer != null) { | |||
writer.flush(); | |||
writer.close(); | |||
} | |||
} | |||
} |
@@ -53,6 +53,8 @@ | |||
*/ | |||
package org.apache.tools.ant.taskdefs.optional.junit.formatter; | |||
import java.io.PrintWriter; | |||
/** | |||
* Display additional messages from a <tt>SummaryFormatter</tt> | |||
@@ -63,6 +65,7 @@ package org.apache.tools.ant.taskdefs.optional.junit.formatter; | |||
public class BriefFormatter extends SummaryFormatter { | |||
public void onTestFailed(int status, String testname, String trace) { | |||
PrintWriter writer = getWriter(); | |||
writer.print("TestCase: "); | |||
writer.print(testname); | |||
if (status == STATUS_ERROR) { | |||
@@ -65,7 +65,7 @@ public abstract class FilterFormatter implements Formatter { | |||
protected Formatter formatter; | |||
protected FilterFormatter(Formatter value){ | |||
protected FilterFormatter(Formatter value) { | |||
formatter = value; | |||
} | |||
@@ -53,10 +53,6 @@ | |||
*/ | |||
package org.apache.tools.ant.taskdefs.optional.junit.formatter; | |||
import java.io.StringWriter; | |||
import java.io.PrintWriter; | |||
import java.io.StringReader; | |||
import java.io.BufferedReader; | |||
import java.util.StringTokenizer; | |||
import org.apache.tools.ant.util.StringUtils; | |||
@@ -83,32 +79,32 @@ import org.apache.tools.ant.util.StringUtils; | |||
public class FilterStackFormatter extends FilterFormatter { | |||
/** the set of matches to look for in a stack trace */ | |||
private final static String[] DEFAULT_TRACE_FILTERS = new String[] { | |||
"junit.framework.TestCase", | |||
"junit.framework.TestResult", | |||
"junit.framework.TestSuite", | |||
"junit.framework.Assert.", // don't filter AssertionFailure | |||
"junit.swingui.TestRunner", | |||
"junit.awtui.TestRunner", | |||
"junit.textui.TestRunner", | |||
"java.lang.reflect.Method.invoke(", | |||
"org.apache.tools.ant." | |||
}; | |||
private final static String[] DEFAULT_TRACE_FILTERS = new String[]{ | |||
"junit.framework.TestCase", | |||
"junit.framework.TestResult", | |||
"junit.framework.TestSuite", | |||
"junit.framework.Assert.", // don't filter AssertionFailure | |||
"junit.swingui.TestRunner", | |||
"junit.awtui.TestRunner", | |||
"junit.textui.TestRunner", | |||
"java.lang.reflect.Method.invoke(", | |||
"org.apache.tools.ant." | |||
}; | |||
/** | |||
* Creates a new <tt>FilterStackFormatter</tt> | |||
* @param formatter the formatter to be filtered. | |||
*/ | |||
public FilterStackFormatter(Formatter formatter){ | |||
public FilterStackFormatter(Formatter formatter) { | |||
super(formatter); | |||
} | |||
public void onTestFailed(int status, String testname, String trace) { | |||
StringTokenizer st = new StringTokenizer(trace,"\r\n"); | |||
StringTokenizer st = new StringTokenizer(trace, "\r\n"); | |||
StringBuffer buf = new StringBuffer(trace.length()); | |||
while ( st.hasMoreTokens() ){ | |||
while (st.hasMoreTokens()) { | |||
String line = st.nextToken(); | |||
if ( accept(line) ){ | |||
if (accept(line)) { | |||
buf.append(line).append(StringUtils.LINE_SEP); | |||
} | |||
} | |||
@@ -120,7 +116,7 @@ public class FilterStackFormatter extends FilterFormatter { | |||
* @param the line to be check for acceptance. | |||
* @return <tt>true</tt> if the line is accepted, <tt>false</tt> if not. | |||
*/ | |||
protected boolean accept(String line){ | |||
protected boolean accept(String line) { | |||
for (int i = 0; i < DEFAULT_TRACE_FILTERS.length; i++) { | |||
if (line.indexOf(DEFAULT_TRACE_FILTERS[i]) > 0) { | |||
return false; | |||
@@ -65,16 +65,16 @@ public interface Formatter extends TestRunListener { | |||
/** | |||
* Sets the stream the formatter is supposed to write its results to. | |||
*/ | |||
public void setOutput( OutputStream out ); | |||
public void setOutput(OutputStream out); | |||
/** | |||
* This is what the test has written to System.out | |||
*/ | |||
public void setSystemOutput( String out ); | |||
public void setSystemOutput(String out); | |||
/** | |||
* This is what the test has written to System.err | |||
*/ | |||
public void setSystemError( String err ); | |||
public void setSystemError(String err); | |||
} |
@@ -53,8 +53,8 @@ | |||
*/ | |||
package org.apache.tools.ant.taskdefs.optional.junit.formatter; | |||
import java.io.PrintWriter; | |||
import java.text.MessageFormat; | |||
import java.util.ResourceBundle; | |||
/** | |||
* Display a summary message at the end of a testsuite stating | |||
@@ -64,19 +64,20 @@ import java.util.ResourceBundle; | |||
*/ | |||
public class SummaryFormatter extends BaseFormatter { | |||
protected MessageFormat mf = new MessageFormat( | |||
protected final MessageFormat mf = new MessageFormat( | |||
"Tests run: {0, number, integer}" + | |||
", Failures: {1, number, integer}" + | |||
", Errors: {2, number, integer}" + | |||
", Time elapsed: {3, number, integer} sec"); | |||
protected void finished(long elapsedtime) { | |||
PrintWriter writer = getWriter(); | |||
writer.print("Testsuite: "); | |||
writer.println(); | |||
String line = mf.format(new Object[]{ | |||
new Integer(runCount), | |||
new Integer(failureCount), | |||
new Integer(errorCount), | |||
new Integer(getRunCount()), | |||
new Integer(getFailureCount()), | |||
new Integer(getErrorCount()), | |||
new Long(elapsedtime / 1000) | |||
}); | |||
writer.print(line); | |||
@@ -61,8 +61,6 @@ import org.w3c.dom.Document; | |||
import org.w3c.dom.Element; | |||
import org.w3c.dom.Text; | |||
import org.apache.tools.ant.taskdefs.optional.junit.TestRunListener; | |||
/** | |||
* | |||
* @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a> | |||
@@ -148,12 +146,12 @@ public class XMLFormatter extends BaseFormatter { | |||
public void onTestEnded(String testname) { | |||
Element currentTest = (Element) testElements.get(testname); | |||
// with a TestSetup, startTest and endTest are not called. | |||
if (currentTest == null){ | |||
if (currentTest == null) { | |||
onTestStarted(testname); | |||
currentTest = (Element) testElements.get(testname); | |||
} | |||
Long l = (Long) testStarts.get(testname); | |||
float time = ((System.currentTimeMillis()-l.longValue()) / 1000.0f); | |||
float time = ((System.currentTimeMillis() - l.longValue()) / 1000.0f); | |||
currentTest.setAttribute(ATTR_TIME, Float.toString(time)); | |||
super.onTestEnded(testname); | |||
// remove the test objects | |||
@@ -201,23 +199,23 @@ public class XMLFormatter extends BaseFormatter { | |||
private static DocumentBuilder getDocumentBuilder() { | |||
try { | |||
return DocumentBuilderFactory.newInstance().newDocumentBuilder(); | |||
} catch(Exception exc) { | |||
} catch (Exception exc) { | |||
throw new ExceptionInInitializerError(exc); | |||
} | |||
} | |||
protected static String[] parseFirstLine(String trace){ | |||
protected static String[] parseFirstLine(String trace) { | |||
int pos = trace.indexOf('\n'); | |||
if (pos == -1){ | |||
return new String[]{ trace, ""}; | |||
if (pos == -1) { | |||
return new String[]{trace, ""}; | |||
} | |||
String line = trace.substring(0, pos); | |||
pos = line.indexOf(':'); | |||
if (pos != -1){ | |||
if (pos != -1) { | |||
String classname = line.substring(0, pos).trim(); | |||
String message = line.substring(pos + 1).trim(); | |||
return new String[]{ classname, message }; | |||
return new String[]{classname, message}; | |||
} | |||
return new String[]{ trace, ""}; | |||
return new String[]{trace, ""}; | |||
} | |||
} |
@@ -57,10 +57,8 @@ import java.io.BufferedReader; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.InputStreamReader; | |||
import java.io.ObjectInputStream; | |||
import java.io.ByteArrayInputStream; | |||
import java.util.Vector; | |||
import java.util.Properties; | |||
import java.util.Vector; | |||
import org.apache.tools.ant.taskdefs.optional.junit.TestRunListener; | |||
@@ -128,7 +126,7 @@ public class MessageReader { | |||
* appropriate message to the listeners. | |||
*/ | |||
protected void processMessage(String message) { | |||
if (message == null){ | |||
if (message == null) { | |||
return; | |||
} | |||
@@ -173,17 +171,16 @@ public class MessageReader { | |||
notifyTestSuiteStopped(elapsedTime); | |||
return; | |||
} | |||
if (message.startsWith(MessageIds.PROPS_START)){ | |||
if (message.startsWith(MessageIds.PROPS_START)) { | |||
try { | |||
byte[] bytes = arg.substring(0, arg.indexOf(MessageIds.PROPS_END)).getBytes(); | |||
bytes = Base64.decode(bytes); | |||
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes)); | |||
sysprops = (Properties)ois.readObject(); | |||
} catch (Exception e){ | |||
sysprops = (Properties) SocketUtil.deserialize(bytes); | |||
notifyTestSystemProperties(sysprops); | |||
} catch (Exception e) { | |||
// ignore now | |||
e.printStackTrace(); | |||
} | |||
notifyTestSystemProperties(sysprops); | |||
} | |||
} | |||
@@ -55,9 +55,6 @@ package org.apache.tools.ant.taskdefs.optional.junit.remote; | |||
import java.io.OutputStream; | |||
import java.io.PrintWriter; | |||
import java.io.ObjectOutputStream; | |||
import java.io.ByteArrayOutputStream; | |||
import java.io.IOException; | |||
import org.apache.tools.ant.taskdefs.optional.junit.TestRunListener; | |||
@@ -80,12 +77,12 @@ public class MessageWriter implements MessageIds { | |||
this.pw = new PrintWriter(out, true); | |||
} | |||
protected void finalize(){ | |||
protected void finalize() { | |||
close(); | |||
} | |||
public void close() { | |||
if (pw != null){ | |||
if (pw != null) { | |||
pw.close(); | |||
pw = null; | |||
} | |||
@@ -140,13 +137,13 @@ public class MessageWriter implements MessageIds { | |||
public void notifySystemProperties() { | |||
try { | |||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |||
ObjectOutputStream oos = new ObjectOutputStream(out); | |||
oos.writeObject(System.getProperties()); | |||
oos.close(); | |||
String msg = new String(Base64.encode(out.toByteArray())); | |||
sendMessage(MessageIds.PROPS_START + msg + MessageIds.PROPS_END); | |||
} catch (IOException e){ | |||
StringBuffer msg = new StringBuffer(512); | |||
msg.append(MessageIds.PROPS_START); | |||
byte[] data = SocketUtil.serialize(System.getProperties()); | |||
msg.append(Base64.encode(data)); | |||
msg.append(MessageIds.PROPS_END); | |||
sendMessage(msg.toString()); | |||
} catch (Exception e) { | |||
// ignore | |||
e.printStackTrace(); | |||
} | |||
@@ -54,7 +54,6 @@ | |||
package org.apache.tools.ant.taskdefs.optional.junit.remote; | |||
import java.io.IOException; | |||
import java.io.PrintWriter; | |||
import java.net.ServerSocket; | |||
import java.net.Socket; | |||
@@ -134,11 +133,11 @@ public class Server { | |||
/** shutdown the server and any running client */ | |||
public void shutdown() { | |||
if (writer != null){ | |||
if (writer != null) { | |||
writer.close(); | |||
writer = null; | |||
} | |||
if (reader != null){ | |||
if (reader != null) { | |||
//@fixme what about the stream ? | |||
reader = null; | |||
} | |||
@@ -152,11 +151,11 @@ public class Server { | |||
} catch (IOException e) { | |||
} | |||
try { | |||
if (server != null){ | |||
if (server != null) { | |||
server.close(); | |||
server = null; | |||
} | |||
} catch (IOException e){ | |||
} catch (IOException e) { | |||
} | |||
} | |||
@@ -0,0 +1,95 @@ | |||
/* | |||
* The Apache Software License, Version 1.1 | |||
* | |||
* Copyright (c) 2002 The Apache Software Foundation. All rights | |||
* reserved. | |||
* | |||
* Redistribution and use in source and binary forms, with or without | |||
* modification, are permitted provided that the following conditions | |||
* are met: | |||
* | |||
* 1. Redistributions of source code must retain the above copyright | |||
* notice, this list of conditions and the following disclaimer. | |||
* | |||
* 2. Redistributions in binary form must reproduce the above copyright | |||
* notice, this list of conditions and the following disclaimer in | |||
* the documentation and/or other materials provided with the | |||
* distribution. | |||
* | |||
* 3. The end-user documentation included with the redistribution, if | |||
* any, must include the following acknowlegement: | |||
* "This product includes software developed by the | |||
* Apache Software Foundation (http://www.apache.org/)." | |||
* Alternately, this acknowlegement may appear in the software itself, | |||
* if and wherever such third-party acknowlegements normally appear. | |||
* | |||
* 4. The names "The Jakarta Project", "Ant", and "Apache Software | |||
* Foundation" must not be used to endorse or promote products derived | |||
* from this software without prior written permission. For written | |||
* permission, please contact apache@apache.org. | |||
* | |||
* 5. Products derived from this software may not be called "Apache" | |||
* nor may "Apache" appear in their names without prior written | |||
* permission of the Apache Group. | |||
* | |||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||
* SUCH DAMAGE. | |||
* ==================================================================== | |||
* | |||
* This software consists of voluntary contributions made by many | |||
* individuals on behalf of the Apache Software Foundation. For more | |||
* information on the Apache Software Foundation, please see | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.tools.ant.taskdefs.optional.junit.remote; | |||
import java.io.ByteArrayInputStream; | |||
import java.io.ByteArrayOutputStream; | |||
import java.io.ObjectInputStream; | |||
import java.io.ObjectOutputStream; | |||
/** | |||
* A set of helper methods related to sockets. | |||
* | |||
* @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a> | |||
*/ | |||
public class SocketUtil { | |||
/** | |||
* Helper method to deserialize an object | |||
* @param bytes the binary data representing the serialized object. | |||
* @return the deserialized object. | |||
* @throws Exception a generic exception if an error occurs when | |||
* deserializing the object. | |||
*/ | |||
public static Object deserialize(byte[] bytes) throws Exception { | |||
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes)); | |||
return ois.readObject(); | |||
} | |||
/** | |||
* Helper method to serialize an object | |||
* @param o the object to serialize. | |||
* @return the binary data representing the serialized object. | |||
* @throws Exception a generic exception if an error occurs when | |||
* serializing the object. | |||
*/ | |||
public static byte[] serialize(Object o) throws Exception { | |||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |||
ObjectOutputStream oos = new ObjectOutputStream(out); | |||
oos.writeObject(o); | |||
oos.close(); | |||
return out.toByteArray(); | |||
} | |||
} |
@@ -54,13 +54,13 @@ | |||
package org.apache.tools.ant.taskdefs.optional.junit.remote; | |||
import java.io.BufferedReader; | |||
import java.io.FileInputStream; | |||
import java.io.IOException; | |||
import java.io.InputStreamReader; | |||
import java.io.FileInputStream; | |||
import java.net.Socket; | |||
import java.util.Vector; | |||
import java.util.Properties; | |||
import java.util.StringTokenizer; | |||
import java.util.Vector; | |||
import junit.framework.AssertionFailedError; | |||
import junit.framework.Test; | |||
@@ -69,9 +69,9 @@ import junit.framework.TestListener; | |||
import junit.framework.TestResult; | |||
import junit.framework.TestSuite; | |||
import org.apache.tools.ant.util.StringUtils; | |||
import org.apache.tools.ant.taskdefs.optional.junit.JUnitHelper; | |||
import org.apache.tools.ant.taskdefs.optional.junit.TestRunListener; | |||
import org.apache.tools.ant.util.StringUtils; | |||
/** | |||
* TestRunner for running tests and send results to a remote server. | |||
@@ -110,7 +110,7 @@ public class TestRunner implements TestListener { | |||
private BufferedReader reader; | |||
/** bean constructor */ | |||
public TestRunner(){ | |||
public TestRunner() { | |||
} | |||
/** | |||
@@ -216,28 +216,29 @@ public class TestRunner implements TestListener { | |||
} | |||
} | |||
} | |||
/** | |||
* Initialize the TestRunner from properties. | |||
* @param the properties containing configuration data. | |||
* @see #init(String[]) | |||
*/ | |||
protected void init(Properties props){ | |||
if ( props.getProperty("debug") != null ){ | |||
protected void init(Properties props) { | |||
if (props.getProperty("debug") != null) { | |||
setDebug(true); | |||
} | |||
String port = props.getProperty("port"); | |||
if (port != null){ | |||
if (port != null) { | |||
setPort(Integer.parseInt(port)); | |||
} | |||
String host = props.getProperty("host"); | |||
if (host != null){ | |||
if (host != null) { | |||
setHost(host); | |||
} | |||
String classnames = props.getProperty("classnames"); | |||
if (classnames != null){ | |||
if (classnames != null) { | |||
StringTokenizer st = new StringTokenizer(classnames); | |||
while (st.hasMoreTokens()){ | |||
addTestClassName( st.nextToken() ); | |||
while (st.hasMoreTokens()) { | |||
addTestClassName(st.nextToken()); | |||
} | |||
} | |||
} | |||
@@ -272,10 +273,10 @@ public class TestRunner implements TestListener { | |||
String classname = (String) testClassNames.elementAt(i); | |||
try { | |||
Test test = JUnitHelper.getTest(null, classname); | |||
if (test != null){ | |||
if (test != null) { | |||
suites.addElement(test); | |||
} | |||
} catch (Exception e){ | |||
} catch (Exception e) { | |||
// notify log error instead ? | |||
log("Could not get Test instance from " + classname); | |||
log(e); | |||
@@ -304,7 +305,7 @@ public class TestRunner implements TestListener { | |||
long startTime = System.currentTimeMillis(); | |||
for (int i = 0; i < suites.length; i++) { | |||
if (suites[i] instanceof TestCase){ | |||
if (suites[i] instanceof TestCase) { | |||
suites[i] = new TestSuite(suites[i].getClass().getName()); | |||
} | |||
suites[i].run(testResult); | |||
@@ -413,14 +414,14 @@ public class TestRunner implements TestListener { | |||
writer.notifyTestEnded(testName); | |||
} | |||
public void log(String msg){ | |||
if (debug){ | |||
public void log(String msg) { | |||
if (debug) { | |||
System.out.println(msg); | |||
} | |||
} | |||
public void log(Throwable t){ | |||
if (debug){ | |||
public void log(Throwable t) { | |||
if (debug) { | |||
t.printStackTrace(); | |||
} | |||
} | |||