From e418ac4c41d81dee6f39d55cbbf0cfa6e5d63e8d Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Thu, 21 Nov 2024 08:19:05 -0500 Subject: [PATCH] Modernize I/O in test --- .../checkstyle/CheckstyleReportTest.java | 32 +++---------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/src/test/java/org/apache/maven/plugins/checkstyle/CheckstyleReportTest.java b/src/test/java/org/apache/maven/plugins/checkstyle/CheckstyleReportTest.java index 038839b1..5c8c336b 100644 --- a/src/test/java/org/apache/maven/plugins/checkstyle/CheckstyleReportTest.java +++ b/src/test/java/org/apache/maven/plugins/checkstyle/CheckstyleReportTest.java @@ -18,16 +18,14 @@ */ package org.apache.maven.plugins.checkstyle; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; -import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.ResourceBundle; import org.apache.maven.artifact.DependencyResolutionRequiredException; import org.apache.maven.doxia.tools.SiteTool; import org.apache.maven.plugin.descriptor.PluginDescriptor; -import org.codehaus.plexus.util.FileUtils; /** * @author Edwin Punzalan @@ -36,7 +34,7 @@ public class CheckstyleReportTest extends AbstractCheckstyleTestCase { public void testNoSource() throws Exception { File generatedReport = generateReport(getGoal(), "no-source-plugin-config.xml"); - assertFalse(FileUtils.fileExists(generatedReport.getAbsolutePath())); + assertFalse(new File(generatedReport.getAbsolutePath()).exists()); } public void testMinConfiguration() throws Exception { @@ -91,26 +89,6 @@ public void testTestSourceDirectory() throws Exception { generateReport("test-source-directory-plugin-config.xml"); } - /** - * 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(); - } - private void generateReport(String pluginXml) throws Exception { File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/" + pluginXml); ResourceBundle bundle = @@ -124,7 +102,7 @@ private void generateReport(String pluginXml) throws Exception { setVariableValueToObject(mojo, "plugin", descriptorStub); File generatedReport = generateReport(mojo, pluginXmlFile); - assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath())); + assertTrue(new File(generatedReport.getAbsolutePath()).exists()); File outputFile = (File) getVariableValueFromObject(mojo, "outputFile"); assertNotNull("Test output file", outputFile); @@ -142,7 +120,7 @@ private void generateReport(String pluginXml) throws Exception { assertTrue("Test useFile exists", useFile.exists()); } - String str = readFile(generatedReport); + String str = new String(Files.readAllBytes(generatedReport.toPath()), StandardCharsets.UTF_8); boolean searchHeaderFound = str.contains(getHtmlHeader(bundle.getString("report.checkstyle.rules"))); Boolean rules = (Boolean) getVariableValueFromObject(mojo, "enableRulesSummary");