Commit e1ef2a59 authored by Phillip Webb's avatar Phillip Webb

Fixup tests to use new ApplicationContextTester

Update existing tests that previously use `ContextLoader` to the newly
introduced `*ApplicationContextTester` classes.

See gh-9634
parent 24d08606
......@@ -23,12 +23,14 @@ import com.hazelcast.client.impl.HazelcastClientProxy;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import org.assertj.core.api.Condition;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.ApplicationContextTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -59,68 +61,72 @@ public class HazelcastAutoConfigurationClientTests {
}
}
private final ContextLoader contextLoader = ContextLoader.standard()
.autoConfig(HazelcastAutoConfiguration.class);
private final ApplicationContextTester context = new ApplicationContextTester()
.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class));
@Test
public void systemProperty() throws IOException {
this.contextLoader
.systemProperty(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY,
"classpath:org/springframework/boot/autoconfigure/hazelcast/"
+ "hazelcast-client-specific.xml")
.load(context -> {
HazelcastInstance hazelcastInstance = context
.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance)
.isInstanceOf(HazelcastClientProxy.class);
assertThat(hazelcastInstance.getName()).startsWith("hz.client_");
this.context
.withSystemProperties(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/"
+ "hazelcast-client-specific.xml")
.run((loaded) -> {
assertThat(loaded).getBean(HazelcastInstance.class)
.isInstanceOf(HazelcastInstance.class)
.has(nameStartingWith("hz.client_"));
});
}
@Test
public void explicitConfigFile() throws IOException {
this.contextLoader
.env("spring.hazelcast.config=org/springframework/boot/autoconfigure/"
+ "hazelcast/hazelcast-client-specific.xml")
.load(context -> {
HazelcastInstance hazelcastInstance = context
.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance)
.isInstanceOf(HazelcastClientProxy.class);
assertThat(hazelcastInstance.getName()).startsWith("hz.client_");
this.context
.withPropertyValues(
"spring.hazelcast.config=org/springframework/boot/autoconfigure/"
+ "hazelcast/hazelcast-client-specific.xml")
.run((loaded) -> {
assertThat(loaded).getBean(HazelcastInstance.class)
.isInstanceOf(HazelcastClientProxy.class)
.has(nameStartingWith("hz.client_"));
});
}
@Test
public void explicitConfigUrl() throws IOException {
this.contextLoader.env("spring.hazelcast.config=hazelcast-client-default.xml")
.load(context -> {
HazelcastInstance hazelcastInstance = context
.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance)
.isInstanceOf(HazelcastClientProxy.class);
assertThat(hazelcastInstance.getName()).startsWith("hz.client_");
this.context
.withPropertyValues(
"spring.hazelcast.config=hazelcast-client-default.xml")
.run((loaded) -> {
assertThat(loaded).getBean(HazelcastInstance.class)
.isInstanceOf(HazelcastClientProxy.class)
.has(nameStartingWith("hz.client_"));
});
}
@Test
public void unknownConfigFile() {
this.contextLoader.env("spring.hazelcast.config=foo/bar/unknown.xml").loadAndFail(
BeanCreationException.class,
ex -> assertThat(ex.getMessage()).contains("foo/bar/unknown.xml"));
this.context.withPropertyValues("spring.hazelcast.config=foo/bar/unknown.xml")
.run((loaded) -> {
assertThat(loaded).getFailure()
.isInstanceOf(BeanCreationException.class)
.hasMessageContaining("foo/bar/unknown.xml");
});
}
@Test
public void clientConfigTakesPrecedence() {
this.contextLoader.config(HazelcastServerAndClientConfig.class)
.env("spring.hazelcast.config=this-is-ignored.xml").load(context -> {
HazelcastInstance hazelcastInstance = context
.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance)
this.context.withUserConfiguration(HazelcastServerAndClientConfig.class)
.withPropertyValues("spring.hazelcast.config=this-is-ignored.xml")
.run((loaded) -> {
assertThat(loaded).getBean(HazelcastInstance.class)
.isInstanceOf(HazelcastClientProxy.class);
});
}
private Condition<HazelcastInstance> nameStartingWith(String prefix) {
return new Condition<HazelcastInstance>((o) -> o.getName().startsWith(prefix),
"Name starts with " + prefix);
}
@Configuration
static class HazelcastServerAndClientConfig {
......
......@@ -27,7 +27,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.ApplicationContextTester;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.context.annotation.Bean;
......@@ -45,44 +46,38 @@ import static org.assertj.core.api.Assertions.assertThat;
@ClassPathExclusions("hazelcast-client-*.jar")
public class HazelcastAutoConfigurationServerTests {
private final ContextLoader contextLoader = ContextLoader.standard()
.autoConfig(HazelcastAutoConfiguration.class);
private final ApplicationContextTester context = new ApplicationContextTester()
.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class));
@Test
public void defaultConfigFile() throws IOException {
// hazelcast.xml present in root classpath
this.contextLoader.load(context -> {
HazelcastInstance hazelcastInstance = context
.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance.getConfig().getConfigurationUrl())
this.context.run((loaded) -> {
Config config = loaded.getBean(HazelcastInstance.class).getConfig();
assertThat(config.getConfigurationUrl())
.isEqualTo(new ClassPathResource("hazelcast.xml").getURL());
});
}
@Test
public void systemProperty() throws IOException {
this.contextLoader
.systemProperty(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY,
"classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml")
.load(context -> {
HazelcastInstance hazelcastInstance = context
.getBean(HazelcastInstance.class);
Map<String, QueueConfig> queueConfigs = hazelcastInstance.getConfig()
.getQueueConfigs();
assertThat(queueConfigs).hasSize(1).containsKey("foobar");
this.context
.withSystemProperties(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml")
.run((loaded) -> {
Config config = loaded.getBean(HazelcastInstance.class).getConfig();
assertThat(config.getQueueConfigs().keySet()).containsOnly("foobar");
});
}
@Test
public void explicitConfigFile() throws IOException {
this.contextLoader
.env("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
this.context.withPropertyValues(
"spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
+ "hazelcast-specific.xml")
.load(context -> {
HazelcastInstance hazelcastInstance = context
.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance.getConfig().getConfigurationFile())
.run((loaded) -> {
Config config = loaded.getBean(HazelcastInstance.class).getConfig();
assertThat(config.getConfigurationFile())
.isEqualTo(new ClassPathResource(
"org/springframework/boot/autoconfigure/hazelcast"
+ "/hazelcast-specific.xml").getFile());
......@@ -91,54 +86,53 @@ public class HazelcastAutoConfigurationServerTests {
@Test
public void explicitConfigUrl() throws IOException {
this.contextLoader.env("spring.hazelcast.config=hazelcast-default.xml")
.load(context -> {
HazelcastInstance hazelcastInstance = context
.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance.getConfig().getConfigurationUrl())
.isEqualTo(new ClassPathResource("hazelcast-default.xml")
.getURL());
this.context.withPropertyValues("spring.hazelcast.config=hazelcast-default.xml")
.run((loaded) -> {
Config config = loaded.getBean(HazelcastInstance.class).getConfig();
assertThat(config.getConfigurationUrl()).isEqualTo(
new ClassPathResource("hazelcast-default.xml").getURL());
});
}
@Test
public void unknownConfigFile() {
this.contextLoader.env("spring.hazelcast.config=foo/bar/unknown.xml").loadAndFail(
BeanCreationException.class,
ex -> assertThat(ex.getMessage()).contains("foo/bar/unknown.xml"));
this.context.withPropertyValues("spring.hazelcast.config=foo/bar/unknown.xml")
.run((loaded) -> {
assertThat(loaded).getFailure()
.isInstanceOf(BeanCreationException.class)
.hasMessageContaining("foo/bar/unknown.xml");
});
}
@Test
public void configInstanceWithName() {
Config config = new Config("my-test-instance");
HazelcastInstance existingHazelcastInstance = Hazelcast
.newHazelcastInstance(config);
HazelcastInstance existing = Hazelcast.newHazelcastInstance(config);
try {
this.contextLoader.config(HazelcastConfigWithName.class)
.env("spring.hazelcast.config=this-is-ignored.xml").load(context -> {
HazelcastInstance hazelcastInstance = context
this.context.withUserConfiguration(HazelcastConfigWithName.class)
.withPropertyValues("spring.hazelcast.config=this-is-ignored.xml")
.run(loaded -> {
HazelcastInstance hazelcast = (loaded)
.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance.getConfig().getInstanceName())
assertThat(hazelcast.getConfig().getInstanceName())
.isEqualTo("my-test-instance");
// Should reuse any existing instance by default.
assertThat(hazelcastInstance)
.isEqualTo(existingHazelcastInstance);
assertThat(hazelcast).isEqualTo(existing);
});
}
finally {
existingHazelcastInstance.shutdown();
existing.shutdown();
}
}
@Test
public void configInstanceWithoutName() {
this.contextLoader.config(HazelcastConfigNoName.class)
.env("spring.hazelcast.config=this-is-ignored.xml").load(context -> {
HazelcastInstance hazelcastInstance = context
.getBean(HazelcastInstance.class);
Map<String, QueueConfig> queueConfigs = hazelcastInstance.getConfig()
.getQueueConfigs();
assertThat(queueConfigs).hasSize(1).containsKey("another-queue");
this.context.withUserConfiguration(HazelcastConfigNoName.class)
.withPropertyValues("spring.hazelcast.config=this-is-ignored.xml")
.run((loaded) -> {
Config config = loaded.getBean(HazelcastInstance.class).getConfig();
Map<String, QueueConfig> queueConfigs = config.getQueueConfigs();
assertThat(queueConfigs.keySet()).containsOnly("another-queue");
});
}
......
......@@ -18,10 +18,12 @@ package org.springframework.boot.autoconfigure.hazelcast;
import java.io.IOException;
import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastInstance;
import org.junit.Test;
import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.ApplicationContextTester;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -33,16 +35,15 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class HazelcastAutoConfigurationTests {
private final ContextLoader contextLoader = ContextLoader.standard()
.autoConfig(HazelcastAutoConfiguration.class);
private final ApplicationContextTester context = new ApplicationContextTester()
.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class));
@Test
public void defaultConfigFile() throws IOException {
// no hazelcast-client.xml and hazelcast.xml is present in root classpath
this.contextLoader.load(context -> {
HazelcastInstance hazelcastInstance = context
.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance.getConfig().getConfigurationUrl())
this.context.run((loaded) -> {
Config config = loaded.getBean(HazelcastInstance.class).getConfig();
assertThat(config.getConfigurationUrl())
.isEqualTo(new ClassPathResource("hazelcast.xml").getURL());
});
}
......
......@@ -37,9 +37,10 @@ import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.test.context.ContextConsumer;
import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.ApplicationContextTester;
import org.springframework.boot.test.context.AssertableApplicationContext;
import org.springframework.boot.test.context.HidePackagesClassLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -57,22 +58,22 @@ import static org.mockito.Mockito.mock;
*/
public class DataSourceAutoConfigurationTests {
private final ContextLoader contextLoader = ContextLoader.standard()
.autoConfig(DataSourceAutoConfiguration.class)
.env("spring.datasource.initialize=false",
private final ApplicationContextTester context = new ApplicationContextTester()
.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
.withPropertyValues("spring.datasource.initialize=false",
"spring.datasource.url:jdbc:hsqldb:mem:testdb-"
+ new Random().nextInt());
@Test
public void testDefaultDataSourceExists() throws Exception {
this.contextLoader.load(
context -> assertThat(context.getBean(DataSource.class)).isNotNull());
this.context
.run((loaded) -> assertThat(loaded).hasSingleBean(DataSource.class));
}
@Test
public void testDataSourceHasEmbeddedDefault() throws Exception {
this.contextLoader.load(context -> {
HikariDataSource dataSource = context.getBean(HikariDataSource.class);
this.context.run((loaded) -> {
HikariDataSource dataSource = loaded.getBean(HikariDataSource.class);
assertThat(dataSource.getJdbcUrl()).isNotNull();
assertThat(dataSource.getDriverClassName()).isNotNull();
});
......@@ -82,8 +83,11 @@ public class DataSourceAutoConfigurationTests {
public void testBadUrl() throws Exception {
try {
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
this.contextLoader.env("spring.datasource.url:jdbc:not-going-to-work")
.loadAndFail(BeanCreationException.class, ex -> {
this.context
.withPropertyValues("spring.datasource.url:jdbc:not-going-to-work")
.run((loaded) -> {
assertThat(loaded).getFailure()
.isInstanceOf(BeanCreationException.class);
});
}
finally {
......@@ -93,10 +97,14 @@ public class DataSourceAutoConfigurationTests {
@Test
public void testBadDriverClass() throws Exception {
this.contextLoader.env("spring.datasource.driverClassName:org.none.jdbcDriver")
.loadAndFail(BeanCreationException.class,
ex -> assertThat(ex.getMessage())
.contains("org.none.jdbcDriver"));
this.context
.withPropertyValues(
"spring.datasource.driverClassName:org.none.jdbcDriver")
.run((loaded) -> {
assertThat(loaded).getFailure()
.isInstanceOf(BeanCreationException.class)
.hasMessageContaining("org.none.jdbcDriver");
});
}
@Test
......@@ -111,14 +119,14 @@ public class DataSourceAutoConfigurationTests {
public void tomcatIsFallback() throws Exception {
assertDataSource(org.apache.tomcat.jdbc.pool.DataSource.class,
Collections.singletonList("com.zaxxer.hikari"),
dataSource -> assertThat(dataSource.getUrl())
(dataSource) -> assertThat(dataSource.getUrl())
.startsWith("jdbc:hsqldb:mem:testdb"));
}
@Test
public void tomcatValidatesConnectionByDefault() {
assertDataSource(org.apache.tomcat.jdbc.pool.DataSource.class,
Collections.singletonList("com.zaxxer.hikari"), dataSource -> {
Collections.singletonList("com.zaxxer.hikari"), (dataSource) -> {
assertThat(dataSource.isTestOnBorrow()).isTrue();
assertThat(dataSource.getValidationQuery())
.isEqualTo(DatabaseDriver.HSQLDB.getValidationQuery());
......@@ -129,14 +137,14 @@ public class DataSourceAutoConfigurationTests {
public void commonsDbcp2IsFallback() throws Exception {
assertDataSource(BasicDataSource.class,
Arrays.asList("com.zaxxer.hikari", "org.apache.tomcat"),
dataSource -> assertThat(dataSource.getUrl())
(dataSource) -> assertThat(dataSource.getUrl())
.startsWith("jdbc:hsqldb:mem:testdb"));
}
@Test
public void commonsDbcp2ValidatesConnectionByDefault() throws Exception {
assertDataSource(org.apache.commons.dbcp2.BasicDataSource.class,
Arrays.asList("com.zaxxer.hikari", "org.apache.tomcat"), dataSource -> {
Arrays.asList("com.zaxxer.hikari", "org.apache.tomcat"), (dataSource) -> {
assertThat(dataSource.getTestOnBorrow()).isEqualTo(true);
assertThat(dataSource.getValidationQuery()).isNull(); // Use
// Connection#isValid()
......@@ -144,12 +152,12 @@ public class DataSourceAutoConfigurationTests {
}
@Test
@SuppressWarnings("resource")
public void testEmbeddedTypeDefaultsUsername() throws Exception {
this.contextLoader.env("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
"spring.datasource.url:jdbc:hsqldb:mem:testdb").load(context -> {
DataSource bean = context.getBean(DataSource.class);
assertThat(bean).isNotNull();
@SuppressWarnings("resource")
this.context.withPropertyValues(
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
"spring.datasource.url:jdbc:hsqldb:mem:testdb").run((loaded) -> {
DataSource bean = loaded.getBean(DataSource.class);
HikariDataSource pool = (HikariDataSource) bean;
assertThat(pool.getDriverClassName())
.isEqualTo("org.hsqldb.jdbcDriver");
......@@ -163,62 +171,62 @@ public class DataSourceAutoConfigurationTests {
*/
@Test
public void explicitTypeNoSupportedDataSource() {
this.contextLoader
.classLoader(new HidePackagesClassLoader("org.apache.tomcat",
this.context
.withClassLoader(new HidePackagesClassLoader("org.apache.tomcat",
"com.zaxxer.hikari", "org.apache.commons.dbcp",
"org.apache.commons.dbcp2"))
.env("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
.withPropertyValues(
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
"spring.datasource.type:"
+ SimpleDriverDataSource.class.getName())
.load(testExplicitType());
.run(this::containsOnlySimpleDriverDataSource);
}
@Test
public void explicitTypeSupportedDataSource() {
this.contextLoader
.env("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
this.context
.withPropertyValues(
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
"spring.datasource.type:"
+ SimpleDriverDataSource.class.getName())
.load(testExplicitType());
.run(this::containsOnlySimpleDriverDataSource);
}
private ContextConsumer testExplicitType() {
return context -> {
assertThat(context.getBeansOfType(DataSource.class)).hasSize(1);
DataSource bean = context.getBean(DataSource.class);
assertThat(bean).isNotNull();
assertThat(bean.getClass()).isEqualTo(SimpleDriverDataSource.class);
};
private void containsOnlySimpleDriverDataSource(AssertableApplicationContext loaded) {
assertThat(loaded).hasSingleBean(DataSource.class);
assertThat(loaded).getBean(DataSource.class)
.isExactlyInstanceOf(SimpleDriverDataSource.class);
}
@Test
public void testExplicitDriverClassClearsUsername() throws Exception {
this.contextLoader.env(
this.context.withPropertyValues(
"spring.datasource.driverClassName:" + DatabaseTestDriver.class.getName(),
"spring.datasource.url:jdbc:foo://localhost").load(context -> {
DataSource dataSource = context.getBean(DataSource.class);
assertThat(dataSource).isNotNull();
assertThat(((HikariDataSource) dataSource).getDriverClassName())
"spring.datasource.url:jdbc:foo://localhost").run((loaded) -> {
assertThat(loaded).hasSingleBean(DataSource.class);
HikariDataSource dataSource = loaded.getBean(HikariDataSource.class);
assertThat(dataSource.getDriverClassName())
.isEqualTo(DatabaseTestDriver.class.getName());
assertThat(((HikariDataSource) dataSource).getUsername()).isNull();
assertThat(dataSource.getUsername()).isNull();
});
}
@Test
public void testDefaultDataSourceCanBeOverridden() throws Exception {
this.contextLoader.config(TestDataSourceConfiguration.class).load(context -> {
DataSource dataSource = context.getBean(DataSource.class);
assertThat(dataSource).isInstanceOf(BasicDataSource.class);
});
this.context.withUserConfiguration(TestDataSourceConfiguration.class)
.run((loaded) -> {
assertThat(loaded).getBean(DataSource.class)
.isInstanceOf(BasicDataSource.class);
});
}
@Test
public void testDataSourceIsInitializedEarly() {
this.contextLoader.config(TestInitializedDataSourceConfiguration.class)
.env("spring.datasource.initialize=true")
.load(context -> assertThat(context
this.context.withUserConfiguration(TestInitializedDataSourceConfiguration.class)
.withPropertyValues("spring.datasource.initialize=true")
.run((loaded) -> assertThat(loaded
.getBean(TestInitializedDataSourceConfiguration.class).called)
.isTrue());
}
......@@ -227,8 +235,8 @@ public class DataSourceAutoConfigurationTests {
List<String> hiddenPackages, Consumer<T> consumer) {
HidePackagesClassLoader classLoader = new HidePackagesClassLoader(
hiddenPackages.toArray(new String[hiddenPackages.size()]));
this.contextLoader.classLoader(classLoader).load(context -> {
DataSource bean = context.getBean(DataSource.class);
this.context.withClassLoader(classLoader).run((loaded) -> {
DataSource bean = loaded.getBean(DataSource.class);
assertThat(bean).isInstanceOf(expectedType);
consumer.accept(expectedType.cast(bean));
});
......
......@@ -23,8 +23,9 @@ import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.ApplicationContextTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -41,67 +42,59 @@ import static org.mockito.Mockito.mockingDetails;
*/
public class ActiveMQAutoConfigurationTests {
private final ContextLoader contextLoader = ContextLoader.standard()
.autoConfig(ActiveMQAutoConfiguration.class, JmsAutoConfiguration.class);
private final ApplicationContextTester context = new ApplicationContextTester()
.withConfiguration(AutoConfigurations.of(ActiveMQAutoConfiguration.class,
JmsAutoConfiguration.class));
@Test
public void brokerIsEmbeddedByDefault() {
this.contextLoader.config(EmptyConfiguration.class).load(context -> {
ConnectionFactory connectionFactory = context
.getBean(ConnectionFactory.class);
assertThat(connectionFactory).isInstanceOf(ActiveMQConnectionFactory.class);
String brokerUrl = ((ActiveMQConnectionFactory) connectionFactory)
.getBrokerURL();
assertThat(brokerUrl).isEqualTo("vm://localhost?broker.persistent=false");
this.context.withUserConfiguration(EmptyConfiguration.class).run((loaded) -> {
assertThat(loaded).getBean(ConnectionFactory.class)
.isInstanceOf(ActiveMQConnectionFactory.class);
assertThat(loaded.getBean(ActiveMQConnectionFactory.class).getBrokerURL())
.isEqualTo("vm://localhost?broker.persistent=false");
});
}
@Test
public void configurationBacksOffWhenCustomConnectionFactoryExists() {
this.contextLoader.config(CustomConnectionFactoryConfiguration.class)
.load(context -> assertThat(
mockingDetails(context.getBean(ConnectionFactory.class)).isMock())
this.context.withUserConfiguration(CustomConnectionFactoryConfiguration.class)
.run((loaded) -> assertThat(
mockingDetails(loaded.getBean(ConnectionFactory.class)).isMock())
.isTrue());
}
@Test
public void customPooledConnectionFactoryConfiguration() {
this.contextLoader.config(EmptyConfiguration.class)
.env("spring.activemq.pool.enabled:true",
this.context.withUserConfiguration(EmptyConfiguration.class)
.withPropertyValues("spring.activemq.pool.enabled:true",
"spring.activemq.pool.maxConnections:256",
"spring.activemq.pool.idleTimeout:512",
"spring.activemq.pool.expiryTimeout:4096",
"spring.activemq.pool.configuration.maximumActiveSessionPerConnection:1024",
"spring.activemq.pool.configuration.timeBetweenExpirationCheckMillis:2048")
.load(context -> {
ConnectionFactory connectionFactory = context
.getBean(ConnectionFactory.class);
assertThat(connectionFactory)
.isInstanceOf(PooledConnectionFactory.class);
PooledConnectionFactory pooledConnectionFactory = (PooledConnectionFactory) connectionFactory;
assertThat(pooledConnectionFactory.getMaxConnections())
.isEqualTo(256);
assertThat(pooledConnectionFactory.getIdleTimeout()).isEqualTo(512);
assertThat(pooledConnectionFactory
.getMaximumActiveSessionPerConnection()).isEqualTo(1024);
assertThat(
pooledConnectionFactory.getTimeBetweenExpirationCheckMillis())
.isEqualTo(2048);
assertThat(pooledConnectionFactory.getExpiryTimeout())
.isEqualTo(4096);
.run((loaded) -> {
ConnectionFactory factory = loaded.getBean(ConnectionFactory.class);
assertThat(factory).isInstanceOf(PooledConnectionFactory.class);
PooledConnectionFactory pooledFactory = (PooledConnectionFactory) factory;
assertThat(pooledFactory.getMaxConnections()).isEqualTo(256);
assertThat(pooledFactory.getIdleTimeout()).isEqualTo(512);
assertThat(pooledFactory.getMaximumActiveSessionPerConnection())
.isEqualTo(1024);
assertThat(pooledFactory.getTimeBetweenExpirationCheckMillis())
.isEqualTo(2048);
assertThat(pooledFactory.getExpiryTimeout()).isEqualTo(4096);
});
}
@Test
public void pooledConnectionFactoryConfiguration() throws JMSException {
this.contextLoader.config(EmptyConfiguration.class)
.env("spring.activemq.pool.enabled:true").load(context -> {
ConnectionFactory connectionFactory = context
.getBean(ConnectionFactory.class);
assertThat(connectionFactory)
.isInstanceOf(PooledConnectionFactory.class);
context.close();
assertThat(connectionFactory.createConnection()).isNull();
this.context.withUserConfiguration(EmptyConfiguration.class)
.withPropertyValues("spring.activemq.pool.enabled:true").run((loaded) -> {
ConnectionFactory factory = loaded.getBean(ConnectionFactory.class);
assertThat(factory).isInstanceOf(PooledConnectionFactory.class);
loaded.getSourceApplicationContext().close();
assertThat(factory.createConnection()).isNull();
});
}
......
......@@ -18,8 +18,8 @@ package org.springframework.boot.autoconfigure.web.reactive;
import org.junit.Test;
import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.ReactiveWebContextLoader;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.ReactiveWebApplicationContextTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.HttpHandler;
......@@ -39,23 +39,25 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class HttpHandlerAutoConfigurationTests {
private final ReactiveWebContextLoader contextLoader = ContextLoader.reactiveWeb()
.autoConfig(HttpHandlerAutoConfiguration.class);
private final ReactiveWebApplicationContextTester context = new ReactiveWebApplicationContextTester()
.withConfiguration(AutoConfigurations.of(HttpHandlerAutoConfiguration.class));
@Test
public void shouldNotProcessIfExistingHttpHandler() {
this.contextLoader.config(CustomHttpHandler.class).load(context -> {
assertThat(context.getBeansOfType(HttpHandler.class)).hasSize(1);
assertThat(context.getBean(HttpHandler.class))
.isSameAs(context.getBean("customHttpHandler"));
this.context.withUserConfiguration(CustomHttpHandler.class).run((loaded) -> {
assertThat(loaded).hasSingleBean(HttpHandler.class);
assertThat(loaded).getBean(HttpHandler.class)
.isSameAs(loaded.getBean("customHttpHandler"));
});
}
@Test
public void shouldConfigureHttpHandlerAnnotation() {
this.contextLoader.autoConfig(WebFluxAutoConfiguration.class).load(context -> {
assertThat(context.getBeansOfType(HttpHandler.class).size()).isEqualTo(1);
});
this.context
.withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class))
.run((loaded) -> {
assertThat(loaded).hasSingleBean(HttpHandler.class);
});
}
@Configuration
......
......@@ -16,14 +16,17 @@
package org.springframework.boot.autoconfigure.webservices;
import java.util.Collection;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.ServletWebContextLoader;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.WebApplicationContextTester;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -37,52 +40,46 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class WebServicesAutoConfigurationTests {
private final ServletWebContextLoader contextLoader = ContextLoader.servletWeb()
.autoConfig(WebServicesAutoConfiguration.class);
private final WebApplicationContextTester context = new WebApplicationContextTester()
.withConfiguration(AutoConfigurations.of(WebServicesAutoConfiguration.class));
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void defaultConfiguration() {
this.contextLoader.load(context -> {
assertThat(context.getBeansOfType(ServletRegistrationBean.class)).hasSize(1);
});
this.context.run((loaded) -> assertThat(loaded)
.hasSingleBean(ServletRegistrationBean.class));
}
@Test
public void customPathMustBeginWithASlash() {
this.contextLoader.env("spring.webservices.path=invalid")
.loadAndFail(BeanCreationException.class, (ex) -> {
System.out.println(ex.getMessage());
assertThat(ex.getMessage()).contains(
"Failed to bind properties under 'spring.webservices'");
this.context.withPropertyValues("spring.webservices.path=invalid")
.run((loaded) -> {
assertThat(loaded).getFailure()
.isInstanceOf(BeanCreationException.class)
.hasMessageContaining(
"Failed to bind properties under 'spring.webservices'");
});
}
@Test
public void customPath() {
this.contextLoader.env("spring.webservices.path=/valid").load(context -> {
ServletRegistrationBean<?> servletRegistrationBean = context
.getBean(ServletRegistrationBean.class);
assertThat(servletRegistrationBean.getUrlMappings()).contains("/valid/*");
});
this.context.withPropertyValues("spring.webservices.path=/valid")
.run((loaded) -> assertThat(getUrlMappings(loaded)).contains("/valid/*"));
}
@Test
public void customPathWithTrailingSlash() {
this.contextLoader.env("spring.webservices.path=/valid/").load(context -> {
ServletRegistrationBean<?> servletRegistrationBean = context
.getBean(ServletRegistrationBean.class);
assertThat(servletRegistrationBean.getUrlMappings()).contains("/valid/*");
});
this.context.withPropertyValues("spring.webservices.path=/valid/")
.run((loaded) -> assertThat(getUrlMappings(loaded)).contains("/valid/*"));
}
@Test
public void customLoadOnStartup() {
this.contextLoader.env("spring.webservices.servlet.load-on-startup=1")
.load(context -> {
ServletRegistrationBean<?> registrationBean = context
this.context.withPropertyValues("spring.webservices.servlet.load-on-startup=1")
.run((loaded) -> {
ServletRegistrationBean<?> registrationBean = loaded
.getBean(ServletRegistrationBean.class);
assertThat(ReflectionTestUtils.getField(registrationBean,
"loadOnStartup")).isEqualTo(1);
......@@ -91,15 +88,22 @@ public class WebServicesAutoConfigurationTests {
@Test
public void customInitParameters() {
this.contextLoader.env("spring.webservices.servlet.init.key1=value1",
"spring.webservices.servlet.init.key2=value2").load(context -> {
ServletRegistrationBean<?> registrationBean = context
.getBean(ServletRegistrationBean.class);
assertThat(registrationBean.getInitParameters()).containsEntry("key1",
"value1");
assertThat(registrationBean.getInitParameters()).containsEntry("key2",
"value2");
});
this.context
.withPropertyValues("spring.webservices.servlet.init.key1=value1",
"spring.webservices.servlet.init.key2=value2")
.run(loaded -> assertThat(
getServletRegistrationBean(loaded).getInitParameters())
.containsEntry("key1", "value1")
.containsEntry("key2", "value2"));
}
private Collection<String> getUrlMappings(ApplicationContext loaded) {
return getServletRegistrationBean(loaded).getUrlMappings();
}
private ServletRegistrationBean<?> getServletRegistrationBean(
ApplicationContext loaded) {
return loaded.getBean(ServletRegistrationBean.class);
}
}
......@@ -20,7 +20,8 @@ import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.ApplicationContextTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
......@@ -37,29 +38,32 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class TestDatabaseAutoConfigurationTests {
private final ContextLoader contextLoader = ContextLoader.standard()
.autoConfig(TestDatabaseAutoConfiguration.class);
private final ApplicationContextTester context = new ApplicationContextTester()
.withConfiguration(
AutoConfigurations.of(TestDatabaseAutoConfiguration.class));
@Test
public void replaceWithNoDataSourceAvailable() {
this.contextLoader.load(context -> {
assertThat(context.getBeansOfType(DataSource.class)).isEmpty();
});
this.context
.run((loaded) -> assertThat(loaded).doesNotHaveBean(DataSource.class));
}
@Test
public void replaceWithUniqueDatabase() {
this.contextLoader.config(ExistingDataSourceConfiguration.class).load(context -> {
DataSource datasource = context.getBean(DataSource.class);
JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
jdbcTemplate.execute("create table example (id int, name varchar);");
this.contextLoader.load(anotherContext -> {
DataSource anotherDatasource = anotherContext.getBean(DataSource.class);
JdbcTemplate anotherJdbcTemplate = new JdbcTemplate(anotherDatasource);
anotherJdbcTemplate
.execute("create table example (id int, name varchar);");
});
});
this.context.withUserConfiguration(ExistingDataSourceConfiguration.class)
.run((loaded) -> {
DataSource datasource = loaded.getBean(DataSource.class);
JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
jdbcTemplate.execute("create table example (id int, name varchar);");
this.context.run((secondContext) -> {
DataSource anotherDatasource = secondContext
.getBean(DataSource.class);
JdbcTemplate anotherJdbcTemplate = new JdbcTemplate(
anotherDatasource);
anotherJdbcTemplate
.execute("create table example (id int, name varchar);");
});
});
}
@Configuration
......
......@@ -22,8 +22,9 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.context.ContextLoader;
import org.springframework.boot.test.context.ApplicationContextTester;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.context.annotation.Bean;
......@@ -43,31 +44,32 @@ import static org.mockito.Mockito.mock;
@ClassPathExclusions({ "h2-*.jar", "hsqldb-*.jar", "derby-*.jar" })
public class TestDatabaseAutoConfigurationNoEmbeddedTests {
private final ContextLoader contextLoader = ContextLoader.standard()
.config(ExistingDataSourceConfiguration.class)
.autoConfig(TestDatabaseAutoConfiguration.class);
private final ApplicationContextTester context = new ApplicationContextTester()
.withUserConfiguration(ExistingDataSourceConfiguration.class)
.withConfiguration(
AutoConfigurations.of(TestDatabaseAutoConfiguration.class));
@Test
public void applyAnyReplace() {
this.contextLoader.loadAndFail(BeanCreationException.class, ex -> {
String message = ex.getMessage();
assertThat(message).contains(
"Failed to replace DataSource with an embedded database for tests.");
assertThat(message).contains(
"If you want an embedded database please put a supported one on the "
+ "classpath");
assertThat(message).contains(
"or tune the replace attribute of @AutoconfigureTestDatabase.");
this.context.run((loaded) -> {
assertThat(loaded).getFailure().isInstanceOf(BeanCreationException.class)
.hasMessageContaining(
"Failed to replace DataSource with an embedded database for tests.")
.hasMessageContaining(
"If you want an embedded database please put a supported one on the classpath")
.hasMessageContaining(
"or tune the replace attribute of @AutoconfigureTestDatabase.");
});
}
@Test
public void applyNoReplace() {
this.contextLoader.env("spring.test.database.replace=NONE").load(context -> {
assertThat(context.getBeansOfType(DataSource.class)).hasSize(1);
assertThat(context.getBean(DataSource.class))
.isSameAs(context.getBean("myCustomDataSource"));
});
this.context.withPropertyValues("spring.test.database.replace=NONE")
.run((loaded) -> {
assertThat(loaded).hasSingleBean(DataSource.class);
assertThat(loaded).getBean(DataSource.class)
.isSameAs(loaded.getBean("myCustomDataSource"));
});
}
@Configuration
......
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