Skip to content

Commit

Permalink
Issue #5752: attempt to minimize usage of extra close for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
romani committed Apr 30, 2018
1 parent 80365b8 commit 2b0ac1b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
54 changes: 33 additions & 21 deletions src/main/java/com/puppycrawl/tools/checkstyle/Main.java
Expand Up @@ -537,7 +537,7 @@ cliOptions.configLocation, new PropertiesExpander(props),
ignoredModulesOptions, multiThreadModeSettings);

// create a listener for output
final AuditListener listener = createListener(cliOptions.format, cliOptions.outputLocation);
final AuditListener listener = createAuditListener(cliOptions.format, cliOptions.outputLocation);

// create RootModule object and run it
final int errorCounter;
Expand Down Expand Up @@ -609,34 +609,24 @@ private static Properties loadProperties(File file)
* @return a fresh new {@code AuditListener}
* @exception IOException when provided output location is not found
*/
private static AuditListener createListener(String format,
String outputLocation)
private static AuditListener createAuditListener(String format,
String outputLocation)
throws IOException {
// setup the output stream
final OutputStream out;
final AutomaticBean.OutputStreamOptions closeOutputStream;
if (outputLocation == null) {
out = System.out;
closeOutputStream = AutomaticBean.OutputStreamOptions.NONE;
}
else {
out = Files.newOutputStream(Paths.get(outputLocation));
closeOutputStream = AutomaticBean.OutputStreamOptions.CLOSE;
}

// setup a listener
final AuditListener listener;
if (XML_FORMAT_NAME.equals(format)) {
listener = new XMLLogger(out, closeOutputStream);
final OutputStream out = getOutputStream(format);
final AutomaticBean.OutputStreamOptions closeOutputStreamOption =
getOutputStreamOptions(outputLocation);
listener = new XMLLogger(out, closeOutputStreamOption);
}
else if (PLAIN_FORMAT_NAME.equals(format)) {
listener = new DefaultLogger(out, closeOutputStream, out,
AutomaticBean.OutputStreamOptions.NONE);
final OutputStream out = getOutputStream(format);
final AutomaticBean.OutputStreamOptions closeOutputStreamOption =
getOutputStreamOptions(outputLocation);
listener = new DefaultLogger(out, closeOutputStreamOption);
}
else {
if (closeOutputStream == AutomaticBean.OutputStreamOptions.CLOSE) {
CommonUtils.close(out);
}
final LocalizedMessage outputFormatExceptionMessage = new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, CREATE_LISTENER_EXCEPTION,
new String[] {format, PLAIN_FORMAT_NAME, XML_FORMAT_NAME}, null,
Expand All @@ -647,6 +637,28 @@ else if (PLAIN_FORMAT_NAME.equals(format)) {
return listener;
}

private static OutputStream getOutputStream(String outputLocation) throws IOException {
final OutputStream result;
if (outputLocation == null) {
result = System.out;
}
else {
result = Files.newOutputStream(Paths.get(outputLocation));
}
return result;
}

private static AutomaticBean.OutputStreamOptions getOutputStreamOptions(String outputLocation) throws IOException {
final AutomaticBean.OutputStreamOptions result;
if (outputLocation == null) {
result = AutomaticBean.OutputStreamOptions.NONE;
}
else {
result = AutomaticBean.OutputStreamOptions.CLOSE;
}
return result;
}

/**
* Determines the files to process.
* @param patternsToExclude The list of directory patterns to exclude from searching.
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java
Expand Up @@ -52,6 +52,7 @@

import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
Expand Down Expand Up @@ -536,6 +537,10 @@ public void testCreateListenerIllegalStateException() throws Exception {
}
}

/**
* We use access to private method here to test stream closure
* @throws Exception
*/
@Test
public void testCreateListenerWithLocationIllegalStateException() throws Exception {
mockStatic(CommonUtils.class);
Expand Down

0 comments on commit 2b0ac1b

Please sign in to comment.