Browse Source

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

pull/7/head
Sam Brannen 14 years ago
parent
commit
ad9c858bd2
  1. 4
      org.springframework.test/src/main/java/org/springframework/test/context/ContextLoader.java
  2. 42
      org.springframework.test/src/main/java/org/springframework/test/context/ResourceTypeAwareContextLoader.java
  3. 12
      org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java
  4. 30
      org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java
  5. 16
      org.springframework.test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoader.java
  6. 2
      org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java
  7. 2
      org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/PojoAndStringConfig.java

4
org.springframework.test/src/main/java/org/springframework/test/context/ContextLoader.java

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

42
org.springframework.test/src/main/java/org/springframework/test/context/ResourceTypeAwareContextLoader.java

@ -0,0 +1,42 @@ @@ -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();
}

12
org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java

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

30
org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java

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

16
org.springframework.test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoader.java

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

2
org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java

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

2
org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTestsConfig.java → org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/PojoAndStringConfig.java

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