|
16 | 16 | package io.serverlessworkflow.impl.test.junit; |
17 | 17 |
|
18 | 18 | import java.io.IOException; |
| 19 | +import java.util.concurrent.TimeUnit; |
19 | 20 | import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
20 | 21 | import org.junit.jupiter.api.extension.ExecutionCondition; |
21 | 22 | import org.junit.jupiter.api.extension.ExtensionContext; |
| 23 | +import org.junit.platform.commons.util.AnnotationUtils; |
22 | 24 |
|
23 | 25 | public class BinaryCheckCondition implements ExecutionCondition { |
24 | 26 |
|
25 | 27 | @Override |
26 | 28 | public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
27 | | - String[] command = |
28 | | - context |
29 | | - .getElement() |
30 | | - .map(el -> el.getAnnotation(DisabledIfBinaryUnavailable.class).value()) |
31 | | - .orElse(null); |
32 | | - if (command == null || command.length == 0) { |
33 | | - throw new IllegalArgumentException("No @DisabledIfBinaryUnavailable annotation is present"); |
34 | | - } |
35 | | - |
36 | | - if (isBinaryAvailable(command)) { |
37 | | - return ConditionEvaluationResult.enabled(command[0] + " is available on this system."); |
38 | | - } else { |
39 | | - return ConditionEvaluationResult.disabled( |
40 | | - "Test disabled: " + command[0] + " command not found."); |
41 | | - } |
| 29 | + return AnnotationUtils.findAnnotation(context.getElement(), DisabledIfBinaryUnavailable.class) |
| 30 | + .map( |
| 31 | + annotation -> { |
| 32 | + String[] binary = annotation.value(); |
| 33 | + if (binary == null || binary.length == 0) { |
| 34 | + return ConditionEvaluationResult.enabled( |
| 35 | + "No command found in the annotation @DisabledIfBinaryUnavailable"); |
| 36 | + } |
| 37 | + if (isBinaryAvailable(binary)) { |
| 38 | + return ConditionEvaluationResult.enabled(binary[0] + " is available."); |
| 39 | + } else { |
| 40 | + return ConditionEvaluationResult.disabled( |
| 41 | + "Test disabled: " + binary[0] + " not found."); |
| 42 | + } |
| 43 | + }) |
| 44 | + .orElse(ConditionEvaluationResult.enabled("No @DisabledIfBinaryUnavailable found.")); |
42 | 45 | } |
43 | 46 |
|
44 | 47 | public boolean isBinaryAvailable(String... command) { |
45 | 48 | try { |
46 | | - Process process = new ProcessBuilder(command).start(); |
47 | | - return process.waitFor() == 0; |
| 49 | + Process process = |
| 50 | + new ProcessBuilder(command) |
| 51 | + .redirectErrorStream(true) |
| 52 | + .redirectOutput(ProcessBuilder.Redirect.DISCARD) |
| 53 | + .start(); |
| 54 | + boolean finished = process.waitFor(2, TimeUnit.SECONDS); |
| 55 | + if (finished) { |
| 56 | + return process.exitValue() == 0; |
| 57 | + } |
| 58 | + process.destroyForcibly(); |
| 59 | + return false; |
48 | 60 | } catch (IOException | InterruptedException e) { |
| 61 | + if (e instanceof InterruptedException) { |
| 62 | + Thread.currentThread().interrupt(); |
| 63 | + } |
49 | 64 | return false; |
50 | 65 | } |
51 | 66 | } |
|
0 commit comments