|
20 | 20 | package com.sonar.it.scanner.msbuild.utils; |
21 | 21 |
|
22 | 22 | import java.nio.file.Path; |
| 23 | +import java.util.Optional; |
| 24 | + |
23 | 25 | import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
24 | 26 | import org.junit.jupiter.api.extension.ExecutionCondition; |
25 | 27 | import org.junit.jupiter.api.extension.ExtensionContext; |
| 28 | +import org.junit.platform.commons.util.AnnotationUtils; |
26 | 29 |
|
27 | | -public class MSBuildMinVersionCondition implements ExecutionCondition { |
| 30 | +public class MSBuildVersionCondition implements ExecutionCondition { |
28 | 31 | private static int msbuildMajorVersion = -1; |
29 | 32 |
|
30 | 33 | @Override |
31 | 34 | public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
32 | 35 | if (OSPlatform.isWindows()) { |
33 | 36 | final var method = context.getRequiredTestMethod(); |
34 | | - final var annotation = method.getDeclaredAnnotation(MSBuildMinVersion.class); |
35 | | - if (annotation != null) { |
| 37 | + Optional<MSBuildMinVersion> minAnnotation = AnnotationUtils.findAnnotation(method, MSBuildMinVersion.class); |
| 38 | + Optional<MSBuildMaxVersion> maxAnnotation = AnnotationUtils.findAnnotation(method, MSBuildMaxVersion.class); |
| 39 | + |
| 40 | + if (minAnnotation.isPresent() || maxAnnotation.isPresent()) { |
36 | 41 | var currentVersion = getMSBuildMajorVersion(); |
37 | | - var minVersion = annotation.value(); |
38 | | - return currentVersion >= minVersion |
39 | | - ? ConditionEvaluationResult.enabled("MSBuild version is " + currentVersion + ", which is greater than or equal to " + minVersion) |
40 | | - : ConditionEvaluationResult.disabled("MSBuild version is " + currentVersion + ", which is less than " + minVersion); |
| 42 | + int minVersion = minAnnotation.map(MSBuildMinVersion::value).orElse(Integer.MIN_VALUE); |
| 43 | + int maxVersion = maxAnnotation.map(MSBuildMaxVersion::value).orElse(Integer.MAX_VALUE); |
| 44 | + |
| 45 | + if (currentVersion >= minVersion && currentVersion <= maxVersion) { |
| 46 | + return ConditionEvaluationResult.enabled( |
| 47 | + String.format("MSBuild version %d satisfies constraints [min: %s, max: %s]", |
| 48 | + currentVersion, |
| 49 | + minAnnotation.isPresent() ? minVersion : "N/A", |
| 50 | + maxAnnotation.isPresent() ? maxVersion : "N/A")); |
| 51 | + } |
| 52 | + else { |
| 53 | + return ConditionEvaluationResult.disabled( |
| 54 | + String.format("MSBuild version check failed: MsBuild Version %d [min: %s, max: %s]", |
| 55 | + currentVersion, |
| 56 | + minAnnotation.isPresent() ? String.valueOf(minVersion) : "N/A", |
| 57 | + maxAnnotation.isPresent() ? String.valueOf(maxVersion) : "N/A")); |
| 58 | + } |
41 | 59 | } |
42 | 60 | } |
43 | 61 | return ConditionEvaluationResult.enabled("Test enabled"); |
|
0 commit comments