Update tests to close the application contexts that they create
This commit updates a number of tests in spring-boot to ensure that they close the application contexts that they create. SimpleMainTests still create a number of contexts that are not closed as there’s no (easy) way to get hold of the context when testing the main method. See gh-4053
This commit is contained in:
		
							parent
							
								
									1bc1785db7
								
							
						
					
					
						commit
						e9878ebb5d
					
				| 
						 | 
				
			
			@ -18,9 +18,11 @@ package org.springframework.boot;
 | 
			
		|||
 | 
			
		||||
import java.io.PrintStream;
 | 
			
		||||
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Rule;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.springframework.boot.test.OutputCapture;
 | 
			
		||||
import org.springframework.context.ConfigurableApplicationContext;
 | 
			
		||||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
import org.springframework.core.env.Environment;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -35,6 +37,15 @@ import static org.junit.Assert.assertThat;
 | 
			
		|||
 */
 | 
			
		||||
public class BannerTests {
 | 
			
		||||
 | 
			
		||||
	private ConfigurableApplicationContext context;
 | 
			
		||||
 | 
			
		||||
	@After
 | 
			
		||||
	public void cleanUp() {
 | 
			
		||||
		if (this.context != null) {
 | 
			
		||||
			this.context.close();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Rule
 | 
			
		||||
	public OutputCapture out = new OutputCapture();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -42,7 +53,7 @@ public class BannerTests {
 | 
			
		|||
	public void testDefaultBanner() throws Exception {
 | 
			
		||||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		assertThat(this.out.toString(), containsString(":: Spring Boot ::"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -51,7 +62,7 @@ public class BannerTests {
 | 
			
		|||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.setBanner(new DummyBanner());
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		assertThat(this.out.toString(), containsString("My Banner"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,6 +16,7 @@
 | 
			
		|||
 | 
			
		||||
package org.springframework.boot;
 | 
			
		||||
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.springframework.boot.sampleconfig.MyComponent;
 | 
			
		||||
| 
						 | 
				
			
			@ -40,6 +41,11 @@ public class BeanDefinitionLoaderTests {
 | 
			
		|||
		this.registry = new StaticApplicationContext();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@After
 | 
			
		||||
	public void cleanUp() {
 | 
			
		||||
		this.registry.close();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void loadClass() throws Exception {
 | 
			
		||||
		BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,9 +16,10 @@
 | 
			
		|||
 | 
			
		||||
package org.springframework.boot;
 | 
			
		||||
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.context.ApplicationContext;
 | 
			
		||||
import org.springframework.context.ConfigurableApplicationContext;
 | 
			
		||||
import org.springframework.context.annotation.Bean;
 | 
			
		||||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
import org.springframework.context.annotation.Primary;
 | 
			
		||||
| 
						 | 
				
			
			@ -33,21 +34,29 @@ import static org.junit.Assert.assertEquals;
 | 
			
		|||
 */
 | 
			
		||||
public class OverrideSourcesTests {
 | 
			
		||||
 | 
			
		||||
	private ConfigurableApplicationContext context;
 | 
			
		||||
 | 
			
		||||
	@After
 | 
			
		||||
	public void cleanUp() {
 | 
			
		||||
		if (this.context != null) {
 | 
			
		||||
			this.context.close();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void beanInjectedToMainConfiguration() {
 | 
			
		||||
		ApplicationContext context = SpringApplication.run(
 | 
			
		||||
				new Object[] { MainConfiguration.class },
 | 
			
		||||
		this.context = SpringApplication.run(new Object[] { MainConfiguration.class },
 | 
			
		||||
				new String[] { "--spring.main.web_environment=false" });
 | 
			
		||||
		assertEquals("foo", context.getBean(Service.class).bean.name);
 | 
			
		||||
		assertEquals("foo", this.context.getBean(Service.class).bean.name);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void primaryBeanInjectedProvingSourcesNotOverridden() {
 | 
			
		||||
		ApplicationContext context = SpringApplication
 | 
			
		||||
		this.context = SpringApplication
 | 
			
		||||
				.run(new Object[] { MainConfiguration.class, TestConfiguration.class },
 | 
			
		||||
						new String[] { "--spring.main.web_environment=false",
 | 
			
		||||
								"--spring.main.sources=org.springframework.boot.OverrideSourcesTests.MainConfiguration" });
 | 
			
		||||
		assertEquals("bar", context.getBean(Service.class).bean.name);
 | 
			
		||||
		assertEquals("bar", this.context.getBean(Service.class).bean.name);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Configuration
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,6 +16,7 @@
 | 
			
		|||
 | 
			
		||||
package org.springframework.boot;
 | 
			
		||||
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.springframework.context.ConfigurableApplicationContext;
 | 
			
		||||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
| 
						 | 
				
			
			@ -31,17 +32,26 @@ import static org.junit.Assert.assertThat;
 | 
			
		|||
 */
 | 
			
		||||
public class ReproTests {
 | 
			
		||||
 | 
			
		||||
	private ConfigurableApplicationContext context;
 | 
			
		||||
 | 
			
		||||
	@After
 | 
			
		||||
	public void cleanUp() {
 | 
			
		||||
		if (this.context != null) {
 | 
			
		||||
			this.context.close();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void enableProfileViaApplicationProperties() throws Exception {
 | 
			
		||||
		// gh-308
 | 
			
		||||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		ConfigurableApplicationContext context = application.run(
 | 
			
		||||
		this.context = application.run(
 | 
			
		||||
				"--spring.config.name=enableprofileviaapplicationproperties",
 | 
			
		||||
				"--spring.profiles.active=dev");
 | 
			
		||||
		assertThat(context.getEnvironment().acceptsProfiles("dev"), equalTo(true));
 | 
			
		||||
		assertThat(context.getEnvironment().acceptsProfiles("a"), equalTo(true));
 | 
			
		||||
		assertThat(this.context.getEnvironment().acceptsProfiles("dev"), equalTo(true));
 | 
			
		||||
		assertThat(this.context.getEnvironment().acceptsProfiles("a"), equalTo(true));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			@ -50,8 +60,8 @@ public class ReproTests {
 | 
			
		|||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		String configName = "--spring.config.name=activeprofilerepro";
 | 
			
		||||
		assertVersionProperty(application.run(configName, "--spring.profiles.active=B"),
 | 
			
		||||
				"B", "B");
 | 
			
		||||
		this.context = application.run(configName, "--spring.profiles.active=B");
 | 
			
		||||
		assertVersionProperty(this.context, "B", "B");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			@ -60,7 +70,8 @@ public class ReproTests {
 | 
			
		|||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		String configName = "--spring.config.name=activeprofilerepro";
 | 
			
		||||
		assertVersionProperty(application.run(configName), "B", "B");
 | 
			
		||||
		this.context = application.run(configName);
 | 
			
		||||
		assertVersionProperty(this.context, "B", "B");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			@ -69,7 +80,8 @@ public class ReproTests {
 | 
			
		|||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		String configName = "--spring.config.name=activeprofilerepro-ordered";
 | 
			
		||||
		assertVersionProperty(application.run(configName), "B", "A", "B");
 | 
			
		||||
		this.context = application.run(configName);
 | 
			
		||||
		assertVersionProperty(this.context, "B", "A", "B");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			@ -78,8 +90,8 @@ public class ReproTests {
 | 
			
		|||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		String configName = "--spring.config.name=activeprofilerepro";
 | 
			
		||||
		assertVersionProperty(application.run(configName, "--spring.profiles.active=C"),
 | 
			
		||||
				"C", "C");
 | 
			
		||||
		this.context = application.run(configName, "--spring.profiles.active=C");
 | 
			
		||||
		assertVersionProperty(this.context, "C", "C");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			@ -88,9 +100,8 @@ public class ReproTests {
 | 
			
		|||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		String configName = "--spring.config.name=activeprofilerepro";
 | 
			
		||||
		assertVersionProperty(
 | 
			
		||||
				application.run(configName, "--spring.profiles.active=A,C"), "C", "A",
 | 
			
		||||
				"C");
 | 
			
		||||
		this.context = application.run(configName, "--spring.profiles.active=A,C");
 | 
			
		||||
		assertVersionProperty(this.context, "C", "A", "C");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			@ -99,9 +110,8 @@ public class ReproTests {
 | 
			
		|||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		String configName = "--spring.config.name=activeprofilerepro";
 | 
			
		||||
		assertVersionProperty(
 | 
			
		||||
				application.run(configName, "--spring.profiles.active=C,A"), "A", "C",
 | 
			
		||||
				"A");
 | 
			
		||||
		this.context = application.run(configName, "--spring.profiles.active=C,A");
 | 
			
		||||
		assertVersionProperty(this.context, "A", "C", "A");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			@ -110,8 +120,8 @@ public class ReproTests {
 | 
			
		|||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		String configName = "--spring.config.name=activeprofilerepro-without-override";
 | 
			
		||||
		assertVersionProperty(application.run(configName, "--spring.profiles.active=B"),
 | 
			
		||||
				"B", "B");
 | 
			
		||||
		this.context = application.run(configName, "--spring.profiles.active=B");
 | 
			
		||||
		assertVersionProperty(this.context, "B", "B");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			@ -120,7 +130,8 @@ public class ReproTests {
 | 
			
		|||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		String configName = "--spring.config.name=activeprofilerepro-without-override";
 | 
			
		||||
		assertVersionProperty(application.run(configName), null);
 | 
			
		||||
		this.context = application.run(configName);
 | 
			
		||||
		assertVersionProperty(this.context, null);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			@ -129,8 +140,8 @@ public class ReproTests {
 | 
			
		|||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		String configName = "--spring.config.name=activeprofilerepro-without-override";
 | 
			
		||||
		assertVersionProperty(application.run(configName, "--spring.profiles.active=C"),
 | 
			
		||||
				"C", "C");
 | 
			
		||||
		this.context = application.run(configName, "--spring.profiles.active=C");
 | 
			
		||||
		assertVersionProperty(this.context, "C", "C");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			@ -139,9 +150,8 @@ public class ReproTests {
 | 
			
		|||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		String configName = "--spring.config.name=activeprofilerepro-without-override";
 | 
			
		||||
		assertVersionProperty(
 | 
			
		||||
				application.run(configName, "--spring.profiles.active=A,C"), "C", "A",
 | 
			
		||||
				"C");
 | 
			
		||||
		this.context = application.run(configName, "--spring.profiles.active=A,C");
 | 
			
		||||
		assertVersionProperty(this.context, "C", "A", "C");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			@ -150,9 +160,8 @@ public class ReproTests {
 | 
			
		|||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		String configName = "--spring.config.name=activeprofilerepro-without-override";
 | 
			
		||||
		assertVersionProperty(
 | 
			
		||||
				application.run(configName, "--spring.profiles.active=C,A"), "A", "C",
 | 
			
		||||
				"A");
 | 
			
		||||
		this.context = application.run(configName, "--spring.profiles.active=C,A");
 | 
			
		||||
		assertVersionProperty(this.context, "A", "C", "A");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private void assertVersionProperty(ConfigurableApplicationContext context,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -162,7 +162,7 @@ public class SpringApplicationTests {
 | 
			
		|||
		SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.setShowBanner(false);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		verify(application, never()).printBanner((Environment) anyObject());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -170,7 +170,7 @@ public class SpringApplicationTests {
 | 
			
		|||
	public void disableBannerViaProperty() throws Exception {
 | 
			
		||||
		SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.run("--spring.main.show_banner=false");
 | 
			
		||||
		this.context = application.run("--spring.main.show_banner=false");
 | 
			
		||||
		verify(application, never()).printBanner((Environment) anyObject());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -178,7 +178,7 @@ public class SpringApplicationTests {
 | 
			
		|||
	public void customBanner() throws Exception {
 | 
			
		||||
		SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.run("--banner.location=classpath:test-banner.txt");
 | 
			
		||||
		this.context = application.run("--banner.location=classpath:test-banner.txt");
 | 
			
		||||
		assertThat(this.output.toString(), startsWith("Running a Test!"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -186,7 +186,8 @@ public class SpringApplicationTests {
 | 
			
		|||
	public void customBannerWithProperties() throws Exception {
 | 
			
		||||
		SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.run("--banner.location=classpath:test-banner-with-placeholder.txt",
 | 
			
		||||
		this.context = application.run(
 | 
			
		||||
				"--banner.location=classpath:test-banner-with-placeholder.txt",
 | 
			
		||||
				"--test.property=123456");
 | 
			
		||||
		assertThat(this.output.toString(),
 | 
			
		||||
				startsWith(String.format("Running a Test!%n%n123456")));
 | 
			
		||||
| 
						 | 
				
			
			@ -308,7 +309,7 @@ public class SpringApplicationTests {
 | 
			
		|||
		application.setWebEnvironment(false);
 | 
			
		||||
		ConfigurableEnvironment environment = new StandardEnvironment();
 | 
			
		||||
		application.setEnvironment(environment);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		verify(application.getLoader()).setEnvironment(environment);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -351,7 +352,7 @@ public class SpringApplicationTests {
 | 
			
		|||
		application.setWebEnvironment(false);
 | 
			
		||||
		ConfigurableEnvironment environment = new StandardEnvironment();
 | 
			
		||||
		application.setEnvironment(environment);
 | 
			
		||||
		application.run("--foo=bar");
 | 
			
		||||
		this.context = application.run("--foo=bar");
 | 
			
		||||
		assertTrue(hasPropertySource(environment, CommandLinePropertySource.class,
 | 
			
		||||
				"commandLineArgs"));
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -365,7 +366,7 @@ public class SpringApplicationTests {
 | 
			
		|||
				new MapPropertySource("commandLineArgs", Collections
 | 
			
		||||
						.<String, Object>singletonMap("foo", "original")));
 | 
			
		||||
		application.setEnvironment(environment);
 | 
			
		||||
		application.run("--foo=bar", "--bar=foo");
 | 
			
		||||
		this.context = application.run("--foo=bar", "--bar=foo");
 | 
			
		||||
		assertTrue(hasPropertySource(environment, CompositePropertySource.class,
 | 
			
		||||
				"commandLineArgs"));
 | 
			
		||||
		assertEquals("foo", environment.getProperty("bar"));
 | 
			
		||||
| 
						 | 
				
			
			@ -379,7 +380,7 @@ public class SpringApplicationTests {
 | 
			
		|||
		application.setWebEnvironment(false);
 | 
			
		||||
		ConfigurableEnvironment environment = new StandardEnvironment();
 | 
			
		||||
		application.setEnvironment(environment);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		assertEquals("bucket", environment.getProperty("foo"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -390,7 +391,7 @@ public class SpringApplicationTests {
 | 
			
		|||
		application.setAdditionalProfiles("foo");
 | 
			
		||||
		ConfigurableEnvironment environment = new StandardEnvironment();
 | 
			
		||||
		application.setEnvironment(environment);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		assertTrue(environment.acceptsProfiles("foo"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -401,7 +402,7 @@ public class SpringApplicationTests {
 | 
			
		|||
		application.setAdditionalProfiles("foo");
 | 
			
		||||
		ConfigurableEnvironment environment = new StandardEnvironment();
 | 
			
		||||
		application.setEnvironment(environment);
 | 
			
		||||
		application.run("--spring.profiles.active=bar,spam");
 | 
			
		||||
		this.context = application.run("--spring.profiles.active=bar,spam");
 | 
			
		||||
		// Command line should always come last
 | 
			
		||||
		assertArrayEquals(new String[] { "foo", "bar", "spam" },
 | 
			
		||||
				environment.getActiveProfiles());
 | 
			
		||||
| 
						 | 
				
			
			@ -414,7 +415,7 @@ public class SpringApplicationTests {
 | 
			
		|||
		application.setAdditionalProfiles("other");
 | 
			
		||||
		ConfigurableEnvironment environment = new StandardEnvironment();
 | 
			
		||||
		application.setEnvironment(environment);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		// Active profile should win over default
 | 
			
		||||
		assertEquals("fromotherpropertiesfile", environment.getProperty("my.property"));
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -425,7 +426,7 @@ public class SpringApplicationTests {
 | 
			
		|||
		application.setWebEnvironment(false);
 | 
			
		||||
		ConfigurableEnvironment environment = new StandardEnvironment();
 | 
			
		||||
		application.setEnvironment(environment);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		assertEquals("bucket", environment.getProperty("foo"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -436,7 +437,7 @@ public class SpringApplicationTests {
 | 
			
		|||
		application.setAddCommandLineProperties(false);
 | 
			
		||||
		ConfigurableEnvironment environment = new StandardEnvironment();
 | 
			
		||||
		application.setEnvironment(environment);
 | 
			
		||||
		application.run("--foo=bar");
 | 
			
		||||
		this.context = application.run("--foo=bar");
 | 
			
		||||
		assertFalse(hasPropertySource(environment, PropertySource.class,
 | 
			
		||||
				"commandLineArgs"));
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -457,7 +458,7 @@ public class SpringApplicationTests {
 | 
			
		|||
		TestSpringApplication application = new TestSpringApplication(sources);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.setUseMockLoader(true);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		Set<Object> initialSources = application.getSources();
 | 
			
		||||
		assertThat(initialSources.toArray(), equalTo(sources));
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -467,7 +468,7 @@ public class SpringApplicationTests {
 | 
			
		|||
		Object[] sources = { "classpath:org/springframework/boot/sample-${sample.app.test.prop}.xml" };
 | 
			
		||||
		TestSpringApplication application = new TestSpringApplication(sources);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			@ -487,18 +488,18 @@ public class SpringApplicationTests {
 | 
			
		|||
	public void exit() throws Exception {
 | 
			
		||||
		SpringApplication application = new SpringApplication(ExampleConfig.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		ApplicationContext context = application.run();
 | 
			
		||||
		assertNotNull(context);
 | 
			
		||||
		assertEquals(0, SpringApplication.exit(context));
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		assertNotNull(this.context);
 | 
			
		||||
		assertEquals(0, SpringApplication.exit(this.context));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void exitWithExplicitCode() throws Exception {
 | 
			
		||||
		SpringApplication application = new SpringApplication(ExampleConfig.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		ApplicationContext context = application.run();
 | 
			
		||||
		assertNotNull(context);
 | 
			
		||||
		assertEquals(2, SpringApplication.exit(context, new ExitCodeGenerator() {
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		assertNotNull(this.context);
 | 
			
		||||
		assertEquals(2, SpringApplication.exit(this.context, new ExitCodeGenerator() {
 | 
			
		||||
			@Override
 | 
			
		||||
			public int getExitCode() {
 | 
			
		||||
				return 2;
 | 
			
		||||
| 
						 | 
				
			
			@ -522,7 +523,7 @@ public class SpringApplicationTests {
 | 
			
		|||
	public void commandLineArgsApplyToSpringApplication() throws Exception {
 | 
			
		||||
		TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.run("--spring.main.show_banner=false");
 | 
			
		||||
		this.context = application.run("--spring.main.show_banner=false");
 | 
			
		||||
		assertThat(application.getShowBanner(), is(false));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -583,7 +584,7 @@ public class SpringApplicationTests {
 | 
			
		|||
	public void headless() throws Exception {
 | 
			
		||||
		TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		assertThat(System.getProperty("java.awt.headless"), equalTo("true"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -592,7 +593,7 @@ public class SpringApplicationTests {
 | 
			
		|||
		TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.setHeadless(false);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		assertThat(System.getProperty("java.awt.headless"), equalTo("false"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -601,7 +602,7 @@ public class SpringApplicationTests {
 | 
			
		|||
		System.setProperty("java.awt.headless", "false");
 | 
			
		||||
		TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		assertThat(System.getProperty("java.awt.headless"), equalTo("false"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -78,8 +78,13 @@ public class ConfigFileEnvironmentPostProcessorTests {
 | 
			
		|||
	@Rule
 | 
			
		||||
	public ExpectedException expected = ExpectedException.none();
 | 
			
		||||
 | 
			
		||||
	private ConfigurableApplicationContext context;
 | 
			
		||||
 | 
			
		||||
	@After
 | 
			
		||||
	public void cleanup() {
 | 
			
		||||
		if (this.context != null) {
 | 
			
		||||
			this.context.close();
 | 
			
		||||
		}
 | 
			
		||||
		System.clearProperty("the.property");
 | 
			
		||||
		System.clearProperty("spring.config.location");
 | 
			
		||||
		System.clearProperty("spring.main.showBanner");
 | 
			
		||||
| 
						 | 
				
			
			@ -600,13 +605,12 @@ public class ConfigFileEnvironmentPostProcessorTests {
 | 
			
		|||
	public void activateProfileFromProfileSpecificProperties() throws Exception {
 | 
			
		||||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		ConfigurableApplicationContext context = application
 | 
			
		||||
				.run("--spring.profiles.active=includeprofile");
 | 
			
		||||
		assertThat(context.getEnvironment(), acceptsProfiles("includeprofile"));
 | 
			
		||||
		assertThat(context.getEnvironment(), acceptsProfiles("specific"));
 | 
			
		||||
		assertThat(context.getEnvironment(), acceptsProfiles("morespecific"));
 | 
			
		||||
		assertThat(context.getEnvironment(), acceptsProfiles("yetmorespecific"));
 | 
			
		||||
		assertThat(context.getEnvironment(), not(acceptsProfiles("missing")));
 | 
			
		||||
		this.context = application.run("--spring.profiles.active=includeprofile");
 | 
			
		||||
		assertThat(this.context.getEnvironment(), acceptsProfiles("includeprofile"));
 | 
			
		||||
		assertThat(this.context.getEnvironment(), acceptsProfiles("specific"));
 | 
			
		||||
		assertThat(this.context.getEnvironment(), acceptsProfiles("morespecific"));
 | 
			
		||||
		assertThat(this.context.getEnvironment(), acceptsProfiles("yetmorespecific"));
 | 
			
		||||
		assertThat(this.context.getEnvironment(), not(acceptsProfiles("missing")));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
| 
						 | 
				
			
			@ -614,9 +618,9 @@ public class ConfigFileEnvironmentPostProcessorTests {
 | 
			
		|||
		// gh-340
 | 
			
		||||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		ConfigurableApplicationContext context = application
 | 
			
		||||
		this.context = application
 | 
			
		||||
				.run("--spring.profiles.active=activeprofilewithsubdoc");
 | 
			
		||||
		String property = context.getEnvironment().getProperty("foobar");
 | 
			
		||||
		String property = this.context.getEnvironment().getProperty("foobar");
 | 
			
		||||
		assertThat(property, equalTo("baz"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -28,6 +28,7 @@ import org.springframework.boot.ansi.AnsiOutput.Enabled;
 | 
			
		|||
import org.springframework.boot.ansi.AnsiOutputEnabledValue;
 | 
			
		||||
import org.springframework.boot.context.config.AnsiOutputApplicationListener;
 | 
			
		||||
import org.springframework.boot.test.EnvironmentTestUtils;
 | 
			
		||||
import org.springframework.context.ConfigurableApplicationContext;
 | 
			
		||||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
import org.springframework.core.env.ConfigurableEnvironment;
 | 
			
		||||
import org.springframework.core.env.StandardEnvironment;
 | 
			
		||||
| 
						 | 
				
			
			@ -42,12 +43,21 @@ import static org.junit.Assert.assertThat;
 | 
			
		|||
 */
 | 
			
		||||
public class AnsiOutputApplicationListenerTests {
 | 
			
		||||
 | 
			
		||||
	private ConfigurableApplicationContext context;
 | 
			
		||||
 | 
			
		||||
	@Before
 | 
			
		||||
	@After
 | 
			
		||||
	public void resetAnsi() {
 | 
			
		||||
		AnsiOutput.setEnabled(Enabled.DETECT);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@After
 | 
			
		||||
	public void cleanUp() {
 | 
			
		||||
		resetAnsi();
 | 
			
		||||
		if (this.context != null) {
 | 
			
		||||
			this.context.close();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void enabled() {
 | 
			
		||||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
| 
						 | 
				
			
			@ -55,7 +65,7 @@ public class AnsiOutputApplicationListenerTests {
 | 
			
		|||
		Map<String, Object> props = new HashMap<String, Object>();
 | 
			
		||||
		props.put("spring.output.ansi.enabled", "ALWAYS");
 | 
			
		||||
		application.setDefaultProperties(props);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		assertThat(AnsiOutputEnabledValue.get(), equalTo(Enabled.ALWAYS));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -66,7 +76,7 @@ public class AnsiOutputApplicationListenerTests {
 | 
			
		|||
		Map<String, Object> props = new HashMap<String, Object>();
 | 
			
		||||
		props.put("spring.output.ansi.enabled", "never");
 | 
			
		||||
		application.setDefaultProperties(props);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		assertThat(AnsiOutputEnabledValue.get(), equalTo(Enabled.NEVER));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -77,7 +87,7 @@ public class AnsiOutputApplicationListenerTests {
 | 
			
		|||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.setEnvironment(environment);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		assertThat(AnsiOutputEnabledValue.get(), equalTo(Enabled.NEVER));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,8 +18,10 @@ package org.springframework.boot.liquibase;
 | 
			
		|||
 | 
			
		||||
import java.lang.reflect.Field;
 | 
			
		||||
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.springframework.boot.SpringApplication;
 | 
			
		||||
import org.springframework.context.ConfigurableApplicationContext;
 | 
			
		||||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
import org.springframework.util.ReflectionUtils;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -35,11 +37,20 @@ import static org.junit.Assert.assertThat;
 | 
			
		|||
 */
 | 
			
		||||
public class LiquibaseServiceLocatorApplicationListenerTests {
 | 
			
		||||
 | 
			
		||||
	private ConfigurableApplicationContext context;
 | 
			
		||||
 | 
			
		||||
	@After
 | 
			
		||||
	public void cleanUp() {
 | 
			
		||||
		if (this.context != null) {
 | 
			
		||||
			this.context.close();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void replacesServiceLocator() throws Exception {
 | 
			
		||||
		SpringApplication application = new SpringApplication(Conf.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		application.run();
 | 
			
		||||
		this.context = application.run();
 | 
			
		||||
		ServiceLocator instance = ServiceLocator.getInstance();
 | 
			
		||||
		Field field = ReflectionUtils.findField(ServiceLocator.class, "classResolver");
 | 
			
		||||
		field.setAccessible(true);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue