001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.context.support;
018
019import java.lang.reflect.Modifier;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025
026import org.springframework.context.annotation.Configuration;
027import org.springframework.core.annotation.AnnotatedElementUtils;
028import org.springframework.test.context.SmartContextLoader;
029import org.springframework.util.Assert;
030import org.springframework.util.ClassUtils;
031
032/**
033 * Utility methods for {@link SmartContextLoader SmartContextLoaders} that deal
034 * with annotated classes (e.g., {@link Configuration @Configuration} classes).
035 *
036 * @author Sam Brannen
037 * @since 3.2
038 */
039public abstract class AnnotationConfigContextLoaderUtils {
040
041        private static final Log logger = LogFactory.getLog(AnnotationConfigContextLoaderUtils.class);
042
043
044        /**
045         * Detect the default configuration classes for the supplied test class.
046         * <p>The returned class array will contain all static nested classes of
047         * the supplied class that meet the requirements for {@code @Configuration}
048         * class implementations as specified in the documentation for
049         * {@link Configuration @Configuration}.
050         * <p>The implementation of this method adheres to the contract defined in the
051         * {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}
052         * SPI. Specifically, this method uses introspection to detect default
053         * configuration classes that comply with the constraints required of
054         * {@code @Configuration} class implementations. If a potential candidate
055         * configuration class does not meet these requirements, this method will log a
056         * debug message, and the potential candidate class will be ignored.
057         * @param declaringClass the test class that declared {@code @ContextConfiguration}
058         * @return an array of default configuration classes, potentially empty but
059         * never {@code null}
060         */
061        public static Class<?>[] detectDefaultConfigurationClasses(Class<?> declaringClass) {
062                Assert.notNull(declaringClass, "Declaring class must not be null");
063
064                List<Class<?>> configClasses = new ArrayList<Class<?>>();
065
066                for (Class<?> candidate : declaringClass.getDeclaredClasses()) {
067                        if (isDefaultConfigurationClassCandidate(candidate)) {
068                                configClasses.add(candidate);
069                        }
070                        else {
071                                if (logger.isDebugEnabled()) {
072                                        logger.debug(String.format(
073                                                "Ignoring class [%s]; it must be static, non-private, non-final, and annotated " +
074                                                                "with @Configuration to be considered a default configuration class.",
075                                                candidate.getName()));
076                                }
077                        }
078                }
079
080                if (configClasses.isEmpty()) {
081                        if (logger.isInfoEnabled()) {
082                                logger.info(String.format("Could not detect default configuration classes for test class [%s]: " +
083                                                "%s does not declare any static, non-private, non-final, nested classes " +
084                                                "annotated with @Configuration.", declaringClass.getName(), declaringClass.getSimpleName()));
085                        }
086                }
087
088                return ClassUtils.toClassArray(configClasses);
089        }
090
091        /**
092         * Determine if the supplied {@link Class} meets the criteria for being
093         * considered a <em>default configuration class</em> candidate.
094         * <p>Specifically, such candidates:
095         * <ul>
096         * <li>must not be {@code null}</li>
097         * <li>must not be {@code private}</li>
098         * <li>must not be {@code final}</li>
099         * <li>must be {@code static}</li>
100         * <li>must be annotated or meta-annotated with {@code @Configuration}</li>
101         * </ul>
102         * @param clazz the class to check
103         * @return {@code true} if the supplied class meets the candidate criteria
104         */
105        private static boolean isDefaultConfigurationClassCandidate(Class<?> clazz) {
106                return (clazz != null && isStaticNonPrivateAndNonFinal(clazz) &&
107                                AnnotatedElementUtils.hasAnnotation(clazz, Configuration.class));
108        }
109
110        private static boolean isStaticNonPrivateAndNonFinal(Class<?> clazz) {
111                Assert.notNull(clazz, "Class must not be null");
112                int modifiers = clazz.getModifiers();
113                return (Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers) && !Modifier.isFinal(modifiers));
114        }
115
116}