Commit 6ed2c658 authored by Dave Syer's avatar Dave Syer

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
parent 1709e79c
......@@ -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(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment