Merge branch 'pr-31' into pmd/5.3.x

This commit is contained in:
Andreas Dangel
2016-01-21 20:23:22 +01:00
5 changed files with 56 additions and 2 deletions

View File

@ -15,7 +15,9 @@ import java.util.List;
import net.sourceforge.pmd.PMD;
import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.BOMInputStream;
public class SourceCode {
@ -68,7 +70,18 @@ public class SourceCode {
@Override
public Reader getReader() throws Exception {
return new InputStreamReader(new FileInputStream(file), encoding);
BOMInputStream inputStream =
new BOMInputStream(new FileInputStream(file),
ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE);
if (inputStream.hasBOM()) {
encoding = inputStream.getBOMCharsetName();
}
return new InputStreamReader(inputStream, encoding);
}
public String getEncoding() {
return encoding;
}
@Override

View File

@ -5,14 +5,18 @@ package net.sourceforge.pmd.cpd;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.util.ArrayList;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.cpd.SourceCode.FileCodeLoader;
import org.junit.Test;
public class SourceCodeTest {
private static final String BASE_RESOURCE_PATH =
"src/test/resources/net/sourceforge/pmd/cpd/files/";
private static final String SAMPLE_CODE =
"Line 1\n" +
"Line 2\n" +
@ -36,4 +40,24 @@ public class SourceCodeTest {
assertEquals("Line 2", sourceCode.getSlice(2, 2));
assertEquals("Line 1" + PMD.EOL + "Line 2", sourceCode.getSlice(1, 2));
}
@Test
public void testEncodingDetectionFromBOM() throws Exception {
FileCodeLoader loader =
new SourceCode.FileCodeLoader(new File(BASE_RESOURCE_PATH + "file_with_utf8_bom.java"), "ISO-8859-1");
//The encoding detection is done when the reader is created
loader.getReader();
assertEquals("UTF-8", loader.getEncoding());
}
@Test
public void testEncodingIsNotChangedWhenThereIsNoBOM() throws Exception {
FileCodeLoader loader =
new SourceCode.FileCodeLoader(new File(BASE_RESOURCE_PATH + "file_with_ISO-8859-1_encoding.java"), "ISO-8859-1");
//The encoding detection is done when the reader is created
loader.getReader();
assertEquals("ISO-8859-1", loader.getEncoding());
}
}

View File

@ -0,0 +1,8 @@
/**
* This file is using ISO-8859-1 (Latin-1) encoding.
*
* ä
*/
public class FileWith_ISO8859-1_Encoding {
}

View File

@ -0,0 +1,8 @@
/**
* This file is using UTF-8 with BOM encoding.
*
* ä
*/
public class FileWith_UTF-8-BOM_Encoding {
}

View File

@ -18,6 +18,7 @@
* [#27](https://github.com/adangel/pmd/pull/27): Added support for Raw String Literals (C++11).
* [#29](https://github.com/adangel/pmd/pull/29): Added support for files with UTF-8 BOM to JSP tokenizer.
* [#30](https://github.com/adangel/pmd/pull/30): Removed file filter for files that are explicitly specified on the CPD command line using the '--files' command line option.
* [#31](https://github.com/adangel/pmd/pull/31): Added file encoding detection to CPD.
* [#79](https://github.com/pmd/pmd/pull/79): do not flag public static void main(String[]) as UseVarargs; ignore @Override for UseVarargs
* [#80](https://github.com/pmd/pmd/pull/80): Update mvn-plugin.md
* [#83](https://github.com/pmd/pmd/pull/83): Adds new Code Climate-compliant JSON renderer