Merge pull request #140 from TheConstructor/resource-list-with-duplicates

#64854 Add preserveduplicates option to ResourceList
diff --git a/WHATSNEW b/WHATSNEW
index f17bf4b..c25a17b 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -1,5 +1,26 @@
 Changes from Ant 1.10.9 TO Ant 1.10.10
 ======================================
+Fixed bugs:
+-----------
+
+ * SCP (with sftp=true) task would fail if fetching file located in root directory
+   Bugzilla Report 64742
+
+Other changes:
+--------------
+
+ * javaversion condition now has a new "atmost" attribute. See the javaversion
+   manual for more details
+
+ * The "listener" nested element of the "junitlauncher" task now has a new
+   "useLegacyReportingName" attribute which can be used to control the test
+   identifiers names that get reported by the listener. See the junitlauncher
+   manual for more details.
+   Note that this change also introduces a new "setUseLegacyReportingName" method
+   on the org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter
+   interface. This will break backward compatibility with any of your custom
+   result formatters which implemented this interface and such implementations
+   are now expected to implement this new method.
 
 Changes from Ant 1.10.8 TO Ant 1.10.9
 =====================================
diff --git a/manual/Tasks/conditions.html b/manual/Tasks/conditions.html
index 846733f..247f2dd 100644
--- a/manual/Tasks/conditions.html
+++ b/manual/Tasks/conditions.html
@@ -945,11 +945,19 @@
     <th scope="col">Required</th>
   </tr>
   <tr>
-    <td>atleast</td>
+    <td class="left">atleast</td>
     <td>The version that this JVM is of at least. The format
       is <code>major.minor.point</code>. Starting with Java 9 really only the major number is
       determined.</td>
-    <td rowspan="2">Exactly one of the two</td>
+    <td rowspan="3">Exactly one of the three</td>
+  </tr>
+  <tr>
+    <td>atmost</td>
+    <td class="left">The version that this JVM is of at most. The format
+      is <code>major.minor.point</code>. Starting with Java 9 really only the major number is
+      determined.<br/>
+      <em>Since Ant 1.10.10</em>
+    </td>
   </tr>
   <tr>
     <td>exactly</td>
diff --git a/manual/Tasks/junitlauncher.html b/manual/Tasks/junitlauncher.html
index ec14e03..4a96e71 100644
--- a/manual/Tasks/junitlauncher.html
+++ b/manual/Tasks/junitlauncher.html
@@ -402,6 +402,14 @@
             is <strong>not</strong> set</a>.</td>
         <td>No</td>
     </tr>
+    <tr>
+        <td>useLegacyReportingName</td>
+        <td>Set to true, if the test identifiers reported by this listener should use legacy (JUnit4
+            style) names. Else set to false. Defaults to true.
+            <p><em>Since Ant 1.10.10</em></p>
+        </td>
+        <td>No</td>
+    </tr>
 </table>
 
 <h4>test</h4>
diff --git a/src/etc/testcases/taskdefs/optional/junitlauncher.xml b/src/etc/testcases/taskdefs/optional/junitlauncher.xml
index f86ddc8..87b00ad 100644
--- a/src/etc/testcases/taskdefs/optional/junitlauncher.xml
+++ b/src/etc/testcases/taskdefs/optional/junitlauncher.xml
@@ -161,7 +161,7 @@
                     <sysproperty key="junitlauncher.test.sysprop.one" value="forked"/>
                 </fork>
 
-                <listener type="legacy-xml" sendSysErr="true" sendSysOut="true"/>
+                <listener type="legacy-xml" sendSysErr="true" sendSysOut="true" useLegacyReportingName="false"/>
             </test>
         </junitlauncher>
     </target>
@@ -368,6 +368,7 @@
                 </fork>
             </testclasses>
             <listener type="legacy-plain" sendSysOut="true" />
+            <listener type="legacy-brief" sendSysOut="true" useLegacyReportingName="true"/>
         </junitlauncher>
     </target>
 </project>
diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/JavaVersion.java b/src/main/org/apache/tools/ant/taskdefs/condition/JavaVersion.java
index e121c30..a221495 100644
--- a/src/main/org/apache/tools/ant/taskdefs/condition/JavaVersion.java
+++ b/src/main/org/apache/tools/ant/taskdefs/condition/JavaVersion.java
@@ -23,10 +23,11 @@
 
 /**
  * An Java version condition.
- * @since Java 1.10.2
+ * @since Ant 1.10.2
  */
 public class JavaVersion implements Condition {
 
+    private String atMost = null;
     private String atLeast = null;
     private String exactly = null;
 
@@ -44,16 +45,19 @@
         if (null != exactly) {
             return actual.isEqual(new DeweyDecimal(exactly));
         }
+        if (atMost != null) {
+            return actual.isLessThanOrEqual(new DeweyDecimal(atMost));
+        }
         //default
         return false;
     }
 
     private void validate() throws BuildException {
-        if (atLeast != null && exactly != null) {
-            throw new BuildException("Only one of atleast or exactly may be set.");
+        if (atLeast != null && exactly != null && atMost != null) {
+            throw new BuildException("Only one of atleast or atmost or exactly may be set.");
         }
-        if (null == atLeast && null == exactly) {
-            throw new BuildException("One of atleast or exactly must be set.");
+        if (null == atLeast && null == exactly && atMost == null) {
+            throw new BuildException("One of atleast or atmost or exactly must be set.");
         }
         if (atLeast != null) {
             try {
@@ -64,6 +68,14 @@
                     "The 'atleast' attribute is not a Dewey Decimal eg 1.1.0 : "
                     + atLeast);
             }
+        } else if (atMost != null) {
+            try {
+                new DeweyDecimal(atMost); //NOSONAR
+            } catch (NumberFormatException e) {
+                throw new BuildException(
+                        "The 'atmost' attribute is not a Dewey Decimal eg 1.1.0 : "
+                                + atMost);
+            }
         } else {
             try {
                 // only created for side effect
@@ -88,13 +100,33 @@
      * Set the atleast attribute.
      * This is of the form major.minor.point.
      * For example 1.7.0.
-     * @param atLeast the version to check against.
+     * @param atLeast the version to set
      */
     public void setAtLeast(String atLeast) {
         this.atLeast = atLeast;
     }
 
     /**
+     * Get the atmost attribute.
+     * @return the atmost attribute.
+     * @since Ant 1.10.10
+     */
+    public String getAtMost() {
+        return atMost;
+    }
+
+    /**
+     * Set the atmost attribute.
+     * This is of the form major.minor.point.
+     * For example 11.0.2
+     * @param atMost the version to set
+     * @since Ant 1.10.10
+     */
+    public void setAtMost(String atMost) {
+        this.atMost = atMost;
+    }
+
+    /**
      * Get the exactly attribute.
      * @return the exactly attribute.
      */
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java
index a5ce996..3b56016 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java
@@ -226,6 +226,7 @@
         testRequest.closeUponCompletion(resultFormatter);
         // set the execution context
         resultFormatter.setContext(this.testExecutionContext);
+        resultFormatter.setUseLegacyReportingName(formatterDefinition.isUseLegacyReportingName());
         // set the destination output stream for writing out the formatted result
         final java.nio.file.Path resultOutputFile = getListenerOutputFile(testRequest, formatterDefinition);
         try {
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyPlainResultFormatter.java b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyPlainResultFormatter.java
index 997e86e..7583d78 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyPlainResultFormatter.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyPlainResultFormatter.java
@@ -47,6 +47,7 @@
     private final Map<TestIdentifier, Stats> testIds = new ConcurrentHashMap<>();
     private TestPlan testPlan;
     private BufferedWriter writer;
+    private boolean useLegacyReportingName = true;
 
     @Override
     public void testPlanExecutionStarted(final TestPlan testPlan) {
@@ -111,7 +112,7 @@
         if (testIdentifier.isTest()) {
             final StringBuilder sb = new StringBuilder();
             sb.append("Test: ");
-            sb.append(testIdentifier.getLegacyReportingName());
+            sb.append(this.useLegacyReportingName ? testIdentifier.getLegacyReportingName() : testIdentifier.getDisplayName());
             sb.append(" took ");
             stats.appendElapsed(sb);
             sb.append(" SKIPPED");
@@ -175,7 +176,7 @@
         if (testIdentifier.isTest() && shouldReportExecutionFinished(testIdentifier, testExecutionResult)) {
             final StringBuilder sb = new StringBuilder();
             sb.append("Test: ");
-            sb.append(testIdentifier.getLegacyReportingName());
+            sb.append(this.useLegacyReportingName ? testIdentifier.getLegacyReportingName() : testIdentifier.getDisplayName());
             if (stats != null) {
                 sb.append(" took ");
                 stats.appendElapsed(sb);
@@ -230,6 +231,11 @@
         this.writer = new BufferedWriter(new OutputStreamWriter(this.outputStream, StandardCharsets.UTF_8));
     }
 
+    @Override
+    public void setUseLegacyReportingName(final boolean useLegacyReportingName) {
+        this.useLegacyReportingName = useLegacyReportingName;
+    }
+
     protected boolean shouldReportExecutionFinished(final TestIdentifier testIdentifier, final TestExecutionResult testExecutionResult) {
         return true;
     }
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyXmlResultFormatter.java b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyXmlResultFormatter.java
index dad1cc8..c0d4bee 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyXmlResultFormatter.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyXmlResultFormatter.java
@@ -63,6 +63,7 @@
     private final AtomicLong numTestsFailed = new AtomicLong(0);
     private final AtomicLong numTestsSkipped = new AtomicLong(0);
     private final AtomicLong numTestsAborted = new AtomicLong(0);
+    private boolean useLegacyReportingName = true;
 
 
     @Override
@@ -141,6 +142,11 @@
         this.outputStream = os;
     }
 
+    @Override
+    public void setUseLegacyReportingName(final boolean useLegacyReportingName) {
+        this.useLegacyReportingName = useLegacyReportingName;
+    }
+
     private final class Stats {
         @SuppressWarnings("unused")
         private final TestIdentifier testIdentifier;
@@ -252,7 +258,8 @@
                 final String classname = (parentClassSource.get()).getClassName();
                 writer.writeStartElement(ELEM_TESTCASE);
                 writer.writeAttribute(ATTR_CLASSNAME, classname);
-                writer.writeAttribute(ATTR_NAME, testId.getLegacyReportingName());
+                writer.writeAttribute(ATTR_NAME, useLegacyReportingName ? testId.getLegacyReportingName()
+                        : testId.getDisplayName());
                 final Stats stats = entry.getValue();
                 writer.writeAttribute(ATTR_TIME, String.valueOf((stats.endedAt - stats.startedAt) / ONE_SECOND));
                 // skipped element if the test was skipped
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/TestResultFormatter.java b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/TestResultFormatter.java
index f59641b..4de168a 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/TestResultFormatter.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/TestResultFormatter.java
@@ -51,6 +51,17 @@
     void setContext(TestExecutionContext context);
 
     /**
+     * This method will be invoked by the {@code junitlauncher} to let the result formatter implementation
+     * know whether or not to use JUnit 4 style, legacy reporting names for test identifiers that get
+     * displayed in the test reports. Result formatter implementations are allowed to default to a specific
+     * reporting style for test identifiers, if this method isn't invoked.
+     * @param useLegacyReportingName {@code true} if legacy reporting name is to be used, {@code false}
+     *                               otherwise.
+     * @since Ant 1.10.10
+     */
+    void setUseLegacyReportingName(boolean useLegacyReportingName);
+
+    /**
      * This method will be invoked by the <code>junitlauncher</code>, <strong>regularly/multiple times</strong>,
      * as and when any content is generated on the standard output stream during the test execution.
      * This method will be only be called if the <code>sendSysOut</code> attribute of the <code>listener</code>,
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/Constants.java b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/Constants.java
index ddd5902..7117907 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/Constants.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/Constants.java
@@ -48,6 +48,7 @@
     public static final String LD_XML_ATTR_SEND_SYS_ERR = "sendSysErr";
     public static final String LD_XML_ATTR_SEND_SYS_OUT = "sendSysOut";
     public static final String LD_XML_ATTR_LISTENER_RESULT_FILE = "resultFile";
+    public static final String LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME = "useLegacyReportingName";
 
 
     private Constants() {
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/ListenerDefinition.java b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/ListenerDefinition.java
index c600e60..ce9fdee 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/ListenerDefinition.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/ListenerDefinition.java
@@ -28,6 +28,7 @@
 
 import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_CLASS_NAME;
 import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_LISTENER_RESULT_FILE;
+import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME;
 import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_OUTPUT_DIRECTORY;
 import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_SEND_SYS_ERR;
 import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_SEND_SYS_OUT;
@@ -51,6 +52,7 @@
     private boolean sendSysOut;
     private boolean sendSysErr;
     private String outputDir;
+    private boolean useLegacyReportingName = true;
 
     public ListenerDefinition() {
 
@@ -135,6 +137,26 @@
         return this.outputDir;
     }
 
+    /**
+     *
+     * @return Returns {@code true} if legacy reporting name (JUnit 4 style) is to be used.
+     *         Else returns {@code false}.
+     * @since Ant 1.10.10
+     */
+    public boolean isUseLegacyReportingName() {
+        return useLegacyReportingName;
+    }
+
+    /**
+     * Set the test identifier reporting style
+     * @param useLegacyReportingName {@code true} if legacy reporting name (JUnit 4 style) is to
+     *                               be used. Else {@code false}.
+     * @since Ant 1.10.10
+     */
+    public void setUseLegacyReportingName(final boolean useLegacyReportingName) {
+        this.useLegacyReportingName = useLegacyReportingName;
+    }
+
     public boolean shouldUse(final Project project) {
         final PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(project);
         return propertyHelper.testIfCondition(this.ifProperty) && propertyHelper.testUnlessCondition(this.unlessProperty);
@@ -157,6 +179,7 @@
         writer.writeAttribute(LD_XML_ATTR_CLASS_NAME, this.className);
         writer.writeAttribute(LD_XML_ATTR_SEND_SYS_ERR, Boolean.toString(this.sendSysErr));
         writer.writeAttribute(LD_XML_ATTR_SEND_SYS_OUT, Boolean.toString(this.sendSysOut));
+        writer.writeAttribute(LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME, Boolean.toString(this.useLegacyReportingName));
         if (this.outputDir != null) {
             writer.writeAttribute(LD_XML_ATTR_OUTPUT_DIRECTORY, this.outputDir);
         }
@@ -187,6 +210,11 @@
         if (resultFile != null) {
             listenerDef.setResultFile(resultFile);
         }
+        final String useLegacyReportingName = reader.getAttributeValue(null,
+                LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME);
+        if (useLegacyReportingName != null) {
+            listenerDef.setUseLegacyReportingName(Boolean.parseBoolean(useLegacyReportingName));
+        }
         reader.nextTag();
         reader.require(XMLStreamConstants.END_ELEMENT, null, LD_XML_ELM_LISTENER);
         return listenerDef;
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessageBySftp.java b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessageBySftp.java
index 0ad0899..7e5edab 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessageBySftp.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessageBySftp.java
@@ -134,9 +134,16 @@
                         final String remoteFile,
                         final File localFile) throws SftpException {
         String pwd = remoteFile;
-        if (remoteFile.lastIndexOf('/') != -1) {
+        final int lastIndexOfFileSeparator = remoteFile.lastIndexOf('/');
+        if (lastIndexOfFileSeparator != -1) {
             if (remoteFile.length() > 1) {
-                pwd = remoteFile.substring(0, remoteFile.lastIndexOf('/'));
+                if (lastIndexOfFileSeparator == 0) {
+                    // the file path is of the form "/foo....." i.e. the file separator
+                    // occurs at the start (and only there).
+                    pwd = "/";
+                } else {
+                    pwd = remoteFile.substring(0, lastIndexOfFileSeparator);
+                }
             }
         }
         channel.cd(pwd);
diff --git a/src/tests/antunit/taskdefs/condition/javaversion-test.xml b/src/tests/antunit/taskdefs/condition/javaversion-test.xml
index a5d9a21..586cad0 100644
--- a/src/tests/antunit/taskdefs/condition/javaversion-test.xml
+++ b/src/tests/antunit/taskdefs/condition/javaversion-test.xml
@@ -25,6 +25,20 @@
     </au:assertTrue>
   </target>
 
+  <target name="test-atmost">
+    <au:assertTrue message="Expected javaversion ${java.version} to be at most 1000.111.211">
+      <!-- a high version value so that the check passes -->
+      <javaversion atmost="1000.111.211" />
+    </au:assertTrue>
+  </target>
+
+  <target name="test-atmost-negative">
+    <au:assertFalse message="Expected javaversion ${java.version} to be at most 1.4.0">
+      <!-- Ant 1.10.x requires Java 8 at runtime - so this check is expected to return false -->
+      <javaversion atmost="1.4.0" />
+    </au:assertFalse>
+  </target>
+
   <target name="test-exactly">
     <au:assertTrue message="Expected javaversion of ${ant.java.version}">
       <javaversion exactly="${ant.java.version}" />
diff --git a/src/tests/antunit/taskdefs/optional/script/scriptdef-test.xml b/src/tests/antunit/taskdefs/optional/script/scriptdef-test.xml
index b604aa2..e2a7718 100644
--- a/src/tests/antunit/taskdefs/optional/script/scriptdef-test.xml
+++ b/src/tests/antunit/taskdefs/optional/script/scriptdef-test.xml
@@ -26,13 +26,17 @@
   </description>
 
   <condition property="prereqs-ok">
-    <or>
-      <and>
-        <available classname="org.apache.bsf.BSFManager" />
-        <available classname="org.apache.bsf.engines.javascript.JavaScriptEngine" />
-      </and>
-      <available classname="javax.script.ScriptEngineManager" />
-    </or>
+    <and>
+      <!-- Starting Java 15, there's no "javascript" script engine (not even nashorn) bundled in JRE -->
+      <javaversion atmost="14"/>
+      <or>
+        <and>
+          <available classname="org.apache.bsf.BSFManager" />
+          <available classname="org.apache.bsf.engines.javascript.JavaScriptEngine" />
+        </and>
+        <available classname="javax.script.ScriptEngineManager" />
+      </or>
+    </and>
   </condition>
 
   <!-- auto doesn't verify the language is supported and selects BSF
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java
index 667db43..4ebf3d6 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java
@@ -20,7 +20,9 @@
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.BuildFileRule;
 import org.apache.tools.ant.Project;
+import org.apache.tools.ant.taskdefs.condition.JavaVersion;
 import org.apache.tools.ant.types.FileSet;
+import org.junit.Assume;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -155,7 +157,11 @@
 
     @Test
     public void testUseCompiled() {
-
+        final JavaVersion atMostJava14 = new JavaVersion();
+        atMostJava14.setAtMost("14");
+        // skip execution since this compilation timing based test consistently fails starting Java 15 (where we use
+        // Graal libraries for Javascript engine)
+        Assume.assumeTrue("Skipping test execution since Java version is greater than Java 14", atMostJava14.eval());
         final long duration;
         {
             long start = System.nanoTime();
diff --git a/src/tests/junit/org/example/junitlauncher/Tracker.java b/src/tests/junit/org/example/junitlauncher/Tracker.java
index ba31ec8..ec5f30a 100644
--- a/src/tests/junit/org/example/junitlauncher/Tracker.java
+++ b/src/tests/junit/org/example/junitlauncher/Tracker.java
@@ -74,6 +74,11 @@
     }
 
     @Override
+    public void setUseLegacyReportingName(final boolean useLegacyReportingName) {
+        // do nothing
+    }
+
+    @Override
     public void close() throws IOException {
         this.writer.flush();
         if (this.appendModeFile != null) {