AnnotationConfigContextLoader.java 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2002-2011 the original author or authors.
 *
 * 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;

19
import java.lang.reflect.Modifier;
20 21 22
import java.util.ArrayList;
import java.util.List;

23 24
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
25
import org.springframework.beans.factory.support.BeanDefinitionReader;
26
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
27
import org.springframework.context.annotation.Configuration;
28
import org.springframework.context.support.GenericApplicationContext;
29 30
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.MergedContextConfiguration;
31
import org.springframework.util.Assert;
32
import org.springframework.util.ObjectUtils;
33 34

/**
35 36
 * Concrete implementation of {@link AbstractGenericContextLoader} that loads
 * bean definitions from
37 38
 * {@link org.springframework.context.annotation.Configuration configuration classes}.
 * 
39 40 41 42 43 44 45 46 47 48 49 50
 * <p>Note: <code>AnnotationConfigContextLoader</code> supports
 * {@link org.springframework.context.annotation.Configuration configuration classes}
 * rather than the String-based resource locations defined by the legacy
 * {@link org.springframework.test.context.ContextLoader ContextLoader} API. Thus,
 * although <code>AnnotationConfigContextLoader</code> extends
 * <code>AbstractGenericContextLoader</code>, <code>AnnotationConfigContextLoader</code>
 * does <em>not</em> support any String-based methods defined by
 * <code>AbstractContextLoader</code> or <code>AbstractGenericContextLoader</code>.
 * Consequently, <code>AnnotationConfigContextLoader</code> should chiefly be
 * considered a {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}
 * rather than a {@link org.springframework.test.context.ContextLoader ContextLoader}. 
 * 
51 52
 * @author Sam Brannen
 * @since 3.1
53 54 55
 * @see #processContextConfiguration()
 * @see #generateDefaultConfigurationClasses()
 * @see #loadBeanDefinitions()
56
 */
57
public class AnnotationConfigContextLoader extends AbstractGenericContextLoader {
58 59 60 61

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


62 63
	// --- SmartContextLoader -----------------------------------------------

64
	/**
65 66 67 68 69 70 71 72
	 * Process configuration classes in the supplied {@link ContextConfigurationAttributes}.
	 * <p>If the configuration classes are <code>null</code> or empty and
	 * {@link #generatesDefaults()} returns <code>true</code>, this
	 * <code>SmartContextLoader</code> will attempt to
	 * {@link #generateDefaultConfigurationClasses generate default configuration classes}.
	 * Otherwise, properties in the supplied configuration attributes will not
	 * be modified.
	 * @param configAttributes the context configuration attributes to process
73
	 * @see org.springframework.test.context.SmartContextLoader#processContextConfiguration()
74 75
	 * @see #generatesDefaults()
	 * @see #generateDefaultConfigurationClasses()
76
	 */
77
	public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
78
		if (ObjectUtils.isEmpty(configAttributes.getClasses()) && generatesDefaults()) {
79 80
			Class<?>[] defaultConfigClasses = generateDefaultConfigurationClasses(configAttributes.getDeclaringClass());
			configAttributes.setClasses(defaultConfigClasses);
81 82 83
		}
	}

84 85 86 87 88 89 90 91
	/**
	 * TODO Document overridden supports(MergedContextConfiguration) implementation.
	 */
	@Override
	public boolean supports(MergedContextConfiguration mergedConfig) {
		return ObjectUtils.isEmpty(mergedConfig.getLocations()) && !ObjectUtils.isEmpty(mergedConfig.getClasses());
	}

92 93 94 95 96 97
	// --- AnnotationConfigContextLoader ---------------------------------------

	private 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));
98 99 100
	}

	/**
101 102
	 * Determine if the supplied {@link Class} meets the criteria for being
	 * considered as a <em>default configuration class</em> candidate.
103 104 105 106 107 108 109 110
	 * <p>Specifically, such candidates:
	 * <ul>
	 * <li>must not be <code>null</code></li>
	 * <li>must not be <code>private</code></li>
	 * <li>must not be <code>final</code></li>
	 * <li>must be <code>static</code></li>
	 * <li>must be annotated with {@code @Configuration}</li>
	 * </ul>
S
Sam Brannen 已提交
111
	 * @param clazz the class to check
112
	 * @return <code>true</code> if the supplied class meets the candidate criteria
113
	 */
114 115
	private boolean isDefaultConfigurationClassCandidate(Class<?> clazz) {
		return clazz != null && isStaticNonPrivateAndNonFinal(clazz) && clazz.isAnnotationPresent(Configuration.class);
116 117
	}

