diff --git a/src/main/java/io/gatling/mojo/GatlingMojo.java b/src/main/java/io/gatling/mojo/GatlingMojo.java index 846e6ca..9ac350c 100644 --- a/src/main/java/io/gatling/mojo/GatlingMojo.java +++ b/src/main/java/io/gatling/mojo/GatlingMojo.java @@ -19,8 +19,6 @@ import static io.gatling.mojo.MojoConstants.*; import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES; import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; -import static java.util.Arrays.stream; -import static org.codehaus.plexus.util.StringUtils.isBlank; import io.gatling.plugin.GatlingConstants; import io.gatling.plugin.SimulationSelector; @@ -120,8 +118,6 @@ public final class GatlingMojo extends AbstractGatlingExecutionMojo { @Parameter(property = "gatling.workingDirectory") private File workingDirectory; - private Set existingRunDirectories; - @Parameter(defaultValue = "${project}", readonly = true) private MavenProject project; @@ -140,7 +136,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { throw new MojoExecutionException( "Could not create resultsFolder " + resultsFolder.getAbsolutePath()); } - existingRunDirectories = runDirectories(); + Set existingRunDirectories = runDirectories(); Exception ex = null; try { @@ -176,7 +172,12 @@ public void execute() throws MojoExecutionException, MojoFailureException { } ex = e instanceof GatlingSimulationAssertionsFailedException ? null : e; } finally { - recordSimulationResults(ex); + try { + saveSimulationResultToFile(existingRunDirectories, ex); + copyJUnitReports(); + } catch (IOException e) { + throw new MojoExecutionException("Could not record simulation results.", e); + } } } @@ -265,50 +266,36 @@ private void executeGatling( } } - private void recordSimulationResults(Exception exception) throws MojoExecutionException { - try { - saveSimulationResultToFile(exception); - copyJUnitReports(); - } catch (IOException e) { - throw new MojoExecutionException("Could not record simulation results.", e); - } - } - - private void saveSimulationResultToFile(Exception exception) throws IOException { + private void saveSimulationResultToFile(Set existingRunDirectories, Exception exception) + throws IOException { Path resultsFile = resultsFolder.toPath().resolve(LAST_RUN_FILE); try (BufferedWriter writer = Files.newBufferedWriter(resultsFile)) { - saveListOfNewRunDirectories(writer); - writeExceptionIfExists(writer, exception); - } - } - - private void saveListOfNewRunDirectories(BufferedWriter writer) throws IOException { - for (File directory : runDirectories()) { - if (!existingRunDirectories.contains(directory)) { - writer.write(directory.getName() + System.lineSeparator()); + for (File directory : runDirectories()) { + if (!existingRunDirectories.contains(directory)) { + writer.write(directory.getName() + System.lineSeparator()); + } + } + if (exception != null) { + writer.write( + LAST_RUN_FILE_ERROR_LINE + getRecursiveCauses(exception) + System.lineSeparator()); } } } - private void writeExceptionIfExists(BufferedWriter writer, Exception exception) - throws IOException { - if (exception != null) { - writer.write( - LAST_RUN_FILE_ERROR_LINE + getRecursiveCauses(exception) + System.lineSeparator()); - } - } - - private String getRecursiveCauses(Throwable e) { - return stream(ExceptionUtils.getThrowables(e)) - .map(ex -> joinNullable(ex.getClass().getName(), ex.getMessage())) + private static String getRecursiveCauses(Throwable e) { + return Arrays.stream(ExceptionUtils.getThrowables(e)) + .map( + ex -> { + String exceptionClassName = ex.getClass().getName(); + String exceptionMessage = ex.getMessage(); + return exceptionMessage != null + ? exceptionClassName + ": " + exceptionMessage + : exceptionClassName; + }) .collect(Collectors.joining(" | ")); } - private String joinNullable(String s, String sNullable) { - return isBlank(sNullable) ? s : s + ": " + sNullable; - } - private void copyJUnitReports() throws MojoExecutionException { try {