[SPR-6184] Introduced ResourceTypeAwareContextLoader interface and removed dependency on AnnotationConfigContextLoader in TestContext.

This commit is contained in:
Sam Brannen 2011-04-03 16:37:24 +00:00
parent a60487655c
commit ad9c858bd2
7 changed files with 92 additions and 16 deletions

View File

@ -19,8 +19,7 @@ package org.springframework.test.context;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
/** /**
* Strategy interface for loading an * Strategy interface for loading an {@link ApplicationContext application context}.
* {@link ApplicationContext application context}.
* *
* <p>Clients of a ContextLoader should call * <p>Clients of a ContextLoader should call
* {@link #processLocations(Class,String...) processLocations()} prior to * {@link #processLocations(Class,String...) processLocations()} prior to
@ -34,6 +33,7 @@ import org.springframework.context.ApplicationContext;
* *
* <p>Spring provides the following out-of-the-box implementations: * <p>Spring provides the following out-of-the-box implementations:
* <ul> * <ul>
* <li>{@link org.springframework.test.context.support.AnnotationConfigContextLoader AnnotationConfigContextLoader}</li>
* <li>{@link org.springframework.test.context.support.GenericXmlContextLoader GenericXmlContextLoader}</li> * <li>{@link org.springframework.test.context.support.GenericXmlContextLoader GenericXmlContextLoader}</li>
* <li>{@link org.springframework.test.context.support.GenericPropertiesContextLoader GenericPropertiesContextLoader}</li> * <li>{@link org.springframework.test.context.support.GenericPropertiesContextLoader GenericPropertiesContextLoader}</li>
* </ul> * </ul>

View File

@ -0,0 +1,42 @@
/*
* 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;
/**
* TODO Document ResourceTypeAwareContextLoader.
*
* @author Sam Brannen
* @since 3.1
*/
public interface ResourceTypeAwareContextLoader extends ContextLoader {
/**
* @return <code>true</code> if this <code>ContextLoader</code> supports
* String-based resource locations
* @see ContextConfiguration#locations()
* @see ContextConfiguration#value()
*/
boolean supportsStringResources();
/**
* @return <code>true</code> if this <code>ContextLoader</code> supports
* Class-based resource locations
* @see ContextConfiguration#classes()
*/
boolean supportsClassResources();
}

View File

