Java Code Examples for org.junit.platform.commons.support.AnnotationSupport#isAnnotated()

The following examples show how to use org.junit.platform.commons.support.AnnotationSupport#isAnnotated() . 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: BQTestExtension.java    From bootique with Apache License 2.0 6 votes vote down vote up
protected Predicate<Field> isRunnable() {
    return f -> {

        // provide diagnostics for misapplied or missing annotations
        // TODO: will it be actually more useful to throw instead of print a warning?
        if (AnnotationSupport.isAnnotated(f, BQApp.class)) {

            if (!BQRuntime.class.isAssignableFrom(f.getType())) {
                logger.warn(() -> "Field '" + f.getName() + "' is annotated with @BQRun but is not a BQRuntime. Ignoring...");
                return false;
            }

            if (!ReflectionUtils.isStatic(f)) {
                logger.warn(() -> "BQRuntime field '" + f.getName() + "' is annotated with @BQRun but is not static. Ignoring...");
                return false;
            }

            return true;
        }

        return false;
    };
}
 
Example 2
Source File: MicronautJunit5Extension.java    From micronaut-test with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
    final Optional<Object> testInstance = extensionContext.getTestInstance();
    if (testInstance.isPresent()) {

        final Class<?> requiredTestClass = extensionContext.getRequiredTestClass();
        if (applicationContext.containsBean(requiredTestClass)) {
            return ConditionEvaluationResult.enabled("Test bean active");
        } else {

            final boolean hasBeanDefinition = isTestSuiteBeanPresent(requiredTestClass);
            if (!hasBeanDefinition) {
                throw new TestInstantiationException(MISCONFIGURED_MESSAGE);
            } else {
                return ConditionEvaluationResult.disabled(DISABLED_MESSAGE);
            }

        }
    } else {
        final Class<?> testClass = extensionContext.getRequiredTestClass();
        if (AnnotationSupport.isAnnotated(testClass, MicronautTest.class)) {
            return ConditionEvaluationResult.enabled("Test bean active");
        } else {
            return ConditionEvaluationResult.disabled(DISABLED_MESSAGE);
        }
    }
}
 
Example 3
Source File: PluggableFlowableExtension.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void afterEach(ExtensionContext context) throws Exception {
    try {
        super.afterEach(context);
    } finally {
        if (AnnotationSupport.isAnnotated(context.getRequiredTestClass(), EnableVerboseExecutionTreeLogging.class)) {
            swapCommandInvoker(getProcessEngine(context), false);
        }
    }
}
 
Example 4
Source File: PluggableFlowableExtension.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
protected ProcessEngine getProcessEngine(ExtensionContext context) {
    ProcessEngine processEngine = getStore(context).getOrComputeIfAbsent(PROCESS_ENGINE, key -> initializeProcessEngine(), ProcessEngine.class);

    // Enable verbose execution tree debugging if needed
    Class<?> testClass = context.getRequiredTestClass();
    if (AnnotationSupport.isAnnotated(testClass, EnableVerboseExecutionTreeLogging.class)) {
        swapCommandInvoker(processEngine, true);
    }
    return processEngine;
}
 
Example 5
Source File: TestcontainersExtension.java    From testcontainers-java with MIT License 5 votes vote down vote up
private static Predicate<Field> isContainer() {
    return field -> {
        boolean isAnnotatedWithContainer = AnnotationSupport.isAnnotated(field, Container.class);
        if (isAnnotatedWithContainer) {
            boolean isStartable = Startable.class.isAssignableFrom(field.getType());

            if (!isStartable) {
                throw new ExtensionConfigurationException(String.format("FieldName: %s does not implement Startable", field.getName()));
            }
            return true;
        }
        return false;
    };
}
 
