Skip to content

Commit

Permalink
JBEHAVE-848: Report files not closed
Browse files Browse the repository at this point in the history
added close() calls to afterStory in PrintStreamOutput and to a few Writers that were not closed.
And changed a few calls that where constructing streams as a parameter to IOUtils.toString.
  • Loading branch information
alexlehm committed Nov 16, 2012
1 parent d52fd4d commit 31000d9
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ public State runBeforeOrAfterStories(Configuration configuration, List<Candidate
}
reporter.get().afterStory(false);
storiesState.set(context.state());
// if we are running with multiple threads, call delayed
// methods, otherwise we will forget to close files on BeforeStories
if (stage == Stage.BEFORE) {
if (reporter.get() instanceof ConcurrentStoryReporter) {
((ConcurrentStoryReporter) reporter.get()).invokeDelayed();
}
}
// handle any after stories failure according to strategy
if (stage == Stage.AFTER) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public LoadFromClasspath(ClassLoader classLoader) {
public String loadResourceAsText(String resourcePath) {
InputStream stream = resourceAsStream(resourcePath);
try {
return IOUtils.toString(stream);
final String result = IOUtils.toString(stream);
stream.close();
return result;
} catch (IOException e) {
throw new InvalidStoryResource(resourcePath, stream, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public String loadStoryAsText(String storyPath) {

protected String loadContent(String path) {
try {
return IOUtils.toString(new FileInputStream(new File(path)));
final FileInputStream input = new FileInputStream(new File(path));
final String result = IOUtils.toString(input);
input.close();
return result;
} catch (Exception e) {
throw new InvalidStoryResource(path, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public class LoadFromURL implements ResourceLoader, StoryLoader {

public String loadResourceAsText(String resourcePath) {
try {
return IOUtils.toString(resourceAsStream(resourcePath));
final InputStream stream = resourceAsStream(resourcePath);
final String result = IOUtils.toString(stream);
stream.close();
return result;
} catch (Exception cause) {
throw new InvalidStoryResource(resourcePath, cause);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ private void writeData() {
}
try {
p.store(output, this.getClass().getName());
output.close();
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ private void print(Meta meta) {

public void afterStory(boolean givenStory) {
print(format("afterStory", "\n"));
// take care not to close System.out
// which is used for ConsoleOutput
if(!givenStory && output!=System.out) {
output.close();
}
}

public void givenStories(GivenStories givenStories) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private File write(File file, String resource, Map<String, Object> dataModel) {
file.getParentFile().mkdirs();
Writer writer = new FileWriter(file);
processor.process(resource, dataModel, writer);
writer.close();
return file;
} catch (Exception e) {
throw new RuntimeException(resource, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ List<Report> createReports(Map<String, List<File>> reportFiles) {
String format = FilenameUtils.getExtension(fileName);
Map<String, Object> dataModel = newDataModel();
dataModel.put("name", name);
dataModel.put("body", IOUtils.toString(new FileReader(file)));
FileReader fr=new FileReader(file);
String body=IOUtils.toString(fr);
fr.close();
dataModel.put("body", body);
// dataModel.put("body", IOUtils.toString(new FileReader(file)));
dataModel.put("format", format);
File outputDirectory = file.getParentFile();
String outputName = viewDirectory + "/" + fileName;
Expand Down Expand Up @@ -235,6 +239,7 @@ private File write(File outputDirectory, String outputName, String resource, Map
file.getParentFile().mkdirs();
Writer writer = new FileWriter(file);
processor.process(resource, dataModel, writer);
writer.close();
return file;
} catch (Exception e) {
throw new ViewGenerationFailedForTemplate(resource, e);
Expand Down Expand Up @@ -409,7 +414,10 @@ public String formatMillis(long millis) {
long minutes = (millis % hour) / minute;
long seconds = ((millis % hour) % minute) / second;
long milliseconds = ((millis % hour) % minute % second);
return new Formatter().format("%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds).toString();
Formatter formatter=new Formatter();
String result=formatter.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds).toString();
formatter.close();
return result;
}

}
Expand Down

0 comments on commit 31000d9

Please sign in to comment.