类org.junit.jupiter.api.extension.ConditionEvaluationResult源码实例Demo

下面列出了怎么用org.junit.jupiter.api.extension.ConditionEvaluationResult的API类实例代码及写法,或者点击链接到github查看源代码。

private ConditionEvaluationResult map(EnabledIfProperty annotation) {
    final String name = annotation.named().trim();
    final String regex = annotation.matches();

    Preconditions.notBlank(name, () -> "The 'named' attribute must not be blank in " + annotation);
    Preconditions.notBlank(regex, () -> "The 'matches' attribute must not be blank in " + annotation);

    return ConfigProviderResolver.instance().getConfig().getOptionalValue(name, String.class)
            .map(actual -> {
                return actual.matches(regex)
                        ? enabled(
                                format("Config property [%s] with value [%s] matches regular expression [%s]",
                                        name, actual, regex))
                        : disabled(
                                format("Config property [%s] with value [%s] does not match regular expression [%s]",
                                        name, actual, regex));
            })
            .orElseGet(() -> {
                return disabled(
                        format("Config property [%s] does not exist", name));
            });
}
 
源代码2 项目: quarkus   文件: DisabledOnNativeImageCondition.java
/**
 * Containers/tests are disabled if {@code @DisabledOnNativeImage} is present on the test
 * class or method and we're running on a native image.
 */
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    Optional<AnnotatedElement> element = context.getElement();
    Optional<DisabledOnNativeImage> disabled = findAnnotation(element, DisabledOnNativeImage.class);
    if (disabled.isPresent()) {
        // Cannot use ExtensionState here because this condition needs to be evaluated before QuarkusTestExtension
        boolean nativeImage = findAnnotation(context.getTestClass(), NativeImageTest.class).isPresent();
        if (nativeImage) {
            String reason = disabled.map(DisabledOnNativeImage::value)
                    .filter(StringUtils::isNotBlank)
                    .orElseGet(() -> element.get() + " is @DisabledOnNativeImage");
            return ConditionEvaluationResult.disabled(reason);
        }
    }
    return ENABLED;
}
 
源代码3 项目: dekorate   文件: ServicePresentCondition.java
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
 Optional<OnServicePresentCondition> annotation = context.getElement().map(e -> e.getAnnotation(OnServicePresentCondition.class));
 if (!annotation.isPresent()) {
  return ConditionEvaluationResult.enabled("Condition not found!");
 }
 OnServicePresentCondition condition = annotation.get();
  try {
    KubernetesClient client = getKubernetesClient(context);
    String namespace = Strings.isNotNullOrEmpty(condition.namespace()) ? condition.namespace() :  client.getNamespace();
    Service service = getKubernetesClient(context).services().inNamespace(namespace).withName(condition.value()).get();
    if (service != null) {
      return ConditionEvaluationResult.enabled("Found service:" + condition.value() + " in namespace:" + namespace + " .");
    } else {
      return ConditionEvaluationResult.disabled("Could not find service:" + condition.value() + " in namespace:" + namespace + " .");
    }
  } catch (Throwable t) {
    return ConditionEvaluationResult.disabled("Could not lookup for service.");
  }
}
 
源代码4 项目: mastering-junit5   文件: OsCondition.java
@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;
}
 
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    if (ipv6Result == null) {
        boolean result = false;
        try {
            result = hasIPv6Loopback();
        } catch (SocketException e) {
            log.warn("Could not determine presence of IPv6 loopback address", e);
        }

        if (result) {
            ipv6Result = ConditionEvaluationResult.enabled("Found IPv6 loopback");
        } else {
            ipv6Result = ConditionEvaluationResult.disabled("Could not find IPv6 loopback");
        }
    }
    return ipv6Result;
}
 
源代码6 项目: junit5-extensions   文件: TestDisabler.java
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(
        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;
}
 
源代码8 项目: status-keycard   文件: CapabilityCondition.java
/**
 * Containers/tests are disabled if {@code @Disabled} is present on the test
 * class or method.
 */
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
  Optional<AnnotatedElement> element = context.getElement();
  Optional<Capabilities> capsAnnotation = findAnnotation(element, Capabilities.class);

  if (capsAnnotation.isPresent()) {
    for (String c : capsAnnotation.get().value()) {
      if (!availableCapabilities.contains(c)) {
        return disabled("The " + c + " capability is not available on the tested target");
      }
    }

    return ENABLED;
  }

  return ENABLED_BY_DEFAULT;
}
 
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    if (ipv6Result == null) {
        boolean result = false;
        try {
            result = hasIPv6Loopback();
        } catch (SocketException e) {
            log.warn("Could not determine presence of IPv6 loopback address", e);
        }

        if (result) {
            ipv6Result = ConditionEvaluationResult.enabled("Found IPv6 loopback");
        } else {
            ipv6Result = ConditionEvaluationResult.disabled("Could not find IPv6 loopback");
        }
    }
    return ipv6Result;
}
 
