Skip to content

Commit

Permalink
[MPMD-370] Remove remaining uses of FileReader
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Oct 20, 2023
1 parent 957ec89 commit 33f42e7
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 70 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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);
}
Expand Down
Expand Up @@ -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()));
}

/**
Expand Down
23 changes: 0 additions & 23 deletions src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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");

Expand Down
22 changes: 0 additions & 22 deletions src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
Expand Up @@ -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;
Expand Down Expand Up @@ -389,26 +387,6 @@ public void testIncludeXmlInSite() throws Exception {
assertTrue(pmdXml.contains("</pmd>"));
}

/**
* 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
*
Expand Down
Expand Up @@ -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;

Expand All @@ -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());
Expand Down
Expand Up @@ -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;

Expand All @@ -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());
Expand Down
Expand Up @@ -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;

Expand All @@ -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());
Expand Down

0 comments on commit 33f42e7

Please sign in to comment.