verified #1405 UnusedPrivateMethod false positive?

This commit is contained in:
Andreas Dangel
2015-09-16 21:37:17 +02:00
parent 2500412b14
commit 64012cc0b4
4 changed files with 51 additions and 5 deletions

View File

@ -1484,6 +1484,23 @@ public class Something {
private static <K, V> Map<K, V> mapOf2(final Function<V, K> keyMapper, final V... values) {
return Stream.of(values).collect(Collectors.toMap(keyMapper, Function.identity()));
}
}
]]></code>
</test-code>
<test-code useAuxClasspath="false">
<description>#1405 UnusedPrivateMethod false positive?</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class blabla implements blabla2 {
@Override
public List<String> getProductImageUrls(final ApparelStyleVariantProductModel product, final String format) {
return getImageUrlsListForVariant(product, format);
}
private List<String> getImageUrlsListForVariant(final VariantProductModel variant, final String format) {
final SortedMap<Integer, String> imageUrls = getImageUrlsMapForVariant(variant, format);
return new ArrayList<String>(imageUrls.values());
}
}
]]></code>
</test-code>

View File

@ -94,7 +94,7 @@ public abstract class RuleTst {
}
}
report = processUsingStringReader(test.getCode(), rule, test.getLanguageVersion());
report = processUsingStringReader(test, rule);
res = report.size();
} catch (Throwable t) {
t.printStackTrace();
@ -190,10 +190,9 @@ public abstract class RuleTst {
System.out.println("--------------------------------------------------------------");
}
private Report processUsingStringReader(String code, Rule rule,
LanguageVersion languageVersion) throws PMDException {
private Report processUsingStringReader(TestDescriptor test, Rule rule) throws PMDException {
Report report = new Report();
runTestFromString(code, rule, report, languageVersion);
runTestFromString(test, rule, report);
return report;
}
@ -201,10 +200,16 @@ public abstract class RuleTst {
* Run the rule on the given code and put the violations in the report.
*/
public void runTestFromString(String code, Rule rule, Report report, LanguageVersion languageVersion) {
runTestFromString(code, rule, report, languageVersion, true);
}
public void runTestFromString(String code, Rule rule, Report report, LanguageVersion languageVersion, boolean isUseAuxClasspath) {
try {
PMD p = new PMD();
p.getConfiguration().setDefaultLanguageVersion(languageVersion);
p.getConfiguration().prependClasspath("."); // configure the "auxclasspath" option for unit testing
if (isUseAuxClasspath) {
p.getConfiguration().prependClasspath("."); // configure the "auxclasspath" option for unit testing
}
RuleContext ctx = new RuleContext();
ctx.setReport(report);
ctx.setSourceCodeFilename("n/a");
@ -218,6 +223,10 @@ public abstract class RuleTst {
}
}
public void runTestFromString(TestDescriptor test, Rule rule, Report report) {
runTestFromString(test.getCode(), rule, report, test.getLanguageVersion(), test.isUseAuxClasspath());
}
/**
* getResourceAsStream tries to find the XML file in weird locations if the
* ruleName includes the package, so we strip it here.
@ -307,6 +316,15 @@ public abstract class RuleTst {
}
}
boolean isUseAuxClasspath = true;
Node useAuxClasspathAttribute = testCode.getAttributes().getNamedItem("useAuxClasspath");
if (useAuxClasspathAttribute != null) {
String useAuxClasspathValue = useAuxClasspathAttribute.getNodeValue();
if ("false".equalsIgnoreCase(useAuxClasspathValue)) {
isUseAuxClasspath = false;
}
}
NodeList ruleProperties = testCode.getElementsByTagName("rule-property");
Properties properties = new Properties();
for (int j = 0; j < ruleProperties.getLength(); j++) {
@ -373,6 +391,7 @@ public abstract class RuleTst {
}
tests[i].setReinitializeRule(reinitializeRule);
tests[i].setRegressionTest(isRegressionTest);
tests[i].setUseAuxClasspath(isUseAuxClasspath);
tests[i].setExpectedMessages(messages);
tests[i].setExpectedLineNumbers(expectedLineNumbers);
tests[i].setProperties(properties);

View File

@ -27,6 +27,7 @@ public class TestDescriptor {
private LanguageVersion languageVersion;
private boolean reinitializeRule = true; //default, avoids unintentional mixing of state between test cases
private boolean isRegressionTest = true;
private boolean useAuxClasspath = true;
private int numberInDocument = -1;
// Empty descriptor added to please mvn surefire plugin
@ -135,4 +136,12 @@ public class TestDescriptor {
public void setRegressionTest(boolean isRegressionTest) {
this.isRegressionTest = isRegressionTest;
}
public void setUseAuxClasspath(boolean useAuxClasspath) {
this.useAuxClasspath = useAuxClasspath;
}
public boolean isUseAuxClasspath() {
return useAuxClasspath;
}
}

View File

@ -28,5 +28,6 @@
* [#1401](https://sourceforge.net/p/pmd/bugs/1401/): False positive for StringBuilder.append called with constructor
* [#1402](https://sourceforge.net/p/pmd/bugs/1402/): Windows-Only: File exclusions are not case insensitive
* [#1403](https://sourceforge.net/p/pmd/bugs/1403/): False positive UnusedPrivateMethod with JAVA8
* [#1405](https://sourceforge.net/p/pmd/bugs/1405/): UnusedPrivateMethod false positive?
**API Changes:**