Skip to content

Commit

Permalink
chore: clean up verify
Browse files Browse the repository at this point in the history
Motivation:

* ugly transient attribute
* too many small methods
  • Loading branch information
slandelle committed Mar 15, 2024
1 parent 805161a commit 4f2486e
Showing 1 changed file with 27 additions and 40 deletions.
67 changes: 27 additions & 40 deletions src/main/java/io/gatling/mojo/GatlingMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -120,8 +118,6 @@ public final class GatlingMojo extends AbstractGatlingExecutionMojo {
@Parameter(property = "gatling.workingDirectory")
private File workingDirectory;

private Set<File> existingRunDirectories;

@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject project;

Expand All @@ -140,7 +136,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
throw new MojoExecutionException(
"Could not create resultsFolder " + resultsFolder.getAbsolutePath());
}
existingRunDirectories = runDirectories();
Set<File> existingRunDirectories = runDirectories();
Exception ex = null;

try {
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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<File> 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 {
Expand Down

0 comments on commit 4f2486e

Please sign in to comment.