Browse Source

[SPR-7960][SPR-8386] Supporting declarative configuration of bean definition profiles in the TestContext framework:

- TextContext now works with MergedContextConfiguration instead of locations and loader
- TextContext now builds context caching key from MergedContextConfiguration
- Test context caching is now based on locations, classes, active profiles, and context loader
- TextContext now delegates to SmartContextLoader or ContextLoader as appropriate
- AbstractContextLoader now implements SmartContextLoader
- AbstractGenericContextLoader now sets active profiles in the GenericApplicationContext 
- Introduced integration tests for profile support in the TCF for both XML and annotation config
pull/7/head
Sam Brannen 14 years ago
parent
commit
2913964b41
  1. 6
      org.springframework.test/.project
  2. 14
      org.springframework.test/.springBeans
  3. 15
      org.springframework.test/src/main/java/org/springframework/test/context/ContextCache.java
  4. 87
      org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java
  5. 3
      org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java
  6. 30
      org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java
  7. 8
      org.springframework.test/src/test/java/org/springframework/test/context/junit4/SpringJUnit4SuiteTests.java
  8. 58
      org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/annotation/DefaultProfileAnnotationConfigTests.java
  9. 35
      org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/annotation/DefaultProfileConfig.java
  10. 38
      org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/annotation/DevProfileAnnotationConfigTests.java
  11. 41
      org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/annotation/DevProfileConfig.java
  12. 37
      org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/annotation/ProfileAnnotationConfigTestSuite.java
  13. 17
      org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/xml/DefaultProfileXmlConfigTests-context.xml
  14. 59
      org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/xml/DefaultProfileXmlConfigTests.java
  15. 40
      org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/xml/DevProfileXmlConfigTests.java
  16. 37
      org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/xml/ProfileXmlConfigTestSuite.java
  17. 10
      org.springframework.test/src/test/resources/log4j.xml

6
org.springframework.test/.project

@ -10,8 +10,14 @@
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
</natures> </natures>
</projectDescription> </projectDescription>

14
org.springframework.test/.springBeans

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[2.6.0.201104111100-PATCH]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
<config>src/test/java/org/springframework/test/context/junit4/profile/xml/DefaultProfileXmlConfigTests-context.xml</config>
</configs>
<configSets>
</configSets>
</beansProjectDescription>

15
org.springframework.test/src/main/java/org/springframework/test/context/ContextCache.java

@ -46,8 +46,7 @@ class ContextCache {
/** /**
* Map of context keys to Spring ApplicationContext instances. * Map of context keys to Spring ApplicationContext instances.
*/ */
private final Map<String, ApplicationContext> contextKeyToContextMap = private final Map<String, ApplicationContext> contextKeyToContextMap = new ConcurrentHashMap<String, ApplicationContext>();
new ConcurrentHashMap<String, ApplicationContext>();
private int hitCount; private int hitCount;
@ -134,7 +133,7 @@ class ContextCache {
} }
/** /**
* Explicitly add a ApplicationContext instance to the cache under the given key. * Explicitly add an ApplicationContext instance to the cache under the given key.
* @param key the context key (never <code>null</code>) * @param key the context key (never <code>null</code>)
* @param context the ApplicationContext instance (never <code>null</code>) * @param context the ApplicationContext instance (never <code>null</code>)
*/ */
@ -188,11 +187,11 @@ class ContextCache {
* as the {@link #hitCount hit} and {@link #missCount miss} counts. * as the {@link #hitCount hit} and {@link #missCount miss} counts.
*/ */
public String toString() { public String toString() {
return new ToStringCreator(this) return new ToStringCreator(this)//
.append("size", size()) .append("size", size())//
.append("hitCount", getHitCount()) .append("hitCount", getHitCount())//
.append("missCount",getMissCount()) .append("missCount", getMissCount())//
.toString(); .toString();
} }
} }

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

