List of usage examples for org.springframework.core.annotation AnnotatedElementUtils findMergedAnnotation
@Nullable public static <A extends Annotation> A findMergedAnnotation(AnnotatedElement element, Class<A> annotationType)
From source file:org.springframework.test.annotation.ProfileValueUtils.java
/** * Determine if the supplied {@code testMethod} is <em>enabled</em> in * the current environment, as specified by the {@link IfProfileValue * @IfProfileValue} annotation, which may be declared on the test * method itself or at the class level. Class-level usage overrides * method-level usage.//ww w . j a v a2s. co m * <p>Defaults to {@code true} if no {@link IfProfileValue * @IfProfileValue} annotation is declared. * @param profileValueSource the ProfileValueSource to use to determine if * the test is enabled * @param testMethod the test method * @param testClass the test class * @return {@code true} if the test is <em>enabled</em> in the current * environment */ public static boolean isTestEnabledInThisEnvironment(ProfileValueSource profileValueSource, Method testMethod, Class<?> testClass) { IfProfileValue ifProfileValue = AnnotatedElementUtils.findMergedAnnotation(testClass, IfProfileValue.class); boolean classLevelEnabled = isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue); if (classLevelEnabled) { ifProfileValue = AnnotatedElementUtils.findMergedAnnotation(testMethod, IfProfileValue.class); return isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue); } return false; }
From source file:org.springframework.test.context.junit.jupiter.AbstractExpressionEvaluatingCondition.java
private static <A extends Annotation> Optional<A> findMergedAnnotation(AnnotatedElement element, Class<A> annotationType) { return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(element, annotationType)); }
From source file:org.springframework.test.context.junit.jupiter.DisabledIfCondition.java
private static <A extends Annotation> Optional<A> findMergedAnnotation(AnnotatedElement element, Class<A> annotationType) { return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(element, annotationType)); }
From source file:org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener.java
/** * Perform the actual work for {@link #beforeTestMethod} and {@link #afterTestMethod} * by dirtying the context if appropriate (i.e., according to the required modes). * @param testContext the test context whose application context should * potentially be marked as dirty; never {@code null} * @param requiredMethodMode the method mode required for a context to * be marked dirty in the current phase; never {@code null} * @param requiredClassMode the class mode required for a context to * be marked dirty in the current phase; never {@code null} * @throws Exception allows any exception to propagate * @since 4.2/*ww w . j av a 2s . com*/ * @see #dirtyContext */ protected void beforeOrAfterTestMethod(TestContext testContext, MethodMode requiredMethodMode, ClassMode requiredClassMode) throws Exception { Assert.notNull(testContext, "TestContext must not be null"); Assert.notNull(requiredMethodMode, "requiredMethodMode must not be null"); Assert.notNull(requiredClassMode, "requiredClassMode must not be null"); Class<?> testClass = testContext.getTestClass(); Method testMethod = testContext.getTestMethod(); Assert.notNull(testClass, "The test class of the supplied TestContext must not be null"); Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null"); DirtiesContext methodAnn = AnnotatedElementUtils.findMergedAnnotation(testMethod, DirtiesContext.class); DirtiesContext classAnn = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class); boolean methodAnnotated = (methodAnn != null); boolean classAnnotated = (classAnn != null); MethodMode methodMode = (methodAnnotated ? methodAnn.methodMode() : null); ClassMode classMode = (classAnnotated ? classAnn.classMode() : null); if (logger.isDebugEnabled()) { String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After"); logger.debug(String.format( "%s test method: context %s, class annotated with @DirtiesContext [%s] " + "with mode [%s], method annotated with @DirtiesContext [%s] with mode [%s].", phase, testContext, classAnnotated, classMode, methodAnnotated, methodMode)); } if ((methodMode == requiredMethodMode) || (classMode == requiredClassMode)) { HierarchyMode hierarchyMode = (methodAnnotated ? methodAnn.hierarchyMode() : classAnn.hierarchyMode()); dirtyContext(testContext, hierarchyMode); } }
From source file:org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener.java
/** * Perform the actual work for {@link #beforeTestClass} and {@link #afterTestClass} * by dirtying the context if appropriate (i.e., according to the required mode). * @param testContext the test context whose application context should * potentially be marked as dirty; never {@code null} * @param requiredClassMode the class mode required for a context to * be marked dirty in the current phase; never {@code null} * @throws Exception allows any exception to propagate * @since 4.2//from w w w . ja v a 2 s .c o m * @see #dirtyContext */ protected void beforeOrAfterTestClass(TestContext testContext, ClassMode requiredClassMode) throws Exception { Assert.notNull(testContext, "TestContext must not be null"); Assert.notNull(requiredClassMode, "requiredClassMode must not be null"); Class<?> testClass = testContext.getTestClass(); Assert.notNull(testClass, "The test class of the supplied TestContext must not be null"); DirtiesContext dirtiesContext = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class); boolean classAnnotated = (dirtiesContext != null); ClassMode classMode = (classAnnotated ? dirtiesContext.classMode() : null); if (logger.isDebugEnabled()) { String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After"); logger.debug(String.format( "%s test class: context %s, class annotated with @DirtiesContext [%s] with mode [%s].", phase, testContext, classAnnotated, classMode)); } if (classMode == requiredClassMode) { dirtyContext(testContext, dirtiesContext.hierarchyMode()); } }
From source file:org.springframework.test.context.transaction.TransactionalTestExecutionListener.java
/** * Determine whether or not to rollback transactions by default for the * supplied {@linkplain TestContext test context}. * <p>Supports {@link Rollback @Rollback} or {@link Commit @Commit} at the * class-level.//from w w w.ja v a 2 s. c o m * @param testContext the test context for which the default rollback flag * should be retrieved * @return the <em>default rollback</em> flag for the supplied test context * @throws Exception if an error occurs while determining the default rollback flag */ protected final boolean isDefaultRollback(TestContext testContext) throws Exception { Class<?> testClass = testContext.getTestClass(); Rollback rollback = AnnotatedElementUtils.findMergedAnnotation(testClass, Rollback.class); boolean rollbackPresent = (rollback != null); if (rollbackPresent) { boolean defaultRollback = rollback.value(); if (logger.isDebugEnabled()) { logger.debug(String.format("Retrieved default @Rollback(%s) for test class [%s].", defaultRollback, testClass.getName())); } return defaultRollback; } // else return true; }
From source file:org.springframework.test.context.transaction.TransactionalTestExecutionListener.java
/** * Determine whether or not to rollback transactions for the supplied * {@linkplain TestContext test context} by taking into consideration the * {@linkplain #isDefaultRollback(TestContext) default rollback} flag and a * possible method-level override via the {@link Rollback @Rollback} * annotation.//from www . ja v a2 s. c o m * @param testContext the test context for which the rollback flag * should be retrieved * @return the <em>rollback</em> flag for the supplied test context * @throws Exception if an error occurs while determining the rollback flag */ protected final boolean isRollback(TestContext testContext) throws Exception { boolean rollback = isDefaultRollback(testContext); Rollback rollbackAnnotation = AnnotatedElementUtils.findMergedAnnotation(testContext.getTestMethod(), Rollback.class); if (rollbackAnnotation != null) { boolean rollbackOverride = rollbackAnnotation.value(); if (logger.isDebugEnabled()) { logger.debug(String.format( "Method-level @Rollback(%s) overrides default rollback [%s] for test context %s.", rollbackOverride, rollback, testContext)); } rollback = rollbackOverride; } else { if (logger.isDebugEnabled()) { logger.debug(String.format( "No method-level @Rollback override: using default rollback [%s] for test context %s.", rollback, testContext)); } } return rollback; }
From source file:org.springframework.transaction.event.ApplicationListenerMethodTransactionalAdapter.java
public ApplicationListenerMethodTransactionalAdapter(String beanName, Class<?> targetClass, Method method) { super(beanName, targetClass, method); this.annotation = AnnotatedElementUtils.findMergedAnnotation(method, TransactionalEventListener.class); if (this.annotation == null) { throw new IllegalStateException("No TransactionalEventListener annotation found on '" + method + "'"); }//from w w w.ja v a 2 s. c om }
From source file:org.springframework.web.method.HandlerMethod.java
private void evaluateResponseStatus() { ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class); if (annotation == null) { annotation = AnnotatedElementUtils.findMergedAnnotation(getBeanType(), ResponseStatus.class); }// w w w. j a v a 2 s.com if (annotation != null) { this.responseStatus = annotation.code(); this.responseStatusReason = annotation.reason(); } }
From source file:org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.java
private static String getTypeRequestMapping(Class<?> controllerType) { Assert.notNull(controllerType, "'controllerType' must not be null"); RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(controllerType, RequestMapping.class); if (requestMapping == null) { return "/"; }// w w w. j av a2s .c o m String[] paths = requestMapping.path(); if (ObjectUtils.isEmpty(paths) || StringUtils.isEmpty(paths[0])) { return "/"; } if (paths.length > 1 && logger.isWarnEnabled()) { logger.warn("Multiple paths on controller " + controllerType.getName() + ", using first one"); } return paths[0]; }