Make real Ant test cases

This commit is contained in:
Clément Fournier
2018-03-05 21:58:05 +01:00
parent f74a6b3887
commit f8864e9060
5 changed files with 63 additions and 19 deletions

View File

@@ -204,14 +204,6 @@ public class PMD {
return 0;
}
if (!configuration.isIgnoreIncrementalAnalysis()
&& configuration.getAnalysisCache() instanceof NoopAnalysisCache
&& LOG.isLoggable(Level.WARNING)) {
final String version = PMDVersion.isUnknown() || PMDVersion.isSnapshot() ? "latest" : "pmd-" + PMDVersion.VERSION;
LOG.warning("This analysis could be faster, please consider using Incremental Analysis: "
+ "https://pmd.github.io/" + version + "/pmd_userdocs_getting_started.html#incremental-analysis");
}
Set<Language> languages = getApplicableLanguages(configuration, ruleSets);
List<DataSource> files = getApplicableFiles(configuration, languages);
@@ -304,6 +296,14 @@ public class PMD {
public static void processFiles(final PMDConfiguration configuration, final RuleSetFactory ruleSetFactory,
final List<DataSource> files, final RuleContext ctx, final List<Renderer> renderers) {
if (!configuration.isIgnoreIncrementalAnalysis()
&& configuration.getAnalysisCache() instanceof NoopAnalysisCache
&& LOG.isLoggable(Level.WARNING)) {
final String version = PMDVersion.isUnknown() || PMDVersion.isSnapshot() ? "latest" : "pmd-" + PMDVersion.VERSION;
LOG.warning("This analysis could be faster, please consider using Incremental Analysis: "
+ "https://pmd.github.io/" + version + "/pmd_userdocs_getting_started.html#incremental-analysis");
}
sortFiles(configuration, files);
// Make sure the cache is listening for analysis results

View File

@@ -256,6 +256,7 @@ public class PMDTask extends Task {
this.cacheLocation = cacheLocation;
}
public boolean isNoCache() {
return noCache;
}

View File

@@ -4,6 +4,9 @@
package net.sourceforge.pmd.ant;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
@@ -11,7 +14,6 @@ import java.util.Locale;
import java.util.Objects;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
@@ -145,7 +147,7 @@ public class PMDTaskTest extends AbstractAntTestHelper {
executeTarget("testFormatterEncodingWithXML");
String report = FileUtils.readFileToString(new File("target/testFormatterEncodingWithXML-pmd.xml"), "UTF-8");
Assert.assertTrue(report.contains("unusedVariableWithÜmlaut"));
assertTrue(report.contains("unusedVariableWithÜmlaut"));
}
@Test
@@ -154,21 +156,33 @@ public class PMDTaskTest extends AbstractAntTestHelper {
executeTarget("testFormatterEncodingWithXMLConsole");
String report = buildRule.getOutput();
Assert.assertTrue(report.startsWith("<?xml version=\"1.0\" encoding=\"windows-1252\"?>"));
Assert.assertTrue(report.contains("unusedVariableWith&#xdc;mlaut"));
assertTrue(report.startsWith("<?xml version=\"1.0\" encoding=\"windows-1252\"?>"));
assertTrue(report.contains("unusedVariableWith&#xdc;mlaut"));
}
// The following two tests just test that the switches are recognised. How to test analysis cache?
@Test
public void testMissingCacheLocation() {
executeTarget("testMissingCacheLocation");
assertOutputContaining("Avoid really long methods");
assertContains(buildRule.getLog(), "This analysis could be faster");
}
@Test
public void testAnalysisCache() {
executeTarget("testAnalysisCache");
assertOutputContaining("Avoid really long methods");
assertDoesntContain(buildRule.getLog(), "This analysis could be faster");
assertTrue(new File(buildRule.getProject().getProperty("tmpfile")).exists());
}
@Test
public void testDisableIncrementalAnalysis() {
executeTarget("testDisableIncrementalAnalysis");
assertOutputContaining("Avoid really long methods");
assertDoesntContain(buildRule.getLog(), "This analysis could be faster");
assertFalse(new File(buildRule.getProject().getProperty("tmpfile")).exists());
}
}

View File

@@ -145,8 +145,17 @@
</target>
<target name="testAnalysisCache">
<pmd rulesetfiles="src/main/resources/category/java/design.xml/ExcessiveMethodLength" cacheLocation="/home/user/.pmd/cache">
<formatter type="text" toConsole="true"/>
<pmd rulesetfiles="src/main/resources/category/java/design.xml/ExcessiveMethodLength" cacheLocation="${tmpfile}">
<formatter type="xml" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>
</fileset>
</pmd>
</target>
<target name="testMissingCacheLocation">
<pmd rulesetfiles="src/main/resources/category/java/design.xml/ExcessiveMethodLength">
<formatter type="xml" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>
</fileset>
@@ -154,8 +163,8 @@
</target>
<target name="testDisableIncrementalAnalysis">
<pmd rulesetfiles="src/main/resources/category/java/design.xml/ExcessiveMethodLength" noCache="true" cacheLocation="/home/user/.pmd/cache">
<formatter type="text" toConsole="true"/>
<pmd rulesetfiles="src/main/resources/category/java/design.xml/ExcessiveMethodLength" noCache="true" cacheLocation="${tmpfile}">
<formatter type="xml" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="*.java"/>
</fileset>

View File

@@ -7,13 +7,17 @@ package net.sourceforge.pmd.ant;
import static java.io.File.separator;
import java.io.File;
import java.nio.file.Path;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.tools.ant.BuildFileRule;
import org.apache.tools.ant.Project;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
/**
* Quite an ugly classe, arguably useful for just 2 units test - nevertheless as
* there is a workaround that must be shared by both tests (PMD and CPD's) I
@@ -46,6 +50,11 @@ public abstract class AbstractAntTestHelper {
// initialize Ant
buildRule.configureProject(pathToTestScript + separator + antTestScriptFilename);
// Each test case gets one temp file name, accessible with ${tmpfile}
// The file doesn't exist before the test is executed
Path tmpFilePath = FileUtils.getTempDirectory().toPath().resolve(RandomStringUtils.randomAlphabetic(30)).toAbsolutePath();
buildRule.getProject().setProperty("tmpfile", tmpFilePath.toString());
Project project = buildRule.getProject();
if (!project.getBaseDir().toString().endsWith(mvnWorkaround)) {
// when running from maven, the path needs to be adapted...
@@ -66,7 +75,18 @@ public abstract class AbstractAntTestHelper {
}
public void assertOutputContaining(String text) {
Assert.assertTrue("Expected to find \"" + text + "\" in the output, but it's missing",
buildRule.getOutput().contains(text));
assertContains(buildRule.getOutput(), text);
}
public void assertContains(String text, String toFind) {
Assert.assertTrue("Expected to find \"" + toFind + "\", but it's missing",
text.contains(toFind));
}
public void assertDoesntContain(String text, String toFind) {
Assert.assertTrue("Expected no occurrence of \"" + toFind + "\", but found at least one",
!text.contains(toFind));
}
}