@ -16,7 +16,6 @@
package org.springframework.test.context; package org.springframework.test.context;
import java.io.Serializable;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -43,9 +42,9 @@ public class TestContext extends AttributeAccessorSupport {
private final ContextCache contextCache; private final ContextCache contextCache;
private final ContextLoader contextLoader; private final String contextKey;
private final String[] locations; private final MergedContextConfiguration mergedContextConfiguration;
private final Class<?> testClass; private final Class<?> testClass;
@ -88,28 +87,28 @@ public class TestContext extends AttributeAccessorSupport {
Assert.notNull(testClass, "Test class must not be null"); Assert.notNull(testClass, "Test class must not be null");
Assert.notNull(contextCache, "ContextCache must not be null"); Assert.notNull(contextCache, "ContextCache must not be null");
MergedContextConfiguration mergedContextConfiguration;
ContextConfiguration contextConfiguration = testClass.getAnnotation(ContextConfiguration.class); ContextConfiguration contextConfiguration = testClass.getAnnotation(ContextConfiguration.class);
ContextLoader contextLoader = null;
String[] locations = null;
if (contextConfiguration == null) { if (contextConfiguration == null) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info(String.format("@ContextConfiguration not found for class [%s]", testClass)); logger.info(String.format("@ContextConfiguration not found for class [%s]", testClass));
} }
mergedContextConfiguration = new MergedContextConfiguration(testClass, null, null, null, null);
} }
else { else {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace(String.format("Retrieved @ContextConfiguration [%s] for class [%s]", contextConfiguration, logger.trace(String.format("Retrieved @ContextConfiguration [%s] for class [%s]", contextConfiguration,
testClass)); testClass));
} }
contextLoader = ContextLoaderUtils.resolveContextLoader(testClass, defaultContextLoaderClassName); mergedContextConfiguration = ContextLoaderUtils.buildMergedContextConfiguration(testClass,
locations = ContextLoaderUtils.resolveContextLocations(contextLoader, testClass); defaultContextLoaderClassName);
} }
this.testClass = testClass;
this.contextCache = contextCache; this.contextCache = contextCache;
this.contextLoader = contextLoader; this.contextKey = generateContextKey(mergedContextConfiguration);
this.locations = locations; this.mergedContextConfiguration = mergedContextConfiguration;
this.testClass = testClass;
} }
/** /**
@ -118,19 +117,44 @@ public class TestContext extends AttributeAccessorSupport {
* @throws Exception if an error occurs while loading the application context * @throws Exception if an error occurs while loading the application context
*/ */
private ApplicationContext loadApplicationContext() throws Exception { private ApplicationContext loadApplicationContext() throws Exception {
Assert.notNull(this.contextLoader, "Can not load an ApplicationContext with a NULL 'contextLoader'. " ContextLoader contextLoader = mergedContextConfiguration.getContextLoader();
+ "Consider annotating your test class with @ContextConfiguration."); Assert.notNull(contextLoader, "Can not load an ApplicationContext with a NULL 'contextLoader'. "
Assert.notNull(this.locations, "Can not load an ApplicationContext with a NULL 'locations' array. "
+ "Consider annotating your test class with @ContextConfiguration."); + "Consider annotating your test class with @ContextConfiguration.");
return this.contextLoader.loadContext(this.locations);
ApplicationContext applicationContext;
if (contextLoader instanceof SmartContextLoader) {
SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader;
applicationContext = smartContextLoader.loadContext(mergedContextConfiguration);
}
else {
String[] locations = mergedContextConfiguration.getLocations();
Assert.notNull(locations, "Can not load an ApplicationContext with a NULL 'locations' array. "
+ "Consider annotating your test class with @ContextConfiguration.");
applicationContext = contextLoader.loadContext(locations);
}
return applicationContext;
} }
/** /**
* Convert the supplied context <code>key</code> to a String representation * Generates a context <code>key</code> from information stored in the
* for use in caching, logging, etc. * {@link MergedContextConfiguration} for this <code>TestContext</code>.
*/ */
private String contextKeyString(Serializable key) { private String generateContextKey(MergedContextConfiguration mergedContextConfiguration) {
return ObjectUtils.nullSafeToString(key);
String[] locations = mergedContextConfiguration.getLocations();
Class<?>[] classes = mergedContextConfiguration.getClasses();
String[] activeProfiles = mergedContextConfiguration.getActiveProfiles();
ContextLoader contextLoader = mergedContextConfiguration.getContextLoader();
String locationsKey = ObjectUtils.nullSafeToString(locations);
String classesKey = ObjectUtils.nullSafeToString(classes);
String activeProfilesKey = ObjectUtils.nullSafeToString(activeProfiles);
String contextLoaderKey = contextLoader == null ? "null" : contextLoader.getClass().getName();
return String.format("locations = [%s], classes = [%s], activeProfiles = [%s], contextLoader = [%s]",
locationsKey, classesKey, activeProfilesKey, contextLoaderKey);
} }
/** /**
@ -141,13 +165,12 @@ public class TestContext extends AttributeAccessorSupport {
* application context * application context
*/ */
public ApplicationContext getApplicationContext() { public ApplicationContext getApplicationContext() {
synchronized (this.contextCache) { synchronized (contextCache) {
String contextKeyString = contextKeyString(this.locations); ApplicationContext context = contextCache.get(contextKey);
ApplicationContext context = this.contextCache.get(contextKeyString);
if (context == null) { if (context == null) {
try { try {
context = loadApplicationContext(); context = loadApplicationContext();
this.contextCache.put(contextKeyString, context); contextCache.put(contextKey, context);
} }
catch (Exception ex) { catch (Exception ex) {
throw new IllegalStateException("Failed to load ApplicationContext", ex); throw new IllegalStateException("Failed to load ApplicationContext", ex);
@ -162,7 +185,7 @@ public class TestContext extends AttributeAccessorSupport {
* @return the test class (never <code>null</code>) * @return the test class (never <code>null</code>)
*/ */
public final Class<?> getTestClass() { public final Class<?> getTestClass() {
return this.testClass; return testClass;
} }
/** /**
@ -172,7 +195,7 @@ public class TestContext extends AttributeAccessorSupport {
* @see #updateState(Object,Method,Throwable) * @see #updateState(Object,Method,Throwable)
*/ */
public final Object getTestInstance() { public final Object getTestInstance() {
return this.testInstance; return testInstance;
} }
/** /**
@ -182,7 +205,7 @@ public class TestContext extends AttributeAccessorSupport {
* @see #updateState(Object, Method, Throwable) * @see #updateState(Object, Method, Throwable)
*/ */
public final Method getTestMethod() { public final Method getTestMethod() {
return this.testMethod; return testMethod;
} }
/** /**
@ -194,7 +217,7 @@ public class TestContext extends AttributeAccessorSupport {
* @see #updateState(Object, Method, Throwable) * @see #updateState(Object, Method, Throwable)
*/ */
public final Throwable getTestException() { public final Throwable getTestException() {
return this.testException; return testException;
} }
/** /**
@ -204,7 +227,7 @@ public class TestContext extends AttributeAccessorSupport {
* replacing a bean definition). * replacing a bean definition).
*/ */
public void markApplicationContextDirty() { public void markApplicationContextDirty() {
this.contextCache.setDirty(contextKeyString(this.locations)); contextCache.setDirty(contextKey);
} }
/** /**
@ -227,11 +250,11 @@ public class TestContext extends AttributeAccessorSupport {
@Override @Override
public String toString() { public String toString() {
return new ToStringCreator(this)// return new ToStringCreator(this)//
.append("testClass", this.testClass)// .append("testClass", testClass)//
.append("locations", this.locations)// .append("testInstance", testInstance)//
.append("testInstance", this.testInstance)// .append("testMethod", testMethod)//
.append("testMethod", this.testMethod)// .append("testException", testException)//
.append("testException", this.testException)// .append("mergedContextConfiguration", mergedContextConfiguration)//
.toString(); .toString();
} }

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

@ -20,6 +20,7 @@ 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.test.context.ResourceTypeAwareContextLoader;
import org.springframework.test.context.SmartContextLoader;
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;
@ -38,7 +39,7 @@ import org.springframework.util.StringUtils;
* @see #generateDefaultLocations * @see #generateDefaultLocations
* @see #modifyLocations * @see #modifyLocations
*/ */
public abstract class AbstractContextLoader implements ResourceTypeAwareContextLoader { public abstract class AbstractContextLoader implements SmartContextLoader, ResourceTypeAwareContextLoader {
/** /**
* If the supplied <code>locations</code> are <code>null</code> or * If the supplied <code>locations</code> are <code>null</code> or

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

@ -20,9 +20,12 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.support.BeanDefinitionReader; import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigUtils; import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -45,6 +48,33 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
protected static final Log logger = LogFactory.getLog(AbstractGenericContextLoader.class); protected static final Log logger = LogFactory.getLog(AbstractGenericContextLoader.class);
/**
* TODO Document loadContext().
*
* @see org.springframework.test.context.SmartContextLoader#loadContext(org.springframework.test.context.MergedContextConfiguration)
*/
public final ApplicationContext loadContext(MergedContextConfiguration mergedContextConfiguration) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Loading ApplicationContext for merged context configuration [%s].",
mergedContextConfiguration));
}
String[] locations = mergedContextConfiguration.getLocations();
Assert.notNull(locations, "Can not load an ApplicationContext with a NULL 'locations' array. "
+ "Consider annotating your test class with @ContextConfiguration.");
GenericApplicationContext context = new GenericApplicationContext();
context.getEnvironment().setActiveProfiles(mergedContextConfiguration.getActiveProfiles());
prepareContext(context);
customizeBeanFactory(context.getDefaultListableBeanFactory());
loadBeanDefinitions(context, locations);
AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
customizeContext(context);
context.refresh();
context.registerShutdownHook();
return context;
}
/** /**
* Loads a Spring ApplicationContext from the supplied <code>locations</code>. * Loads a Spring ApplicationContext from the supplied <code>locations</code>.
* <p>Implementation details: * <p>Implementation details:

8
org.springframework.test/src/test/java/org/springframework/test/context/junit4/SpringJUnit4SuiteTests.java

@ -25,6 +25,10 @@ import org.springframework.test.context.junit4.annotation.AnnotationConfigSpring
import org.springframework.test.context.junit4.annotation.DefaultConfigClassesBaseTests; import org.springframework.test.context.junit4.annotation.DefaultConfigClassesBaseTests;
import org.springframework.test.context.junit4.annotation.DefaultConfigClassesInheritedTests; import org.springframework.test.context.junit4.annotation.DefaultConfigClassesInheritedTests;
import org.springframework.test.context.junit4.orm.HibernateSessionFlushingTests; import org.springframework.test.context.junit4.orm.HibernateSessionFlushingTests;
import org.springframework.test.context.junit4.profile.annotation.DefaultProfileAnnotationConfigTests;
import org.springframework.test.context.junit4.profile.annotation.DevProfileAnnotationConfigTests;
import org.springframework.test.context.junit4.profile.xml.DefaultProfileXmlConfigTests;
import org.springframework.test.context.junit4.profile.xml.DevProfileXmlConfigTests;
/** /**
* <p> * <p>
@ -54,6 +58,10 @@ StandardJUnit4FeaturesTests.class,//
AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.class,// AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.class,//
DefaultConfigClassesBaseTests.class,// DefaultConfigClassesBaseTests.class,//
DefaultConfigClassesInheritedTests.class,// DefaultConfigClassesInheritedTests.class,//
DefaultProfileAnnotationConfigTests.class,//
DevProfileAnnotationConfigTests.class, //
DefaultProfileXmlConfigTests.class,//
DevProfileXmlConfigTests.class, //
ExpectedExceptionSpringRunnerTests.class,// ExpectedExceptionSpringRunnerTests.class,//
TimedSpringRunnerTests.class,// TimedSpringRunnerTests.class,//
RepeatedSpringRunnerTests.class,// RepeatedSpringRunnerTests.class,//

58
org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/annotation/DefaultProfileAnnotationConfigTests.java

@ -0,0 +1,58 @@
/*
* Copyright 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.junit4.profile.annotation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.Employee;
import org.springframework.beans.Pet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
/**
* @author Sam Brannen
* @since 3.1
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { DefaultProfileConfig.class, DevProfileConfig.class }, loader = AnnotationConfigContextLoader.class)
public class DefaultProfileAnnotationConfigTests {
@Autowired
protected Pet pet;
@Autowired(required = false)
protected Employee employee;
@Test
public void pet() {
assertNotNull(pet);
assertEquals("Fido", pet.getName());
}
@Test
public void employee() {
assertNull("employee bean should not be created for the default profile", employee);
}
}

35
org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/annotation/DefaultProfileConfig.java

@ -0,0 +1,35 @@
/*
* Copyright 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.junit4.profile.annotation;
import org.springframework.beans.Pet;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Sam Brannen
* @since 3.1
*/
@Configuration
public class DefaultProfileConfig {
@Bean
public Pet pet() {
return new Pet("Fido");
}
}

38
org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/annotation/DevProfileAnnotationConfigTests.java

@ -0,0 +1,38 @@
/*
* Copyright 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.junit4.profile.annotation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.test.context.ActiveProfiles;
/**
* @author Sam Brannen
* @since 3.1
*/
@ActiveProfiles("dev")
public class DevProfileAnnotationConfigTests extends DefaultProfileAnnotationConfigTests {
@Test
public void employee() {
assertNotNull("employee bean should be loaded for the 'dev' profile", employee);
assertEquals("John Smith", employee.getName());
}
}

41
org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/annotation/DevProfileConfig.java

@ -0,0 +1,41 @@
/*
* Copyright 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.junit4.profile.annotation;
import org.springframework.beans.Employee;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
/**
* @author Sam Brannen
* @since 3.1
*/
@Profile("dev")
@Configuration
public class DevProfileConfig {
@Bean
public Employee employee() {
Employee employee = new Employee();
employee.setName("John Smith");
employee.setAge(42);
employee.setCompany("Acme Widgets, Inc.");
return employee;
}
}

37
org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/annotation/ProfileAnnotationConfigTestSuite.java

@ -0,0 +1,37 @@
/*
* 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.junit4.profile.annotation;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
* JUnit test suite for <em>bean definition profile</em> support in the
* Spring TestContext Framework with XML-based configuration.
*
* @author Sam Brannen
* @since 3.1
*/
@RunWith(Suite.class)
// Note: the following 'multi-line' layout is for enhanced code readability.
@SuiteClasses({//
DefaultProfileAnnotationConfigTests.class,//
DevProfileAnnotationConfigTests.class //
})
public class ProfileAnnotationConfigTestSuite {
}

17
org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/xml/DefaultProfileXmlConfigTests-context.xml

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="pet" class="org.springframework.beans.Pet">
<constructor-arg value="Fido" />
</bean>
<beans profile="dev">
<bean id="employee" class="org.springframework.beans.Employee">
<property name="name" value="John Smith" />
<property name="age" value="42" />
<property name="company" value="Acme Widgets, Inc." />
</bean>
</beans>
</beans>

59
org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/xml/DefaultProfileXmlConfigTests.java

@ -0,0 +1,59 @@
/*
* Copyright 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.junit4.profile.xml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.Employee;
import org.springframework.beans.Pet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* TODO Document DefaultProfileXmlConfigTests.
*
* @author Sam Brannen
* @since 3.1
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class DefaultProfileXmlConfigTests {
@Autowired
protected Pet pet;
@Autowired(required = false)
protected Employee employee;
@Test
public void pet() {
assertNotNull(pet);
assertEquals("Fido", pet.getName());
}
@Test
public void employee() {
assertNull("employee bean should not be created for the default profile", employee);
}
}

40
org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/xml/DevProfileXmlConfigTests.java

@ -0,0 +1,40 @@
/*
* Copyright 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.junit4.profile.xml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.test.context.ActiveProfiles;
/**
* TODO Document DefaultProfileXmlConfigTests.
*
* @author Sam Brannen
* @since 3.1
*/
@ActiveProfiles("dev")
public class DevProfileXmlConfigTests extends DefaultProfileXmlConfigTests {
@Test
public void employee() {
assertNotNull("employee bean should be loaded for the 'dev' profile", employee);
assertEquals("John Smith", employee.getName());
}
}

37
org.springframework.test/src/test/java/org/springframework/test/context/junit4/profile/xml/ProfileXmlConfigTestSuite.java

@ -0,0 +1,37 @@
/*
* 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.junit4.profile.xml;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
* JUnit test suite for <em>bean definition profile</em> support in the
* Spring TestContext Framework with XML-based configuration.
*
* @author Sam Brannen
* @since 3.1
*/
@RunWith(Suite.class)
// Note: the following 'multi-line' layout is for enhanced code readability.
@SuiteClasses({//
DefaultProfileXmlConfigTests.class,//
DevProfileXmlConfigTests.class //
})
public class ProfileXmlConfigTestSuite {
}

10
org.springframework.test/src/test/resources/log4j.xml

@ -15,8 +15,14 @@
<level value="warn" /> <level value="warn" />
</logger> </logger>
<logger name="org.springframework.binding"> <logger name="org.springframework.test.context.TestContext">
<level value="debug" /> <level value="warn" />
</logger>
<logger name="org.springframework.test.context.ContextLoaderUtils">
<level value="warn" />
</logger>
<logger name="org.springframework.test.context.support.AbstractGenericContextLoader">
<level value="warn" />
</logger> </logger>
<!-- Root Logger --> <!-- Root Logger -->

Loading…
Cancel
Save