@Override public ConditionEvaluationResult evaluateExecutionCondition( ExtensionContext context) { Optional<AnnotatedElement> element = context.getElement(); ConditionEvaluationResult out = ConditionEvaluationResult .enabled("@DisabledOnOs is not present"); Optional<DisabledOnOs> disabledOnOs = AnnotationUtils .findAnnotation(element, DisabledOnOs.class); if (disabledOnOs.isPresent()) { Os myOs = Os.determine(); if (Arrays.asList(disabledOnOs.get().value()).contains(myOs)) { out = ConditionEvaluationResult .disabled("Test is disabled on " + myOs); } else { out = ConditionEvaluationResult .enabled("Test is not disabled on " + myOs); } } System.out.println("--> " + out.getReason().get()); return out; }
private ConditionEvaluationResult evaluate( LinkedList<String> displayName, ExtensionContext context) { Optional<ConditionEvaluationResult> result = context .getElement() .flatMap(element -> evaluateElement(displayName, element)); if (result.isPresent()) { return result.get(); } displayName.addFirst(context.getDisplayName()); return context.getParent() .map(parent -> evaluate(displayName, parent)) .orElse(ConditionEvaluationResult.enabled(null)); }
@Override public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context) { return context.getElement() .flatMap(annotatedElement -> findAnnotation(annotatedElement, EnabledOnOperatingSystem.class)) .map(EnabledOnOperatingSystem::value) .map(enabledOses -> Objects.requireNonNull(enabledOses, "OperatingSystem value must not be null")) .map(enabledOses -> Arrays.stream(enabledOses) .filter(OperatingSystem::isRunning) .collect(Collectors.collectingAndThen(Collectors.toList(), runningOses -> { if (!runningOses.isEmpty()) { return enabled("Enabled for OperatingSystem" + runningOses); } else { return disabled("Current supported OperatingSystem types=" + OperatingSystem.current() + " does not contain " + Arrays.toString(enabledOses)); } })) ).orElse(DEFAULT); }
@Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { // Search for the @DisabledWeekdays annotation from the TestExtensionContext Optional<AnnotatedElement> contextElement = context.getElement(); AnnotatedElement annotatedElement = contextElement.orElse(null); if (annotatedElement == null) return null; DisabledWeekdays weekdayAnnotation = annotatedElement.getAnnotation(DisabledWeekdays.class); // Determine whether the test should be disabled boolean weekdayToday = IntStream.of(weekdayAnnotation.value()) .anyMatch(day -> day == Calendar.getInstance().get(Calendar.DAY_OF_WEEK)); // Return a ConditionEvaluationResult based on the outcome of the boolean weekdayToday return weekdayToday ? ConditionEvaluationResult.disabled("I spare you today.") : ConditionEvaluationResult.enabled("Don't spare you on other days though >:("); }
/** * Containers and tests are disabled if they are annotated with {@link DisabledOnCIServer} and they tests are run on * the CI server. */ @Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { if (!isCIServer()) { return ENABLED; } Optional<AnnotatedElement> element = context.getElement(); Optional<DisabledOnCIServer> disabled = AnnotationUtils.findAnnotation(element, DisabledOnCIServer.class); if (disabled.isPresent()) { String reason = disabled.map(DisabledOnCIServer::value) .filter(StringUtil::isNotBlank) .orElseGet(() -> element.get() + " is disabled on CI server"); return ConditionEvaluationResult.disabled(reason); } return ENABLED; }
ConditionEvaluationResult isTypeEnabled(GenerateVideo gv) { Type type = gv.value(); if (type == ALL) return ENABLED_TYPE; if (!EncoderY4M.LOSSLESS && type == MAIN) return ENABLED_TYPE; if (EncoderY4M.LOSSLESS && type == LOSSLESS) return ENABLED_TYPE; return DISABLED_TYPE; }
@Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) { AnnotatedElement element = extensionContext.getElement().orElseThrow(AssertionError::new); Disabled disabled = findAnnotation(element, Disabled.class).orElseThrow(AssertionError::new); String name = disabled.value(); String reason = name + "=" + System.getProperty(name); boolean result = Boolean.getBoolean(name); if (disabled.not()) { result = !result; } if (result) { return ConditionEvaluationResult.disabled(reason); } return ConditionEvaluationResult.enabled(reason); }
Optional<ConditionEvaluationResult> evaluateElement( List<String> currentDisplayName, AnnotatedElement element) { List<Disable> disabledTests = findRepeatableAnnotations(element, Disable.class); return disabledTests .stream() .filter( disabledTest -> Arrays.asList(disabledTest.value()).equals(currentDisplayName)) .map(disabledTest -> ConditionEvaluationResult.disabled(disabledTest.reason())) .findFirst(); }
@Override public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context) { return context.getElement() .flatMap(annotatedElement -> findAnnotation(annotatedElement, RunOnDay.class)) .map(RunOnDay::value) .map(RunOnDayCondition::evaluateIfRunningOnDay) .orElse(DEFAULT); }
private static ConditionEvaluationResult evaluateIfRunningOnDay(final DayOfWeek[] days) { // TODO: figure out how to inject a configurable clock instance final DayOfWeek currentDay = LocalDate.now().getDayOfWeek(); final boolean runningInday = Stream.of(days).anyMatch(currentDay::equals); if (runningInday) { return enabled("Current day is " + currentDay + ", in the specified days of " + Arrays.toString(days)); } else { return disabled("Current day is " + currentDay + ", not in the specified days of " + Arrays.toString(days)); } }
@Override public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context) { return context.getElement() .flatMap(annotatedElement -> AnnotationSupport.findAnnotation(annotatedElement, DisabledOnJava8.class)) .map(annotation -> evaluatedIfJava8()) .orElse(DEFAULT); }
private static ConditionEvaluationResult evaluatedIfJava8() { final String specificationVersion = System.getProperty("java.specification.version"); if ("1.8".equals(specificationVersion)) { return DISABLED; } else { return ConditionEvaluationResult.enabled("Enabled on java.specification.version=" + specificationVersion); } }
@Override public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context) { return context.getElement() .flatMap(annotatedElement -> AnnotationSupport.findAnnotation(annotatedElement, DisabledOnJava9.class)) .map(annotation -> evaluatedIfJava8()) .orElse(DEFAULT); }
private static ConditionEvaluationResult evaluatedIfJava8() { final String specificationVersion = System.getProperty("java.specification.version"); if ("9".equals(specificationVersion)) { return DISABLED; } else { return ConditionEvaluationResult.enabled("Enabled on java.specification.version=" + specificationVersion); } }
@Override public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context) { return context.getElement() .flatMap(annotatedElement -> findAnnotation(annotatedElement, DisabledWhen.class)) .map(DisabledWhen::value) .map(supplierClass -> ReflectionSupport.newInstance(supplierClass)) .map(Supplier::get) .map(result -> Objects.requireNonNull(result, "Supplier result must not be null")) .map(shouldDisable -> shouldDisable ? disabled(DisabledWhen.class + " evaluated to true") : enabled(DisabledWhen.class + " evaluated to false")) .orElse(DEFAULT); }
@Override public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context) { return context.getElement() .flatMap(annotatedElement -> findAnnotation(annotatedElement, EnabledWhen.class)) .map(EnabledWhen::value) .map(supplierClass -> ReflectionSupport.newInstance(supplierClass)) .map(Supplier::get) .map(result -> Objects.requireNonNull(result, "Result must not be null")) .map(shouldEnable -> shouldEnable ? enabled(EnabledWhen.class + " evaluated to false") : disabled(EnabledWhen.class + " evaluated to true")) .orElse(DEFAULT); }
private ConditionEvaluationResult evaluateIfAnnotated(Optional<AnnotatedElement> element) { Optional<DisabledOnOs> disabled = element.flatMap(el -> findAnnotation(el, DisabledOnOs.class)); if (disabled.isPresent()) return disabledIfOn(disabled.get().value()); Optional<TestExceptOnOs> testExcept = element.flatMap(el -> findAnnotation(el, TestExceptOnOs.class)); if (testExcept.isPresent()) return disabledIfOn(testExcept.get().value()); return ConditionEvaluationResult.enabled(""); }
private ConditionEvaluationResult disabledIfOn(OS[] disabledOnOs) { OS os = OS.determine(); if (Arrays.asList(disabledOnOs).contains(os)) return ConditionEvaluationResult.disabled("Test is disabled on " + os + "."); else return ConditionEvaluationResult.enabled("Test is not disabled in " + os + "."); }
@Override public ConditionEvaluationResult evaluate(TestExtensionContext context) { Class<? extends Exception>[] exceptionTypes = context.getTestClass() .flatMap(testClass -> findAnnotation(testClass, DisabledIfTestFailedWith.class)) .orElseThrow(() -> new IllegalStateException("The extension should not be executed " + "unless the test class is annotated with @DisabledIfTestFailedWith.")) .value(); return disableIfExceptionWasThrown(context, exceptionTypes); }
private ConditionEvaluationResult disableIfExceptionWasThrown( ExtensionContext context, Class<? extends Exception>[] exceptions) { return Arrays.stream(exceptions) .filter(ex -> wasThrown(context, ex)) .findAny() .map(thrown -> ConditionEvaluationResult.disabled(thrown.getSimpleName() + " was thrown.")) .orElseGet(() -> ConditionEvaluationResult.enabled("")); }
private ConditionEvaluationResult evaluateIfAnnotated(Optional<AnnotatedElement> element) { // @formatter:off Optional<ConditionEvaluationResult> disabledResult = element .flatMap(el -> findAnnotation(el, DisabledOnOs.class)) .map(DisabledOnOs::value) .map(this::disabledIfOn); // @formatter:on if (disabledResult.isPresent()) { return disabledResult.get(); } // @formatter:off Optional<ConditionEvaluationResult> enabledResult = element .flatMap(el -> findAnnotation(el, EnabledOnOs.class)) .map(EnabledOnOs::value) // invert the selection to disable the test // if it NOT one of those given to the annotation .map(OS::except) .map(this::disabledIfOn); // @formatter:on if (enabledResult.isPresent()) { return enabledResult.get(); } // if both annotations are missing, the container/test is enabled return ConditionEvaluationResult.enabled(NO_CONDITION_PRESENT); }
private ConditionEvaluationResult disabledIfOn(OS[] disabledOnOs) { OS os = osDetector.get(); if (Arrays.asList(disabledOnOs).contains(os)) { return ConditionEvaluationResult.disabled(format(TEST_DISABLED, os)); } else { return ConditionEvaluationResult.enabled(format(TEST_ENABLED, os)); } }
@Test void evaluateContainer_notAnnotated_enabled() throws Exception { ExtensionContext context = createContextReturning(UnconditionalTestCase.class); OsCondition condition = new OsCondition(); ConditionEvaluationResult result = condition.evaluateExecutionCondition(context); assertThat(result.isDisabled()).isFalse(); assertThat(result.getReason()).contains(OsCondition.NO_CONDITION_PRESENT); }
@Test void evaluateContainer_disabledOnOtherOs_enabled() throws Exception { ExtensionContext context = createContextReturning(DisabledOnLinuxTestCase.class); OsCondition conditionOnWindows = new OsCondition(() -> OS.WINDOWS); ConditionEvaluationResult result = conditionOnWindows.evaluateExecutionCondition(context); assertEnabledOn(result, OS.WINDOWS); }
@Test void evaluateContainer_disabledOnRunningOs_disabled() throws Exception { ExtensionContext context = createContextReturning(DisabledOnLinuxTestCase.class); OsCondition conditionOnWindows = new OsCondition(() -> OS.LINUX); ConditionEvaluationResult result = conditionOnWindows.evaluateExecutionCondition(context); assertDisabledOn(result, OS.LINUX); }
@Test void evaluateContainer_enabledOnOtherOs_disabled() throws Exception { ExtensionContext context = createContextReturning(EnabledOnLinuxTestCase.class); OsCondition conditionOnWindows = new OsCondition(() -> OS.WINDOWS); ConditionEvaluationResult result = conditionOnWindows.evaluateExecutionCondition(context); assertDisabledOn(result, OS.WINDOWS); }
@Test void evaluateContainer_enabledOnRunningOs_enabled() throws Exception { ExtensionContext context = createContextReturning(EnabledOnLinuxTestCase.class); OsCondition conditionOnWindows = new OsCondition(() -> OS.LINUX); ConditionEvaluationResult result = conditionOnWindows.evaluateExecutionCondition(context); assertEnabledOn(result, OS.LINUX); }
@Test void evaluateMethod_notAnnotated_enabled() throws Exception { ExtensionContext context = createContextReturning(UnconditionalTestCase.class, "unconditionalTest"); OsCondition condition = new OsCondition(); ConditionEvaluationResult result = condition.evaluateExecutionCondition(context); assertThat(result.isDisabled()).isFalse(); assertThat(result.getReason()).contains(OsCondition.NO_CONDITION_PRESENT); }
@Test void evaluateMethod_disabledOnOtherOs_enabled() throws Exception { ExtensionContext context = createContextReturning(EnabledAndDisabledTestMethods.class, "disabledOnLinuxTest"); OsCondition condition = new OsCondition(() -> OS.WINDOWS); ConditionEvaluationResult result = condition.evaluateExecutionCondition(context); assertEnabledOn(result, OS.WINDOWS); }
@Test void evaluateMethod_disabledOnRunningOs_disabled() throws Exception { ExtensionContext context = createContextReturning(EnabledAndDisabledTestMethods.class, "disabledOnLinuxTest"); OsCondition condition = new OsCondition(() -> OS.LINUX); ConditionEvaluationResult result = condition.evaluateExecutionCondition(context); assertDisabledOn(result, OS.LINUX); }
@Test void evaluateMethod_enabledOnOtherOs_disabled() throws Exception { ExtensionContext context = createContextReturning(EnabledAndDisabledTestMethods.class, "enabledOnLinuxTest"); OsCondition condition = new OsCondition(() -> OS.WINDOWS); ConditionEvaluationResult result = condition.evaluateExecutionCondition(context); assertDisabledOn(result, OS.WINDOWS); }
@Test void evaluateMethod_enabledOnRunningOs_enabled() throws Exception { ExtensionContext context = createContextReturning(EnabledAndDisabledTestMethods.class, "enabledOnLinuxTest"); OsCondition condition = new OsCondition(() -> OS.LINUX); ConditionEvaluationResult result = condition.evaluateExecutionCondition(context); assertEnabledOn(result, OS.LINUX); }
@Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { boolean monday = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY; return monday ? ConditionEvaluationResult.disabled("I spare you on Mondays.") : ConditionEvaluationResult.enabled("Don't spare you on other days though >:("); }
@Override public ConditionEvaluationResult evaluateExecutionCondition( ExtensionContext context) { return findAnnotation(context.getElement(), GenerateVideo.class) .map(this::isTypeEnabled).orElse(ENABLED_NO_ANNOTATION); }
@Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { return evaluate(new LinkedList<>(), context); }
@Override public ConditionEvaluationResult evaluate(ContainerExtensionContext context) { return evaluateIfAnnotated(context.getElement()); }
@Override public ConditionEvaluationResult evaluate(TestExtensionContext context) { return evaluateIfAnnotated(context.getElement()); }
private ConditionEvaluationResult evaluateIfAnnotated(Optional<AnnotatedElement> element) { return element.filter(el -> isAnnotated(el, Disabled.class)) .map(el -> ConditionEvaluationResult.disabled(el + " is @Disabled")) .orElse(ENABLED); }