118
	/**
119 120 121 122 123
	 * Generate the default configuration class array for the supplied test class.
	 * <p>The generated class array will contain all static inner classes of
	 * the supplied class that meet the requirements for {@code @Configuration}
	 * class implementations as specified in the documentation for
	 * {@link Configuration @Configuration}.
124 125 126 127
	 * <p>The implementation of this method adheres to the contract defined in the
	 * {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}
	 * SPI. Specifically, this method will <em>preemptively</em> verify that the
	 * generated default configuration classes exist <b>and</b> that such classes
128 129 130 131 132 133 134
	 * comply with the constraints required of {@code @Configuration} class
	 * implementations. If a candidate configuration class does meet these
	 * requirements, this method will log a warning, and the candidate class will
	 * be ignored.
	 * @param declaringClass the test class that declared {@code @ContextConfiguration}
	 * @return an array of default configuration classes, potentially empty but
	 * never <code>null</code>
135 136 137 138 139
	 */
	protected Class<?>[] generateDefaultConfigurationClasses(Class<?> declaringClass) {
		Assert.notNull(declaringClass, "Declaring class must not be null");

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

		for (Class<?> configClass : declaringClass.getDeclaredClasses()) {
			if (isDefaultConfigurationClassCandidate(configClass)) {
143 144 145
				configClasses.add(configClass);
			}
			else {
146 147 148 149 150 151
				if (logger.isDebugEnabled()) {
					logger.debug(String.format(
						"Ignoring class [%s]; it must be static, non-private, non-final, and annotated "
								+ "with @Configuration to be considered a default configuration class.",
						configClass.getName()));
				}
152
			}
153
		}
154 155

		if (configClasses.isEmpty()) {
156 157
			if (logger.isInfoEnabled()) {
				logger.info(String.format("Cannot generate default configuration classes for test class [%s]: "
S
Sam Brannen 已提交
158 159
						+ "%s does not declare any static, non-private, non-final, inner classes "
						+ "annotated with @Configuration.", declaringClass.getName(), declaringClass.getSimpleName()));
160
			}
161 162 163 164 165
		}

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

166 167
	// --- AbstractContextLoader -----------------------------------------------

168
	/**
169 170 171 172 173 174
	 * <code>AnnotationConfigContextLoader</code> should be used as a
	 * {@link org.springframework.test.context.SmartContextLoader SmartContextLoader},
	 * not as a legacy {@link org.springframework.test.context.ContextLoader ContextLoader}.
	 * Consequently, this method is not supported.
	 * @see AbstractContextLoader#modifyLocations
	 * @throws UnsupportedOperationException
175 176
	 */
	@Override
177
	protected String[] modifyLocations(Class<?> clazz, String... locations) {
178
		throw new UnsupportedOperationException(
179
			"AnnotationConfigContextLoader does not support the modifyLocations(Class, String...) method");
180 181 182
	}

	/**
183 184 185 186 187 188
	 * <code>AnnotationConfigContextLoader</code> should be used as a
	 * {@link org.springframework.test.context.SmartContextLoader SmartContextLoader},
	 * not as a legacy {@link org.springframework.test.context.ContextLoader ContextLoader}.
	 * Consequently, this method is not supported.
	 * @see AbstractContextLoader#generateDefaultLocations
	 * @throws UnsupportedOperationException
189 190
	 */
	@Override
191 192 193
	protected String[] generateDefaultLocations(Class<?> clazz) {
		throw new UnsupportedOperationException(
			"AnnotationConfigContextLoader does not support the generateDefaultLocations(Class) method");
194 195 196
	}

	/**
197 198 199 200 201 202
	 * <code>AnnotationConfigContextLoader</code> should be used as a 
	 * {@link org.springframework.test.context.SmartContextLoader SmartContextLoader},
	 * not as a legacy {@link org.springframework.test.context.ContextLoader ContextLoader}.
	 * Consequently, this method is not supported.
	 * @see AbstractContextLoader#getResourceSuffix
	 * @throws UnsupportedOperationException
203 204
	 */
	@Override
205
	protected String getResourceSuffix() {
206
		throw new UnsupportedOperationException(
207
			"AnnotationConfigContextLoader does not support the getResourceSuffix() method");
208 209
	}

210 211
	// --- AbstractGenericContextLoader ----------------------------------------

212
	/**
213 214 215 216 217 218 219 220 221 222 223 224
	 * Register {@link org.springframework.context.annotation.Configuration configuration classes}
	 * in the supplied {@link GenericApplicationContext context} from the classes
	 * in the supplied {@link MergedContextConfiguration}.
	 * <p>Each class must represent an annotated configuration class or component. An
	 * {@link AnnotatedBeanDefinitionReader} is used to register the appropriate
	 * bean definitions.
	 * <p>Note that this method does not call {@link #createBeanDefinitionReader}
	 * since <code>AnnotatedBeanDefinitionReader</code> is not an instance of
	 * {@link BeanDefinitionReader}.
	 * @param context the context in which the configuration classes should be registered
	 * @param mergedConfig the merged configuration from which the classes should be retrieved
	 * @see AbstractGenericContextLoader#loadBeanDefinitions
225 226
	 */
	@Override
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
	protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) {
		Class<?>[] configClasses = mergedConfig.getClasses();
		if (logger.isDebugEnabled()) {
			logger.debug("Registering configuration classes: " + ObjectUtils.nullSafeToString(configClasses));
		}
		new AnnotatedBeanDefinitionReader(context).register(configClasses);
	}

	/**
	 * <code>AnnotationConfigContextLoader</code> should be used as a 
	 * {@link org.springframework.test.context.SmartContextLoader SmartContextLoader},
	 * not as a legacy {@link org.springframework.test.context.ContextLoader ContextLoader}.
	 * Consequently, this method is not supported.
	 * @see #loadBeanDefinitions
	 * @see AbstractGenericContextLoader#createBeanDefinitionReader
	 * @throws UnsupportedOperationException
	 */
	@Override
	protected BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context) {
246
		throw new UnsupportedOperationException(
247
			"AnnotationConfigContextLoader does not support the createBeanDefinitionReader(GenericApplicationContext) method");
248 249
	}

250
}