-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88d00ab
commit 721c0ff
Showing
8 changed files
with
51 additions
and
50 deletions.
There are no files selected for viewing
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
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
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
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
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
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: 39 additions & 9 deletions
48
exporter/src/test/java/io/zeebe/redis/testcontainers/OnFailureExtension.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 |
---|---|---|
@@ -1,20 +1,50 @@ | ||
package io.zeebe.redis.testcontainers; | ||
|
||
import org.junit.jupiter.api.extension.AfterTestExecutionCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.*; | ||
import org.testcontainers.junit.jupiter.Container; | ||
|
||
public class OnFailureExtension implements AfterTestExecutionCallback { | ||
import java.util.Arrays; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
ZeebeTestContainer zeebeTestContainer; | ||
public class OnFailureExtension implements TestWatcher, BeforeEachCallback, AfterEachCallback { | ||
|
||
public void setZeebeTestContainer(ZeebeTestContainer zeebeTestContainer) { | ||
this.zeebeTestContainer = zeebeTestContainer; | ||
private static final Logger LOGGER = Logger.getLogger("ZeebeTestContainer"); | ||
|
||
private String containerLogs; | ||
|
||
@Override | ||
public void testFailed(ExtensionContext context, Throwable cause) { | ||
if (containerLogs != null) { | ||
var prefix = "\n----------------------------------------------------------------------" | ||
+ "\nZeebe container log for " + context.getDisplayName() | ||
+ "\n----------------------------------------------------------------------\n"; | ||
LOGGER.log(Level.WARNING, prefix + containerLogs); | ||
} | ||
} | ||
|
||
@Override | ||
public void afterTestExecution(ExtensionContext extensionContext) throws Exception { | ||
if (extensionContext.getExecutionException() != null && zeebeTestContainer != null) { | ||
zeebeTestContainer.writeContainerLogs(); | ||
public void afterEach(ExtensionContext extensionContext) { | ||
var testInstance = extensionContext.getTestInstance(); | ||
if (testInstance.isEmpty()) return; | ||
var zeebeTestContainerField = Arrays.stream(testInstance.get().getClass().getDeclaredFields()) | ||
.filter(field -> field.isAnnotationPresent(Container.class) | ||
&& field.getType().equals(ZeebeTestContainer.class)).findFirst(); | ||
if (zeebeTestContainerField.isPresent()) { | ||
var field = zeebeTestContainerField.get(); | ||
field.setAccessible(true); | ||
try { | ||
var zeebeTestContainer = (ZeebeTestContainer) field.get(testInstance.get()); | ||
containerLogs = zeebeTestContainer.getLogs(); | ||
} catch (IllegalAccessException ex) { | ||
// NOOP | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext extensionContext) { | ||
containerLogs = null; | ||
} | ||
|
||
} |
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