diff --git a/src/main/java/org/apache/maven/plugins/pmd/ExcludeDuplicationsFromFile.java b/src/main/java/org/apache/maven/plugins/pmd/ExcludeDuplicationsFromFile.java index fc2be596..ea199a8c 100644 --- a/src/main/java/org/apache/maven/plugins/pmd/ExcludeDuplicationsFromFile.java +++ b/src/main/java/org/apache/maven/plugins/pmd/ExcludeDuplicationsFromFile.java @@ -18,13 +18,15 @@ */ package org.apache.maven.plugins.pmd; -import java.io.FileReader; import java.io.IOException; -import java.io.LineNumberReader; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; import net.sourceforge.pmd.cpd.Mark; import net.sourceforge.pmd.cpd.Match; @@ -102,13 +104,10 @@ public void loadExcludeFromFailuresData(final String excludeFromFailureFile) thr return; } - try (LineNumberReader reader = new LineNumberReader(new FileReader(excludeFromFailureFile))) { - String line; - while ((line = reader.readLine()) != null) { - if (!line.startsWith("#")) { - exclusionList.add(createSetFromExclusionLine(line)); - } - } + try (Stream lines = Files.lines(Paths.get(excludeFromFailureFile))) { + exclusionList.addAll(lines.filter(line -> !line.startsWith("#")) + .map(line -> createSetFromExclusionLine(line)) + .collect(Collectors.toList())); } catch (final IOException e) { throw new MojoExecutionException("Cannot load file " + excludeFromFailureFile, e); } diff --git a/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java b/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java index fce2159b..18a22597 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java +++ b/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java @@ -134,8 +134,8 @@ protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws /** * Read the contents of the specified file object into a string */ - protected String readFile(File pmdTestDir, String fileName) throws IOException { - return new String(Files.readAllBytes(pmdTestDir.toPath().resolve(fileName))); + protected String readFile(File file) throws IOException { + return new String(Files.readAllBytes(file.toPath())); } /** diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java index 55748a5f..b7657bf1 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java @@ -21,10 +21,7 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; -import java.io.IOException; import java.util.Locale; import org.apache.commons.io.FileUtils; @@ -137,26 +134,6 @@ public void testInvalidFormat() throws Exception { } } - /** - * Read the contents of the specified file object into a string - * - * @param file the file to be read - * @return a String object that contains the contents of the file - * @throws java.io.IOException - */ - private String readFile(File file) throws IOException { - String strTmp; - StringBuilder str = new StringBuilder((int) file.length()); - try (BufferedReader in = new BufferedReader(new FileReader(file))) { - while ((strTmp = in.readLine()) != null) { - str.append(' '); - str.append(strTmp); - } - } - - return str.toString(); - } - public void testWriteNonHtml() throws Exception { generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml"); diff --git a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java index e3f4b3ec..b8ec9c33 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java @@ -18,9 +18,7 @@ */ package org.apache.maven.plugins.pmd; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.IOException; import java.net.ServerSocket; import java.net.URL; @@ -389,26 +387,6 @@ public void testIncludeXmlInSite() throws Exception { assertTrue(pmdXml.contains("")); } - /** - * Read the contents of the specified file object into a string - * - * @param file the file to be read - * @return a String object that contains the contents of the file - * @throws java.io.IOException - */ - private String readFile(File file) throws IOException { - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { - final StringBuilder str = new StringBuilder((int) file.length()); - - for (String line = reader.readLine(); line != null; line = reader.readLine()) { - str.append(' '); - str.append(line); - str.append('\n'); - } - return str.toString(); - } - } - /** * Verify the correct working of the locationTemp method * diff --git a/src/test/java/org/apache/maven/plugins/pmd/stubs/CustomConfigurationMavenProjectStub.java b/src/test/java/org/apache/maven/plugins/pmd/stubs/CustomConfigurationMavenProjectStub.java index 93609dc7..1d892b7f 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/stubs/CustomConfigurationMavenProjectStub.java +++ b/src/test/java/org/apache/maven/plugins/pmd/stubs/CustomConfigurationMavenProjectStub.java @@ -19,7 +19,8 @@ package org.apache.maven.plugins.pmd.stubs; import java.io.File; -import java.io.FileReader; +import java.io.FileInputStream; +import java.io.InputStream; import java.util.ArrayList; import java.util.List; @@ -43,12 +44,11 @@ public CustomConfigurationMavenProjectStub() { MavenXpp3Reader pomReader = new MavenXpp3Reader(); Model model = null; - try { - model = pomReader.read(new FileReader(new File(getBasedir() - + "/src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml"))); + try (InputStream is = new FileInputStream(new File(getBasedir() + + "/src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml"))) { + model = pomReader.read(is); setModel(model); } catch (Exception e) { - } setGroupId(model.getGroupId()); diff --git a/src/test/java/org/apache/maven/plugins/pmd/stubs/DefaultConfigurationMavenProjectStub.java b/src/test/java/org/apache/maven/plugins/pmd/stubs/DefaultConfigurationMavenProjectStub.java index b16da8d4..e02a9a8b 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/stubs/DefaultConfigurationMavenProjectStub.java +++ b/src/test/java/org/apache/maven/plugins/pmd/stubs/DefaultConfigurationMavenProjectStub.java @@ -19,7 +19,8 @@ package org.apache.maven.plugins.pmd.stubs; import java.io.File; -import java.io.FileReader; +import java.io.FileInputStream; +import java.io.InputStream; import java.util.ArrayList; import java.util.List; @@ -43,12 +44,11 @@ public DefaultConfigurationMavenProjectStub() { MavenXpp3Reader pomReader = new MavenXpp3Reader(); Model model = null; - try (FileReader reader = new FileReader(new File(getBasedir() + try (InputStream is = new FileInputStream(new File(getBasedir() + "/src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml"))) { - model = pomReader.read(reader); + model = pomReader.read(is); setModel(model); } catch (Exception e) { - } setGroupId(model.getGroupId()); diff --git a/src/test/java/org/apache/maven/plugins/pmd/stubs/InvalidFormatMavenProjectStub.java b/src/test/java/org/apache/maven/plugins/pmd/stubs/InvalidFormatMavenProjectStub.java index c08e19cb..6e0a86dd 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/stubs/InvalidFormatMavenProjectStub.java +++ b/src/test/java/org/apache/maven/plugins/pmd/stubs/InvalidFormatMavenProjectStub.java @@ -19,7 +19,8 @@ package org.apache.maven.plugins.pmd.stubs; import java.io.File; -import java.io.FileReader; +import java.io.FileInputStream; +import java.io.InputStream; import java.util.ArrayList; import java.util.List; @@ -40,12 +41,11 @@ public InvalidFormatMavenProjectStub() { MavenXpp3Reader pomReader = new MavenXpp3Reader(); Model model = null; - try { - model = pomReader.read(new FileReader(new File( - getBasedir() + "/src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml"))); + try (InputStream is = new FileInputStream( + new File(getBasedir() + "/src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml"))) { + model = pomReader.read(is); setModel(model); } catch (Exception e) { - } setGroupId(model.getGroupId());