001/*
002 * Copyright 2002-2019 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.lang.Nullable;
029import org.springframework.test.context.SmartContextLoader;
030import org.springframework.util.Assert;
031import org.springframework.util.ClassUtils;
032
033/**
034 * Utility methods for {@link SmartContextLoader SmartContextLoaders} that deal
035 * with component classes (e.g., {@link Configuration @Configuration} classes).
036 *
037 * @author Sam Brannen
038 * @since 3.2
039 */
040public abstract class AnnotationConfigContextLoaderUtils {
041
042        private static final Log logger = LogFactory.getLog(AnnotationConfigContextLoaderUtils.class);
043
044
045        /**
046         * Detect the default configuration classes for the supplied test class.
047         * <p>The returned class array will contain all static nested classes of
048         * the supplied class that meet the requirements for {@code @Configuration}
049         * class implementations as specified in the documentation for
050         * {@link Configuration @Configuration}.
051         * <p>The implementation of this method adheres to the contract defined in the
052         * {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}
053         * SPI. Specifically, this method uses introspection to detect default
054         * configuration classes that comply with the constraints required of
055         * {@code @Configuration} class implementations. If a potential candidate
056         * configuration class does not meet these requirements, this method will log a
057         * debug message, and the potential candidate class will be ignored.
058         * @param declaringClass the test class that declared {@code @ContextConfiguration}
059         * @return an array of default configuration classes, potentially empty but
060         * never {@code null}
061         */
062        public static Class<?>[] detectDefaultConfigurationClasses(Class<?> declaringClass) {
063                Assert.notNull(declaringClass, "Declaring class must not be null");
064
065                List<Class<?>> configClasses = new ArrayList<>();
066
067                for (Class<?> candidate : declaringClass.getDeclaredClasses()) {
068                        if (isDefaultConfigurationClassCandidate(candidate)) {
069                                configClasses.add(candidate);
070                        }
071                        else {
072                                if (logger.isDebugEnabled()) {
073                                        logger.debug(String.format(
074                                                "Ignoring class [%s]; it must be static, non-private, non-final, and annotated " +
075                                                                "with @Configuration to be considered a default configuration class.",
076                                                candidate.getName()));
077                                }
078                        }
079                }
080
081                if (configClasses.isEmpty()) {
082                        if (logger.isInfoEnabled()) {
083                                logger.info(String.format("Could not detect default configuration classes for test class [%s]: " +
084                                                "%s does not declare any static, non-private, non-final, nested classes " +
085                                                "annotated with @Configuration.", declaringClass.getName(), declaringClass.getSimpleName()));
086                        }
087                }
088
089                return ClassUtils.toClassArray(configClasses);
090        }
091
092        /**
093         * Determine if the supplied {@link Class} meets the criteria for being
094         * considered a <em>default configuration class</em> candidate.
095         * <p>Specifically, such candidates:
096         * <ul>
097         * <li>must not be {@code null}</li>
098         * <li>must not be {@code private}</li>
099         * <li>must not be {@code final}</li>
100         * <li>must be {@code static}</li>
101         * <li>must be annotated or meta-annotated with {@code @Configuration}</li>
102         * </ul>
103         * @param clazz the class to check
104         * @return {@code true} if the supplied class meets the candidate criteria
105         */
106        private static boolean isDefaultConfigurationClassCandidate(@Nullable Class<?> clazz) {
107                return (clazz != null && isStaticNonPrivateAndNonFinal(clazz) &&
108                                AnnotatedElementUtils.hasAnnotation(clazz, Configuration.class));
109        }
110
111        private static boolean isStaticNonPrivateAndNonFinal(Class<?> clazz) {
112                Assert.notNull(clazz, "Class must not be null");
113                int modifiers = clazz.getModifiers();
114                return (Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers) && !Modifier.isFinal(modifiers));
115        }
116
117}