Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
import org.apache.maven.eventspy.AbstractEventSpy;
import org.apache.maven.eventspy.EventSpy;
import org.apache.maven.execution.ExecutionEvent;
import org.apache.maven.execution.MavenExecutionResult;
import org.codehaus.plexus.PlexusContainer;
import org.eclipse.aether.transfer.ArtifactTransferException;
import org.junit.jupiter.api.io.TempDir;

public abstract class AbstractMavenTest {
private static final int MAVEN_BUILD_ATTEMPTS = 3;

@TempDir static Path WORKING_DIRECTORY;

protected AbstractMavenTest() {
Expand All @@ -43,15 +47,6 @@ protected void executeMaven(
PrintStream stderr,
String... additionalArgs)
throws Exception {
MojoStartedSpy spy = new MojoStartedSpy(mojoStartedHandler);
MavenCli mavenCli =
new MavenCli() {
@Override
protected void customizeContainer(PlexusContainer container) {
container.addComponent(spy, EventSpy.class, null);
}
};

File pomFile = new File(AbstractMavenTest.class.getResource(pomPath).toURI());

List<String> arguments = new ArrayList<>();
Expand All @@ -67,11 +62,24 @@ protected void customizeContainer(PlexusContainer container) {
}
addAll(arguments, additionalArgs);

mavenCli.doMain(
arguments.toArray(new String[0]),
WORKING_DIRECTORY.toAbsolutePath().toString(),
stdOut,
stderr);
MojoStartedSpy spy;
int attempt = 0;
do {
spy = new MojoStartedSpy(mojoStartedHandler);
MojoStartedSpy attemptSpy = spy;
MavenCli mavenCli =
new MavenCli() {
@Override
protected void customizeContainer(PlexusContainer container) {
container.addComponent(attemptSpy, EventSpy.class, null);
}
};
mavenCli.doMain(
arguments.toArray(new String[0]),
WORKING_DIRECTORY.toAbsolutePath().toString(),
stdOut,
stderr);
} while (spy.artifactTransferFailed && ++attempt < MAVEN_BUILD_ATTEMPTS);
Comment on lines +65 to +82

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth to have some sleep(5-10 seconds) between attempts? or am I missing something?


Exception error = spy.handlerError.get();
if (error != null) {
Expand All @@ -80,17 +88,33 @@ protected void customizeContainer(PlexusContainer container) {
assertTrue(spy.handlerRan.get());
}

private static boolean hasArtifactTransferFailure(MavenExecutionResult result) {
for (Throwable exception : result.getExceptions()) {
for (Throwable cause = exception; cause != null; cause = cause.getCause()) {
if (cause instanceof ArtifactTransferException) {
return true;
}
}
}
return false;
}

private static final class MojoStartedSpy extends AbstractEventSpy {
private final Function<ExecutionEvent, Boolean> mojoStartedHandler;
private final AtomicBoolean handlerRan = new AtomicBoolean(false);
private final AtomicReference<Exception> handlerError = new AtomicReference<>(null);
private boolean artifactTransferFailed;

private MojoStartedSpy(Function<ExecutionEvent, Boolean> mojoStartedHandler) {
this.mojoStartedHandler = mojoStartedHandler;
}

@Override
public void onEvent(Object o) {
if (o instanceof MavenExecutionResult) {
artifactTransferFailed = hasArtifactTransferFailure((MavenExecutionResult) o);
return;
}
if (!(o instanceof ExecutionEvent)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.gradle.tooling.internal.consumer.DefaultGradleConnector;
import org.gradle.util.GradleVersion;
import org.gradle.wrapper.Download;
import org.gradle.wrapper.GradleUserHomeLookup;
import org.gradle.wrapper.Install;
import org.gradle.wrapper.PathAssembler;
import org.gradle.wrapper.WrapperConfiguration;
Expand Down Expand Up @@ -514,7 +515,8 @@ private void ensureDependenciesDownloaded(String gradleVersion) {
GradleVersion.current().getVersion(),
GRADLE_DISTRIBUTION_NETWORK_TIMEOUT);

java.io.File userHomeDir = testKitFolder.toFile();
// Gradle TestKit installs distributions in the Gradle user home of the current JVM
java.io.File userHomeDir = GradleUserHomeLookup.gradleUserHome();
java.io.File projectDir = projectFolder.toFile();
Install install = new Install(logger, download, new PathAssembler(userHomeDir, projectDir));

Expand Down
Loading