Example usage for org.springframework.core.annotation AnnotationUtils findAnnotation

List of usage examples for org.springframework.core.annotation AnnotationUtils findAnnotation

Introduction

In this page you can find the example usage for org.springframework.core.annotation AnnotationUtils findAnnotation.

Prototype

@Nullable
public static <A extends Annotation> A findAnnotation(Class<?> clazz, @Nullable Class<A> annotationType) 

Source Link

Document

Find a single Annotation of annotationType on the supplied Class , traversing its interfaces, annotations, and superclasses if the annotation is not directly present on the given class itself.

Usage

From source file:software.coolstuff.springframework.owncloud.service.impl.local.OwncloudLocalFileTestExecutionListener.java

@Override
public void beforeTestClass(TestContext testContext) throws Exception {
    if (isTestClassAssignableFromOwncloudLocalFileTest(testContext)) {
        Class<?> testClass = testContext.getTestClass();
        ActiveProfiles activeProfiles = AnnotationUtils.findAnnotation(testClass, ActiveProfiles.class);
        OwncloudProperties owncloudProperties = loadProperties(activeProfiles.value());
        if (StringUtils.startsWith(owncloudProperties.getLocation(), "file:")) {
            copyResource(owncloudProperties);
        }/*w ww .  j  a v  a2  s  .c o m*/
    }
}

From source file:software.coolstuff.springframework.owncloud.service.impl.local.OwncloudLocalFileTestExecutionListener.java

@Override
public void afterTestMethod(TestContext testContext) throws Exception {
    if (isTestClassAssignableFromOwncloudLocalFileTest(testContext)) {
        ApplicationContext applicationContext = testContext.getApplicationContext();

        Class<?> testClass = testContext.getTestClass();
        Method testMethod = testContext.getTestMethod();

        DisposableBean localDataService = applicationContext.getBean(OwncloudLocalUserDataServiceImpl.class);
        localDataService.destroy();/*from ww  w .j a  va 2  s.c  om*/

        ResourceLoader resourceLoader = applicationContext;
        OwncloudProperties properties = applicationContext.getBean(OwncloudProperties.class);
        Resource target = resourceLoader.getResource(properties.getLocation());

        boolean hasSpecificResourceTest = false;
        for (Method method : testContext.getTestClass().getMethods()) {
            // is this Method annotated by @CompareResourceAfter
            CompareResourceAfter compareResourceAfter = AnnotationUtils.findAnnotation(method,
                    CompareResourceAfter.class);
            if (compareResourceAfter == null
                    || !StringUtils.equals(compareResourceAfter.value(), testMethod.getName())) {
                continue;
            }

            // a Method annotated by @Test cannot also be annotated by
            // @CompareResourceAfter
            if (AnnotationUtils.findAnnotation(method, Test.class) != null) {
                log.warn("Method {} of Class {} cannot be annotated by {} and {}", method.getName(),
                        testClass.getName(), CompareResourceAfter.class, Test.class);
                continue;
            }

            // the @CompareResourceAfter annotated Method must have exactly 2
            // Parameters of Type org.springframework.core.io.Resource
            if (method.getParameterCount() != 1) {
                log.warn("Method {} of Class {} is annotated by {} but has {} Parameters instead of 1",
                        method.getName(), testClass.getName(), CompareResourceAfter.class.getName(),
                        method.getParameterCount());
                continue;
            }
            boolean correctParameterTypes = true;
            for (Class<?> parameterClass : method.getParameterTypes()) {
                correctParameterTypes = correctParameterTypes
                        && Resource.class.isAssignableFrom(parameterClass);
            }
            if (!correctParameterTypes) {
                log.warn("Method {} of Class {} (annotated by {}) must have 1 Parameter of Type {}",
                        method.getName(), testClass.getName(), CompareResourceAfter.class.getName(),
                        Resource.class.getName());
                continue;
            }

            log.debug("Call the Resource Comparsion Method {} on Class {}", method.getName(),
                    testClass.getName());
            hasSpecificResourceTest = true;
            try {
                method.invoke(testContext.getTestInstance(), target);
            } catch (InvocationTargetException e) {
                if (e.getCause() instanceof Exception) {
                    throw (Exception) e.getCause();
                }
                throw (Error) e.getCause();
            }
        }

        if (!hasSpecificResourceTest && ((OwncloudLocalFileTest) testContext.getTestInstance())
                .isCheckAllResourcesAgainstOriginal()) {
            compareResourcesWithOriginalSource(resourceLoader, target);
        }
    }
}

From source file:software.coolstuff.springframework.owncloud.service.impl.local.OwncloudLocalResourceServiceCopyWebdavDirectoryTestExecutionListener.java

private void copyDirectory(TestContext testContext) throws IOException {
    Class<?> testClass = testContext.getTestClass();
    ActiveProfiles activeProfiles = AnnotationUtils.findAnnotation(testClass, ActiveProfiles.class);
    getResourceLocation(activeProfiles.value()).ifPresent(this::copyDirectory);
}

From source file:software.coolstuff.springframework.owncloud.service.impl.local.OwncloudLocalResourceServiceCopyWebdavDirectoryTestExecutionListener.java

private void cleanDirectory(TestContext testContext) throws IOException {
    Class<?> testClass = testContext.getTestClass();
    ActiveProfiles activeProfiles = AnnotationUtils.findAnnotation(testClass, ActiveProfiles.class);
    getResourceLocation(activeProfiles.value()).ifPresent(this::cleanDirectory);
}