You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by ja...@apache.org on 2023/03/02 06:34:13 UTC

[ant] branch master updated: 66504: junitlauncher task should resolve the outputDir against the basedir of the project

This is an automated email from the ASF dual-hosted git repository.

jaikiran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ant.git


The following commit(s) were added to refs/heads/master by this push:
     new 1200a76e7 66504: junitlauncher task should resolve the outputDir against the basedir of the project
1200a76e7 is described below

commit 1200a76e74ccce33da28f322f6574400786780a8
Author: Jaikiran Pai <ja...@apache.org>
AuthorDate: Thu Mar 2 11:37:30 2023 +0530

    66504: junitlauncher task should resolve the outputDir against the basedir of the project
---
 WHATSNEW                                           |  9 +++
 .../taskdefs/optional/junitlauncher-outputdir.xml  | 75 ++++++++++++++++++
 .../optional/junitlauncher/LauncherSupport.java    |  5 +-
 .../junitlauncher/confined/ListenerDefinition.java | 12 +--
 .../junitlauncher/confined/SingleTestClass.java    |  6 +-
 .../junitlauncher/confined/TestClasses.java        |  4 +-
 .../junitlauncher/confined/TestDefinition.java     |  8 +-
 .../junitlauncher/OutputDirLocationTest.java       | 92 ++++++++++++++++++++++
 .../example/junitlauncher/jupiter/PassingTest.java | 29 +++++++
 9 files changed, 226 insertions(+), 14 deletions(-)

diff --git a/WHATSNEW b/WHATSNEW
index 7e1627530..288d1581a 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -1,9 +1,18 @@
 Changes from Ant 1.10.13 TO Ant 1.10.14
 =======================================
 
+Fixed bugs:
+-----------
+
  * log only the stylesheet name in the xslt task.
    Github Pull Request #199
 
+ * junitlauncher task's "test" and "listener" elements which take
+   a "outputDir" property were incorrectly resolving the outputDir
+   against the current working directory instead of the project's
+   basedir. This has now been fixed.
+   Bugzilla Report 66504  
+
 Changes from Ant 1.10.12 TO Ant 1.10.13
 =======================================
 
diff --git a/src/etc/testcases/taskdefs/optional/junitlauncher-outputdir.xml b/src/etc/testcases/taskdefs/optional/junitlauncher-outputdir.xml
new file mode 100644
index 000000000..a92f22570
--- /dev/null
+++ b/src/etc/testcases/taskdefs/optional/junitlauncher-outputdir.xml
@@ -0,0 +1,75 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      https://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<!-- the basedir used here is intentionally different than the dir containing this build file.
+See issue 65504 for details
+ -->
+<project name="junitlauncher-outputdir-test" basedir="../../../../../build/testcases">
+
+    <property name="report-dir" value="bz-66504-reports"/>
+    <property name="build.classes.dir" value="${build.tests.value}"/>
+
+    <property name="lib.optional" value="${root}/lib/optional"/>
+
+    <path id="junit.platform.classpath">
+        <fileset dir="${lib.optional}" includes="junit-platform*.jar"/>
+    </path>
+
+    <path id="junit.engine.jupiter.classpath">
+        <fileset dir="${lib.optional}">
+            <include name="junit-jupiter*.jar"/>
+            <include name="opentest4j*.jar"/>
+        </fileset>
+    </path>
+
+    <path id="test.classpath">
+        <pathelement location="${build.classes.dir}"/>
+        <path refid="junit.engine.jupiter.classpath"/>
+    </path>
+
+    <target name="test-report-dir">
+        <mkdir dir="${report-dir}"/>
+        <junitlauncher>
+            <listener classname="org.example.junitlauncher.Tracker"
+                      outputDir="${report-dir}"
+                      resultFile="${test-report-dir.tracker}"
+                      if="test-report-dir.tracker"/>
+            <listener type="legacy-xml"/>
+            <!-- A specific test meant to pass -->
+            <test name="org.example.junitlauncher.jupiter.PassingTest" outputDir="${report-dir}"/>
+            <classpath refid="test.classpath"/>
+        </junitlauncher>
+    </target>
+
+    <target name="test-report-dir-fork">
+        <mkdir dir="${report-dir}"/>
+        <junitlauncher>
+            <listener classname="org.example.junitlauncher.Tracker"
+                      outputDir="${report-dir}"
+                      resultFile="${test-report-dir-fork.tracker}"
+                      if="test-report-dir-fork.tracker"/>
+            <listener type="legacy-xml"/>
+            <!-- A specific test meant to pass -->
+            <test name="org.example.junitlauncher.jupiter.PassingTest" outputDir="${report-dir}">
+                <fork/>
+            </test>
+            <classpath refid="test.classpath"/>
+        </junitlauncher>
+    </target>
+
+</project>
+
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 00b76df8a..65335dc03 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
@@ -45,6 +45,7 @@ import org.junit.platform.launcher.core.LauncherFactory;
 import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
 import org.junit.platform.launcher.listeners.TestExecutionSummary;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -287,11 +288,11 @@ public class LauncherSupport {
         }
         if (listener.getOutputDir() != null) {
             // use the output dir defined on the listener
-            return Paths.get(listener.getOutputDir(), filename);
+            return new File(listener.getOutputDir(), filename).toPath();
         }
         // check on the enclosing test definition, in context of which this listener is being run
         if (test.getOutputDir() != null) {
-            return Paths.get(test.getOutputDir(), filename);
+            return new File(test.getOutputDir(), filename).toPath();
         }
         // neither listener nor the test define a output dir, so use basedir of the project
         final TestExecutionContext testExecutionContext = this.testExecutionContext;
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 e661fd3c4..0433f49c4 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
@@ -17,6 +17,8 @@
  */
 package org.apache.tools.ant.taskdefs.optional.junitlauncher.confined;
 
