-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Create junit-vintage module #10351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Create junit-vintage module #10351
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fb35732
Create Testcontainers Rule
kcooney f3b80bb
Fix FailureDetectingExternalResource to properly handle assumption fa…
kcooney 4e717b6
Update FailureDetectingExternalResource to suppress post-test failure…
kcooney 35e5731
Add TemporaryNetwork Rule
kcooney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
description = "Testcontainers :: JUnit4 Rule" | ||
|
||
dependencies { | ||
api project(":testcontainers") | ||
api 'junit:junit:4.13.2' | ||
implementation platform('org.junit:junit-bom:5.10.3') | ||
implementation 'org.junit.jupiter:junit-jupiter-api' | ||
|
||
testImplementation 'com.datastax.oss:java-driver-core:4.17.0' | ||
testImplementation 'org.assertj:assertj-core:3.26.3' | ||
} |
15 changes: 15 additions & 0 deletions
15
modules/junit-vintage/src/main/java/org/testcontainers/junit/vintage/Container.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.testcontainers.junit.vintage; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* The {@code @Container} annotation marks containers that should be managed by the | ||
* {@code Testcontainers} rule. | ||
*/ | ||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Container { | ||
} |
96 changes: 96 additions & 0 deletions
96
...tage/src/main/java/org/testcontainers/junit/vintage/FailureDetectingExternalResource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.testcontainers.junit.vintage; | ||
|
||
import org.junit.rules.TestRule; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.MultipleFailureException; | ||
import org.junit.runners.model.Statement; | ||
import org.testcontainers.lifecycle.TestDescription; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* {@link TestRule} which is called before and after each test, and also is notified on success/failure. | ||
* | ||
* This mimics the behaviour of TestWatcher to some degree, but failures occurring in {@code starting()} | ||
* prevent the test from being run. | ||
*/ | ||
class FailureDetectingExternalResource implements TestRule { | ||
|
||
@Override | ||
public final Statement apply(Statement base, Description description) { | ||
return new Statement() { | ||
@Override | ||
public void evaluate() throws Throwable { | ||
List<Throwable> errors = new ArrayList<Throwable>(); | ||
Optional<Throwable> failure = Optional.empty(); | ||
|
||
try { | ||
starting(description); | ||
base.evaluate(); | ||
notifySucceeded(description, errors); | ||
} catch (org.junit.internal.AssumptionViolatedException e) { | ||
failure = Optional.of(e); | ||
} catch (Throwable e) { | ||
failure = Optional.of(e); | ||
errors.add(e); | ||
notifyFailed(e, description); | ||
} finally { | ||
notifyFinished(failure, description, errors); | ||
} | ||
|
||
MultipleFailureException.assertEmpty(errors); | ||
} | ||
}; | ||
} | ||
|
||
protected void starting(Description description) throws Throwable {} | ||
|
||
protected void succeeded(Description description) throws Throwable {} | ||
|
||
protected void failed(Throwable e, Description description) throws Throwable {} | ||
|
||
protected void finished(Description description) throws Throwable {} | ||
|
||
private void notifySucceeded(Description description, List<Throwable> errors) { | ||
try { | ||
succeeded(description); | ||
} catch (Throwable e) { | ||
errors.add(e); | ||
} | ||
} | ||
|
||
private void notifyFailed(Throwable failure, Description description) { | ||
try { | ||
failed(failure, description); | ||
} catch (Throwable e) { | ||
failure.addSuppressed(e); | ||
} | ||
} | ||
|
||
private void notifyFinished(Optional<Throwable> failure, Description description, List<Throwable> errors) { | ||
try { | ||
finished(description); | ||
} catch (Throwable e) { | ||
failure.ifPresent(f -> f.addSuppressed(e)); // ifPresentOrElse() requires Java 9 | ||
if (!failure.isPresent()) { | ||
errors.add(e); | ||
} | ||
} | ||
} | ||
|
||
protected static final TestDescription toTestDescription(Description description) { | ||
return new TestDescription() { | ||
@Override | ||
public String getTestId() { | ||
return description.getDisplayName(); | ||
} | ||
|
||
@Override | ||
public String getFilesystemFriendlyName() { | ||
return description.getClassName() + "-" + description.getMethodName(); | ||
} | ||
}; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
modules/junit-vintage/src/main/java/org/testcontainers/junit/vintage/TemporaryNetwork.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.testcontainers.junit.vintage; | ||
|
||
import org.junit.rules.ExternalResource; | ||
import org.testcontainers.containers.Network; | ||
|
||
/** | ||
* Integrates {@link Network} with the JUnit4 lifecycle. | ||
*/ | ||
public final class TemporaryNetwork extends ExternalResource implements Network { | ||
|
||
private final Network network; | ||
|
||
private volatile State state = State.BEFORE_RULE; | ||
|
||
/** | ||
* Creates an instance. | ||
* | ||
* <p>The passed-in network will be closed when the current test completes. | ||
* | ||
* @param network Network that the rule will delegate to. | ||
*/ | ||
public TemporaryNetwork(Network network) { | ||
this.network = network; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
if (state == State.AFTER_RULE) { | ||
throw new IllegalStateException("Cannot reference the network after the test completes"); | ||
} | ||
return network.getId(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
switch (state) { | ||
case BEFORE_RULE: | ||
throw new IllegalStateException("Cannot close the network before the test starts"); | ||
case INSIDE_RULE: | ||
break; | ||
case AFTER_RULE: | ||
throw new IllegalStateException("Cannot reference the network after the test completes"); | ||
} | ||
network.close(); | ||
} | ||
|
||
@Override | ||
protected void before() throws Throwable { | ||
state = State.AFTER_RULE; // Just in case an exception is thrown below. | ||
network.getId(); // This has the side-effect of creating the network. | ||
|
||
state = State.INSIDE_RULE; | ||
} | ||
|
||
@Override | ||
protected void after() { | ||
state = State.AFTER_RULE; | ||
network.close(); | ||
} | ||
|
||
private enum State { | ||
BEFORE_RULE, | ||
INSIDE_RULE, | ||
AFTER_RULE, | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.