Java Code Examples for org.junit.jupiter.api.extension.ConditionEvaluationResult#disabled()

The following examples show how to use org.junit.jupiter.api.extension.ConditionEvaluationResult#disabled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaVersionCheckExtension.java    From sarl with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example 2
Source File: DisabledOnQAEnvironmentExtension.java    From tutorials with MIT License 6 votes vote down vote up
@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");
}
 
Example 3
Source File: EnabledOnGitVersionCondition.java    From gocd with Apache License 2.0 6 votes vote down vote up
@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.");
}
 
Example 4
Source File: ServicePresentCondition.java    From dekorate with Apache License 2.0 6 votes vote down vote up
@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.");
  }
}
 
Example 5
Source File: DisabledOnWeekday.java    From JUnit-5-Quick-Start-Guide-and-Framework-Support with MIT License 6 votes vote down vote up
@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 >:(");
}
 
Example 6
Source File: DisabledOnEnvironmentCondition.java    From tutorials with MIT License 6 votes vote down vote up
@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);
}
 
Example 7
Source File: OsCondition.java    From Mastering-Software-Testing-with-JUnit-5 with MIT License 6 votes vote down vote up
@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;
}
 
Example 8
Source File: OpenShiftOnlyCondition.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
    KubeClusterResource clusterResource = KubeClusterResource.getInstance();

    if (clusterResource.cluster() instanceof OpenShift || clusterResource.cluster() instanceof Minishift) {
        return ConditionEvaluationResult.enabled("Test is enabled");
    } else {
        LOGGER.info("{} is @OpenShiftOnly, but the running cluster is not OpenShift: Ignoring {}",
                extensionContext.getDisplayName(),
                extensionContext.getDisplayName()
        );
        return ConditionEvaluationResult.disabled("Test is disabled");
    }
}
 
Example 9
Source File: AssumeKubernetesCondition.java    From enmasse with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    Optional<Kubernetes> annotation = findAnnotation(context.getElement(), Kubernetes.class);
    if (annotation.isPresent()) {
        ClusterType cluster = annotation.get().type();
        MultinodeCluster multinode = annotation.get().multinode();
        if (!io.enmasse.systemtest.platform.Kubernetes.getInstance().getCluster().toString().equals(cluster.toString().toLowerCase()) &&
                (multinode == MultinodeCluster.WHATEVER || multinode == io.enmasse.systemtest.platform.Kubernetes.getInstance().isClusterMultinode())) {
            return ConditionEvaluationResult.disabled("Test is not supported on current cluster");
        } else {
            return ConditionEvaluationResult.enabled("Test is supported on current cluster");
        }
    }
    return ConditionEvaluationResult.enabled("No rule set, test is enabled");
}
 
Example 10
Source File: DisabledOnMonday.java    From JUnit-5-Quick-Start-Guide-and-Framework-Support with MIT License 5 votes vote down vote up
@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 >:(");
}
 
Example 11
Source File: AssumeConnectionCondition.java    From code-examples with MIT License 5 votes vote down vote up
@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.");
}
 
Example 12
Source File: InjectExtensionsTest.java    From junit5-extensions with MIT License 5 votes vote down vote up
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
  if (context.getRequiredTestMethod().getName().equals(testMethodToSkip)) {
    return ConditionEvaluationResult.disabled("Disabled by InjectableExtension.");
  }

  return ConditionEvaluationResult.enabled("");
}
 
Example 13
Source File: IamAuthCondition.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    if (IS_IAM_ENABLED) {
        return ConditionEvaluationResult.disabled("Test is not supported when using IAM.");
    } else {
        return ConditionEvaluationResult.enabled("Test enabled.");
    }
}
 
Example 14
Source File: TestcontainersExtension.java    From testcontainers-java with MIT License 5 votes vote down vote up
private ConditionEvaluationResult evaluate(Testcontainers testcontainers) {
    if (testcontainers.disabledWithoutDocker()) {
        if (isDockerAvailable()) {
            return ConditionEvaluationResult.enabled("Docker is available");
        }
        return ConditionEvaluationResult.disabled("disabledWithoutDocker is true and Docker is not available");
    }
    return ConditionEvaluationResult.enabled("disabledWithoutDocker is false");
}
 
