From 8de81028e45c54b750a0024bf64eaa0ea9e369ea Mon Sep 17 00:00:00 2001 From: "Jesse N. Glick" Date: Tue, 6 Jul 2010 16:16:27 +0000 Subject: [PATCH] Fixing various dependencies, mostly from FindBugs, some from the NetBeans editor. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@960931 13f79535-47bb-0310-9956-ffa450edef68 --- .../optional/junit/FailureRecorder.java | 20 +++++++++----- .../optional/junit/FormatterElement.java | 11 ++++++-- .../junit/JUnit4TestMethodAdapter.java | 4 ++- .../taskdefs/optional/junit/JUnitTask.java | 27 ++++++++++--------- .../taskdefs/optional/junit/JUnitTest.java | 13 +++++---- .../optional/junit/JUnitTestRunner.java | 4 +-- 6 files changed, 49 insertions(+), 30 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/FailureRecorder.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/FailureRecorder.java index 5e62fb75f..9358dea4b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/FailureRecorder.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/FailureRecorder.java @@ -249,7 +249,9 @@ public class FailureRecorder extends ProjectComponent implements JUnitResultForm File sourceFile = new File((getLocationName() + ".java")); verbose("Write collector class to '" + sourceFile.getAbsolutePath() + "'"); - sourceFile.delete(); + if (!sourceFile.delete()) { + throw new IOException("could not delete " + sourceFile); + } writer = new BufferedWriter(new FileWriter(sourceFile)); createClassHeader(); @@ -334,13 +336,13 @@ public class FailureRecorder extends ProjectComponent implements JUnitResultForm /** * TestInfos holds information about a given test for later use. */ - public class TestInfos implements Comparable { + public static class TestInfos implements Comparable { /** The class name of the test. */ - private String className; + private final String className; /** The method name of the testcase. */ - private String methodName; + private final String methodName; /** * This constructor extracts the needed information from the given test. @@ -348,8 +350,8 @@ public class FailureRecorder extends ProjectComponent implements JUnitResultForm */ public TestInfos(Test test) { className = test.getClass().getName(); - methodName = test.toString(); - methodName = methodName.substring(0, methodName.indexOf('(')); + String _methodName = test.toString(); + methodName = _methodName.substring(0, _methodName.indexOf('(')); } /** @@ -378,6 +380,12 @@ public class FailureRecorder extends ProjectComponent implements JUnitResultForm return -1; } } + public boolean equals(Object obj) { + return obj instanceof TestInfos && toString().equals(obj.toString()); + } + public int hashCode() { + return toString().hashCode(); + } } // ===== BuildListener ===== diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java index effc5a5ae..462ed4d4a 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java @@ -23,6 +23,7 @@ import java.io.FileOutputStream; import java.io.OutputStream; import java.io.BufferedOutputStream; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.apache.tools.ant.BuildException; @@ -326,8 +327,10 @@ public class FormatterElement { // there is already a project reference so dont overwrite this needToSetProjectReference = false; } - } catch (Exception e) { + } catch (NoSuchFieldException e) { // no field present, so no previous reference exists + } catch (IllegalAccessException e) { + throw new BuildException(e); } if (needToSetProjectReference) { @@ -335,8 +338,12 @@ public class FormatterElement { try { setter = r.getClass().getMethod("setProject", new Class[] {Project.class}); setter.invoke(r, new Object[] {project}); - } catch (Exception e) { + } catch (NoSuchMethodException e) { // no setProject to invoke; just ignore + } catch (IllegalAccessException e) { + throw new BuildException(e); + } catch (InvocationTargetException e) { + throw new BuildException(e); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnit4TestMethodAdapter.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnit4TestMethodAdapter.java index 58f31c5c0..e1302e9d0 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnit4TestMethodAdapter.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnit4TestMethodAdapter.java @@ -74,7 +74,7 @@ public class JUnit4TestMethodAdapter implements Test { } } this.testClass = testClass; - this.methodNames = methodNames; + this.methodNames = (String[]) methodNames.clone(); this.cache = Cache.instance; // Warning: If 'testClass' is an old-style (pre-JUnit-4) class, @@ -194,6 +194,8 @@ public class JUnit4TestMethodAdapter implements Test { * of type {@code JUnit4TestAdapter}. */ private static final class Cache extends JUnit4TestAdapterCache { + + private static final long serialVersionUID = 8454901854293461610L; private static final Cache instance = new Cache(); diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java index f2f7c2014..3b5fbb7ba 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java @@ -29,6 +29,8 @@ import java.io.OutputStream; import java.io.PrintStream; import java.lang.reflect.Constructor; import java.net.URL; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; @@ -652,8 +654,6 @@ public class JUnitTask extends Task { * @since Ant 1.2 */ public JUnitTask() throws Exception { - getCommandline() - .setClassname("org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner"); } /** @@ -716,16 +716,17 @@ public class JUnitTask extends Task { * @since Ant 1.7.1 */ protected void setupJUnitDelegate() { - ClassLoader myLoader = JUnitTask.class.getClassLoader(); + final ClassLoader myLoader = JUnitTask.class.getClassLoader(); if (splitJunit) { - Path path = new Path(getProject()); + final Path path = new Path(getProject()); path.add(antRuntimeClasses); Path extra = getCommandline().getClasspath(); if (extra != null) { path.add(extra); } - mirrorLoader = - new SplitClassLoader(myLoader, path, getProject(), + mirrorLoader = (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + return new SplitClassLoader(myLoader, path, getProject(), new String[] { "BriefJUnitResultFormatter", "JUnit4TestMethodAdapter", @@ -739,6 +740,8 @@ public class JUnitTask extends Task { "TearDownOnVmCrash", "XMLJUnitResultFormatter", }); + } + }); } else { mirrorLoader = myLoader; } @@ -884,7 +887,7 @@ public class JUnitTask extends Task { log(e.toString(), Project.MSG_ERR); throw new BuildException(e); } finally { - FILE_UTILS.close(writer); + FileUtils.close(writer); try { FILE_UTILS.tryHardToDelete(casesFile); @@ -925,7 +928,6 @@ public class JUnitTask extends Task { } catch (CloneNotSupportedException e) { throw new BuildException("This shouldn't happen", e, getLocation()); } - cmd.setClassname("org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner"); if (casesFile == null) { cmd.createArgument().setValue(test.getName()); if (test.getMethods() != null) { @@ -1177,13 +1179,13 @@ public class JUnitTask extends Task { return new File(FILE_UTILS.fromURI(url1)) .equals(new File(FILE_UTILS.fromURI(url2))); } - return u1.equals(u2); + return url1.equals(url2); } private static String maybeStripJarAndClass(URL u) { String s = u.toString(); if (s.startsWith("jar:")) { - int pling = s.indexOf("!"); + int pling = s.indexOf('!'); s = s.substring(4, pling == -1 ? s.length() : pling); } return s; @@ -1723,6 +1725,7 @@ public class JUnitTask extends Task { protected CommandlineJava getCommandline() { if (commandline == null) { commandline = new CommandlineJava(); + commandline.setClassname("org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner"); } return commandline; } @@ -1940,7 +1943,7 @@ public class JUnitTask extends Task { /** * A value class that contains the result of a test. */ - protected class TestResultHolder { + protected static class TestResultHolder { // CheckStyle:VisibilityModifier OFF - bc /** the exit code of the test. */ public int exitCode = JUnitTaskMirror.JUnitTestRunnerMirror.ERRORS; @@ -2019,7 +2022,7 @@ public class JUnitTask extends Task { */ private static JUnitTest createDummyTestForBatchTest(JUnitTest test) { JUnitTest t = (JUnitTest) test.clone(); - int index = test.getName().indexOf("."); + int index = test.getName().indexOf('.'); // make sure test looks as if it was in the same "package" as // the last test of the batch String pack = index > 0 ? test.getName().substring(0, index + 1) : ""; diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTest.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTest.java index 8079b9598..84bc542c6 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTest.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTest.java @@ -97,8 +97,7 @@ public class JUnitTest extends BaseTest implements Cloneable { * @param haltOnError if true halt the tests if there is an error. * @param haltOnFailure if true halt the tests if there is a failure. * @param filtertrace if true filter stack traces. - * @param methods if true run only test methods that failed during the - * previous run of the test suite + * @param methods if non-null run only these test methods * @since 1.8.2 */ public JUnitTest(String name, boolean haltOnError, boolean haltOnFailure, @@ -107,8 +106,8 @@ public class JUnitTest extends BaseTest implements Cloneable { this.haltOnError = haltOnError; this.haltOnFail = haltOnFailure; this.filtertrace = filtertrace; - this.methods = methods; - this.methodsSpecified = (methods != null); + this.methodsSpecified = methods != null; + this.methods = methodsSpecified ? (String[]) methods.clone() : null; } /** @@ -298,10 +297,10 @@ public class JUnitTest extends BaseTest implements Cloneable { break; case stateInsideWord: if (c == ',') { - result[wordIndex++] = new String(methodNames.substring(wordStartIndex, i)); + result[wordIndex++] = methodNames.substring(wordStartIndex, i); state = stateBeforeWord; } else if (c == ' ') { - result[wordIndex++] = new String(methodNames.substring(wordStartIndex, i)); + result[wordIndex++] = methodNames.substring(wordStartIndex, i); state = stateAfterWord; } else if (Character.isJavaIdentifierPart(c)) { // remain in the same state @@ -327,7 +326,7 @@ public class JUnitTest extends BaseTest implements Cloneable { case stateAfterWord: break; case stateInsideWord: - result[wordIndex++] = new String(methodNames.substring(wordStartIndex, chars.length)); + result[wordIndex++] = methodNames.substring(wordStartIndex, chars.length); break; default: // this should never happen diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java index 58ced0030..efb49a15e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java @@ -289,13 +289,13 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR boolean filtertrace, boolean haltOnFailure, boolean showOutput, boolean logTestListenerEvents, ClassLoader loader) { - JUnitTestRunner.filtertrace = filtertrace; + JUnitTestRunner.filtertrace = filtertrace; // XXX clumsy, should use instance field somehow this.junitTest = test; this.haltOnError = haltOnError; this.haltOnFailure = haltOnFailure; this.showOutput = showOutput; this.logTestListenerEvents = logTestListenerEvents; - this.methods = methods; + this.methods = methods != null ? (String[]) methods.clone() : null; this.loader = loader; }