Migrate integration tests to context runner
Migrate `IntegrationAutoConfigurationTests` to use the `ApplicationContextRunner`.
This commit is contained in:
parent
728b522307
commit
74cede5cdc
|
|
@ -16,26 +16,20 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.integration;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration.IntegrationComponentScanAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
|
||||
import org.springframework.boot.jdbc.DataSourceInitializationMode;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
|
@ -47,7 +41,6 @@ import org.springframework.integration.support.management.IntegrationManagementC
|
|||
import org.springframework.jdbc.BadSqlGrammarException;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jmx.export.MBeanExporter;
|
||||
import org.springframework.test.context.support.TestPropertySourceUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
|
@ -64,158 +57,146 @@ public class IntegrationAutoConfigurationTests {
|
|||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
if (this.context.getParent() != null) {
|
||||
((ConfigurableApplicationContext) this.context.getParent()).close();
|
||||
}
|
||||
}
|
||||
}
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(JmxAutoConfiguration.class,
|
||||
IntegrationAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
public void integrationIsAvailable() {
|
||||
load();
|
||||
assertThat(this.context.getBean(TestGateway.class)).isNotNull();
|
||||
assertThat(this.context.getBean(IntegrationComponentScanAutoConfiguration.class))
|
||||
.isNotNull();
|
||||
this.contextRunner.run((context) -> {
|
||||
assertThat(context).hasSingleBean(TestGateway.class);
|
||||
assertThat(context)
|
||||
.hasSingleBean(IntegrationComponentScanAutoConfiguration.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void explicitIntegrationComponentScan() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(IntegrationComponentScanConfiguration.class,
|
||||
JmxAutoConfiguration.class, IntegrationAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(TestGateway.class)).isNotNull();
|
||||
assertThat(this.context
|
||||
.getBeansOfType(IntegrationComponentScanAutoConfiguration.class))
|
||||
.isEmpty();
|
||||
this.contextRunner
|
||||
.withUserConfiguration(IntegrationComponentScanConfiguration.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(TestGateway.class);
|
||||
assertThat(context).doesNotHaveBean(
|
||||
IntegrationComponentScanAutoConfiguration.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parentContext() {
|
||||
load();
|
||||
AnnotationConfigApplicationContext parent = this.context;
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
ConfigurationPropertySources.attach(this.context.getEnvironment());
|
||||
this.context.setParent(parent);
|
||||
this.context.register(JmxAutoConfiguration.class,
|
||||
IntegrationAutoConfiguration.class);
|
||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
|
||||
"spring.jmx.default_domain=org.foo");
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(HeaderChannelRegistry.class)).isNotNull();
|
||||
this.contextRunner.run((context) -> {
|
||||
this.contextRunner.withParent(context)
|
||||
.withPropertyValues("spring.jmx.default_domain=org.foo")
|
||||
.run((child) -> {
|
||||
assertThat(child).hasSingleBean(HeaderChannelRegistry.class);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jmxIntegrationEnabledByDefault() {
|
||||
load();
|
||||
MBeanServer mBeanServer = this.context.getBean(MBeanServer.class);
|
||||
assertDomains(mBeanServer, true, "org.springframework.integration",
|
||||
"org.springframework.integration.monitor");
|
||||
Object bean = this.context
|
||||
.getBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME);
|
||||
assertThat(bean).isNotNull();
|
||||
this.contextRunner.run((context) -> {
|
||||
MBeanServer mBeanServer = context.getBean(MBeanServer.class);
|
||||
assertThat(mBeanServer.getDomains()).contains(
|
||||
"org.springframework.integration",
|
||||
"org.springframework.integration.monitor");
|
||||
assertThat(context)
|
||||
.hasBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disableJmxIntegration() {
|
||||
load("spring.jmx.enabled=false");
|
||||
assertThat(this.context.getBeansOfType(MBeanServer.class)).hasSize(0);
|
||||
assertThat(this.context.getBeansOfType(IntegrationManagementConfigurer.class))
|
||||
.isNotEmpty(); // As of Boot 2.0 we always configure
|
||||
this.contextRunner.withPropertyValues("spring.jmx.enabled=false")
|
||||
.run((context) -> {
|
||||
assertThat(context).doesNotHaveBean(MBeanServer.class);
|
||||
assertThat(context)
|
||||
.hasSingleBean(IntegrationManagementConfigurer.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeJmxDomain() {
|
||||
load("spring.jmx.default_domain=org.foo");
|
||||
MBeanServer mBeanServer = this.context.getBean(MBeanServer.class);
|
||||
assertDomains(mBeanServer, true, "org.foo");
|
||||
assertDomains(mBeanServer, false, "org.springframework.integration",
|
||||
"org.springframework.integration.monitor");
|
||||
this.contextRunner.withPropertyValues("spring.jmx.default_domain=org.foo")
|
||||
.run((context) -> {
|
||||
MBeanServer mBeanServer = context.getBean(MBeanServer.class);
|
||||
assertThat(mBeanServer.getDomains()).contains("org.foo")
|
||||
.doesNotContain("org.springframework.integration",
|
||||
"org.springframework.integration.monitor");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void primaryExporterIsAllowed() {
|
||||
load(new Class[] { CustomMBeanExporter.class });
|
||||
assertThat(this.context.getBeansOfType(MBeanExporter.class)).hasSize(2);
|
||||
assertThat(this.context.getBean(MBeanExporter.class))
|
||||
.isSameAs(this.context.getBean("myMBeanExporter"));
|
||||
this.contextRunner.withUserConfiguration(CustomMBeanExporter.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).getBeans(MBeanExporter.class).hasSize(2);
|
||||
assertThat(context.getBean(MBeanExporter.class))
|
||||
.isSameAs(context.getBean("myMBeanExporter"));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void integrationJdbcDataSourceInitializerEnabled() {
|
||||
load(new Class[] { EmbeddedDataSourceConfiguration.class,
|
||||
DataSourceTransactionManagerAutoConfiguration.class,
|
||||
JdbcTemplateAutoConfiguration.class, IntegrationAutoConfiguration.class },
|
||||
"spring.datasource.generate-unique-name=true",
|
||||
"spring.integration.jdbc.initialize-schema=always");
|
||||
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
|
||||
.getInitializeSchema()).isEqualTo(DataSourceInitializationMode.ALWAYS);
|
||||
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
|
||||
assertThat(jdbcOperations.queryForList("select * from INT_MESSAGE")).isEmpty();
|
||||
assertThat(jdbcOperations.queryForList("select * from INT_GROUP_TO_MESSAGE"))
|
||||
.isEmpty();
|
||||
assertThat(jdbcOperations.queryForList("select * from INT_MESSAGE_GROUP"))
|
||||
.isEmpty();
|
||||
assertThat(jdbcOperations.queryForList("select * from INT_LOCK")).isEmpty();
|
||||
assertThat(jdbcOperations.queryForList("select * from INT_CHANNEL_MESSAGE"))
|
||||
.isEmpty();
|
||||
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
|
||||
.withConfiguration(AutoConfigurations.of(
|
||||
DataSourceTransactionManagerAutoConfiguration.class,
|
||||
JdbcTemplateAutoConfiguration.class,
|
||||
IntegrationAutoConfiguration.class))
|
||||
.withPropertyValues("spring.datasource.generate-unique-name=true",
|
||||
"spring.integration.jdbc.initialize-schema=always")
|
||||
.run((context) -> {
|
||||
IntegrationProperties properties = context
|
||||
.getBean(IntegrationProperties.class);
|
||||
assertThat(properties.getJdbc().getInitializeSchema())
|
||||
.isEqualTo(DataSourceInitializationMode.ALWAYS);
|
||||
JdbcOperations jdbc = context.getBean(JdbcOperations.class);
|
||||
assertThat(jdbc.queryForList("select * from INT_MESSAGE")).isEmpty();
|
||||
assertThat(jdbc.queryForList("select * from INT_GROUP_TO_MESSAGE"))
|
||||
.isEmpty();
|
||||
assertThat(jdbc.queryForList("select * from INT_MESSAGE_GROUP"))
|
||||
.isEmpty();
|
||||
assertThat(jdbc.queryForList("select * from INT_LOCK")).isEmpty();
|
||||
assertThat(jdbc.queryForList("select * from INT_CHANNEL_MESSAGE"))
|
||||
.isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void integrationJdbcDataSourceInitializerDisabled() {
|
||||
load(new Class[] { EmbeddedDataSourceConfiguration.class,
|
||||
DataSourceTransactionManagerAutoConfiguration.class,
|
||||
JdbcTemplateAutoConfiguration.class, IntegrationAutoConfiguration.class },
|
||||
"spring.datasource.generate-unique-name=true",
|
||||
"spring.integration.jdbc.initialize-schema=never");
|
||||
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
|
||||
.getInitializeSchema()).isEqualTo(DataSourceInitializationMode.NEVER);
|
||||
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
|
||||
this.thrown.expect(BadSqlGrammarException.class);
|
||||
jdbcOperations.queryForList("select * from INT_MESSAGE");
|
||||
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
|
||||
.withConfiguration(AutoConfigurations.of(
|
||||
DataSourceTransactionManagerAutoConfiguration.class,
|
||||
JdbcTemplateAutoConfiguration.class,
|
||||
IntegrationAutoConfiguration.class))
|
||||
.withPropertyValues("spring.datasource.generate-unique-name=true",
|
||||
"spring.integration.jdbc.initialize-schema=never")
|
||||
.run((context) -> {
|
||||
IntegrationProperties properties = context
|
||||
.getBean(IntegrationProperties.class);
|
||||
assertThat(properties.getJdbc().getInitializeSchema())
|
||||
.isEqualTo(DataSourceInitializationMode.NEVER);
|
||||
JdbcOperations jdbc = context.getBean(JdbcOperations.class);
|
||||
this.thrown.expect(BadSqlGrammarException.class);
|
||||
jdbc.queryForList("select * from INT_MESSAGE");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void integrationJdbcDataSourceInitializerEnabledByDefaultWithEmbeddedDb() {
|
||||
load(new Class[] { EmbeddedDataSourceConfiguration.class,
|
||||
DataSourceTransactionManagerAutoConfiguration.class,
|
||||
JdbcTemplateAutoConfiguration.class, IntegrationAutoConfiguration.class },
|
||||
"spring.datasource.generate-unique-name=true");
|
||||
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
|
||||
.getInitializeSchema()).isEqualTo(DataSourceInitializationMode.EMBEDDED);
|
||||
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
|
||||
jdbcOperations.queryForList("select * from INT_MESSAGE").isEmpty();
|
||||
}
|
||||
|
||||
private static void assertDomains(MBeanServer mBeanServer, boolean expected,
|
||||
String... domains) {
|
||||
List<String> actual = Arrays.asList(mBeanServer.getDomains());
|
||||
for (String domain : domains) {
|
||||
assertThat(actual.contains(domain)).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
|
||||
public void load(String... environment) {
|
||||
load(null, environment);
|
||||
}
|
||||
|
||||
private void load(Class<?>[] configs, String... environment) {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
TestPropertyValues.of(environment).applyTo(ctx);
|
||||
if (configs != null) {
|
||||
ctx.register(configs);
|
||||
}
|
||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(ctx, environment);
|
||||
ConfigurationPropertySources.attach(ctx.getEnvironment());
|
||||
ctx.register(JmxAutoConfiguration.class, IntegrationAutoConfiguration.class);
|
||||
ctx.refresh();
|
||||
this.context = ctx;
|
||||
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
|
||||
.withConfiguration(AutoConfigurations.of(
|
||||
DataSourceTransactionManagerAutoConfiguration.class,
|
||||
JdbcTemplateAutoConfiguration.class,
|
||||
IntegrationAutoConfiguration.class))
|
||||
.withPropertyValues("spring.datasource.generate-unique-name=true")
|
||||
.run((context) -> {
|
||||
IntegrationProperties properties = context
|
||||
.getBean(IntegrationProperties.class);
|
||||
assertThat(properties.getJdbc().getInitializeSchema())
|
||||
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
|
||||
JdbcOperations jdbc = context.getBean(JdbcOperations.class);
|
||||
jdbc.queryForList("select * from INT_MESSAGE").isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
|
|
|||
Loading…
Reference in New Issue