Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize I/O in test #161

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,22 @@
*/
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
*
*/
public class CheckstyleReportTest extends AbstractCheckstyleTestCase {
public void testNoSource() throws Exception {
File generatedReport = generateReport(getGoal(), "no-source-plugin-config.xml");
String absolutePath = generatedReport.getAbsolutePath();
assertFalse(absolutePath + " exists", FileUtils.fileExists(absolutePath));
assertTrue(new File(generatedReport.getAbsolutePath()).exists());
}

public void testMinConfiguration() throws Exception {
Expand Down Expand Up @@ -70,7 +66,7 @@ public void testFailOnError() {

fail("Must throw exception on errors");
} catch (Exception e) {
// expected
assertNotNull(e.getMessage());
}
}

Expand All @@ -92,26 +88,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 @@ -125,7 +101,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 @@ -143,7 +119,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
Loading