Merge branch 'unit-test-for-pr-170'

This commit is contained in:
Andreas Dangel
2017-01-21 13:53:57 +01:00
7 changed files with 1157 additions and 1 deletions

View File

@ -138,7 +138,6 @@
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -154,5 +154,10 @@
<artifactId>slf4j-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -4,7 +4,18 @@
package net.sourceforge.pmd.ant;
import java.io.File;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Objects;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.rules.ExternalResource;
import org.junit.rules.TestRule;
public class PMDTaskTest extends AbstractAntTestHelper {
@ -79,4 +90,67 @@ public class PMDTaskTest extends AbstractAntTestHelper {
public void testClasspath() {
executeTarget("testClasspath");
}
@Rule
public final TestRule restoreSystemProperties = new RestoreSystemProperties();
@Rule
public final TestRule restoreLocale = new ExternalResource() {
private Locale originalLocale;
@Override
protected void before() throws Throwable {
originalLocale = Locale.getDefault();
}
@Override
protected void after() {
Locale.setDefault(originalLocale);
}
};
// See http://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding and http://stackoverflow.com/a/14987992/1169968
private static void setDefaultCharset(String charsetName) {
try {
System.setProperty("file.encoding", charsetName);
Field charset = Charset.class.getDeclaredField("defaultCharset");
charset.setAccessible(true);
charset.set(null,null);
Objects.requireNonNull(Charset.defaultCharset());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Rule
public final TestRule restoreDefaultCharset = new ExternalResource() {
private Charset defaultCharset;
@Override
protected void before() throws Throwable {
defaultCharset = Charset.defaultCharset();
}
@Override
protected void after() {
setDefaultCharset(defaultCharset.name());
}
};
@Test
public void testFormatterEncodingWithXML() throws Exception {
Locale.setDefault(Locale.FRENCH);
setDefaultCharset("cp1252");
executeTarget("testFormatterEncodingWithXML");
String report = FileUtils.readFileToString(new File("target/testFormatterEncodingWithXML-pmd.xml"), "UTF-8");
assertTrue(report.contains("unusedVariableWithÜmlaut"));
}
@Test
public void testFormatterEncodingWithXMLConsole() throws Exception {
setDefaultCharset("cp1252");
executeTarget("testFormatterEncodingWithXMLConsole");
String report = getOutput();
assertTrue(report.startsWith("<?xml version=\"1.0\" encoding=\"windows-1252\"?>"));
assertTrue(report.contains("unusedVariableWith&#xdc;mlaut"));
}
}

View File

@ -0,0 +1,9 @@
public class EncodingTestClass {
public static void main(String[] args) {
// UnusedLocalVariable: the rule violation message includes the variable name
// so, the encoding matters
// NOTE: This file is stored with encoding windows-1252 or cp1252
// The Umlaut &Uuml; has codepoint U+00DC, which is the same in cp1252 and iso-8859-1
String unusedVariableWithÜmlaut = "unused";
}
}

View File

@ -107,4 +107,41 @@
</classpath>
</pmd>
</target>
<target name="testFormatterEncodingWithXML">
<!-- source encoding is cp1252 -->
<pmd encoding="cp1252">
<ruleset>java-strings</ruleset>
<ruleset>java-unusedcode</ruleset>
<!--
<formatter type="xml" toConsole="true"/>
-->
<formatter type="xml" toFile="${pmd.home}/target/testFormatterEncodingWithXML-pmd.xml">
<param name="encoding" value="UTF-8" /> <!-- report encoding -->
</formatter>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="EncodingTestClass.java" />
<include name="MoreThanThousandLinesOfCodeWithDuplicateLiterals.java" />
</fileset>
</pmd>
</target>
<target name="testFormatterEncodingWithXMLConsole">
<!-- source encoding is cp1252 -->
<pmd encoding="cp1252">
<ruleset>java-unusedcode</ruleset>
<formatter type="xml" toConsole="true">
<!-- specifying a encoding here will override the console encoding.
The output might have junk characters in it.
<param name="encoding" value="UTF-8" />
The test doesn't specify the report encoding, so that the console/default encoding is used
-->
</formatter>
<fileset dir="${pmd.home}/src/test/resources/ant/java">
<include name="EncodingTestClass.java" />
</fileset>
</pmd>
</target>
</project>

View File

@ -871,6 +871,11 @@
<artifactId>groovy</artifactId>
<version>2.4.7</version>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.8.0</version>
</dependency>
</dependencies>
</dependencyManagement>