http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitVersionHelper.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitVersionHelper.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitVersionHelper.java index 9a21cae..5ac988e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitVersionHelper.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitVersionHelper.java @@ -82,9 +82,8 @@ public class JUnitVersionHelper { if (name.endsWith(")")) { int paren = name.lastIndexOf('('); return name.substring(0, paren); - } else { - return name; } + return name; } if (t instanceof TestCase && testCaseName != null) { try { @@ -94,17 +93,16 @@ public class JUnitVersionHelper { } } else { try { - Method getNameMethod = null; + Method getNameMethod; try { getNameMethod = - t.getClass().getMethod("getName", new Class [0]); + t.getClass().getMethod("getName"); } catch (NoSuchMethodException e) { - getNameMethod = t.getClass().getMethod("name", - new Class [0]); + getNameMethod = t.getClass().getMethod("name"); } if (getNameMethod != null && getNameMethod.getReturnType() == String.class) { - return (String) getNameMethod.invoke(t, new Object[0]); + return (String) getNameMethod.invoke(t); } } catch (Throwable ignored) { // ignore @@ -125,8 +123,7 @@ public class JUnitVersionHelper { String className = test.getClass().getName(); if (test instanceof JUnitTaskMirrorImpl.VmExitErrorTest) { className = ((JUnitTaskMirrorImpl.VmExitErrorTest) test).getClassName(); - } else - if (className.equals(JUNIT_FRAMEWORK_JUNIT4_TEST_CASE_FACADE)) { + } else if (className.equals(JUNIT_FRAMEWORK_JUNIT4_TEST_CASE_FACADE)) { // JUnit 4 wraps solo tests this way. We can extract // the original test name with a little hack. String name = test.toString(); @@ -152,28 +149,22 @@ public class JUnitVersionHelper { Class<?> testClass = Class.forName(JUnitVersionHelper.getTestCaseClassName(test)); Method testMethod = testClass.getMethod(JUnitVersionHelper.getTestCaseName(test)); - Class ignoreAnnotation = Class.forName("org.junit.Ignore"); + Class<? extends Annotation> ignoreAnnotation = Class + .forName("org.junit.Ignore").asSubclass(Annotation.class); Annotation annotation = testMethod.getAnnotation(ignoreAnnotation); if (annotation != null) { - Method valueMethod = annotation.getClass().getMethod("value"); + Method valueMethod = annotation.annotationType().getMethod("value"); String value = (String) valueMethod.invoke(annotation); if (value != null && value.length() > 0) { message = value; } } - } - } catch (NoSuchMethodException e) { - // silently ignore - we'll report a skip with no message - } catch (ClassNotFoundException e) { - // silently ignore - we'll report a skip with no message - } catch (InvocationTargetException e) { - // silently ignore - we'll report a skip with no message - } catch (IllegalAccessException e) { + } catch (NoSuchMethodException | ClassNotFoundException + | InvocationTargetException | IllegalAccessException e) { // silently ignore - we'll report a skip with no message } return message; - } }
http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java index 07264b7..85cfb0c 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/PlainJUnitResultFormatter.java @@ -24,6 +24,7 @@ import java.io.OutputStream; import java.io.StringWriter; import java.text.NumberFormat; import java.util.Hashtable; +import java.util.Map; import junit.framework.AssertionFailedError; import junit.framework.Test; @@ -37,7 +38,6 @@ import org.apache.tools.ant.util.StringUtils; * Prints plain text output of the test to a specified Writer. * */ - public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredTestListener { private static final double ONE_SECOND = 1000.0; @@ -49,7 +49,7 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT /** * Timing helper. */ - private Hashtable testStarts = new Hashtable(); + private Map<Test, Long> testStarts = new Hashtable<>(); /** * Where to write the log to. */ @@ -65,7 +65,7 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT /** * Suppress endTest if testcase failed. */ - private Hashtable failed = new Hashtable(); + private Map<Test, Boolean> failed = new Hashtable<>(); private String systemOutput = null; private String systemError = null; @@ -77,16 +77,19 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT } /** {@inheritDoc}. */ + @Override public void setOutput(OutputStream out) { this.out = out; } /** {@inheritDoc}. */ + @Override public void setSystemOutput(String out) { systemOutput = out; } /** {@inheritDoc}. */ + @Override public void setSystemError(String err) { systemError = err; } @@ -96,15 +99,14 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT * @param suite the test suite * @throws BuildException if unable to write the output */ + @Override public void startTestSuite(JUnitTest suite) throws BuildException { if (out == null) { return; // Quick return - no output do nothing. } - StringBuffer sb = new StringBuffer("Testsuite: "); - sb.append(suite.getName()); - sb.append(StringUtils.LINE_SEP); try { - out.write(sb.toString().getBytes()); + out.write(new StringBuilder("Testsuite: ").append(suite.getName()) + .append(StringUtils.LINE_SEP).toString().getBytes()); out.flush(); } catch (IOException ex) { throw new BuildException("Unable to write output", ex); @@ -116,10 +118,11 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT * @param suite the test suite * @throws BuildException if unable to write the output */ + @Override public void endTestSuite(JUnitTest suite) throws BuildException { boolean success = false; try { - StringBuffer sb = new StringBuffer("Tests run: "); + StringBuilder sb = new StringBuilder("Tests run: "); sb.append(suite.runCount()); sb.append(", Failures: "); sb.append(suite.failureCount()); @@ -185,6 +188,7 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT * <p>A new Test is started. * @param t the test. */ + @Override public void startTest(Test t) { testStarts.put(t, new Long(System.currentTimeMillis())); failed.put(t, Boolean.FALSE); @@ -196,6 +200,7 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT * <p>A Test is finished. * @param test the test. */ + @Override public void endTest(Test test) { if (Boolean.TRUE.equals(failed.get(test))) { return; @@ -204,7 +209,7 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT try { wri.write("Testcase: " + JUnitVersionHelper.getTestCaseName(test)); - Long l = (Long) testStarts.get(test); + Long l = testStarts.get(test); double seconds = 0; // can be null if an error occurred in setUp if (l != null) { @@ -238,6 +243,7 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT * @param test the test. * @param t the assertion that failed. */ + @Override public void addFailure(Test test, AssertionFailedError t) { addFailure(test, (Throwable) t); } @@ -249,6 +255,7 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT * @param test the test. * @param t the exception. */ + @Override public void addError(Test test, Throwable t) { formatError("\tCaused an ERROR", test, t); } @@ -274,6 +281,7 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT } } + @Override public void testIgnored(Test test) { formatSkip(test, JUnitVersionHelper.getIgnoreMessage(test)); } @@ -297,6 +305,7 @@ public class PlainJUnitResultFormatter implements JUnitResultFormatter, IgnoredT } + @Override public void testAssumptionFailure(Test test, Throwable throwable) { formatSkip(test, throwable.getMessage()); } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/junit/SummaryJUnitResultFormatter.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/SummaryJUnitResultFormatter.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/SummaryJUnitResultFormatter.java index 4eb30fb..27239d6 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/SummaryJUnitResultFormatter.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/SummaryJUnitResultFormatter.java @@ -52,12 +52,6 @@ public class SummaryJUnitResultFormatter private String systemError = null; /** - * Empty - */ - public SummaryJUnitResultFormatter() { - } - - /** * Insures that a line of log output is written and flushed as a single * operation, to prevent lines from being spliced into other lines. * (Hopefully this solves the issue of run on lines - @@ -81,9 +75,10 @@ public class SummaryJUnitResultFormatter * The testsuite started. * @param suite the testsuite. */ + @Override public void startTestSuite(JUnitTest suite) { String newLine = System.getProperty("line.separator"); - StringBuffer sb = new StringBuffer("Running "); + StringBuilder sb = new StringBuilder("Running "); int antThreadID = suite.getThread(); sb.append(suite.getName()); @@ -99,12 +94,14 @@ public class SummaryJUnitResultFormatter * Empty * @param t not used. */ + @Override public void startTest(Test t) { } /** * Empty * @param test not used. */ + @Override public void endTest(Test test) { } /** @@ -121,6 +118,7 @@ public class SummaryJUnitResultFormatter * @param test not used. * @param t not used. */ + @Override public void addFailure(Test test, AssertionFailedError t) { addFailure(test, (Throwable) t); } @@ -129,20 +127,24 @@ public class SummaryJUnitResultFormatter * @param test not used. * @param t not used. */ + @Override public void addError(Test test, Throwable t) { } /** {@inheritDoc}. */ + @Override public void setOutput(OutputStream out) { this.out = out; } /** {@inheritDoc}. */ + @Override public void setSystemOutput(String out) { systemOutput = out; } /** {@inheritDoc}. */ + @Override public void setSystemError(String err) { systemError = err; } @@ -152,6 +154,7 @@ public class SummaryJUnitResultFormatter * the summary. * @param value if true write System.out and System.err to the summary. */ + @Override public void setWithOutAndErr(boolean value) { withOutAndErr = value; } @@ -161,9 +164,10 @@ public class SummaryJUnitResultFormatter * @param suite the testsuite. * @throws BuildException if there is an error. */ + @Override public void endTestSuite(JUnitTest suite) throws BuildException { String newLine = System.getProperty("line.separator"); - StringBuffer sb = new StringBuffer("Tests run: "); + StringBuilder sb = new StringBuilder("Tests run: "); sb.append(suite.runCount()); sb.append(", Failures: "); sb.append(suite.failureCount()); http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/junit/TearDownOnVmCrash.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/TearDownOnVmCrash.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/TearDownOnVmCrash.java index e381a70..a500839 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/TearDownOnVmCrash.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/TearDownOnVmCrash.java @@ -19,7 +19,6 @@ package org.apache.tools.ant.taskdefs.optional.junit; import java.io.OutputStream; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import junit.framework.AssertionFailedError; @@ -43,6 +42,7 @@ public class TearDownOnVmCrash implements JUnitResultFormatter { * Records the suite's name to later determine the class to invoke * tearDown on. */ + @Override public void startTestSuite(final JUnitTest suite) { suiteName = suite.getName(); if (suiteName != null && @@ -57,6 +57,7 @@ public class TearDownOnVmCrash implements JUnitResultFormatter { * test we get when a Batch fails and the error is an actual * error generated by Ant. */ + @Override public void addError(final Test fakeTest, final Throwable t) { if (suiteName != null && fakeTest instanceof JUnitTaskMirrorImpl.VmExitErrorTest) { @@ -67,25 +68,32 @@ public class TearDownOnVmCrash implements JUnitResultFormatter { // no need to implement the rest public void addFailure(Test test, Throwable t) {} + @Override public void addFailure(Test test, AssertionFailedError t) {} + @Override public void startTest(Test test) {} + @Override public void endTest(Test test) {} + @Override public void endTestSuite(JUnitTest suite) {} + @Override public void setOutput(OutputStream out) {} + @Override public void setSystemOutput(String out) {} + @Override public void setSystemError(String err) {} private void tearDown() { try { // first try to load the class and let's hope it is on our // classpath - Class testClass = null; + Class<?> testClass = null; if (Thread.currentThread().getContextClassLoader() != null) { try { testClass = Thread.currentThread().getContextClassLoader() @@ -111,7 +119,7 @@ public class TearDownOnVmCrash implements JUnitResultFormatter { // which test of the executed suite timed out, ignore it try { // check if there is a suite method - testClass.getMethod("suite", new Class[0]); + testClass.getMethod("suite"); return; } catch (NoSuchMethodException e) { // no suite method @@ -122,9 +130,9 @@ public class TearDownOnVmCrash implements JUnitResultFormatter { // doesn't have any tearDown method. try { - Method td = testClass.getMethod("tearDown", new Class[0]); + Method td = testClass.getMethod("tearDown"); if (td.getReturnType() == Void.TYPE) { - td.invoke(testClass.newInstance(), new Object[0]); + td.invoke(testClass.newInstance()); } } catch (NoSuchMethodException nsme) { // no tearDown, fine @@ -133,9 +141,6 @@ public class TearDownOnVmCrash implements JUnitResultFormatter { } catch (ClassNotFoundException cnfe) { // class probably is not in our classpath, there is // nothing we can do - } catch (InvocationTargetException ite) { - System.err.println("Caught an exception while trying to invoke" - + " tearDown: " + ite.getMessage()); } catch (Throwable t) { System.err.println("Caught an exception while trying to invoke" + " tearDown: " + t.getMessage()); http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/junit/TestListenerWrapper.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/TestListenerWrapper.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/TestListenerWrapper.java index 692e4fc..d65b7f2 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/TestListenerWrapper.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/TestListenerWrapper.java @@ -32,31 +32,38 @@ public class TestListenerWrapper implements TestListener, IgnoredTestListener { wrapped = listener; } + @Override public void addError(Test test, Throwable throwable) { wrapped.addError(test, throwable); } + @Override public void addFailure(Test test, AssertionFailedError assertionFailedError) { wrapped.addFailure(test, assertionFailedError); } + @Override public void endTest(Test test) { wrapped.endTest(test); } + @Override public void startTest(Test test) { wrapped.startTest(test); } + @Override public void testIgnored(Test test) { if (wrapped instanceof IgnoredTestListener) { - ((IgnoredTestListener)wrapped).testIgnored(test); + ((IgnoredTestListener) wrapped).testIgnored(test); } } + @Override public void testAssumptionFailure(Test test, Throwable throwable) { if (wrapped instanceof IgnoredTestListener) { - ((IgnoredTestListener)wrapped).testAssumptionFailure(test, throwable); + ((IgnoredTestListener) wrapped).testAssumptionFailure(test, + throwable); } } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLJUnitResultFormatter.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLJUnitResultFormatter.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLJUnitResultFormatter.java index 416c10d..d62a847 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLJUnitResultFormatter.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLJUnitResultFormatter.java @@ -26,8 +26,8 @@ import java.io.Writer; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Date; -import java.util.Enumeration; import java.util.Hashtable; +import java.util.Map; import java.util.Properties; import javax.xml.parsers.DocumentBuilder; @@ -50,7 +50,6 @@ import org.w3c.dom.Text; * * @see FormatterElement */ - public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstants, IgnoredTestListener { private static final double ONE_SECOND = 1000.0; @@ -84,45 +83,44 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan * so we can't easily match Test objects without manually iterating over all keys and checking * individual fields. */ - private final Hashtable<String, Element> testElements = new Hashtable<String, Element>(); + private final Hashtable<String, Element> testElements = new Hashtable<>(); /** * tests that failed. */ - private final Hashtable failedTests = new Hashtable(); + private final Map<Test, Test> failedTests = new Hashtable<>(); /** * Tests that were skipped. */ - private final Hashtable<String, Test> skippedTests = new Hashtable<String, Test>(); + private final Map<String, Test> skippedTests = new Hashtable<>(); /** * Tests that were ignored. See the note above about the key being a bit of a hack. */ - private final Hashtable<String, Test> ignoredTests = new Hashtable<String, Test>(); + private final Map<String, Test> ignoredTests = new Hashtable<>(); /** * Timing helper. */ - private final Hashtable<String, Long> testStarts = new Hashtable<String, Long>(); + private final Map<String, Long> testStarts = new Hashtable<>(); /** * Where to write the log to. */ private OutputStream out; - /** No arg constructor. */ - public XMLJUnitResultFormatter() { - } - /** {@inheritDoc}. */ + @Override public void setOutput(final OutputStream out) { this.out = out; } /** {@inheritDoc}. */ + @Override public void setSystemOutput(final String out) { formatOutput(SYSTEM_OUT, out); } /** {@inheritDoc}. */ + @Override public void setSystemError(final String out) { formatOutput(SYSTEM_ERR, out); } @@ -131,6 +129,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan * The whole testsuite started. * @param suite the testsuite. */ + @Override public void startTestSuite(final JUnitTest suite) { doc = getDocumentBuilder().newDocument(); rootElement = doc.createElement(TESTSUITE); @@ -149,9 +148,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan rootElement.appendChild(propsElement); final Properties props = suite.getProperties(); if (props != null) { - final Enumeration e = props.propertyNames(); - while (e.hasMoreElements()) { - final String name = (String) e.nextElement(); + for (String name : props.stringPropertyNames()) { final Element propElement = doc.createElement(PROPERTY); propElement.setAttribute(ATTR_NAME, name); propElement.setAttribute(ATTR_VALUE, props.getProperty(name)); @@ -182,19 +179,20 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan * @param suite the testsuite. * @throws BuildException on error. */ + @Override public void endTestSuite(final JUnitTest suite) throws BuildException { - rootElement.setAttribute(ATTR_TESTS, "" + suite.runCount()); - rootElement.setAttribute(ATTR_FAILURES, "" + suite.failureCount()); - rootElement.setAttribute(ATTR_ERRORS, "" + suite.errorCount()); - rootElement.setAttribute(ATTR_SKIPPED, "" + suite.skipCount()); + rootElement.setAttribute(ATTR_TESTS, Long.toString(suite.runCount())); + rootElement.setAttribute(ATTR_FAILURES, Long.toString(suite.failureCount())); + rootElement.setAttribute(ATTR_ERRORS, Long.toString(suite.errorCount())); + rootElement.setAttribute(ATTR_SKIPPED, Long.toString(suite.skipCount())); rootElement.setAttribute( - ATTR_TIME, "" + (suite.getRunTime() / ONE_SECOND)); + ATTR_TIME, Double.toString(suite.getRunTime() / ONE_SECOND)); if (out != null) { Writer wri = null; try { wri = new BufferedWriter(new OutputStreamWriter(out, "UTF8")); wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); - (new DOMElementWriter()).write(rootElement, wri, 0, " "); + new DOMElementWriter().write(rootElement, wri, 0, " "); } catch (final IOException exc) { throw new BuildException("Unable to write log file", exc); } finally { @@ -218,12 +216,14 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan * <p>A new Test is started. * @param t the test. */ + @Override public void startTest(final Test t) { testStarts.put(createDescription(t), System.currentTimeMillis()); } private static String createDescription(final Test test) throws BuildException { - return JUnitVersionHelper.getTestCaseName(test) + "(" + JUnitVersionHelper.getTestCaseClassName(test) + ")"; + return JUnitVersionHelper.getTestCaseName(test) + "(" + + JUnitVersionHelper.getTestCaseClassName(test) + ")"; } /** @@ -232,6 +232,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan * <p>A Test is finished. * @param test the test. */ + @Override public void endTest(final Test test) { final String testDescription = createDescription(test); @@ -242,7 +243,9 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan startTest(test); } Element currentTest; - if (!failedTests.containsKey(test) && !skippedTests.containsKey(testDescription) && !ignoredTests.containsKey(testDescription)) { + if (!failedTests.containsKey(test) + && !skippedTests.containsKey(testDescription) + && !ignoredTests.containsKey(testDescription)) { currentTest = doc.createElement(TESTCASE); final String n = JUnitVersionHelper.getTestCaseName(test); currentTest.setAttribute(ATTR_NAME, @@ -259,7 +262,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan final Long l = testStarts.get(createDescription(test)); currentTest.setAttribute(ATTR_TIME, - "" + ((System.currentTimeMillis() - l) / ONE_SECOND)); + Double.toString((System.currentTimeMillis() - l) / ONE_SECOND)); } /** @@ -280,6 +283,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan * @param test the test. * @param t the assertion. */ + @Override public void addFailure(final Test test, final AssertionFailedError t) { addFailure(test, (Throwable) t); } @@ -291,6 +295,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan * @param test the test. * @param t the error. */ + @Override public void addError(final Test test, final Throwable t) { formatError(ERROR, test, t); } @@ -302,12 +307,8 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan } final Element nested = doc.createElement(type); - Element currentTest; - if (test != null) { - currentTest = testElements.get(createDescription(test)); - } else { - currentTest = rootElement; - } + Element currentTest = test == null ? rootElement + : testElements.get(createDescription(test)); currentTest.appendChild(nested); @@ -328,6 +329,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan nested.appendChild(doc.createCDATASection(output)); } + @Override public void testIgnored(final Test test) { formatSkip(test, JUnitVersionHelper.getIgnoreMessage(test)); if (test != null) { @@ -335,7 +337,6 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan } } - public void formatSkip(final Test test, final String message) { if (test != null) { endTest(test); @@ -347,17 +348,14 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan nested.setAttribute("message", message); } - Element currentTest; - if (test != null) { - currentTest = testElements.get(createDescription(test)); - } else { - currentTest = rootElement; - } + Element currentTest = test == null ? rootElement + : testElements.get(createDescription(test)); currentTest.appendChild(nested); } + @Override public void testAssumptionFailure(final Test test, final Throwable failure) { formatSkip(test, failure.getMessage()); skippedTests.put(createDescription(test), test); http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java index 92e3553..b877c0b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java @@ -24,8 +24,8 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.nio.file.Files; -import java.util.Enumeration; import java.util.Vector; +import java.util.stream.Stream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -61,7 +61,7 @@ public class XMLResultAggregator extends Task implements XMLConstants { // CheckStyle:VisibilityModifier OFF - bc /** the list of all filesets, that should contains the xml to aggregate */ - protected Vector filesets = new Vector(); + protected Vector<FileSet> filesets = new Vector<>(); /** the name of the result file */ protected String toFile; @@ -69,7 +69,7 @@ public class XMLResultAggregator extends Task implements XMLConstants { /** the directory to write the file to */ protected File toDir; - protected Vector transformers = new Vector(); + protected Vector<AggregateTransformer> transformers = new Vector<>(); /** The default directory: <tt>.</tt>. It is resolved from the project directory */ public static final String DEFAULT_DIR = "."; @@ -103,7 +103,7 @@ public class XMLResultAggregator extends Task implements XMLConstants { */ public AggregateTransformer createReport() { AggregateTransformer transformer = new AggregateTransformer(this); - transformers.addElement(transformer); + transformers.add(transformer); return transformer; } @@ -133,7 +133,7 @@ public class XMLResultAggregator extends Task implements XMLConstants { * @param fs the new fileset of xml results. */ public void addFileSet(FileSet fs) { - filesets.addElement(fs); + filesets.add(fs); } /** @@ -142,6 +142,7 @@ public class XMLResultAggregator extends Task implements XMLConstants { * @throws BuildException thrown if there is a serious error while writing * the document. */ + @Override public void execute() throws BuildException { Element rootElement = createDocument(); File destFile = getDestinationFile(); @@ -152,10 +153,7 @@ public class XMLResultAggregator extends Task implements XMLConstants { throw new BuildException("Unable to write test aggregate to '" + destFile + "'", e); } // apply transformation - Enumeration e = transformers.elements(); - while (e.hasMoreElements()) { - AggregateTransformer transformer = - (AggregateTransformer) e.nextElement(); + for (AggregateTransformer transformer : transformers) { transformer.setXmlDocument(rootElement.getOwnerDocument()); transformer.transform(); } @@ -182,26 +180,16 @@ public class XMLResultAggregator extends Task implements XMLConstants { * @return all files in the fileset that end with a '.xml'. */ protected File[] getFiles() { - Vector v = new Vector(); - final int size = filesets.size(); - for (int i = 0; i < size; i++) { - FileSet fs = (FileSet) filesets.elementAt(i); - DirectoryScanner ds = fs.getDirectoryScanner(getProject()); + Project p = getProject(); + return filesets.stream().flatMap(fs -> { + DirectoryScanner ds = fs.getDirectoryScanner(p); ds.scan(); - String[] f = ds.getIncludedFiles(); - for (int j = 0; j < f.length; j++) { - String pathname = f[j]; - if (pathname.endsWith(".xml")) { - File file = new File(ds.getBasedir(), pathname); - file = getProject().resolveFile(file.getPath()); - v.addElement(file); - } - } - } - - File[] files = new File[v.size()]; - v.copyInto(files); - return files; + return Stream.of(ds.getIncludedFiles()) + .filter(pathname -> pathname.endsWith(".xml")).map(pathname -> { + return p.resolveFile( + new File(ds.getBasedir(), pathname).getPath()); + }); + }).toArray(File[]::new); } //----- from now, the methods are all related to DOM tree manipulation @@ -216,7 +204,8 @@ public class XMLResultAggregator extends Task implements XMLConstants { try (OutputStream os = Files.newOutputStream(file.toPath()); PrintWriter wri = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(os), "UTF8"))) { wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); - (new DOMElementWriter()).write(doc.getDocumentElement(), wri, 0, " "); + new DOMElementWriter().write(doc.getDocumentElement(), wri, 0, + " "); wri.flush(); // writers do not throw exceptions, so check for them. if (wri.checkError()) { @@ -247,9 +236,8 @@ public class XMLResultAggregator extends Task implements XMLConstants { try { log("Parsing file: '" + file + "'", Project.MSG_VERBOSE); if (file.length() > 0) { - Document testsuiteDoc - = builder.parse( - FileUtils.getFileUtils().toURI(files[i].getAbsolutePath())); + Document testsuiteDoc = builder.parse(FileUtils + .getFileUtils().toURI(files[i].getAbsolutePath())); Element elem = testsuiteDoc.getDocumentElement(); // make sure that this is REALLY a testsuite. if (TESTSUITE.equals(elem.getNodeName())) { http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/BuiltinNative2Ascii.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/BuiltinNative2Ascii.java b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/BuiltinNative2Ascii.java index 244b8c1..f8059fe 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/BuiltinNative2Ascii.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/BuiltinNative2Ascii.java @@ -25,13 +25,14 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.io.PrintWriter; import java.io.Writer; +import java.util.function.UnaryOperator; import java.nio.file.Files; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.taskdefs.optional.Native2Ascii; import org.apache.tools.ant.util.Native2AsciiUtils; -import org.apache.tools.ant.util.StringUtils; /** * Encapsulates the built-in Native2Ascii implementation. @@ -48,8 +49,10 @@ public class BuiltinNative2Ascii implements Native2AsciiAdapter { boolean reverse = args.getReverse(); String encoding = args.getEncoding(); try (BufferedReader input = getReader(srcFile, encoding, reverse); - Writer output = getWriter(destFile, encoding, reverse)) { - translate(input, output, reverse); + Writer output = getWriter(destFile, encoding, reverse)) { + + translate(input, output, reverse ? Native2AsciiUtils::ascii2native + : Native2AsciiUtils::native2ascii); return true; } catch (IOException ex) { throw new BuildException("Exception trying to translate data", ex); @@ -79,15 +82,12 @@ public class BuiltinNative2Ascii implements Native2AsciiAdapter { } private void translate(BufferedReader input, Writer output, - boolean reverse) throws IOException { - String line = null; - while ((line = input.readLine()) != null) { - if (!reverse) { - output.write(Native2AsciiUtils.native2ascii(line)); - } else { - output.write(Native2AsciiUtils.ascii2native(line)); - } - output.write(StringUtils.LINE_SEP); + UnaryOperator<String> translation) throws IOException { + PrintWriter pw = new PrintWriter(output); + + for (String line : (Iterable<String>) () -> input.lines() + .map(translation).iterator()) { + pw.println(line); } } } http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/DefaultNative2Ascii.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/DefaultNative2Ascii.java b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/DefaultNative2Ascii.java index 3cd52af..438bfe9 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/DefaultNative2Ascii.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/DefaultNative2Ascii.java @@ -47,6 +47,7 @@ public abstract class DefaultNative2Ascii implements Native2AsciiAdapter { * (delegated to {@link #addFiles addFiles}) and running the tool * (delegated to {@link #run run}). */ + @Override public final boolean convert(Native2Ascii args, File srcFile, File destFile) throws BuildException { Commandline cmd = new Commandline(); http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/KaffeNative2Ascii.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/KaffeNative2Ascii.java b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/KaffeNative2Ascii.java index da4836f..e29634a 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/KaffeNative2Ascii.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/KaffeNative2Ascii.java @@ -43,6 +43,7 @@ public final class KaffeNative2Ascii extends DefaultNative2Ascii { public static final String IMPLEMENTATION_NAME = "kaffe"; /** {@inheritDoc} */ + @Override protected void setup(Commandline cmd, Native2Ascii args) throws BuildException { if (args.getReverse()) { @@ -52,13 +53,14 @@ public final class KaffeNative2Ascii extends DefaultNative2Ascii { } /** {@inheritDoc} */ + @Override protected boolean run(Commandline cmd, ProjectComponent log) throws BuildException { ExecuteJava ej = new ExecuteJava(); - Class c = getN2aClass(); + Class<?> c = getN2aClass(); if (c == null) { - throw new BuildException("Couldn't load Kaffe's Native2Ascii" - + " class"); + throw new BuildException( + "Couldn't load Kaffe's Native2Ascii class"); } cmd.setExecutable(c.getName()); @@ -74,7 +76,7 @@ public final class KaffeNative2Ascii extends DefaultNative2Ascii { * * @return null if neither class can get loaded. */ - private static Class getN2aClass() { + private static Class<?> getN2aClass() { for (int i = 0; i < N2A_CLASSNAMES.length; i++) { try { return Class.forName(N2A_CLASSNAMES[i]); http://git-wip-us.apache.org/repos/asf/ant/blob/b7d1e9bd/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/SunNative2Ascii.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/SunNative2Ascii.java b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/SunNative2Ascii.java index fac94b1..fdcce7d 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/SunNative2Ascii.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/SunNative2Ascii.java @@ -36,7 +36,10 @@ public final class SunNative2Ascii extends DefaultNative2Ascii { */ public static final String IMPLEMENTATION_NAME = "sun"; + private static final String SUN_TOOLS_NATIVE2ASCII_MAIN = "sun.tools.native2ascii.Main"; + /** {@inheritDoc} */ + @Override protected void setup(Commandline cmd, Native2Ascii args) throws BuildException { if (args.getReverse()) { @@ -46,23 +49,20 @@ public final class SunNative2Ascii extends DefaultNative2Ascii { } /** {@inheritDoc} */ + @Override protected boolean run(Commandline cmd, ProjectComponent log) throws BuildException { try { - Class n2aMain = Class.forName("sun.tools.native2ascii.Main"); - Class[] param = new Class[] {String[].class}; - Method convert = n2aMain.getMethod("convert", param); - if (convert == null) { - throw new BuildException("Could not find convert() method in " - + "sun.tools.native2ascii.Main"); - } - Object o = n2aMain.newInstance(); - return ((Boolean) convert.invoke(o, - new Object[] {cmd.getArguments()}) - ).booleanValue(); + Class<?> n2aMain = Class.forName(SUN_TOOLS_NATIVE2ASCII_MAIN); + Method convert = n2aMain.getMethod("convert", String[].class); + return Boolean.TRUE.equals(convert.invoke(n2aMain.newInstance(), + (Object) cmd.getArguments())); } catch (BuildException ex) { //rethrow throw ex; + } catch (NoSuchMethodException ex) { + throw new BuildException("Could not find convert() method in %s", + SUN_TOOLS_NATIVE2ASCII_MAIN); } catch (Exception ex) { //wrap throw new BuildException("Error starting Sun's native2ascii: ", ex);
