[dist] Add integration test for ant

This commit is contained in:
Andreas Dangel
2021-11-18 09:15:23 +01:00
parent f18ceebd7b
commit d8fbd24e69
6 changed files with 120 additions and 4 deletions

View File

@ -77,6 +77,35 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-ant</id>
<phase>pre-integration-test</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>${ant.version}</version>
</artifactItem>
<artifactItem>
<groupId>org.apache.ant</groupId>
<artifactId>ant-launcher</artifactId>
<version>${ant.version}</version>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/ant</outputDirectory>
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>

View File

@ -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

View File

@ -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();
}
}

View File

@ -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<String> 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);
}

View File

@ -0,0 +1,19 @@
<project name="ant-it" default="pmd">
<path id="pmd.classpath">
<fileset dir="${pmd.home}/lib">
<include name="*.jar"/>
</fileset>
</path>
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.classpath"/>
<target name="pmd">
<pmd>
<ruleset>rulesets/java/quickstart.xml</ruleset>
<formatter type="text" toConsole="true"/>
<fileset dir="src">
<include name="*.java"/>
</fileset>
</pmd>
</target>
</project>

View File

@ -0,0 +1 @@
public class Sample {}