From 6ed2c658523f11d58f22a2491a724cc57e997938 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 29 Oct 2014 12:28:48 +0000 Subject: [PATCH] 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 --- .../SpringApplicationBuilderTests.java | 54 +++++++++++++++---- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java b/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java index 471ff132f3..07235dbf4f 100644 --- a/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java @@ -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(