+import java.io.File;
+
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.PropertyHelper;
 import org.apache.tools.ant.types.EnumeratedAttribute;
@@ -53,7 +55,7 @@ public class ListenerDefinition {
     private String extension = "txt";
     private boolean sendSysOut;
     private boolean sendSysErr;
-    private String outputDir;
+    private File outputDir;
     private boolean useLegacyReportingName = true;
 
     public ListenerDefinition() {
@@ -146,11 +148,11 @@ public class ListenerDefinition {
      * @param dir Path to the output directory
      * @since Ant 1.10.6
      */
-    public void setOutputDir(final String dir) {
+    public void setOutputDir(final File dir) {
         this.outputDir = dir;
     }
 
-    public String getOutputDir() {
+    public File getOutputDir() {
         return this.outputDir;
     }
 
@@ -198,7 +200,7 @@ public class ListenerDefinition {
         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);
+            writer.writeAttribute(LD_XML_ATTR_OUTPUT_DIRECTORY, this.outputDir.getPath());
         }
         if (this.resultFile != null) {
             writer.writeAttribute(LD_XML_ATTR_LISTENER_RESULT_FILE, this.resultFile);
@@ -224,7 +226,7 @@ public class ListenerDefinition {
         }
         final String outputDir = reader.getAttributeValue(null, LD_XML_ATTR_OUTPUT_DIRECTORY);
         if (outputDir != null) {
-            listenerDef.setOutputDir(outputDir);
+            listenerDef.setOutputDir(new File(outputDir));
         }
         final String resultFile = reader.getAttributeValue(null, LD_XML_ATTR_LISTENER_RESULT_FILE);
         if (resultFile != null) {
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/SingleTestClass.java b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/SingleTestClass.java
index 25961b824..603869eab 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/SingleTestClass.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/SingleTestClass.java
@@ -21,6 +21,8 @@ import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
+
+import java.io.File;
 import java.util.Collections;
 import java.util.LinkedHashSet;
 import java.util.Set;
@@ -108,7 +110,7 @@ public class SingleTestClass extends TestDefinition implements NamedTest {
             writer.writeAttribute(LD_XML_ATTR_HALT_ON_FAILURE, haltOnFailure.toString());
         }
         if (outputDir != null) {
-            writer.writeAttribute(LD_XML_ATTR_OUTPUT_DIRECTORY, outputDir);
+            writer.writeAttribute(LD_XML_ATTR_OUTPUT_DIRECTORY, outputDir.getPath());
         }
         if (includeEngines != null) {
             writer.writeAttribute(LD_XML_ATTR_INCLUDE_ENGINES, includeEngines);
@@ -144,7 +146,7 @@ public class SingleTestClass extends TestDefinition implements NamedTest {
         }
         final String outDir = reader.getAttributeValue(null, LD_XML_ATTR_OUTPUT_DIRECTORY);
         if (outDir != null) {
-            testDefinition.setOutputDir(outDir);
+            testDefinition.setOutputDir(new File(outDir));
         }
         final String includeEngs = reader.getAttributeValue(null, LD_XML_ATTR_INCLUDE_ENGINES);
         if (includeEngs != null) {
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/TestClasses.java b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/TestClasses.java
index 4286f4435..86aa525d9 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/TestClasses.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/TestClasses.java
@@ -84,7 +84,7 @@ public class TestClasses extends TestDefinition {
                 writer.writeAttribute(LD_XML_ATTR_HALT_ON_FAILURE, haltOnFailure.toString());
             }
             if (outputDir != null) {
-                writer.writeAttribute(LD_XML_ATTR_OUTPUT_DIRECTORY, outputDir);
+                writer.writeAttribute(LD_XML_ATTR_OUTPUT_DIRECTORY, outputDir.getPath());
             }
             if (includeEngines != null) {
                 writer.writeAttribute(LD_XML_ATTR_INCLUDE_ENGINES, includeEngines);
@@ -122,7 +122,7 @@ public class TestClasses extends TestDefinition {
             }
             final String outDir = reader.getAttributeValue(null, LD_XML_ATTR_OUTPUT_DIRECTORY);
             if (outDir != null) {
-                testDefinition.setOutputDir(outDir);
+                testDefinition.setOutputDir(new File(outDir));
             }
             final String includeEngs = reader.getAttributeValue(null, LD_XML_ATTR_INCLUDE_ENGINES);
             if (includeEngs != null) {
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/TestDefinition.java b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/TestDefinition.java
index a3df6d507..34b8eb8f8 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/TestDefinition.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/TestDefinition.java
@@ -23,6 +23,8 @@ import org.apache.tools.ant.PropertyHelper;
 
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
+
+import java.io.File;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -36,7 +38,7 @@ public abstract class TestDefinition {
     protected String unlessProperty;
     protected Boolean haltOnFailure;
     protected String failureProperty;
-    protected String outputDir;
+    protected File outputDir;
     protected String includeEngines;
     protected String excludeEngines;
     protected ForkDefinition forkDefinition;
@@ -87,11 +89,11 @@ public abstract class TestDefinition {
         return Collections.unmodifiableList(this.listeners);
     }
 
-    public void setOutputDir(final String dir) {
+    public void setOutputDir(final File dir) {
         this.outputDir = dir;
     }
 
-    public String getOutputDir() {
+    public File getOutputDir() {
         return this.outputDir;
     }
 
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junitlauncher/OutputDirLocationTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junitlauncher/OutputDirLocationTest.java
new file mode 100644
index 000000000..44310f11d
--- /dev/null
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junitlauncher/OutputDirLocationTest.java
@@ -0,0 +1,92 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.tools.ant.taskdefs.optional.junitlauncher;
+
+import java.io.File;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.apache.tools.ant.BuildFileRule;
+import org.example.junitlauncher.jupiter.PassingTest;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import static org.example.junitlauncher.Tracker.verifySuccess;
+import static org.example.junitlauncher.Tracker.wasTestRun;
+
+/**
+ * Tests that the outputDir property used by the junitlauncher task's elements resolve against
+ * the basedir of the project.
+ */
+public class OutputDirLocationTest {
+
+    @Rule
+    public final BuildFileRule buildRule = new BuildFileRule();
+
+    @Before
+    public void setUp() {
+        buildRule.configureProject("src/etc/testcases/taskdefs/optional/junitlauncher-outputdir.xml");
+    }
+
+    /**
+     * basedir of the project is a different directory than the one which contains the project build
+     * file. A target which uses junitlauncher task which runs in-vm tests and uses a outputDir,
+     * is launched. The test verifies that the outputDir resolves against the basedir of the project
+     *
+     * @see "Bugzilla issue 66504"
+     */
+    @Test
+    public void testJunitLauncherReportDir() throws Exception {
+        final String targetName = "test-report-dir";
+        final Path trackerFile = setupTrackerProperty(targetName);
+        buildRule.executeTarget(targetName);
+        // make sure the right test(s) were run
+        Assert.assertTrue("PassingTest test was expected to be run",
+                wasTestRun(trackerFile, PassingTest.class.getName()));
+        Assert.assertTrue("PassingTest#testSucceeds was expected to succeed",
+                verifySuccess(trackerFile, PassingTest.class.getName(), "testSucceeds"));
+    }
+
+    /**
+     * basedir of the project is a different directory than the one which contains the project build
+     * file. A target which uses junitlauncher task which runs forked tests and uses a outputDir,
+     * is launched. The test verifies that the outputDir resolves against the basedir of the project.
+     *
+     * @see "Bugzilla issue 66504"
+     */
+    @Test
+    public void testForkedJunitLauncherReportDir() throws Exception {
+        final String targetName = "test-report-dir-fork";
+        final Path trackerFile = setupTrackerProperty(targetName);
+        buildRule.executeTarget(targetName);
+        // make sure the right test(s) were run
+        Assert.assertTrue("PassingTest test was expected to be run",
+                wasTestRun(trackerFile, PassingTest.class.getName()));
+        Assert.assertTrue("PassingTest#testSucceeds was expected to succeed",
+                verifySuccess(trackerFile, PassingTest.class.getName(), "testSucceeds"));
+    }
+
+    private Path setupTrackerProperty(final String targetName) {
+        final String filename = targetName + "-tracker.txt";
+        buildRule.getProject().setProperty(targetName + ".tracker", filename);
+        final String outputDir = buildRule.getProject().getProperty("report-dir");
+        final File resolvedOutputDir = buildRule.getProject().resolveFile(outputDir);
+        return Paths.get(resolvedOutputDir.toString(), filename);
+    }
+}
diff --git a/src/tests/junit/org/example/junitlauncher/jupiter/PassingTest.java b/src/tests/junit/org/example/junitlauncher/jupiter/PassingTest.java
new file mode 100644
index 000000000..66907a7a6
--- /dev/null
+++ b/src/tests/junit/org/example/junitlauncher/jupiter/PassingTest.java
@@ -0,0 +1,29 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.example.junitlauncher.jupiter;
+
+import org.junit.jupiter.api.Test;
+
+// A jupiter test case that always passes
+public class PassingTest {
+    @Test
+    void testSucceeds() {
+        System.out.println("Test1234");
+        System.out.print("<some-other-message>Hello world! <!-- some comment --></some-other-message>");
+    }
+}