Skip to content

Commit

Permalink
Modernize I/O in test
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Nov 21, 2024
1 parent 09d871a commit e418ac4
Showing 1 changed file with 5 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 =
Expand All @@ -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);
Expand All @@ -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");
Expand Down

0 comments on commit e418ac4

Please sign in to comment.