|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright 2012-2019 the original author or authors.
|
|
|
|
|
* Copyright 2012-2020 the original author or authors.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
@ -24,6 +24,7 @@ import org.junit.jupiter.api.AfterEach;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.ApplicationArguments;
|
|
|
|
|
import org.springframework.boot.ApplicationContextFactory;
|
|
|
|
|
import org.springframework.boot.WebApplicationType;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
@ -68,7 +69,8 @@ class SpringApplicationBuilderTests {
|
|
|
|
|
@Test
|
|
|
|
|
void profileAndProperties() {
|
|
|
|
|
SpringApplicationBuilder application = new SpringApplicationBuilder().sources(ExampleConfig.class)
|
|
|
|
|
.contextClass(StaticApplicationContext.class).profiles("foo").properties("foo=bar");
|
|
|
|
|
.contextFactory(ApplicationContextFactory.forContextClass(StaticApplicationContext.class))
|
|
|
|
|
.profiles("foo").properties("foo=bar");
|
|
|
|
|
this.context = application.run();
|
|
|
|
|
assertThat(this.context).isInstanceOf(StaticApplicationContext.class);
|
|
|
|
|
assertThat(this.context.getEnvironment().getProperty("foo")).isEqualTo("bucket");
|
|
|
|
@ -78,7 +80,8 @@ class SpringApplicationBuilderTests {
|
|
|
|
|
@Test
|
|
|
|
|
void propertiesAsMap() {
|
|
|
|
|
SpringApplicationBuilder application = new SpringApplicationBuilder().sources(ExampleConfig.class)
|
|
|
|
|
.contextClass(StaticApplicationContext.class).properties(Collections.singletonMap("bar", "foo"));
|
|
|
|
|
.contextFactory(ApplicationContextFactory.forContextClass(StaticApplicationContext.class))
|
|
|
|
|
.properties(Collections.singletonMap("bar", "foo"));
|
|
|
|
|
this.context = application.run();
|
|
|
|
|
assertThat(this.context.getEnvironment().getProperty("bar")).isEqualTo("foo");
|
|
|
|
|
}
|
|
|
|
@ -86,7 +89,7 @@ class SpringApplicationBuilderTests {
|
|
|
|
|
@Test
|
|
|
|
|
void propertiesAsProperties() {
|
|
|
|
|
SpringApplicationBuilder application = new SpringApplicationBuilder().sources(ExampleConfig.class)
|
|
|
|
|
.contextClass(StaticApplicationContext.class)
|
|
|
|
|
.contextFactory(ApplicationContextFactory.forContextClass(StaticApplicationContext.class))
|
|
|
|
|
.properties(StringUtils.splitArrayElementsIntoProperties(new String[] { "bar=foo" }, "="));
|
|
|
|
|
this.context = application.run();
|
|
|
|
|
assertThat(this.context.getEnvironment().getProperty("bar")).isEqualTo("foo");
|
|
|
|
@ -95,7 +98,7 @@ class SpringApplicationBuilderTests {
|
|
|
|
|
@Test
|
|
|
|
|
void propertiesWithRepeatSeparator() {
|
|
|
|
|
SpringApplicationBuilder application = new SpringApplicationBuilder().sources(ExampleConfig.class)
|
|
|
|
|
.contextClass(StaticApplicationContext.class)
|
|
|
|
|
.contextFactory(ApplicationContextFactory.forContextClass(StaticApplicationContext.class))
|
|
|
|
|
.properties("one=c:\\logging.file.name", "two=a:b", "three:c:\\logging.file.name", "four:a:b");
|
|
|
|
|
this.context = application.run();
|
|
|
|
|
ConfigurableEnvironment environment = this.context.getEnvironment();
|
|
|
|
@ -106,6 +109,7 @@ class SpringApplicationBuilderTests {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
|
void specificApplicationContextClass() {
|
|
|
|
|
SpringApplicationBuilder application = new SpringApplicationBuilder().sources(ExampleConfig.class)
|
|
|
|
|
.contextClass(StaticApplicationContext.class);
|
|
|
|
@ -113,10 +117,18 @@ class SpringApplicationBuilderTests {
|
|
|
|
|
assertThat(this.context).isInstanceOf(StaticApplicationContext.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void specificApplicationContextFactory() {
|
|
|
|
|
SpringApplicationBuilder application = new SpringApplicationBuilder().sources(ExampleConfig.class)
|
|
|
|
|
.contextFactory(ApplicationContextFactory.forContextClass(StaticApplicationContext.class));
|
|
|
|
|
this.context = application.run();
|
|
|
|
|
assertThat(this.context).isInstanceOf(StaticApplicationContext.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void parentContextCreationThatIsRunDirectly() {
|
|
|
|
|
SpringApplicationBuilder application = new SpringApplicationBuilder(ChildConfig.class)
|
|
|
|
|
.contextClass(SpyApplicationContext.class);
|
|
|
|
|
.contextFactory(ApplicationContextFactory.forContextClass(SpyApplicationContext.class));
|
|
|
|
|
application.parent(ExampleConfig.class);
|
|
|
|
|
this.context = application.run("foo.bar=baz");
|
|
|
|
|
verify(((SpyApplicationContext) this.context).getApplicationContext()).setParent(any(ApplicationContext.class));
|
|
|
|
@ -129,7 +141,7 @@ class SpringApplicationBuilderTests {
|
|
|
|
|
@Test
|
|
|
|
|
void parentContextCreationThatIsBuiltThenRun() {
|
|
|
|
|
SpringApplicationBuilder application = new SpringApplicationBuilder(ChildConfig.class)
|
|
|
|
|
.contextClass(SpyApplicationContext.class);
|
|
|
|
|
.contextFactory(ApplicationContextFactory.forContextClass(SpyApplicationContext.class));
|
|
|
|
|
application.parent(ExampleConfig.class);
|
|
|
|
|
this.context = application.build("a=alpha").run("b=bravo");
|
|
|
|
|
verify(((SpyApplicationContext) this.context).getApplicationContext()).setParent(any(ApplicationContext.class));
|
|
|
|
@ -141,7 +153,8 @@ class SpringApplicationBuilderTests {
|
|
|
|
|
@Test
|
|
|
|
|
void parentContextCreationWithChildShutdown() {
|
|
|
|
|
SpringApplicationBuilder application = new SpringApplicationBuilder(ChildConfig.class)
|
|
|
|
|
.contextClass(SpyApplicationContext.class).registerShutdownHook(true);
|
|
|
|
|
.contextFactory(ApplicationContextFactory.forContextClass(SpyApplicationContext.class))
|
|
|
|
|
.registerShutdownHook(true);
|
|
|
|
|
application.parent(ExampleConfig.class);
|
|
|
|
|
this.context = application.run();
|
|
|
|
|
verify(((SpyApplicationContext) this.context).getApplicationContext()).setParent(any(ApplicationContext.class));
|
|
|
|
@ -151,7 +164,7 @@ class SpringApplicationBuilderTests {
|
|
|
|
|
@Test
|
|
|
|
|
void contextWithClassLoader() {
|
|
|
|
|
SpringApplicationBuilder application = new SpringApplicationBuilder(ExampleConfig.class)
|
|
|
|
|
.contextClass(SpyApplicationContext.class);
|
|
|
|
|
.contextFactory(ApplicationContextFactory.forContextClass(SpyApplicationContext.class));
|
|
|
|
|
ClassLoader classLoader = new URLClassLoader(new URL[0], getClass().getClassLoader());
|
|
|
|
|
application.resourceLoader(new DefaultResourceLoader(classLoader));
|
|
|
|
|
this.context = application.run();
|
|
|
|
@ -161,7 +174,7 @@ class SpringApplicationBuilderTests {
|
|
|
|
|
@Test
|
|
|
|
|
void parentContextWithClassLoader() {
|
|
|
|
|
SpringApplicationBuilder application = new SpringApplicationBuilder(ChildConfig.class)
|
|
|
|
|
.contextClass(SpyApplicationContext.class);
|
|
|
|
|
.contextFactory(ApplicationContextFactory.forContextClass(SpyApplicationContext.class));
|
|
|
|
|
ClassLoader classLoader = new URLClassLoader(new URL[0], getClass().getClassLoader());
|
|
|
|
|
application.resourceLoader(new DefaultResourceLoader(classLoader));
|
|
|
|
|
application.parent(ExampleConfig.class);
|
|
|
|
@ -173,7 +186,7 @@ class SpringApplicationBuilderTests {
|
|
|
|
|
void parentFirstCreation() {
|
|
|
|
|
SpringApplicationBuilder application = new SpringApplicationBuilder(ExampleConfig.class)
|
|
|
|
|
.child(ChildConfig.class);
|
|
|
|
|
application.contextClass(SpyApplicationContext.class);
|
|
|
|
|
application.contextFactory(ApplicationContextFactory.forContextClass(SpyApplicationContext.class));
|
|
|
|
|
this.context = application.run();
|
|
|
|
|
verify(((SpyApplicationContext) this.context).getApplicationContext()).setParent(any(ApplicationContext.class));
|
|
|
|
|
assertThat(((SpyApplicationContext) this.context).getRegisteredShutdownHook()).isFalse();
|
|
|
|
@ -230,7 +243,7 @@ class SpringApplicationBuilderTests {
|
|
|
|
|
void parentContextIdentical() {
|
|
|
|
|
SpringApplicationBuilder application = new SpringApplicationBuilder(ExampleConfig.class);
|
|
|
|
|
application.parent(ExampleConfig.class);
|
|
|
|
|
application.contextClass(SpyApplicationContext.class);
|
|
|
|
|
application.contextFactory(ApplicationContextFactory.forContextClass(SpyApplicationContext.class));
|
|
|
|
|
this.context = application.run();
|
|
|
|
|
verify(((SpyApplicationContext) this.context).getApplicationContext()).setParent(any(ApplicationContext.class));
|
|
|
|
|
}
|
|
|
|
|