Example 6
Source File: TestParametersSupport.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("checkstyle:ReturnCount")
public boolean supportsParameter(final ParameterContext parameterContext,
                                 final ExtensionContext extensionContext) throws ParameterResolutionException {
    final Parameter parameter = parameterContext.getParameter();
    if (parameter.getAnnotations().length > 0) {
        if (AnnotationSupport.isAnnotated(parameter, Jit.class)) {
            return true;
        } else if (!isQualifierAnnotation(parameter.getAnnotations())) {
            // if any other annotation declared on the parameter - skip it (possibly other extension's parameter)
            return false;
        }
    }

    final Class<?> type = parameter.getType();
    if (Application.class.isAssignableFrom(type) || Configuration.class.isAssignableFrom(type)) {
        // special case when exact app or configuration class used
        return true;
    } else {
        for (Class<?> cls : supportedClasses) {
            if (type.equals(cls)) {
                return true;
            }
        }
    }

    // declared guice binding (by class only)
    return getInjector(extensionContext)
            .map(it -> it.getExistingBinding(getKey(parameter)) != null)
            .orElse(false);
}
 
Example 7
Source File: TestParametersSupport.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
private Key<?> getKey(final Parameter parameter) {
    final Key<?> key;
    if (parameter.getAnnotations().length > 0
            && !AnnotationSupport.isAnnotated(parameter, Jit.class)) {
        // qualified bean
        key = Key.get(parameter.getParameterizedType(), parameter.getAnnotations()[0]);
    } else {
        key = Key.get(parameter.getParameterizedType());
    }
    return key;
}
 
Example 8
Source File: RestClientBuilder.java    From microshed-testing with Apache License 2.0 4 votes vote down vote up
private static String locateApplicationPath(Class<?> clazz) {
    String resourcePackage = clazz.getPackage().getName();

    // If the rest client directly extends Application, look for ApplicationPath on it
    if (AnnotationSupport.isAnnotated(clazz, ApplicationPath.class))
        return AnnotationSupport.findAnnotation(clazz, ApplicationPath.class).get().value();

    // First check for a javax.ws.rs.core.Application in the same package as the resource
    List<Class<?>> appClasses = ReflectionSupport.findAllClassesInPackage(resourcePackage,
                                                                          c -> Application.class.isAssignableFrom(c) &&
                                                                               AnnotationSupport.isAnnotated(c, ApplicationPath.class),
                                                                          n -> true);
    if (appClasses.size() == 0) {
        LOG.debug("no classes implementing Application found in pkg: " + resourcePackage);
        // If not found, check under the 3rd package, so com.foo.bar.*
        // Classpath scanning can be expensive, so we jump straight to the 3rd package from root instead
        // of recursing up one package at a time and scanning the entire CP for each step
        String[] pkgs = resourcePackage.split("\\.");
        if (pkgs.length > 3) {
            String checkPkg = pkgs[0] + '.' + pkgs[1] + '.' + pkgs[2];
            LOG.debug("checking in pkg: " + checkPkg);
            appClasses = ReflectionSupport.findAllClassesInPackage(checkPkg,
                                                                   c -> Application.class.isAssignableFrom(c) &&
                                                                        AnnotationSupport.isAnnotated(c, ApplicationPath.class),
                                                                   n -> true);
        }
    }

    if (appClasses.size() == 0) {
        LOG.info("No classes implementing 'javax.ws.rs.core.Application' found on classpath to set as context root for " + clazz +
                 ". Defaulting context root to '/'");
        return "";
    }

    Class<?> selectedClass = appClasses.stream()
                    .sorted((c1, c2) -> c1.getName().compareTo(c2.getName()))
                    .findFirst()
                    .get();
    ApplicationPath appPath = AnnotationSupport.findAnnotation(selectedClass, ApplicationPath.class).get();
    if (appClasses.size() > 1) {
        LOG.warn("Found multiple classes implementing 'javax.ws.rs.core.Application' on classpath: " + appClasses +
                 ". Setting context root to the first class discovered (" + selectedClass.getCanonicalName() + ") with path: " +
                 appPath.value());
    }
    LOG.debug("Using ApplicationPath of '" + appPath.value() + "'");
    return appPath.value();
}
 
Example 9
Source File: TestParametersSupport.java    From dropwizard-guicey with MIT License 4 votes vote down vote up
private boolean isQualifierAnnotation(final Annotation... annotations) {
    final Annotation ann = annotations[0];
    return annotations.length == 1
            && (AnnotationSupport.isAnnotated(ann.annotationType(), Qualifier.class)
            || AnnotationSupport.isAnnotated(ann.annotationType(), BindingAnnotation.class));
}