From d8fbd24e696497188d3d7e34e0f41ecdeb5568bf Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 18 Nov 2021 09:15:23 +0100 Subject: [PATCH] [dist] Add integration test for ant --- pmd-dist/pom.xml | 29 ++++++++ .../it/AbstractBinaryDistributionTest.java | 3 +- .../java/net/sourceforge/pmd/it/AntIT.java | 67 +++++++++++++++++++ .../net/sourceforge/pmd/it/PMDExecutor.java | 5 +- pmd-dist/src/test/resources/ant-it/build.xml | 19 ++++++ .../src/test/resources/ant-it/src/Sample.java | 1 + 6 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 pmd-dist/src/test/java/net/sourceforge/pmd/it/AntIT.java create mode 100644 pmd-dist/src/test/resources/ant-it/build.xml create mode 100644 pmd-dist/src/test/resources/ant-it/src/Sample.java diff --git a/pmd-dist/pom.xml b/pmd-dist/pom.xml index 82c6168386..fb98252848 100644 --- a/pmd-dist/pom.xml +++ b/pmd-dist/pom.xml @@ -77,6 +77,35 @@ + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-ant + pre-integration-test + + copy + + + + + org.apache.ant + ant + ${ant.version} + + + org.apache.ant + ant-launcher + ${ant.version} + + + ${project.build.directory}/ant + true + + + + org.apache.maven.plugins maven-failsafe-plugin diff --git a/pmd-dist/src/test/java/net/sourceforge/pmd/it/AbstractBinaryDistributionTest.java b/pmd-dist/src/test/java/net/sourceforge/pmd/it/AbstractBinaryDistributionTest.java index 2822d2867a..d45bcd7cd6 100644 --- a/pmd-dist/src/test/java/net/sourceforge/pmd/it/AbstractBinaryDistributionTest.java +++ b/pmd-dist/src/test/java/net/sourceforge/pmd/it/AbstractBinaryDistributionTest.java @@ -14,9 +14,10 @@ import org.junit.rules.TemporaryFolder; import net.sourceforge.pmd.PMDVersion; public abstract class AbstractBinaryDistributionTest { + public static final String PMD_BIN_PREFIX = "pmd-bin-"; protected static File getBinaryDistribution() { - return new File(".", "target/pmd-bin-" + PMDVersion.VERSION + ".zip"); + return new File(".", "target/" + PMD_BIN_PREFIX + PMDVersion.VERSION + ".zip"); } @ClassRule diff --git a/pmd-dist/src/test/java/net/sourceforge/pmd/it/AntIT.java b/pmd-dist/src/test/java/net/sourceforge/pmd/it/AntIT.java new file mode 100644 index 0000000000..c81858aedc --- /dev/null +++ b/pmd-dist/src/test/java/net/sourceforge/pmd/it/AntIT.java @@ -0,0 +1,67 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.it; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; +import org.shaded.apache.commons.io.FileUtils; + +import net.sourceforge.pmd.PMDVersion; + +public class AntIT extends AbstractBinaryDistributionTest { + @Test + public void runAnt() throws IOException, InterruptedException { + String antBasepath = new File("target/ant").getAbsolutePath(); + String pmdHome = tempDir.resolve(PMD_BIN_PREFIX + PMDVersion.VERSION).toAbsolutePath().toString(); + File antTestProjectFolder = prepareAntTestProjectFolder(); + + ExecutionResult result = runAnt(antBasepath, pmdHome, antTestProjectFolder); + result.assertExecutionResult(0, "BUILD SUCCESSFUL"); + result.assertExecutionResult(0, "NoPackage"); // the no package rule + } + + + private File prepareAntTestProjectFolder() throws IOException { + File sourceProjectFolder = new File("src/test/resources/ant-it"); + File projectFolder = folder.newFolder(); + FileUtils.copyDirectory(sourceProjectFolder, projectFolder); + return projectFolder; + } + + + private ExecutionResult runAnt(String antLibPath, String pmdHomePath, File antTestProjectFolder) + throws IOException, InterruptedException { + ProcessBuilder pb = new ProcessBuilder(System.getenv("JAVA_HOME") + "/bin/java", "-cp", antLibPath + "/*", + "-jar", antLibPath + "/ant-launcher.jar", "-Dpmd.home=" + pmdHomePath); + pb.directory(antTestProjectFolder); + pb.redirectErrorStream(true); + + final ExecutionResult.Builder result = new ExecutionResult.Builder(); + final Process process = pb.start(); + Thread outputReader = new Thread(new Runnable() { + @Override + public void run() { + try (InputStream in = process.getInputStream()) { + String output = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8); + result.withOutput(output); + } catch (IOException e) { + result.withOutput("Exception occurred: " + e.toString()); + } + } + }); + outputReader.start(); + int exitCode = process.waitFor(); + outputReader.join(TimeUnit.SECONDS.toMillis(5)); + + result.withExitCode(exitCode); + return result.build(); + } +} diff --git a/pmd-dist/src/test/java/net/sourceforge/pmd/it/PMDExecutor.java b/pmd-dist/src/test/java/net/sourceforge/pmd/it/PMDExecutor.java index cd19c7f4ea..d25373ffab 100644 --- a/pmd-dist/src/test/java/net/sourceforge/pmd/it/PMDExecutor.java +++ b/pmd-dist/src/test/java/net/sourceforge/pmd/it/PMDExecutor.java @@ -24,7 +24,6 @@ import net.sourceforge.pmd.PMDVersion; * @author Andreas Dangel */ public class PMDExecutor { - private static final String PMD_BIN_PREFIX = "pmd-bin-"; private static final String SOURCE_DIRECTORY_FLAG = "-d"; private static final String RULESET_FLAG = "-R"; private static final String FORMAT_FLAG = "-f"; @@ -36,7 +35,7 @@ public class PMDExecutor { } private static ExecutionResult runPMDUnix(Path tempDir, Path reportFile, String ... arguments) throws Exception { - String cmd = tempDir.resolve(PMD_BIN_PREFIX + PMDVersion.VERSION + "/bin/run.sh").toAbsolutePath().toString(); + String cmd = tempDir.resolve(AbstractBinaryDistributionTest.PMD_BIN_PREFIX + PMDVersion.VERSION + "/bin/run.sh").toAbsolutePath().toString(); List args = new ArrayList<>(); args.add("pmd"); args.addAll(Arrays.asList(arguments)); @@ -44,7 +43,7 @@ public class PMDExecutor { } private static ExecutionResult runPMDWindows(Path tempDir, Path reportFile, String ... arguments) throws Exception { - String cmd = tempDir.resolve(PMD_BIN_PREFIX + PMDVersion.VERSION + "/bin/pmd.bat").toAbsolutePath().toString(); + String cmd = tempDir.resolve(AbstractBinaryDistributionTest.PMD_BIN_PREFIX + PMDVersion.VERSION + "/bin/pmd.bat").toAbsolutePath().toString(); return runPMD(cmd, Arrays.asList(arguments), reportFile); } diff --git a/pmd-dist/src/test/resources/ant-it/build.xml b/pmd-dist/src/test/resources/ant-it/build.xml new file mode 100644 index 0000000000..f3c91db8ac --- /dev/null +++ b/pmd-dist/src/test/resources/ant-it/build.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + rulesets/java/quickstart.xml + + + + + + + diff --git a/pmd-dist/src/test/resources/ant-it/src/Sample.java b/pmd-dist/src/test/resources/ant-it/src/Sample.java new file mode 100644 index 0000000000..0dd08d397a --- /dev/null +++ b/pmd-dist/src/test/resources/ant-it/src/Sample.java @@ -0,0 +1 @@ +public class Sample {}