All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.stargate.it.storage.BundleAvailableCondition Maven / Gradle / Ivy

There is a newer version: 2.1.0-BETA-16
Show newest version
package io.stargate.it.storage;

import java.io.File;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Optional;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.commons.support.AnnotationSupport;
import org.junit.platform.commons.support.SearchOption;

/**
 * A condition that implements the logic for {@link IfBundleAvailable}. It checks to make sure a
 * bundle exists so that tests can be run against it.
 */
public class BundleAvailableCondition implements ExecutionCondition {
  @Override
  public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    IfBundleAvailable bundleSkip =
        context
            .getElement()
            .flatMap(
                element -> {
                  if (element instanceof Class) {
                    return AnnotationSupport.findAnnotation(
                        (Class) element,
                        IfBundleAvailable.class,
                        SearchOption.INCLUDE_ENCLOSING_CLASSES);
                  } else if (element instanceof Method) {
                    return AnnotationSupport.findAnnotation(
                        ((Method) element).getDeclaringClass(),
                        IfBundleAvailable.class,
                        SearchOption.INCLUDE_ENCLOSING_CLASSES);
                  } else {
                    return Optional.empty();
                  }
                })
            .orElseThrow(
                () ->
                    new IllegalStateException(
                        "Can not locate the @IfBundleAvailable annotation on the annotated element."));

    String bundleName = bundleSkip.bundleName();
    File[] files = StargateExtension.LIB_DIR.listFiles();
    return Arrays.stream(files)
        .filter(f -> f.getName().startsWith(bundleName))
        .filter(f -> f.getName().endsWith(".jar"))
        .findFirst()
        .map(f -> ConditionEvaluationResult.enabled(String.format("Found bundle '%s'", bundleName)))
        .orElse(
            ConditionEvaluationResult.disabled(
                String.format("Unable to find bundle '%s'", bundleName)));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy