More testing of context hierarchy creation

There actually isn't anything much we can (or should) change here. Spring always
copies PropertySources and profiles down into the child context when you set its parent.
That seems sensible. So you can add a new profile in a child context but it will always
"inherit" from its parent.

See gh-1776
This commit is contained in:
Dave Syer 2014-10-29 12:28:48 +00:00
parent 1709e79cb6
commit 6ed2c65852
1 changed files with 44 additions and 10 deletions

View File

@ -16,6 +16,15 @@
package org.springframework.boot.builder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
@ -29,19 +38,11 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StringUtils;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link SpringApplicationBuilder}.
*
@ -184,11 +185,44 @@ public class SpringApplicationBuilderTests {
ExampleConfig.class).profiles("node").properties("transport=redis")
.child(ChildConfig.class).profiles("admin").web(false);
this.context = application.run();
assertThat(this.context.getEnvironment().acceptsProfiles("node", "admin"), is(true));
assertThat(this.context.getEnvironment().acceptsProfiles("node", "admin"),
is(true));
assertThat(this.context.getParent().getEnvironment().acceptsProfiles("admin"),
is(false));
}
@Test
public void parentWithDifferentProfile() throws Exception {
SpringApplicationBuilder shared = new SpringApplicationBuilder(
ExampleConfig.class).profiles("node").properties("transport=redis");
SpringApplicationBuilder application = shared.child(ChildConfig.class)
.profiles("admin").web(false);
shared.profiles("parent");
this.context = application.run();
assertThat(this.context.getEnvironment().acceptsProfiles("node", "admin"),
is(true));
assertThat(
this.context.getParent().getEnvironment()
.acceptsProfiles("node", "parent"), is(true));
assertThat(this.context.getParent().getEnvironment().acceptsProfiles("admin"),
is(false));
}
@Test
public void parentFirstWithDifferentProfileAndExplicitEnvironment() throws Exception {
SpringApplicationBuilder application = new SpringApplicationBuilder(
ExampleConfig.class).environment(new StandardEnvironment())
.profiles("node").properties("transport=redis").child(ChildConfig.class)
.profiles("admin").web(false);
this.context = application.run();
assertThat(this.context.getEnvironment().acceptsProfiles("node", "admin"),
is(true));
// Now they share an Environment explicitly so there's no way to keep the profiles
// separate
assertThat(this.context.getParent().getEnvironment().acceptsProfiles("admin"),
is(true));
}
@Test
public void parentContextIdentical() throws Exception {
SpringApplicationBuilder application = new SpringApplicationBuilder(