@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 >:(");
}
 
源代码11 项目: enmasse   文件: AssumeOpenshiftCondition.java
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    Optional<OpenShift> annotation = findAnnotation(context.getElement(), OpenShift.class);
    if (annotation.isPresent()) {
        var version = annotation.get().version();
        var type = annotation.get().type();
        var multinode = type.equals(ClusterType.CRC) ? MultinodeCluster.NO : annotation.get().multinode();
        if ((Kubernetes.getInstance().getCluster().toString().equals(ClusterType.OPENSHIFT.toString().toLowerCase()) ||
                Kubernetes.getInstance().getCluster().toString().equals(ClusterType.CRC.toString().toLowerCase())) &&
                (version == OpenShiftVersion.WHATEVER || version == Kubernetes.getInstance().getOcpVersion()) &&
                (multinode == MultinodeCluster.WHATEVER || multinode == Kubernetes.getInstance().isClusterMultinode())) {
            return ConditionEvaluationResult.enabled("Test is supported on current cluster");
        } else {
            return ConditionEvaluationResult.disabled("Test is not supported on current cluster");
        }
    }
    return ConditionEvaluationResult.enabled("No rule set, test is enabled");
}
 
源代码12 项目: enmasse   文件: SupportedInstallTypeCondition.java
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    Optional<SupportedInstallType> annotation = findAnnotation(context.getRequiredTestClass(), SupportedInstallType.class);
    if (annotation.isPresent()) {
        SupportedInstallType supports = annotation.get();
        io.enmasse.systemtest.platform.Kubernetes kube = io.enmasse.systemtest.platform.Kubernetes.getInstance();
        Environment env = Environment.getInstance();
        String reason = String.format("Env is supported types %s type used %s olmAvailability %s",
                supports.value().toString(), env.installType(), kube.isOLMAvailable());
        if (isTestEnabled(supports, kube.isOLMAvailable(), env.installType())) {
           return ConditionEvaluationResult.enabled(reason);
        } else {
            return ConditionEvaluationResult.disabled(reason);
        }
    }
    return ConditionEvaluationResult.enabled("No rule set, test is enabled");
}
 
源代码13 项目: sarl   文件: JavaVersionCheckExtension.java
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
	// Check if the minimal version of Java is used for running the tests.
	final JavaVersion cVersion = JavaVersion.fromQualifier(System.getProperty("java.specification.version"));
	if (cVersion == null) {
		return ConditionEvaluationResult.disabled("You must use JDK " + SARLVersion.MINIMAL_JDK_VERSION_FOR_SARL_COMPILATION_ENVIRONMENT + " or higher for running the tests.");
	}
	final JavaVersion mVersion = JavaVersion.fromQualifier(SARLVersion.MINIMAL_JDK_VERSION_FOR_SARL_COMPILATION_ENVIRONMENT);
	if (mVersion == null || !cVersion.isAtLeast(mVersion)) {
		return ConditionEvaluationResult.disabled("You must use JDK " + SARLVersion.MINIMAL_JDK_VERSION_FOR_SARL_COMPILATION_ENVIRONMENT + " or higher for running the tests.");
	}
	final JavaVersion xVersion = JavaVersion.fromQualifier(SARLVersion.INCOMPATIBLE_JDK_VERSION_FOR_SARL_COMPILATION_ENVIRONMENT);
	// If null the max version that is specified into the SARL configuration is not yey supported by Xtext enumeration
	if (xVersion != null && cVersion.isAtLeast(xVersion)) {
		return ConditionEvaluationResult.disabled("You must use JDK strictly below " + SARLVersion.INCOMPATIBLE_JDK_VERSION_FOR_SARL_COMPILATION_ENVIRONMENT + " for running the tests.");
	}
	return ConditionEvaluationResult.enabled("supported version of JDK");
}
 