@ -29,7 +29,6 @@ import org.springframework.context.ApplicationContext;
import org.springframework.core.AttributeAccessorSupport; import org.springframework.core.AttributeAccessorSupport;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.style.ToStringCreator; import org.springframework.core.style.ToStringCreator;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -231,9 +230,9 @@ public class TestContext extends AttributeAccessorSupport {
// TODO [SPR-6184] Implement recursive search for configuration classes. // TODO [SPR-6184] Implement recursive search for configuration classes.
// This needs to integrate seamlessly (i.e., analogous yet mutually // This needs to integrate seamlessly (i.e., analogous yet mutually
// exclusive) with the existing locations search. Furthermore, the // exclusive) with the existing locations search.
// solution must not depend on an explicit ACCL check. if ((contextLoader instanceof ResourceTypeAwareContextLoader)
if (contextLoader instanceof AnnotationConfigContextLoader) { && ((ResourceTypeAwareContextLoader) contextLoader).supportsClassResources()) {
ContextConfiguration cc = declaringClass.getAnnotation(annotationType); ContextConfiguration cc = declaringClass.getAnnotation(annotationType);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
@ -317,11 +316,12 @@ public class TestContext extends AttributeAccessorSupport {
*/ */
public ApplicationContext getApplicationContext() { public ApplicationContext getApplicationContext() {
synchronized (this.contextCache) { synchronized (this.contextCache) {
ApplicationContext context = this.contextCache.get(contextKeyString(this.locations)); String contextKeyString = contextKeyString(this.locations);
ApplicationContext context = this.contextCache.get(contextKeyString);
if (context == null) { if (context == null) {
try { try {
context = loadApplicationContext(); context = loadApplicationContext();
this.contextCache.put(contextKeyString(this.locations), context); this.contextCache.put(contextKeyString, context);
} }
catch (Exception ex) { catch (Exception ex) {
throw new IllegalStateException("Failed to load ApplicationContext", ex); throw new IllegalStateException("Failed to load ApplicationContext", ex);

View File

@ -19,6 +19,7 @@ package org.springframework.test.context.support;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.core.io.support.ResourcePatternUtils; import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.test.context.ContextLoader; import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.ResourceTypeAwareContextLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
@ -37,7 +38,7 @@ import org.springframework.util.StringUtils;
* @see #generateDefaultLocations * @see #generateDefaultLocations
* @see #modifyLocations * @see #modifyLocations
*/ */
public abstract class AbstractContextLoader implements ContextLoader { public abstract class AbstractContextLoader implements ResourceTypeAwareContextLoader {
/** /**
* If the supplied <code>locations</code> are <code>null</code> or * If the supplied <code>locations</code> are <code>null</code> or
@ -58,8 +59,8 @@ public abstract class AbstractContextLoader implements ContextLoader {
* @see org.springframework.test.context.ContextLoader#processLocations * @see org.springframework.test.context.ContextLoader#processLocations
*/ */
public final String[] processLocations(Class<?> clazz, String... locations) { public final String[] processLocations(Class<?> clazz, String... locations) {
return (ObjectUtils.isEmpty(locations) && isGenerateDefaultLocations()) ? return (ObjectUtils.isEmpty(locations) && isGenerateDefaultLocations()) ? generateDefaultLocations(clazz)
generateDefaultLocations(clazz) : modifyLocations(clazz, locations); : modifyLocations(clazz, locations);
} }
/** /**
@ -80,8 +81,8 @@ public abstract class AbstractContextLoader implements ContextLoader {
Assert.notNull(clazz, "Class must not be null"); Assert.notNull(clazz, "Class must not be null");
String suffix = getResourceSuffix(); String suffix = getResourceSuffix();
Assert.hasText(suffix, "Resource suffix must not be empty"); Assert.hasText(suffix, "Resource suffix must not be empty");
return new String[] { ResourceUtils.CLASSPATH_URL_PREFIX + "/" + return new String[] { ResourceUtils.CLASSPATH_URL_PREFIX + "/"
ClassUtils.convertClassNameToResourcePath(clazz.getName()) + suffix }; + ClassUtils.convertClassNameToResourcePath(clazz.getName()) + suffix };
} }
/** /**
@ -119,7 +120,6 @@ public abstract class AbstractContextLoader implements ContextLoader {
return modifiedLocations; return modifiedLocations;
} }
/** /**
* Determine whether or not <em>default</em> resource locations should be * Determine whether or not <em>default</em> resource locations should be
* generated if the <code>locations</code> provided to * generated if the <code>locations</code> provided to
@ -141,4 +141,22 @@ public abstract class AbstractContextLoader implements ContextLoader {
*/ */
protected abstract String getResourceSuffix(); protected abstract String getResourceSuffix();
/**
* TODO Document supportsStringResources() implementation.
*
* @return <code>true</code>
*/
public boolean supportsStringResources() {
return true;
}
/**
* TODO Document supportsClassResources() implementations.
*
* @return <code>false</code>
*/
public boolean supportsClassResources() {
return false;
}
} }

View File

@ -127,4 +127,20 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
return "Config"; return "Config";
} }
/**
* @return <code>true</code>
*/
@Override
public boolean supportsClassResources() {
return true;
}
/**
* @return <code>false</code>
*/
@Override
public boolean supportsStringResources() {
return false;
}
} }

View File

@ -26,7 +26,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
* @author Sam Brannen * @author Sam Brannen
* @since 3.1 * @since 3.1
*/ */
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AnnotationConfigSpringJUnit4ClassRunnerAppCtxTestsConfig.class, inheritLocations = false) @ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = PojoAndStringConfig.class)
public class AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests { public class AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
/* all tests are in the parent class. */ /* all tests are in the parent class. */
} }

View File

@ -28,7 +28,7 @@ import org.springframework.context.annotation.Configuration;
* @since 3.1 * @since 3.1
*/ */
@Configuration @Configuration
public class AnnotationConfigSpringJUnit4ClassRunnerAppCtxTestsConfig { public class PojoAndStringConfig {
@Bean @Bean
public Employee employee() { public Employee employee() {