|
| 1 | +package io.qameta.allure; |
| 2 | + |
| 3 | +import io.qameta.allure.model.Stage; |
| 4 | +import io.qameta.allure.model.TestResult; |
| 5 | +import org.junit.platform.engine.TestExecutionResult; |
| 6 | +import org.junit.platform.launcher.TestExecutionListener; |
| 7 | +import org.junit.platform.launcher.TestIdentifier; |
| 8 | + |
| 9 | +import java.math.BigInteger; |
| 10 | +import java.security.MessageDigest; |
| 11 | +import java.security.NoSuchAlgorithmException; |
| 12 | +import java.util.UUID; |
| 13 | + |
| 14 | +import static io.qameta.allure.model.Status.FAILED; |
| 15 | +import static io.qameta.allure.model.Status.PASSED; |
| 16 | +import static io.qameta.allure.model.Status.SKIPPED; |
| 17 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author ehborisov |
| 21 | + */ |
| 22 | +public class AllureJunit5 implements TestExecutionListener { |
| 23 | + |
| 24 | + private final ThreadLocal<String> tests |
| 25 | + = InheritableThreadLocal.withInitial(() -> UUID.randomUUID().toString()); |
| 26 | + |
| 27 | + private final AllureLifecycle lifecycle; |
| 28 | + |
| 29 | + public AllureJunit5(final AllureLifecycle lifecycle) { |
| 30 | + this.lifecycle = lifecycle; |
| 31 | + } |
| 32 | + |
| 33 | + public AllureJunit5() { |
| 34 | + this.lifecycle = Allure.getLifecycle(); |
| 35 | + } |
| 36 | + |
| 37 | + public AllureLifecycle getLifecycle() { |
| 38 | + return lifecycle; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void executionStarted(final TestIdentifier testIdentifier) { |
| 43 | + if (testIdentifier.isTest()) { |
| 44 | + final String uuid = tests.get(); |
| 45 | + final TestResult result = new TestResult() |
| 46 | + .withUuid(uuid) |
| 47 | + .withName(testIdentifier.getDisplayName()) |
| 48 | + .withHistoryId(md5(testIdentifier.getUniqueId())) |
| 49 | + .withStage(Stage.RUNNING); |
| 50 | + getLifecycle().scheduleTestCase(result); |
| 51 | + getLifecycle().startTestCase(uuid); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void executionFinished(final TestIdentifier testIdentifier, final TestExecutionResult testExecutionResult) { |
| 57 | + if (testIdentifier.isTest()) { |
| 58 | + final String uuid = tests.get(); |
| 59 | + tests.remove(); |
| 60 | + getLifecycle().updateTestCase(uuid, result -> { |
| 61 | + result.setStage(Stage.FINISHED); |
| 62 | + switch (testExecutionResult.getStatus()) { |
| 63 | + case FAILED: |
| 64 | + testExecutionResult.getThrowable().ifPresent(throwable -> { |
| 65 | + result.setStatus(ResultsUtils.getStatus(throwable).orElse(FAILED)); |
| 66 | + result.setStatusDetails(ResultsUtils.getStatusDetails(throwable).orElse(null)); |
| 67 | + }); |
| 68 | + break; |
| 69 | + case SUCCESSFUL: |
| 70 | + result.setStatus(PASSED); |
| 71 | + break; |
| 72 | + default: |
| 73 | + result.setStatus(SKIPPED); |
| 74 | + testExecutionResult.getThrowable().ifPresent(throwable -> |
| 75 | + result.setStatusDetails(ResultsUtils.getStatusDetails(throwable).orElse(null)) |
| 76 | + ); |
| 77 | + break; |
| 78 | + } |
| 79 | + }); |
| 80 | + getLifecycle().stopTestCase(uuid); |
| 81 | + getLifecycle().writeTestCase(uuid); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private String md5(final String source) { |
| 86 | + final byte[] bytes = getMessageDigest().digest(source.getBytes(UTF_8)); |
| 87 | + return new BigInteger(1, bytes).toString(16); |
| 88 | + } |
| 89 | + |
| 90 | + private MessageDigest getMessageDigest() { |
| 91 | + try { |
| 92 | + return MessageDigest.getInstance("md5"); |
| 93 | + } catch (NoSuchAlgorithmException e) { |
| 94 | + throw new IllegalStateException("Could not find md5 hashing algorithm", e); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments