Search in sources :

Example 1 with ActiveProfiles

use of org.springframework.test.context.ActiveProfiles in project spring-framework by spring-projects.

the class ActiveProfilesUtils method resolveActiveProfiles.

/**
	 * Resolve <em>active bean definition profiles</em> for the supplied {@link Class}.
	 * <p>Note that the {@link ActiveProfiles#inheritProfiles inheritProfiles} flag of
	 * {@link ActiveProfiles @ActiveProfiles} will be taken into consideration.
	 * Specifically, if the {@code inheritProfiles} flag is set to {@code true}, profiles
	 * defined in the test class will be merged with those defined in superclasses.
	 * @param testClass the class for which to resolve the active profiles (must not be
	 * {@code null})
	 * @return the set of active profiles for the specified class, including active
	 * profiles from superclasses if appropriate (never {@code null})
	 * @see ActiveProfiles
	 * @see ActiveProfilesResolver
	 * @see org.springframework.context.annotation.Profile
	 */
static String[] resolveActiveProfiles(Class<?> testClass) {
    Assert.notNull(testClass, "Class must not be null");
    final List<String[]> profileArrays = new ArrayList<>();
    Class<ActiveProfiles> annotationType = ActiveProfiles.class;
    AnnotationDescriptor<ActiveProfiles> descriptor = MetaAnnotationUtils.findAnnotationDescriptor(testClass, annotationType);
    if (descriptor == null && logger.isDebugEnabled()) {
        logger.debug(String.format("Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]", annotationType.getName(), testClass.getName()));
    }
    while (descriptor != null) {
        Class<?> rootDeclaringClass = descriptor.getRootDeclaringClass();
        Class<?> declaringClass = descriptor.getDeclaringClass();
        ActiveProfiles annotation = descriptor.synthesizeAnnotation();
        if (logger.isTraceEnabled()) {
            logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s]", annotation, declaringClass.getName()));
        }
        Class<? extends ActiveProfilesResolver> resolverClass = annotation.resolver();
        if (ActiveProfilesResolver.class == resolverClass) {
            resolverClass = DefaultActiveProfilesResolver.class;
        }
        ActiveProfilesResolver resolver;
        try {
            resolver = BeanUtils.instantiateClass(resolverClass, ActiveProfilesResolver.class);
        } catch (Exception ex) {
            String msg = String.format("Could not instantiate ActiveProfilesResolver of type [%s] " + "for test class [%s]", resolverClass.getName(), rootDeclaringClass.getName());
            logger.error(msg);
            throw new IllegalStateException(msg, ex);
        }
        String[] profiles = resolver.resolve(rootDeclaringClass);
        if (profiles == null) {
            String msg = String.format("ActiveProfilesResolver [%s] returned a null array of bean definition profiles", resolverClass.getName());
            logger.error(msg);
            throw new IllegalStateException(msg);
        }
        profileArrays.add(profiles);
        descriptor = (annotation.inheritProfiles() ? MetaAnnotationUtils.findAnnotationDescriptor(rootDeclaringClass.getSuperclass(), annotationType) : null);
    }
    // Reverse the list so that we can traverse "down" the hierarchy.
    Collections.reverse(profileArrays);
    final Set<String> activeProfiles = new LinkedHashSet<>();
    for (String[] profiles : profileArrays) {
        for (String profile : profiles) {
            if (StringUtils.hasText(profile)) {
                activeProfiles.add(profile.trim());
            }
        }
    }
    return StringUtils.toStringArray(activeProfiles);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ActiveProfilesResolver(org.springframework.test.context.ActiveProfilesResolver) ArrayList(java.util.ArrayList) ActiveProfiles(org.springframework.test.context.ActiveProfiles)

Example 2 with ActiveProfiles

use of org.springframework.test.context.ActiveProfiles in project spring-framework by spring-projects.

the class DefaultActiveProfilesResolver method resolve.

/**
	 * Resolve the <em>bean definition profiles</em> for the given {@linkplain
	 * Class test class} based on profiles configured declaratively via
	 * {@link ActiveProfiles#profiles} or {@link ActiveProfiles#value}.
	 * @param testClass the test class for which the profiles should be resolved;
	 * never {@code null}
	 * @return the list of bean definition profiles to use when loading the
	 * {@code ApplicationContext}; never {@code null}
	 */
@Override
public String[] resolve(Class<?> testClass) {
    Assert.notNull(testClass, "Class must not be null");
    final Set<String> activeProfiles = new LinkedHashSet<>();
    Class<ActiveProfiles> annotationType = ActiveProfiles.class;
    AnnotationDescriptor<ActiveProfiles> descriptor = findAnnotationDescriptor(testClass, annotationType);
    if (descriptor == null) {
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]", annotationType.getName(), testClass.getName()));
        }
    } else {
        Class<?> declaringClass = descriptor.getDeclaringClass();
        ActiveProfiles annotation = descriptor.synthesizeAnnotation();
        if (logger.isTraceEnabled()) {
            logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s].", annotation, declaringClass.getName()));
        }
        for (String profile : annotation.profiles()) {
            if (StringUtils.hasText(profile)) {
                activeProfiles.add(profile.trim());
            }
        }
    }
    return StringUtils.toStringArray(activeProfiles);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ActiveProfiles(org.springframework.test.context.ActiveProfiles)

Aggregations

LinkedHashSet (java.util.LinkedHashSet)2 ActiveProfiles (org.springframework.test.context.ActiveProfiles)2 ArrayList (java.util.ArrayList)1 ActiveProfilesResolver (org.springframework.test.context.ActiveProfilesResolver)1