|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2012 the original author or authors. |
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -67,7 +67,6 @@ import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*; |
|
|
|
import static org.springframework.context.ConfigurableApplicationContext.*; |
|
|
|
import static org.springframework.context.ConfigurableApplicationContext.*; |
|
|
|
import static org.springframework.core.env.EnvironmentIntegrationTests.Constants.*; |
|
|
|
import static org.springframework.core.env.EnvironmentIntegrationTests.Constants.*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Integration tests for container support of {@link Environment} |
|
|
|
* Integration tests for container support of {@link Environment} |
|
|
|
* interface. |
|
|
|
* interface. |
|
|
|
@ -84,29 +83,15 @@ import static org.springframework.core.env.EnvironmentIntegrationTests.Constants |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Chris Beams |
|
|
|
* @author Chris Beams |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
@SuppressWarnings("resource") |
|
|
|
public class EnvironmentIntegrationTests { |
|
|
|
public class EnvironmentIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
|
private ConfigurableEnvironment prodEnv; |
|
|
|
private ConfigurableEnvironment prodEnv; |
|
|
|
private ConfigurableEnvironment devEnv; |
|
|
|
|
|
|
|
private ConfigurableEnvironment prodWebEnv; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Constants used both locally and in scan* sub-packages |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static class Constants { |
|
|
|
|
|
|
|
public static final String XML_PATH = "org/springframework/core/env/EnvironmentIntegrationTests-context.xml"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final String ENVIRONMENT_AWARE_BEAN_NAME = "envAwareBean"; |
|
|
|
private ConfigurableEnvironment devEnv; |
|
|
|
|
|
|
|
|
|
|
|
public static final String PROD_BEAN_NAME = "prodBean"; |
|
|
|
private ConfigurableEnvironment prodWebEnv; |
|
|
|
public static final String DEV_BEAN_NAME = "devBean"; |
|
|
|
|
|
|
|
public static final String DERIVED_DEV_BEAN_NAME = "derivedDevBean"; |
|
|
|
|
|
|
|
public static final String TRANSITIVE_BEAN_NAME = "transitiveBean"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final String PROD_ENV_NAME = "prod"; |
|
|
|
|
|
|
|
public static final String DEV_ENV_NAME = "dev"; |
|
|
|
|
|
|
|
public static final String DERIVED_DEV_ENV_NAME = "derivedDev"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
@Before |
|
|
|
public void setUp() { |
|
|
|
public void setUp() { |
|
|
|
@ -122,9 +107,7 @@ public class EnvironmentIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void genericApplicationContext_standardEnv() { |
|
|
|
public void genericApplicationContext_standardEnv() { |
|
|
|
ConfigurableApplicationContext ctx = |
|
|
|
ConfigurableApplicationContext ctx = new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); |
|
|
|
new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx.refresh(); |
|
|
|
ctx.refresh(); |
|
|
|
|
|
|
|
|
|
|
|
assertHasStandardEnvironment(ctx); |
|
|
|
assertHasStandardEnvironment(ctx); |
|
|
|
@ -134,8 +117,7 @@ public class EnvironmentIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void genericApplicationContext_customEnv() { |
|
|
|
public void genericApplicationContext_customEnv() { |
|
|
|
GenericApplicationContext ctx = |
|
|
|
GenericApplicationContext ctx = new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); |
|
|
|
new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); |
|
|
|
|
|
|
|
ctx.setEnvironment(prodEnv); |
|
|
|
ctx.setEnvironment(prodEnv); |
|
|
|
ctx.refresh(); |
|
|
|
ctx.refresh(); |
|
|
|
|
|
|
|
|
|
|
|
@ -191,11 +173,8 @@ public class EnvironmentIntegrationTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void genericXmlApplicationContext() { |
|
|
|
public void genericXmlApplicationContext() { |
|
|
|
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); |
|
|
|
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); |
|
|
|
|
|
|
|
|
|
|
|
assertHasStandardEnvironment(ctx); |
|
|
|
assertHasStandardEnvironment(ctx); |
|
|
|
|
|
|
|
|
|
|
|
ctx.setEnvironment(prodEnv); |
|
|
|
ctx.setEnvironment(prodEnv); |
|
|
|
|
|
|
|
|
|
|
|
ctx.load(XML_PATH); |
|
|
|
ctx.load(XML_PATH); |
|
|
|
ctx.refresh(); |
|
|
|
ctx.refresh(); |
|
|
|
|
|
|
|
|
|
|
|
@ -208,8 +187,7 @@ public class EnvironmentIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void classPathXmlApplicationContext() { |
|
|
|
public void classPathXmlApplicationContext() { |
|
|
|
ConfigurableApplicationContext ctx = |
|
|
|
ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(XML_PATH); |
|
|
|
new ClassPathXmlApplicationContext(new String[] { XML_PATH }); |
|
|
|
|
|
|
|
ctx.setEnvironment(prodEnv); |
|
|
|
ctx.setEnvironment(prodEnv); |
|
|
|
ctx.refresh(); |
|
|
|
ctx.refresh(); |
|
|
|
|
|
|
|
|
|
|
|
@ -228,7 +206,7 @@ public class EnvironmentIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
|
// strange - FSXAC strips leading '/' unless prefixed with 'file:'
|
|
|
|
// strange - FSXAC strips leading '/' unless prefixed with 'file:'
|
|
|
|
ConfigurableApplicationContext ctx = |
|
|
|
ConfigurableApplicationContext ctx = |
|
|
|
new FileSystemXmlApplicationContext(new String[] { "file:"+tmpFile.getPath() }, false); |
|
|
|
new FileSystemXmlApplicationContext(new String[] {"file:" + tmpFile.getPath()}, false); |
|
|
|
ctx.setEnvironment(prodEnv); |
|
|
|
ctx.setEnvironment(prodEnv); |
|
|
|
ctx.refresh(); |
|
|
|
ctx.refresh(); |
|
|
|
assertEnvironmentBeanRegistered(ctx); |
|
|
|
assertEnvironmentBeanRegistered(ctx); |
|
|
|
@ -336,11 +314,8 @@ public class EnvironmentIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void webApplicationContext() { |
|
|
|
public void webApplicationContext() { |
|
|
|
GenericWebApplicationContext ctx = |
|
|
|
GenericWebApplicationContext ctx = new GenericWebApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); |
|
|
|
new GenericWebApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertHasStandardServletEnvironment(ctx); |
|
|
|
assertHasStandardServletEnvironment(ctx); |
|
|
|
|
|
|
|
|
|
|
|
ctx.setEnvironment(prodWebEnv); |
|
|
|
ctx.setEnvironment(prodWebEnv); |
|
|
|
ctx.refresh(); |
|
|
|
ctx.refresh(); |
|
|
|
|
|
|
|
|
|
|
|
@ -588,7 +563,8 @@ public class EnvironmentIntegrationTests { |
|
|
|
try { |
|
|
|
try { |
|
|
|
ctx.refresh(); |
|
|
|
ctx.refresh(); |
|
|
|
fail("expected missing property exception"); |
|
|
|
fail("expected missing property exception"); |
|
|
|
} catch (MissingRequiredPropertiesException ex) { |
|
|
|
} |
|
|
|
|
|
|
|
catch (MissingRequiredPropertiesException ex) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -598,7 +574,6 @@ public class EnvironmentIntegrationTests { |
|
|
|
ctx.setEnvironment(new MockEnvironment().withProperty("foo", "fooValue")); |
|
|
|
ctx.setEnvironment(new MockEnvironment().withProperty("foo", "fooValue")); |
|
|
|
ctx.refresh(); // should succeed
|
|
|
|
ctx.refresh(); // should succeed
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -652,6 +627,7 @@ public class EnvironmentIntegrationTests { |
|
|
|
assertThat(ctx.getBean(EnvironmentAwareBean.class).environment, is(expectedEnv)); |
|
|
|
assertThat(ctx.getBean(EnvironmentAwareBean.class).environment, is(expectedEnv)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class EnvironmentAwareBean implements EnvironmentAware { |
|
|
|
private static class EnvironmentAwareBean implements EnvironmentAware { |
|
|
|
|
|
|
|
|
|
|
|
public Environment environment; |
|
|
|
public Environment environment; |
|
|
|
@ -660,9 +636,9 @@ public class EnvironmentIntegrationTests { |
|
|
|
public void setEnvironment(Environment environment) { |
|
|
|
public void setEnvironment(Environment environment) { |
|
|
|
this.environment = environment; |
|
|
|
this.environment = environment; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Mirrors the structure of beans and environment-specific config files |
|
|
|
* Mirrors the structure of beans and environment-specific config files |
|
|
|
* in EnvironmentIntegrationTests-context.xml |
|
|
|
* in EnvironmentIntegrationTests-context.xml |
|
|
|
@ -711,4 +687,25 @@ public class EnvironmentIntegrationTests { |
|
|
|
return new Object(); |
|
|
|
return new Object(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Constants used both locally and in scan* sub-packages |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static class Constants { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final String XML_PATH = "org/springframework/core/env/EnvironmentIntegrationTests-context.xml"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final String ENVIRONMENT_AWARE_BEAN_NAME = "envAwareBean"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final String PROD_BEAN_NAME = "prodBean"; |
|
|
|
|
|
|
|
public static final String DEV_BEAN_NAME = "devBean"; |
|
|
|
|
|
|
|
public static final String DERIVED_DEV_BEAN_NAME = "derivedDevBean"; |
|
|
|
|
|
|
|
public static final String TRANSITIVE_BEAN_NAME = "transitiveBean"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final String PROD_ENV_NAME = "prod"; |
|
|
|
|
|
|
|
public static final String DEV_ENV_NAME = "dev"; |
|
|
|
|
|
|
|
public static final String DERIVED_DEV_ENV_NAME = "derivedDev"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|