Example 15
Source File: KubernetesExtension.java    From dekorate with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example 16
Source File: RequireSystemPropertyExists.java    From Lavalink-Client with MIT License 5 votes vote down vote up
@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.");
}
 
Example 17
Source File: EnvironmentalContext.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context) {
    return isActive() ? ConditionEvaluationResult.enabled("provider is active")
            : ConditionEvaluationResult.disabled("provider is disabled");
}
 
Example 18
Source File: DisabledInHeadlessGraphicsEnvironment.java    From triplea with GNU General Public License v3.0 4 votes vote down vote up
@VisibleForTesting
static ConditionEvaluationResult evaluateExecutionCondition(final boolean headless) {
  return headless
      ? ConditionEvaluationResult.disabled("Test disabled in headless graphics environment")
      : ConditionEvaluationResult.enabled(null);
}
 
Example 19
Source File: CustomResourceCondition.java    From dekorate with Apache License 2.0 4 votes vote down vote up
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
 Optional<OnCustomResourcePresentCondition> annotation = context.getElement().map(e -> e.getAnnotation(OnCustomResourcePresentCondition.class));
 if (!annotation.isPresent()) {
  return ConditionEvaluationResult.enabled("Condition not found!");
 }
 OnCustomResourcePresentCondition condition = annotation.get();
  try {
    String apiVersion = condition.apiVersion();
    String kind = condition.kind();
    String plural = Strings.isNotNullOrEmpty(condition.plural()) ? condition.plural() : Pluralize.FUNCTION.apply(kind).toLowerCase();
    String name = condition.name();
    String namespace = condition.namespace();

    KubernetesClient client = getKubernetesClient(context);
    Config config = client.getConfiguration();
    OkHttpClient http = client.adapt(OkHttpClient.class);

    List<String> parts = new ArrayList<>();
    parts.add(config.getMasterUrl());
    parts.add("apis");
    parts.add(apiVersion);

    if (Strings.isNotNullOrEmpty(namespace)) {
      parts.add("namespaces");
      parts.add(namespace);
    }

    parts.add(plural);
    if (Strings.isNotNullOrEmpty(name)) {
      parts.add(name);
    }
    parts.add(plural);
    String requestUrl = URLUtils.join(parts.stream().toArray(s->new String[s]));
    Request request = new Request.Builder().get().url(requestUrl).build();
    Response response = http.newCall(request).execute();

    if (!response.isSuccessful()) {
      return ConditionEvaluationResult.disabled("Could not lookup custom resource.");
    }


    //TODO: Add support for cases where name() is empty. In this case the result will be a list.
    //We need to check if empty.
    return ConditionEvaluationResult.enabled("Found resource with apiVersion:" + apiVersion + " kind:" + kind + " namespace: " + (Strings.isNullOrEmpty(namespace) ? "any" : namespace) + " name: " + (Strings.isNullOrEmpty(name) ? "any" : name));

  } catch (Throwable t) {
    return ConditionEvaluationResult.disabled("Could not lookup for service.");
  }
}
 
Example 20
Source File: CassandraExtension.java    From calcite with Apache License 2.0 3 votes vote down vote up
/**
 * Whether to run this test.
 * <p>Enabled by default, unless explicitly disabled
 * from command line ({@code -Dcalcite.test.cassandra=false}) or running on incompatible JDK
 * version (see below).
 *
 * <p>As of this wiring Cassandra 4.x is not yet released and we're using 3.x
 * (which fails on JDK11+). All cassandra tests will be skipped if
 * running on JDK11+.
 *
 * @see <a href="https://issues.apache.org/jira/browse/CASSANDRA-9608">CASSANDRA-9608</a>
 * @return {@code true} if test is compatible with current environment,
 *         {@code false} otherwise
 */
@Override public ConditionEvaluationResult evaluateExecutionCondition(
    final ExtensionContext context) {
  boolean enabled = CalciteSystemProperty.TEST_CASSANDRA.value();
  Bug.upgrade("remove JDK version check once current adapter supports Cassandra 4.x");
  boolean compatibleJdk = TestUtil.getJavaMajorVersion() < 11;
  if (enabled && compatibleJdk) {
    return ConditionEvaluationResult.enabled("Cassandra enabled");
  }
  return ConditionEvaluationResult.disabled("Cassandra tests disabled");
}