SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader; smartContextLoader.processContextConfiguration(configAttributes); locations.addAll(0, Arrays.asList(configAttributes.getLocations())); classes.addAll(0, Arrays.asList(configAttributes.getClasses())); configAttributes.getDeclaringClass(), configAttributes.getLocations()); locations.addAll(0, Arrays.asList(processedLocations)); initializers.addAll(0, Arrays.asList(configAttributes.getInitializers())); if (!configAttributes.isInheritLocations()) { break;
/** * Process <em>annotated classes</em> in the supplied {@link ContextConfigurationAttributes}. * <p>If the <em>annotated classes</em> are {@code null} or empty and * {@link #isGenerateDefaultLocations()} returns {@code true}, this * {@code SmartContextLoader} will attempt to {@link * #detectDefaultConfigurationClasses detect default configuration classes}. * If defaults are detected they will be * {@link ContextConfigurationAttributes#setClasses(Class[]) set} in the * supplied configuration attributes. Otherwise, properties in the supplied * configuration attributes will not be modified. * @param configAttributes the context configuration attributes to process * @see org.springframework.test.context.SmartContextLoader#processContextConfiguration(ContextConfigurationAttributes) * @see #isGenerateDefaultLocations() * @see #detectDefaultConfigurationClasses(Class) */ @Override public void processContextConfiguration(ContextConfigurationAttributes configAttributes) { if (!configAttributes.hasClasses() && isGenerateDefaultLocations()) { configAttributes.setClasses(detectDefaultConfigurationClasses(configAttributes.getDeclaringClass())); } }
@Test public void processContextConfigurationWithConfigurationClass() { Class<?>[] classes = new Class<?>[] {getClass()}; ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes( getClass(), EMPTY_STRING_ARRAY, classes, true, null, true, ContextLoader.class); loader.processContextConfiguration(configAttributes); assertArrayEquals(classes, configAttributes.getClasses()); assertEmpty(configAttributes.getLocations()); }
/** * Determine if this {@code ContextConfigurationAttributes} instance has * either path-based resource locations or class-based resources. * @return {@code true} if either the {@link #getLocations() locations} * or the {@link #getClasses() classes} array is not empty * @see #hasLocations() * @see #hasClasses() */ public boolean hasResources() { return (hasLocations() || hasClasses()); }
/** * For backwards compatibility with the {@link ContextLoader} SPI, the * default implementation simply delegates to {@link #processLocations(Class, String...)}, * passing it the {@link ContextConfigurationAttributes#getDeclaringClass() * declaring class} and {@link ContextConfigurationAttributes#getLocations() * resource locations} retrieved from the supplied * {@link ContextConfigurationAttributes configuration attributes}. The * processed locations are then * {@link ContextConfigurationAttributes#setLocations(String[]) set} in * the supplied configuration attributes. * <p>Can be overridden in subclasses — for example, to process * annotated classes instead of resource locations. * @since 3.1 * @see #processLocations(Class, String...) */ @Override public void processContextConfiguration(ContextConfigurationAttributes configAttributes) { String[] processedLocations = processLocations(configAttributes.getDeclaringClass(), configAttributes.getLocations()); configAttributes.setLocations(processedLocations); }
void assertAttributes(ContextConfigurationAttributes attributes, Class<?> expectedDeclaringClass, String[] expectedLocations, Class<?>[] expectedClasses, Class<? extends ContextLoader> expectedContextLoaderClass, boolean expectedInheritLocations) { assertEquals("declaring class", expectedDeclaringClass, attributes.getDeclaringClass()); assertArrayEquals("locations", expectedLocations, attributes.getLocations()); assertArrayEquals("classes", expectedClasses, attributes.getClasses()); assertEquals("inherit locations", expectedInheritLocations, attributes.isInheritLocations()); assertEquals("context loader", expectedContextLoaderClass, attributes.getContextLoaderClass()); }
@Override public void processContextConfiguration( ContextConfigurationAttributes configAttributes ) { Class<?>[] classes = configAttributes.getClasses(); if( classes.length == 0 ) { // Allow empty loaders, in this case everything should be mocked configAttributes.setClasses( new Class<?>[]{Object.class} ); } super.processContextConfiguration( configAttributes ); }
@Override public void processContextConfiguration( ContextConfigurationAttributes configAttributes) { super.processContextConfiguration(configAttributes); if (!configAttributes.hasResources()) { Class<?>[] defaultConfigClasses = detectDefaultConfigurationClasses( configAttributes.getDeclaringClass()); configAttributes.setClasses(defaultConfigClasses); } }
Assert.isTrue(!(configAttributes.hasLocations() && configAttributes.hasClasses()), String.format( "Cannot process locations AND configuration classes for context " + "configuration %s; configure one or the other, but not both.", configAttributes)); if (configAttributes.hasLocations()) { delegateProcessing(xmlLoader, configAttributes); else if (configAttributes.hasClasses()) { delegateProcessing(annotationConfigLoader, configAttributes); boolean xmlLoaderDetectedDefaults = configAttributes.hasLocations(); if (configAttributes.hasClasses()) { throw new IllegalStateException(String.format( "%s should NOT have detected default configuration classes for context configuration %s.", if (configAttributes.hasClasses()) { if (logger.isInfoEnabled()) { logger.info(String.format( if (!xmlLoaderDetectedDefaults && configAttributes.hasLocations()) { throw new IllegalStateException(String.format( "%s should NOT have detected default locations for context configuration %s.", if (!configAttributes.hasResources()) { throw new IllegalStateException(String.format( "Neither %s nor %s was able to detect defaults for context configuration %s.", name(xmlLoader), if (configAttributes.hasLocations() && configAttributes.hasClasses()) {
/** * Convenience method for creating a {@link ContextConfigurationAttributes} * instance from the supplied {@link ContextConfiguration} annotation and * declaring class and then adding the attributes to the supplied list. */ private static void convertContextConfigToConfigAttributesAndAddToList(ContextConfiguration contextConfiguration, Class<?> declaringClass, final List<ContextConfigurationAttributes> attributesList) { if (logger.isTraceEnabled()) { logger.trace(String.format("Retrieved @ContextConfiguration [%s] for declaring class [%s].", contextConfiguration, declaringClass.getName())); } ContextConfigurationAttributes attributes = new ContextConfigurationAttributes(declaringClass, contextConfiguration); if (logger.isTraceEnabled()) { logger.trace("Resolved context configuration attributes: " + attributes); } attributesList.add(attributes); }
Class<?> declaringClass = reversedList.get(0).getDeclaringClass();
/** * Determine if this {@code ContextConfigurationAttributes} instance has * path-based resource locations. * @return {@code true} if the {@link #getLocations() locations} array is not empty * @see #hasResources() * @see #hasClasses() */ public boolean hasLocations() { return (getLocations().length > 0); }
private boolean isFromConfiguration(MergedContextConfiguration candidateConfig, ContextConfiguration configuration) { ContextConfigurationAttributes attributes = new ContextConfigurationAttributes( candidateConfig.getTestClass(), configuration); Set<Class<?>> configurationClasses = new HashSet<>( Arrays.asList(attributes.getClasses())); for (Class<?> candidate : candidateConfig.getClasses()) { if (configurationClasses.contains(candidate)) { return true; } } return false; }
/** * Determine if this {@code ContextConfigurationAttributes} instance has * class-based resources. * @return {@code true} if the {@link #getClasses() classes} array is not empty * @see #hasResources() * @see #hasLocations() */ public boolean hasClasses() { return (getClasses().length > 0); }
logger.trace("Processing context initializers for configuration attributes " + configAttributes); Collections.addAll(initializerClasses, configAttributes.getInitializers()); if (!configAttributes.isInheritInitializers()) { break;
/** * Used to reproduce bug reported in https://jira.spring.io/browse/SPR-10997 */ @Test public void buildContextHierarchyMapForTestClassHierarchyWithMultiLevelContextHierarchiesAndOverriddenInitializers() { Map<String, List<ContextConfigurationAttributes>> map = buildContextHierarchyMap(TestClass2WithMultiLevelContextHierarchyWithOverriddenInitializers.class); assertThat(map.size(), is(2)); assertThat(map.keySet(), hasItems("alpha", "beta")); List<ContextConfigurationAttributes> alphaConfig = map.get("alpha"); assertThat(alphaConfig.size(), is(2)); assertThat(alphaConfig.get(0).getLocations().length, is(1)); assertThat(alphaConfig.get(0).getLocations()[0], is("1-A.xml")); assertThat(alphaConfig.get(0).getInitializers().length, is(0)); assertThat(alphaConfig.get(1).getLocations().length, is(0)); assertThat(alphaConfig.get(1).getInitializers().length, is(1)); assertEquals(DummyApplicationContextInitializer.class, alphaConfig.get(1).getInitializers()[0]); List<ContextConfigurationAttributes> betaConfig = map.get("beta"); assertThat(betaConfig.size(), is(2)); assertThat(betaConfig.get(0).getLocations().length, is(1)); assertThat(betaConfig.get(0).getLocations()[0], is("1-B.xml")); assertThat(betaConfig.get(0).getInitializers().length, is(0)); assertThat(betaConfig.get(1).getLocations().length, is(0)); assertThat(betaConfig.get(1).getInitializers().length, is(1)); assertEquals(DummyApplicationContextInitializer.class, betaConfig.get(1).getInitializers()[0]); }
configAttributes)); Class<? extends ContextLoader> contextLoaderClass = configAttributes.getContextLoaderClass(); if (ContextLoader.class != contextLoaderClass) { if (logger.isDebugEnabled()) {
public void processContextConfiguration(final ContextConfigurationAttributes configAttributes) { Assert.notNull(configAttributes, "configAttributes must not be null"); Assert.isTrue(!(configAttributes.hasLocations() && configAttributes.hasClasses()), () -> String.format("Cannot process locations AND classes for context configuration %s: " + "configure one or the other, but not both.", configAttributes)); if (configAttributes.hasLocations()) { delegateProcessing(getXmlLoader(), configAttributes); else if (configAttributes.hasClasses()) { delegateProcessing(getAnnotationConfigLoader(), configAttributes); boolean xmlLoaderDetectedDefaults = configAttributes.hasLocations(); Assert.state(!configAttributes.hasClasses(), () -> String.format( "%s should NOT have detected default configuration classes for context configuration %s.", name(getXmlLoader()), configAttributes)); if (configAttributes.hasClasses()) { if (logger.isInfoEnabled()) { logger.info(String.format("%s detected default configuration classes for context configuration %s.", Assert.state(xmlLoaderDetectedDefaults || !configAttributes.hasLocations(), () -> String.format( "%s should NOT have detected default locations for context configuration %s.", name(getAnnotationConfigLoader()), configAttributes)); if (configAttributes.hasLocations() && configAttributes.hasClasses()) { String msg = String.format( "Configuration error: both default locations AND default configuration classes " +
/** * For backwards compatibility with the {@link ContextLoader} SPI, the * default implementation simply delegates to {@link #processLocations(Class, String...)}, * passing it the {@link ContextConfigurationAttributes#getDeclaringClass() * declaring class} and {@link ContextConfigurationAttributes#getLocations() * resource locations} retrieved from the supplied * {@link ContextConfigurationAttributes configuration attributes}. The * processed locations are then * {@link ContextConfigurationAttributes#setLocations(String[]) set} in * the supplied configuration attributes. * <p>Can be overridden in subclasses — for example, to process * annotated classes instead of resource locations. * @since 3.1 * @see #processLocations(Class, String...) */ @Override public void processContextConfiguration(ContextConfigurationAttributes configAttributes) { String[] processedLocations = processLocations(configAttributes.getDeclaringClass(), configAttributes.getLocations()); configAttributes.setLocations(processedLocations); }
private void addConfigAttributesClasses( ContextConfigurationAttributes configAttributes, Class<?>[] classes) { List<Class<?>> combined = new ArrayList<>(); combined.addAll(Arrays.asList(classes)); if (configAttributes.getClasses() != null) { combined.addAll(Arrays.asList(configAttributes.getClasses())); } configAttributes.setClasses(ClassUtils.toClassArray(combined)); }