源代码14 项目: gocd   文件: EnabledOnGitVersionCondition.java
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    Optional<EnabledOnGitVersions> maybe = findAnnotation(context.getElement(), EnabledOnGitVersions.class);
    if (maybe.isPresent()) {
        final EnabledOnGitVersions annotation = maybe.get();
        final Version gitVersion = fetchGitVersion();

        if (noneSpecified(annotation)) {
            return ConditionEvaluationResult.enabled("Version requirements not provided. Running by default.");
        }

        if (atLeast(annotation.from(), gitVersion) && atMost(annotation.through(), gitVersion)) {
            return ConditionEvaluationResult.enabled(
                    format("Git version %s satisfies %s", gitVersion, criteria(annotation))
            );
        } else {
            return ConditionEvaluationResult.disabled(
                    format("Git version %s does not satisfy %s", gitVersion, criteria(annotation))
            );
        }
    }

    return ConditionEvaluationResult.enabled("Version requirements not provided. Running by default.");
}
 
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    Properties properties = new Properties();
    try {
        properties.load(DisabledOnQAEnvironmentExtension.class.getClassLoader()
            .getResourceAsStream("application.properties"));
        if ("qa".equalsIgnoreCase(properties.getProperty("env"))) {
            String reason = String.format("The test '%s' is disabled on QA environment", context.getDisplayName());
            System.out.println(reason);
            return ConditionEvaluationResult.disabled(reason);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return ConditionEvaluationResult.enabled("Test enabled");
}
 
源代码16 项目: tutorials   文件: DisabledOnEnvironmentCondition.java
@Override
public ConditionEvaluationResult evaluate(TestExtensionContext context) {
    Properties props = new Properties();
    String env = "";
    try {
        props.load(ConnectionUtil.class.getResourceAsStream("/application.properties"));
        env = props.getProperty("env");
    } catch (IOException e) {
        e.printStackTrace();
    }
    Optional<DisabledOnEnvironment> disabled = AnnotationSupport.findAnnotation(context.getElement().get(), DisabledOnEnvironment.class);
    if (disabled.isPresent()) {
        String[] envs = disabled.get().value();
        if (Arrays.asList(envs).contains(env)) {
            return ConditionEvaluationResult.disabled("Disabled on environment " + env);
        }
    }

    return ConditionEvaluationResult.enabled("Enabled on environment "+env);
}
 
源代码17 项目: camel-quarkus   文件: EnabledIfCondition.java
private ConditionEvaluationResult map(EnabledIf annotation) {
    for (Class<? extends BooleanSupplier> type : annotation.value()) {
        try {
            if (!type.newInstance().getAsBoolean()) {
                return disabled(format("Condition %s is false", type.getName()));
            }
        } catch (InstantiationException | IllegalAccessException e) {
            return disabled(format("Unable to evaluate condition: %s", type.getName()));
        }
    }
    return enabled("All conditions match");
}
 
private void assertResult(ConditionEvaluationResult result, boolean disabled, Matcher<String> matcher) {
	assertNotNull(result);

	if (disabled) {
		assertTrue(result.isDisabled());
	}
	else {
		assertFalse(result.isDisabled());
	}

	Optional<String> reason = result.getReason();
	assertTrue(reason.isPresent());
	assertThat(reason.get(), matcher);
}
 
源代码19 项目: skara   文件: DisableAllBotsTestsOnWindows.java
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    if (!OS.WINDOWS.isCurrentOs()) {
        return enabled("Non-Windows OS");
    }
    var test = context.getRequiredTestClass();
    var bots = test.getPackageName().startsWith("org.openjdk.skara.bots.");
    return bots ? disabled("All bots tests are disabled on Windows") : enabled("Non-bots test");
}
 
private void assertResult(ConditionEvaluationResult result, boolean disabled, Matcher<String> matcher) {
	assertNotNull(result);

	if (disabled) {
		assertTrue(result.isDisabled());
	}
	else {
		assertFalse(result.isDisabled());
	}

	Optional<String> reason = result.getReason();
	assertTrue(reason.isPresent());
	assertThat(reason.get(), matcher);
}
 
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    boolean isAuthEnabled = Boolean.valueOf(TestProperties.get(StackInputParameterKeyEnum.ENABLE_SSL_AUTH));
    if (isAuthEnabled) {
        return ConditionEvaluationResult.enabled("Test enabled");
    }
    else {
        return ConditionEvaluationResult.disabled("Bdsql Sync Tests disabled as authentication is disabled");
    }
}
 
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    Optional<RequireSystemProperty> annotation = AnnotationSupport.findAnnotation(context.getElement(), RequireSystemProperty.class);
    if (annotation.isPresent()) {
        for (String propertyKey : annotation.get().value()) {
            String propertyValue = System.getProperty(propertyKey);
            if (propertyValue == null || propertyValue.isEmpty()) {
                return ConditionEvaluationResult.disabled(String.format("System property '%s' not set. Skipping test.", propertyKey));
            }
        }
        return ConditionEvaluationResult.enabled("All required system properties present. Continuing test.");
    }
    return ConditionEvaluationResult.enabled("No RequireSystemProperty annotation found. Continuing test.");
}
 
