Skip to content

Commit

Permalink
feat: updated report generation to use atomic operations
Browse files Browse the repository at this point in the history
  • Loading branch information
YamStranger committed Feb 29, 2016
1 parent fa25694 commit 1314d42
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Expand Up @@ -50,12 +50,16 @@ public File generateReportFor(TestOutcome testOutcome,
String unique = UUID.randomUUID().toString();
File temporary = new File(getOutputDirectory(), reportFilename.concat(unique));
File report = new File(getOutputDirectory(), reportFilename);
report.createNewFile();

LOGGER.debug("Generating JSON report for {} to file {} (using temp file {})", testOutcome.getTitle(), report.getAbsolutePath(), temporary.getAbsolutePath());

try(OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(temporary))){
jsonConverter.toJson(storedTestOutcome, outputStream);
outputStream.flush();
Files.move(temporary.toPath(),report.toPath(), StandardCopyOption.REPLACE_EXISTING);
Files.move(temporary.toPath(), report.toPath(),
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE
);
}
return temporary;
}
Expand Down
Expand Up @@ -47,15 +47,18 @@ public void generateReportsFor(TestOutcomes testOutcomes) throws IOException {
for(String testCase : testOutcomesGroupedByTestCase.keySet()) {
List<TestOutcome> testCaseOutcomes = testOutcomesGroupedByTestCase.get(testCase);
String reportFilename = reportFilenameFor(testCaseOutcomes.get(0));
File report = new File(getOutputDirectory(), reportFilename);
String unique = UUID.randomUUID().toString();
File temporary = new File(getOutputDirectory(), reportFilename.concat(unique));
LOGGER.debug("GENERATING JUNIT REPORT {} using temporary file {}", reportFilename, temporary);
File report = new File(getOutputDirectory(), reportFilename);
report.createNewFile();

LOGGER.debug("GENERATING JUNIT REPORT {} using temporary file {}", reportFilename, temporary);
try(OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(temporary))){
junitXMLConverter.write(testCase, testCaseOutcomes, outputStream);
outputStream.flush();
Files.move(temporary.toPath(), report.toPath(), StandardCopyOption.REPLACE_EXISTING);
Files.move(temporary.toPath(), report.toPath(),
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE
);
} catch (ParserConfigurationException e) {
throw new IOException(e);
} catch (TransformerException e) {
Expand Down
Expand Up @@ -75,6 +75,7 @@ public File generateReportFor(final TestOutcome testOutcome, final TestOutcomes
String unique = UUID.randomUUID().toString();
File temporary = new File(getOutputDirectory(), reportFilename.concat(unique));
File report = new File(getOutputDirectory(), reportFilename);
report.createNewFile();

LOGGER.debug("Generating XML report for {} to file {} (using temp file {})", testOutcome.getTitle(), report.getAbsolutePath(), temporary.getAbsolutePath());

Expand All @@ -83,7 +84,9 @@ public File generateReportFor(final TestOutcome testOutcome, final TestOutcomes
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
xstream.toXML(storedTestOutcome, writer);
writer.flush();
Files.move(temporary.toPath(), report.toPath(), StandardCopyOption.REPLACE_EXISTING);
Files.move(temporary.toPath(), report.toPath(),
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE
);
LOGGER.debug("XML report generated ({} bytes) {}", report.getAbsolutePath(), report.length());
}
return report;
Expand Down

0 comments on commit 1314d42

Please sign in to comment.