AnnotationConfigContextLoaderUtils.java 4.4 KB
Newer Older
1
/*
2
 * Copyright 2002-2015 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.test.context.support;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
J
Juergen Hoeller 已提交
25

26
import org.springframework.context.annotation.Configuration;
27
import org.springframework.test.context.SmartContextLoader;
28 29 30
import org.springframework.util.Assert;

/**
31 32
 * Utility methods for {@link SmartContextLoader SmartContextLoaders} that deal
 * with annotated classes (e.g., {@link Configuration @Configuration} classes).
33 34 35 36
 *
 * @author Sam Brannen
 * @since 3.2
 */
37
public abstract class AnnotationConfigContextLoaderUtils {
38 39 40 41 42 43

	private static final Log logger = LogFactory.getLog(AnnotationConfigContextLoaderUtils.class);


	/**
	 * Detect the default configuration classes for the supplied test class.
44
	 * <p>The returned class array will contain all static nested classes of
45 46 47 48 49 50 51 52 53
	 * the supplied class that meet the requirements for {@code @Configuration}
	 * class implementations as specified in the documentation for
	 * {@link Configuration @Configuration}.
	 * <p>The implementation of this method adheres to the contract defined in the
	 * {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}
	 * SPI. Specifically, this method uses introspection to detect default
	 * configuration classes that comply with the constraints required of
	 * {@code @Configuration} class implementations. If a potential candidate
	 * configuration class does not meet these requirements, this method will log a
54
	 * debug message, and the potential candidate class will be ignored.
55 56
	 * @param declaringClass the test class that declared {@code @ContextConfiguration}
	 * @return an array of default configuration classes, potentially empty but
57
	 * never {@code null}
58
	 */
59
	public static Class<?>[] detectDefaultConfigurationClasses(Class<?> declaringClass) {
60 61 62 63 64 65 66
		Assert.notNull(declaringClass, "Declaring class must not be null");

		List<Class<?>> configClasses = new ArrayList<Class<?>>();

		for (Class<?> candidate : declaringClass.getDeclaredClasses()) {
			if (isDefaultConfigurationClassCandidate(candidate)) {
				configClasses.add(candidate);
J
Juergen Hoeller 已提交
67 68
			}
			else {
69 70
				if (logger.isDebugEnabled()) {
					logger.debug(String.format(
J
Juergen Hoeller 已提交
71 72
						"Ignoring class [%s]; it must be static, non-private, non-final, and annotated " +
								"with @Configuration to be considered a default configuration class.",
73 74 75 76 77 78 79
						candidate.getName()));
				}
			}
		}

		if (configClasses.isEmpty()) {
			if (logger.isInfoEnabled()) {
J
Juergen Hoeller 已提交
80
				logger.info(String.format("Could not detect default configuration classes for test class [%s]: " +
81
						"%s does not declare any static, non-private, non-final, nested classes " +
J
Juergen Hoeller 已提交
82
						"annotated with @Configuration.", declaringClass.getName(), declaringClass.getSimpleName()));
83 84 85 86 87 88
			}
		}

		return configClasses.toArray(new Class<?>[configClasses.size()]);
	}

J
Juergen Hoeller 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	/**
	 * Determine if the supplied {@link Class} meets the criteria for being
	 * considered a <em>default configuration class</em> candidate.
	 * <p>Specifically, such candidates:
	 * <ul>
	 * <li>must not be {@code null}</li>
	 * <li>must not be {@code private}</li>
	 * <li>must not be {@code final}</li>
	 * <li>must be {@code static}</li>
	 * <li>must be annotated with {@code @Configuration}</li>
	 * </ul>
	 * @param clazz the class to check
	 * @return {@code true} if the supplied class meets the candidate criteria
	 */
	private static boolean isDefaultConfigurationClassCandidate(Class<?> clazz) {
		return (clazz != null && isStaticNonPrivateAndNonFinal(clazz) && clazz.isAnnotationPresent(Configuration.class));
	}

	private static boolean isStaticNonPrivateAndNonFinal(Class<?> clazz) {
		Assert.notNull(clazz, "Class must not be null");
		int modifiers = clazz.getModifiers();
		return (Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers) && !Modifier.isFinal(modifiers));
	}

113
}