源代码23 项目: quarkus   文件: EnabledIfTest.java
private ConditionEvaluationResult map(EnabledIf annotation) {
    for (Class<? extends BooleanSupplier> type : annotation.value()) {
        try {
            if (!type.newInstance().getAsBoolean()) {
                return disabled(format("Condition %s is false", type.getName()));
            }
        } catch (InstantiationException | IllegalAccessException e) {
            return disabled(format("Unable to evaluate condition: %s", type.getName()));
        }
    }
    return enabled("All conditions match");
}
 
源代码24 项目: dekorate   文件: KubernetesExtension.java
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
  try {
    VersionInfo version = getKubernetesClient(context).getVersion();
    String message = "Found version:" + version.getMajor() + "." + version.getMinor();
    LOGGER.info(message);
    return ConditionEvaluationResult.enabled(message);
  } catch (Throwable t) {
    String reason = "Could not communicate with KubernetesExtension API server.";
    LOGGER.error(reason);
    return ConditionEvaluationResult.disabled(reason);
  }
}
 
源代码25 项目: testing-video   文件: GenerateVideoExtension.java
ConditionEvaluationResult isTypeEnabled(GenerateVideo gv) {
    Type type = gv.value();
    if (type == ALL) return ENABLED_TYPE;
    if (!GeneratorFactory.LOSSLESS && type == MAIN) return ENABLED_TYPE;
    if (GeneratorFactory.LOSSLESS && type == LOSSLESS) return ENABLED_TYPE;
    return DISABLED_TYPE;
}
 
源代码26 项目: junit5-extensions   文件: InjectExtensionsTest.java
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
  if (context.getRequiredTestMethod().getName().equals(testMethodToSkip)) {
    return ConditionEvaluationResult.disabled("Disabled by InjectableExtension.");
  }

  return ConditionEvaluationResult.enabled("");
}
 
源代码27 项目: junit5-extensions   文件: TestDisabler.java
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();
}
 
源代码28 项目: code-examples   文件: AssumeConnectionCondition.java
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
  Optional<AssumeConnection> annotation = findAnnotation(context.getElement(), AssumeConnection.class);
  if (annotation.isPresent()) {
    String uri = annotation.get().uri();
    ConnectionChecker checker = new ConnectionChecker(uri);
    if (!checker.connect()) {
      return ConditionEvaluationResult.disabled(String.format("Could not connect to '%s'. Skipping test!", uri));
    } else {
      return ConditionEvaluationResult.enabled(String.format("Successfully connected to '%s'. Continuing test!", uri));
    }
  }
  return ConditionEvaluationResult.enabled("No AssumeConnection annotation found. Continuing test.");
}
 
源代码29 项目: junit5-demo   文件: EnabledOnDayOfWeekCondition.java
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
	Optional<EnabledOnDayOfWeek> optional = findAnnotation(context.getElement(), EnabledOnDayOfWeek.class);

	if (!optional.isPresent()) {
		return ENABLED_BY_DEFAULT;
	}

	DayOfWeek today = LocalDate.now().getDayOfWeek();
	return Arrays.stream(optional.get().value()).anyMatch(today::equals) ? //
			enabled("Enabled on " + today) : //
			disabled("Disabled on " + today);
}
 
源代码30 项目: junit5-demo   文件: DisabledOnDayOfWeekCondition.java
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
	Optional<DisabledOnDayOfWeek> optional = findAnnotation(context.getElement(), DisabledOnDayOfWeek.class);

	if (!optional.isPresent()) {
		return ENABLED_BY_DEFAULT;
	}

	DayOfWeek today = LocalDate.now().getDayOfWeek();
	return Arrays.stream(optional.get().value()).anyMatch(today::equals) ? //
			disabled("Disabled on " + today) : //
			enabled("Enabled on " + today);
}
 
 类所在包
 同包方法