Skip to content

Commit

Permalink
fix: updated report generation to use temp files, it fixes bugs with …
Browse files Browse the repository at this point in the history
…running tests with multiple workers (and different Java Runtime as well)
  • Loading branch information
YamStranger committed Feb 29, 2016
1 parent e3fabf1 commit 5afea72
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
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,11 +47,17 @@ 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))){
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);
}
return report;
return temporary;
}

public File getOutputDirectory() {
Expand Down
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 @@ -45,9 +48,14 @@ public void generateReportsFor(TestOutcomes testOutcomes) throws IOException {
List<TestOutcome> testCaseOutcomes = testOutcomesGroupedByTestCase.get(testCase);
String reportFilename = reportFilenameFor(testCaseOutcomes.get(0));
File report = new File(getOutputDirectory(), reportFilename);
LOGGER.debug("GENERATING JUNIT REPORT " + reportFilename);
String unique = UUID.randomUUID().toString();
File temporary = new File(getOutputDirectory(), reportFilename.concat(unique));
LOGGER.debug("GENERATING JUNIT REPORT {} using temporary file {}", reportFilename, temporary);

try(OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(report))){
junitXMLConverter.write(testCase, testCaseOutcomes, outputStream);
outputStream.flush();
Files.move(temporary.toPath(), report.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (ParserConfigurationException e) {
throw new IOException(e);
} catch (TransformerException e) {
Expand Down
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,18 @@ 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);

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);
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
xstream.toXML(storedTestOutcome, writer);
writer.flush();
Files.move(temporary.toPath(), report.toPath(), StandardCopyOption.REPLACE_EXISTING);
LOGGER.debug("XML report generated ({} bytes) {}", report.getAbsolutePath(), report.length());
}
return report;
Expand Down

0 comments on commit 5afea72

Please sign in to comment.