Skip to content

Commit

Permalink
Merge pull request #326 from YamStranger/generating_report_in_differe…
Browse files Browse the repository at this point in the history
…nt_runtime_fix

Updated report generation to use temp files.
  • Loading branch information
YamStranger committed Feb 29, 2016
2 parents e3fabf1 + 80d82d0 commit 68664b8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

public class JSONTestOutcomeReporter implements AcceptanceTestReporter, AcceptanceTestLoader {

Expand Down Expand Up @@ -44,9 +47,19 @@ public File generateReportFor(TestOutcome testOutcome,
TestOutcome storedTestOutcome = testOutcome.withQualifier(qualifier);
Preconditions.checkNotNull(outputDirectory);
String reportFilename = reportFor(storedTestOutcome);
String unique = UUID.randomUUID().toString();
File temporary = new File(getOutputDirectory(), reportFilename.concat(unique));
File report = new File(getOutputDirectory(), reportFilename);
try(OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(report))){
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, StandardCopyOption.ATOMIC_MOVE
);
}
return report;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class JUnitXMLOutcomeReporter {

Expand Down Expand Up @@ -44,10 +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));
String unique = UUID.randomUUID().toString();
File temporary = new File(getOutputDirectory(), reportFilename.concat(unique));
File report = new File(getOutputDirectory(), reportFilename);
LOGGER.debug("GENERATING JUNIT REPORT " + reportFilename);
try(OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(report))){
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, StandardCopyOption.ATOMIC_MOVE
);
} catch (ParserConfigurationException e) {
throw new IOException(e);
} catch (TransformerException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

import static net.thucydides.core.model.ReportType.XML;

Expand Down Expand Up @@ -69,14 +72,21 @@ public File generateReportFor(final TestOutcome testOutcome, final TestOutcomes

String reportFilename = reportFor(storedTestOutcome);

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 {}", testOutcome.getTitle(), report.getAbsolutePath());
LOGGER.debug("Generating XML report for {} to file {} (using temp file {})", testOutcome.getTitle(), report.getAbsolutePath(), temporary.getAbsolutePath());

try(
OutputStream outputStream = new FileOutputStream(report);
OutputStream outputStream = new FileOutputStream(temporary);
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
xstream.toXML(storedTestOutcome, writer);
writer.flush();
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 68664b8

Please sign in to comment.