forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce 'EnabledIfDockerAvailable' junit-jupiter conditional (testc…
- Loading branch information
1 parent
18ea976
commit 3bb8b24
Showing
5 changed files
with
132 additions
and
7 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
...junit-jupiter/src/main/java/org/testcontainers/junit/jupiter/DockerAvailableDetector.java
This file contains 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.jupiter; | ||
|
||
import org.testcontainers.DockerClientFactory; | ||
|
||
class DockerAvailableDetector { | ||
|
||
public boolean isDockerAvailable() { | ||
try { | ||
DockerClientFactory.instance().client(); | ||
return true; | ||
} catch (Throwable ex) { | ||
return false; | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...unit-jupiter/src/main/java/org/testcontainers/junit/jupiter/EnabledIfDockerAvailable.java
This file contains 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,19 @@ | ||
package org.testcontainers.junit.jupiter; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* {@code EnabledIfDockerAvailable} is a JUnit Jupiter extension to enable tests only if Docker is available. | ||
*/ | ||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@ExtendWith(EnabledIfDockerAvailableCondition.class) | ||
public @interface EnabledIfDockerAvailable { | ||
} |
47 changes: 47 additions & 0 deletions
47
...ter/src/main/java/org/testcontainers/junit/jupiter/EnabledIfDockerAvailableCondition.java
This file contains 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,47 @@ | ||
package org.testcontainers.junit.jupiter; | ||
|
||
import org.junit.jupiter.api.extension.ConditionEvaluationResult; | ||
import org.junit.jupiter.api.extension.ExecutionCondition; | ||
import org.junit.jupiter.api.extension.ExtensionConfigurationException; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.platform.commons.support.AnnotationSupport; | ||
|
||
import java.util.Optional; | ||
|
||
public class EnabledIfDockerAvailableCondition implements ExecutionCondition { | ||
|
||
private final DockerAvailableDetector dockerDetector = new DockerAvailableDetector(); | ||
|
||
@Override | ||
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { | ||
return findAnnotation(context) | ||
.map(this::evaluate) | ||
.orElseThrow(() -> new ExtensionConfigurationException("@EnabledIfDockerAvailable not found")); | ||
} | ||
|
||
boolean isDockerAvailable() { | ||
return dockerDetector.isDockerAvailable(); | ||
} | ||
|
||
private ConditionEvaluationResult evaluate(EnabledIfDockerAvailable testcontainers) { | ||
if (isDockerAvailable()) { | ||
return ConditionEvaluationResult.enabled("Docker is available"); | ||
} | ||
return ConditionEvaluationResult.disabled("Docker is not available"); | ||
} | ||
|
||
private Optional<EnabledIfDockerAvailable> findAnnotation(ExtensionContext context) { | ||
Optional<ExtensionContext> current = Optional.of(context); | ||
while (current.isPresent()) { | ||
Optional<EnabledIfDockerAvailable> enabledIfDockerAvailable = AnnotationSupport.findAnnotation( | ||
current.get().getRequiredTestClass(), | ||
EnabledIfDockerAvailable.class | ||
); | ||
if (enabledIfDockerAvailable.isPresent()) { | ||
return enabledIfDockerAvailable; | ||
} | ||
current = current.get().getParent(); | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
This file contains 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
48 changes: 48 additions & 0 deletions
48
...jupiter/src/test/java/org/testcontainers/junit/jupiter/EnabledIfDockerAvailableTests.java
This file contains 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,48 @@ | ||
package org.testcontainers.junit.jupiter; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ConditionEvaluationResult; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class EnabledIfDockerAvailableTests { | ||
|
||
@Test | ||
void whenDockerIsAvailableTestsAreEnabled() { | ||
ConditionEvaluationResult result = new TestEnabledIfDockerAvailableCondition(true) | ||
.evaluateExecutionCondition(extensionContext(DisabledWithoutDocker.class)); | ||
assertThat(result.isDisabled()).isFalse(); | ||
} | ||
|
||
@Test | ||
void whenDockerIsUnavailableTestsAreDisabled() { | ||
ConditionEvaluationResult result = new TestEnabledIfDockerAvailableCondition(false) | ||
.evaluateExecutionCondition(extensionContext(DisabledWithoutDocker.class)); | ||
assertThat(result.isDisabled()).isTrue(); | ||
} | ||
|
||
private ExtensionContext extensionContext(Class clazz) { | ||
ExtensionContext extensionContext = mock(ExtensionContext.class); | ||
when(extensionContext.getRequiredTestClass()).thenReturn(clazz); | ||
return extensionContext; | ||
} | ||
|
||
@EnabledIfDockerAvailable | ||
static final class DisabledWithoutDocker {} | ||
|
||
static final class TestEnabledIfDockerAvailableCondition extends EnabledIfDockerAvailableCondition { | ||
|
||
private final boolean dockerAvailable; | ||
|
||
private TestEnabledIfDockerAvailableCondition(boolean dockerAvailable) { | ||
this.dockerAvailable = dockerAvailable; | ||
} | ||
|
||
boolean isDockerAvailable() { | ||
return dockerAvailable; | ||
} | ||
} | ||
} |