Migrate tests to ApplicationContextRunner
This commit is contained in:
parent
62d70dacf0
commit
22deceb7f2
|
@ -24,8 +24,6 @@ import javax.management.MalformedObjectNameException;
|
|||
import javax.management.ObjectInstance;
|
||||
import javax.management.ObjectName;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
@ -34,14 +32,14 @@ import org.springframework.beans.factory.BeanFactoryUtils;
|
|||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
@ -56,52 +54,42 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
|
|||
|
||||
private static final String ENABLE_ADMIN_PROP = "spring.application.admin.enabled=true";
|
||||
|
||||
private static final String JMX_NAME_PROPERTY = "spring.application.admin.jmx-name";
|
||||
|
||||
private static final String DEFAULT_JMX_NAME = "org.springframework.boot:type=Admin,name=SpringApplication";
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
private final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
|
||||
private MBeanServer mBeanServer;
|
||||
|
||||
@Before
|
||||
public void setup() throws MalformedObjectNameException {
|
||||
this.mBeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(JmxAutoConfiguration.class,
|
||||
SpringApplicationAdminJmxAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
public void notRegisteredByDefault()
|
||||
throws MalformedObjectNameException, InstanceNotFoundException {
|
||||
load();
|
||||
this.thrown.expect(InstanceNotFoundException.class);
|
||||
this.mBeanServer.getObjectInstance(createDefaultObjectName());
|
||||
this.contextRunner.run((context) -> {
|
||||
this.thrown.expect(InstanceNotFoundException.class);
|
||||
this.mBeanServer.getObjectInstance(createDefaultObjectName());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registeredWithProperty() throws Exception {
|
||||
load(ENABLE_ADMIN_PROP);
|
||||
ObjectName objectName = createDefaultObjectName();
|
||||
ObjectInstance objectInstance = this.mBeanServer.getObjectInstance(objectName);
|
||||
assertThat(objectInstance).as("Lifecycle bean should have been registered")
|
||||
.isNotNull();
|
||||
this.contextRunner.withPropertyValues(ENABLE_ADMIN_PROP).run((context) -> {
|
||||
ObjectName objectName = createDefaultObjectName();
|
||||
ObjectInstance objectInstance = this.mBeanServer.getObjectInstance(objectName);
|
||||
assertThat(objectInstance).as("Lifecycle bean should have been registered")
|
||||
.isNotNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerWithCustomJmxName() throws InstanceNotFoundException {
|
||||
String customJmxName = "org.acme:name=FooBar";
|
||||
System.setProperty(JMX_NAME_PROPERTY, customJmxName);
|
||||
try {
|
||||
load(ENABLE_ADMIN_PROP);
|
||||
this.contextRunner.withSystemProperties(
|
||||
"spring.application.admin.jmx-name=" + customJmxName)
|
||||
.withPropertyValues(ENABLE_ADMIN_PROP).run((context) -> {
|
||||
try {
|
||||
this.mBeanServer.getObjectInstance(createObjectName(customJmxName));
|
||||
}
|
||||
|
@ -110,27 +98,25 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
|
|||
}
|
||||
this.thrown.expect(InstanceNotFoundException.class); // Should not be exposed
|
||||
this.mBeanServer.getObjectInstance(createDefaultObjectName());
|
||||
}
|
||||
finally {
|
||||
System.clearProperty(JMX_NAME_PROPERTY);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerWithSimpleWebApp() throws Exception {
|
||||
this.context = new SpringApplicationBuilder()
|
||||
try (ConfigurableApplicationContext context = new SpringApplicationBuilder()
|
||||
.sources(ServletWebServerFactoryAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class,
|
||||
JmxAutoConfiguration.class,
|
||||
SpringApplicationAdminJmxAutoConfiguration.class)
|
||||
.run("--" + ENABLE_ADMIN_PROP, "--server.port=0");
|
||||
assertThat(this.context).isInstanceOf(ServletWebServerApplicationContext.class);
|
||||
assertThat(this.mBeanServer.getAttribute(createDefaultObjectName(),
|
||||
"EmbeddedWebApplication")).isEqualTo(Boolean.TRUE);
|
||||
int expected = ((ServletWebServerApplicationContext) this.context).getWebServer()
|
||||
.getPort();
|
||||
String actual = getProperty(createDefaultObjectName(), "local.server.port");
|
||||
assertThat(actual).isEqualTo(String.valueOf(expected));
|
||||
.run("--" + ENABLE_ADMIN_PROP, "--server.port=0")) {
|
||||
assertThat(context).isInstanceOf(ServletWebServerApplicationContext.class);
|
||||
assertThat(this.mBeanServer.getAttribute(createDefaultObjectName(),
|
||||
"EmbeddedWebApplication")).isEqualTo(Boolean.TRUE);
|
||||
int expected = ((ServletWebServerApplicationContext) context).getWebServer()
|
||||
.getPort();
|
||||
String actual = getProperty(createDefaultObjectName(), "local.server.port");
|
||||
assertThat(actual).isEqualTo(String.valueOf(expected));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -145,8 +131,8 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
|
|||
|
||||
try (ConfigurableApplicationContext parent = parentBuilder
|
||||
.run("--" + ENABLE_ADMIN_PROP);
|
||||
ConfigurableApplicationContext child = childBuilder
|
||||
.run("--" + ENABLE_ADMIN_PROP)) {
|
||||
ConfigurableApplicationContext child = childBuilder
|
||||
.run("--" + ENABLE_ADMIN_PROP)) {
|
||||
BeanFactoryUtils.beanOfType(parent.getBeanFactory(),
|
||||
SpringApplicationAdminMXBeanRegistrar.class);
|
||||
this.thrown.expect(NoSuchBeanDefinitionException.class);
|
||||
|
@ -173,13 +159,4 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
|
|||
new Object[] { key }, new String[] { String.class.getName() });
|
||||
}
|
||||
|
||||
private void load(String... environment) {
|
||||
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
|
||||
TestPropertyValues.of(environment).applyTo(applicationContext);
|
||||
applicationContext.register(JmxAutoConfiguration.class,
|
||||
SpringApplicationAdminJmxAutoConfiguration.class);
|
||||
applicationContext.refresh();
|
||||
this.context = applicationContext;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ import javax.net.ssl.SSLSocketFactory;
|
|||
|
||||
import com.rabbitmq.client.Address;
|
||||
import org.aopalliance.aop.Advice;
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
@ -45,8 +44,9 @@ import org.springframework.amqp.rabbit.support.ValueExpression;
|
|||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
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;
|
||||
|
@ -72,231 +72,265 @@ public class RabbitAutoConfigurationTests {
|
|||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(RabbitAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
public void testDefaultRabbitConfiguration() {
|
||||
load(TestConfiguration.class);
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
RabbitMessagingTemplate messagingTemplate = this.context
|
||||
.getBean(RabbitMessagingTemplate.class);
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
|
||||
RabbitAdmin amqpAdmin = this.context.getBean(RabbitAdmin.class);
|
||||
assertThat(rabbitTemplate.getConnectionFactory()).isEqualTo(connectionFactory);
|
||||
assertThat(getMandatory(rabbitTemplate)).isFalse();
|
||||
assertThat(messagingTemplate.getRabbitTemplate()).isEqualTo(rabbitTemplate);
|
||||
assertThat(amqpAdmin).isNotNull();
|
||||
assertThat(connectionFactory.getHost()).isEqualTo("localhost");
|
||||
assertThat(dfa.getPropertyValue("publisherConfirms")).isEqualTo(false);
|
||||
assertThat(dfa.getPropertyValue("publisherReturns")).isEqualTo(false);
|
||||
assertThat(this.context.containsBean("rabbitListenerContainerFactory"))
|
||||
.as("Listener container factory should be created by default").isTrue();
|
||||
this.contextRunner
|
||||
.withUserConfiguration(TestConfiguration.class).run((context) -> {
|
||||
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
|
||||
RabbitMessagingTemplate messagingTemplate = context
|
||||
.getBean(RabbitMessagingTemplate.class);
|
||||
CachingConnectionFactory connectionFactory = context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
|
||||
RabbitAdmin amqpAdmin = context.getBean(RabbitAdmin.class);
|
||||
assertThat(rabbitTemplate.getConnectionFactory()).isEqualTo(connectionFactory);
|
||||
assertThat(getMandatory(rabbitTemplate)).isFalse();
|
||||
assertThat(messagingTemplate.getRabbitTemplate()).isEqualTo(rabbitTemplate);
|
||||
assertThat(amqpAdmin).isNotNull();
|
||||
assertThat(connectionFactory.getHost()).isEqualTo("localhost");
|
||||
assertThat(dfa.getPropertyValue("publisherConfirms")).isEqualTo(false);
|
||||
assertThat(dfa.getPropertyValue("publisherReturns")).isEqualTo(false);
|
||||
assertThat(context.containsBean("rabbitListenerContainerFactory"))
|
||||
.as("Listener container factory should be created by default").isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionFactoryWithOverrides() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.host:remote-server",
|
||||
"spring.rabbitmq.port:9000", "spring.rabbitmq.username:alice",
|
||||
"spring.rabbitmq.password:secret", "spring.rabbitmq.virtual_host:/vhost",
|
||||
"spring.rabbitmq.connection-timeout:123");
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertThat(connectionFactory.getHost()).isEqualTo("remote-server");
|
||||
assertThat(connectionFactory.getPort()).isEqualTo(9000);
|
||||
assertThat(connectionFactory.getVirtualHost()).isEqualTo("/vhost");
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
|
||||
com.rabbitmq.client.ConnectionFactory rcf = (com.rabbitmq.client.ConnectionFactory) dfa
|
||||
.getPropertyValue("rabbitConnectionFactory");
|
||||
assertThat(rcf.getConnectionTimeout()).isEqualTo(123);
|
||||
assertThat((Address[]) dfa.getPropertyValue("addresses")).hasSize(1);
|
||||
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.host:remote-server",
|
||||
"spring.rabbitmq.port:9000", "spring.rabbitmq.username:alice",
|
||||
"spring.rabbitmq.password:secret",
|
||||
"spring.rabbitmq.virtual_host:/vhost",
|
||||
"spring.rabbitmq.connection-timeout:123").run((context) -> {
|
||||
CachingConnectionFactory connectionFactory = context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertThat(connectionFactory.getHost()).isEqualTo("remote-server");
|
||||
assertThat(connectionFactory.getPort()).isEqualTo(9000);
|
||||
assertThat(connectionFactory.getVirtualHost()).isEqualTo("/vhost");
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
|
||||
com.rabbitmq.client.ConnectionFactory rcf = (com.rabbitmq.client.ConnectionFactory) dfa
|
||||
.getPropertyValue("rabbitConnectionFactory");
|
||||
assertThat(rcf.getConnectionTimeout()).isEqualTo(123);
|
||||
assertThat((Address[]) dfa.getPropertyValue("addresses")).hasSize(1);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionFactoryEmptyVirtualHost() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.virtual_host:");
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertThat(connectionFactory.getVirtualHost()).isEqualTo("/");
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.virtual_host:").run((context) -> {
|
||||
CachingConnectionFactory connectionFactory = context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertThat(connectionFactory.getVirtualHost()).isEqualTo("/");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionFactoryVirtualHostNoLeadingSlash() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.virtual_host:foo");
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertThat(connectionFactory.getVirtualHost()).isEqualTo("foo");
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.virtual_host:foo").run((context) -> {
|
||||
CachingConnectionFactory connectionFactory = context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertThat(connectionFactory.getVirtualHost()).isEqualTo("foo");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionFactoryVirtualHostMultiLeadingSlashes() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.virtual_host:///foo");
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertThat(connectionFactory.getVirtualHost()).isEqualTo("///foo");
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.virtual_host:///foo")
|
||||
.run((context) -> {
|
||||
CachingConnectionFactory connectionFactory = context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertThat(connectionFactory.getVirtualHost()).isEqualTo("///foo");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionFactoryDefaultVirtualHost() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.virtual_host:/");
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertThat(connectionFactory.getVirtualHost()).isEqualTo("/");
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.virtual_host:/").run((context) -> {
|
||||
CachingConnectionFactory connectionFactory = context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertThat(connectionFactory.getVirtualHost()).isEqualTo("/");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionFactoryPublisherSettings() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.publisher-confirms=true",
|
||||
"spring.rabbitmq.publisher-returns=true");
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
|
||||
assertThat(dfa.getPropertyValue("publisherConfirms")).isEqualTo(true);
|
||||
assertThat(dfa.getPropertyValue("publisherReturns")).isEqualTo(true);
|
||||
assertThat(getMandatory(rabbitTemplate)).isTrue();
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.publisher-confirms=true",
|
||||
"spring.rabbitmq.publisher-returns=true").run(context -> {
|
||||
CachingConnectionFactory connectionFactory = context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
|
||||
assertThat(dfa.getPropertyValue("publisherConfirms")).isEqualTo(true);
|
||||
assertThat(dfa.getPropertyValue("publisherReturns")).isEqualTo(true);
|
||||
assertThat(getMandatory(rabbitTemplate)).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitTemplateMessageConverters() {
|
||||
load(MessageConvertersConfiguration.class);
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
assertThat(rabbitTemplate.getMessageConverter())
|
||||
.isSameAs(this.context.getBean("myMessageConverter"));
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitTemplate);
|
||||
assertThat(dfa.getPropertyValue("retryTemplate")).isNull();
|
||||
this.contextRunner.withUserConfiguration(
|
||||
MessageConvertersConfiguration.class).run((context) -> {
|
||||
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
|
||||
assertThat(rabbitTemplate.getMessageConverter())
|
||||
.isSameAs(context.getBean("myMessageConverter"));
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitTemplate);
|
||||
assertThat(dfa.getPropertyValue("retryTemplate")).isNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitTemplateRetry() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.template.retry.enabled:true",
|
||||
"spring.rabbitmq.template.retry.maxAttempts:4",
|
||||
"spring.rabbitmq.template.retry.initialInterval:2000",
|
||||
"spring.rabbitmq.template.retry.multiplier:1.5",
|
||||
"spring.rabbitmq.template.retry.maxInterval:5000",
|
||||
"spring.rabbitmq.template.receiveTimeout:123",
|
||||
"spring.rabbitmq.template.replyTimeout:456");
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitTemplate);
|
||||
assertThat(dfa.getPropertyValue("receiveTimeout")).isEqualTo(123L);
|
||||
assertThat(dfa.getPropertyValue("replyTimeout")).isEqualTo(456L);
|
||||
RetryTemplate retryTemplate = (RetryTemplate) dfa
|
||||
.getPropertyValue("retryTemplate");
|
||||
assertThat(retryTemplate).isNotNull();
|
||||
dfa = new DirectFieldAccessor(retryTemplate);
|
||||
SimpleRetryPolicy retryPolicy = (SimpleRetryPolicy) dfa
|
||||
.getPropertyValue("retryPolicy");
|
||||
ExponentialBackOffPolicy backOffPolicy = (ExponentialBackOffPolicy) dfa
|
||||
.getPropertyValue("backOffPolicy");
|
||||
assertThat(retryPolicy.getMaxAttempts()).isEqualTo(4);
|
||||
assertThat(backOffPolicy.getInitialInterval()).isEqualTo(2000);
|
||||
assertThat(backOffPolicy.getMultiplier()).isEqualTo(1.5);
|
||||
assertThat(backOffPolicy.getMaxInterval()).isEqualTo(5000);
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.template.retry.enabled:true",
|
||||
"spring.rabbitmq.template.retry.maxAttempts:4",
|
||||
"spring.rabbitmq.template.retry.initialInterval:2000",
|
||||
"spring.rabbitmq.template.retry.multiplier:1.5",
|
||||
"spring.rabbitmq.template.retry.maxInterval:5000",
|
||||
"spring.rabbitmq.template.receiveTimeout:123",
|
||||
"spring.rabbitmq.template.replyTimeout:456"
|
||||
).run((context) -> {
|
||||
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitTemplate);
|
||||
assertThat(dfa.getPropertyValue("receiveTimeout")).isEqualTo(123L);
|
||||
assertThat(dfa.getPropertyValue("replyTimeout")).isEqualTo(456L);
|
||||
RetryTemplate retryTemplate = (RetryTemplate) dfa
|
||||
.getPropertyValue("retryTemplate");
|
||||
assertThat(retryTemplate).isNotNull();
|
||||
dfa = new DirectFieldAccessor(retryTemplate);
|
||||
SimpleRetryPolicy retryPolicy = (SimpleRetryPolicy) dfa
|
||||
.getPropertyValue("retryPolicy");
|
||||
ExponentialBackOffPolicy backOffPolicy = (ExponentialBackOffPolicy) dfa
|
||||
.getPropertyValue("backOffPolicy");
|
||||
assertThat(retryPolicy.getMaxAttempts()).isEqualTo(4);
|
||||
assertThat(backOffPolicy.getInitialInterval()).isEqualTo(2000);
|
||||
assertThat(backOffPolicy.getMultiplier()).isEqualTo(1.5);
|
||||
assertThat(backOffPolicy.getMaxInterval()).isEqualTo(5000);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitTemplateMandatory() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.template.mandatory:true");
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
assertThat(getMandatory(rabbitTemplate)).isTrue();
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.template.mandatory:true")
|
||||
.run((context) -> {
|
||||
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
|
||||
assertThat(getMandatory(rabbitTemplate)).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitTemplateMandatoryDisabledEvenIfPublisherReturnsIsSet() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.template.mandatory:false",
|
||||
"spring.rabbitmq.publisher-returns=true");
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
assertThat(getMandatory(rabbitTemplate)).isFalse();
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.template.mandatory:false",
|
||||
"spring.rabbitmq.publisher-returns=true")
|
||||
.run(context -> {
|
||||
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
|
||||
assertThat(getMandatory(rabbitTemplate)).isFalse();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionFactoryBackOff() {
|
||||
load(TestConfiguration2.class);
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertThat(connectionFactory).isEqualTo(rabbitTemplate.getConnectionFactory());
|
||||
assertThat(connectionFactory.getHost()).isEqualTo("otherserver");
|
||||
assertThat(connectionFactory.getPort()).isEqualTo(8001);
|
||||
this.contextRunner.withUserConfiguration(
|
||||
TestConfiguration2.class).run((context) -> {
|
||||
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
|
||||
CachingConnectionFactory connectionFactory = context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertThat(connectionFactory).isEqualTo(rabbitTemplate.getConnectionFactory());
|
||||
assertThat(connectionFactory.getHost()).isEqualTo("otherserver");
|
||||
assertThat(connectionFactory.getPort()).isEqualTo(8001);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionFactoryCacheSettings() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.cache.channel.size=23",
|
||||
"spring.rabbitmq.cache.channel.checkoutTimeout=1000",
|
||||
"spring.rabbitmq.cache.connection.mode=CONNECTION",
|
||||
"spring.rabbitmq.cache.connection.size=2");
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
|
||||
assertThat(dfa.getPropertyValue("channelCacheSize")).isEqualTo(23);
|
||||
assertThat(dfa.getPropertyValue("cacheMode")).isEqualTo(CacheMode.CONNECTION);
|
||||
assertThat(dfa.getPropertyValue("connectionCacheSize")).isEqualTo(2);
|
||||
assertThat(dfa.getPropertyValue("channelCheckoutTimeout")).isEqualTo(1000L);
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.cache.channel.size=23",
|
||||
"spring.rabbitmq.cache.channel.checkoutTimeout=1000",
|
||||
"spring.rabbitmq.cache.connection.mode=CONNECTION",
|
||||
"spring.rabbitmq.cache.connection.size=2").run((context) -> {
|
||||
CachingConnectionFactory connectionFactory = context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
|
||||
assertThat(dfa.getPropertyValue("channelCacheSize")).isEqualTo(23);
|
||||
assertThat(dfa.getPropertyValue("cacheMode")).isEqualTo(CacheMode.CONNECTION);
|
||||
assertThat(dfa.getPropertyValue("connectionCacheSize")).isEqualTo(2);
|
||||
assertThat(dfa.getPropertyValue("channelCheckoutTimeout")).isEqualTo(1000L);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitTemplateBackOff() {
|
||||
load(TestConfiguration3.class);
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
assertThat(rabbitTemplate.getMessageConverter())
|
||||
.isEqualTo(this.context.getBean("testMessageConverter"));
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration3.class)
|
||||
.run((context) -> {
|
||||
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
|
||||
assertThat(rabbitTemplate.getMessageConverter())
|
||||
.isEqualTo(context.getBean("testMessageConverter"));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitMessagingTemplateBackOff() {
|
||||
load(TestConfiguration4.class);
|
||||
RabbitMessagingTemplate messagingTemplate = this.context
|
||||
.getBean(RabbitMessagingTemplate.class);
|
||||
assertThat(messagingTemplate.getDefaultDestination()).isEqualTo("fooBar");
|
||||
this.contextRunner.withUserConfiguration(
|
||||
TestConfiguration4.class).run((context) -> {
|
||||
RabbitMessagingTemplate messagingTemplate = context
|
||||
.getBean(RabbitMessagingTemplate.class);
|
||||
assertThat(messagingTemplate.getDefaultDestination()).isEqualTo("fooBar");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticQueues() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.dynamic:false");
|
||||
// There should NOT be an AmqpAdmin bean when dynamic is switch to false
|
||||
this.thrown.expect(NoSuchBeanDefinitionException.class);
|
||||
this.thrown.expectMessage("No qualifying bean of type");
|
||||
this.thrown.expectMessage(AmqpAdmin.class.getName());
|
||||
this.context.getBean(AmqpAdmin.class);
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.dynamic:false").run((context) -> {
|
||||
// There should NOT be an AmqpAdmin bean when dynamic is switch to false
|
||||
this.thrown.expect(NoSuchBeanDefinitionException.class);
|
||||
this.thrown.expectMessage("No qualifying bean of type");
|
||||
this.thrown.expectMessage(AmqpAdmin.class.getName());
|
||||
context.getBean(AmqpAdmin.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnableRabbitCreateDefaultContainerFactory() {
|
||||
load(EnableRabbitConfiguration.class);
|
||||
RabbitListenerContainerFactory<?> rabbitListenerContainerFactory = this.context
|
||||
.getBean("rabbitListenerContainerFactory",
|
||||
RabbitListenerContainerFactory.class);
|
||||
assertThat(rabbitListenerContainerFactory.getClass())
|
||||
.isEqualTo(SimpleRabbitListenerContainerFactory.class);
|
||||
this.contextRunner.withUserConfiguration(
|
||||
EnableRabbitConfiguration.class).run((context) -> {
|
||||
RabbitListenerContainerFactory<?> rabbitListenerContainerFactory = context
|
||||
.getBean("rabbitListenerContainerFactory",
|
||||
RabbitListenerContainerFactory.class);
|
||||
assertThat(rabbitListenerContainerFactory.getClass())
|
||||
.isEqualTo(SimpleRabbitListenerContainerFactory.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitListenerContainerFactoryBackOff() {
|
||||
load(TestConfiguration5.class);
|
||||
SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory = this.context
|
||||
.getBean("rabbitListenerContainerFactory",
|
||||
SimpleRabbitListenerContainerFactory.class);
|
||||
rabbitListenerContainerFactory.setTxSize(10);
|
||||
verify(rabbitListenerContainerFactory).setTxSize(10);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitListenerContainerFactory);
|
||||
Advice[] adviceChain = (Advice[]) dfa.getPropertyValue("adviceChain");
|
||||
assertThat(adviceChain).isNull();
|
||||
this.contextRunner.withUserConfiguration(
|
||||
TestConfiguration5.class).run((context) -> {
|
||||
SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory = context
|
||||
.getBean("rabbitListenerContainerFactory",
|
||||
SimpleRabbitListenerContainerFactory.class);
|
||||
rabbitListenerContainerFactory.setTxSize(10);
|
||||
verify(rabbitListenerContainerFactory).setTxSize(10);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitListenerContainerFactory);
|
||||
Advice[] adviceChain = (Advice[]) dfa.getPropertyValue("adviceChain");
|
||||
assertThat(adviceChain).isNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleRabbitListenerContainerFactoryWithCustomSettings() {
|
||||
load(new Class<?>[] { MessageConvertersConfiguration.class,
|
||||
MessageRecoverersConfiguration.class },
|
||||
this.contextRunner.withUserConfiguration(MessageConvertersConfiguration.class,
|
||||
MessageRecoverersConfiguration.class).withPropertyValues(
|
||||
"spring.rabbitmq.listener.simple.retry.enabled:true",
|
||||
"spring.rabbitmq.listener.simple.retry.maxAttempts:4",
|
||||
"spring.rabbitmq.listener.simple.retry.initialInterval:2000",
|
||||
|
@ -309,21 +343,22 @@ public class RabbitAutoConfigurationTests {
|
|||
"spring.rabbitmq.listener.simple.prefetch:40",
|
||||
"spring.rabbitmq.listener.simple.defaultRequeueRejected:false",
|
||||
"spring.rabbitmq.listener.simple.idleEventInterval:5",
|
||||
"spring.rabbitmq.listener.simple.transactionSize:20");
|
||||
SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory = this.context
|
||||
.getBean("rabbitListenerContainerFactory",
|
||||
SimpleRabbitListenerContainerFactory.class);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitListenerContainerFactory);
|
||||
assertThat(dfa.getPropertyValue("concurrentConsumers")).isEqualTo(5);
|
||||
assertThat(dfa.getPropertyValue("maxConcurrentConsumers")).isEqualTo(10);
|
||||
assertThat(dfa.getPropertyValue("txSize")).isEqualTo(20);
|
||||
checkCommonProps(dfa);
|
||||
"spring.rabbitmq.listener.simple.transactionSize:20").run((context) -> {
|
||||
SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory = context
|
||||
.getBean("rabbitListenerContainerFactory",
|
||||
SimpleRabbitListenerContainerFactory.class);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitListenerContainerFactory);
|
||||
assertThat(dfa.getPropertyValue("concurrentConsumers")).isEqualTo(5);
|
||||
assertThat(dfa.getPropertyValue("maxConcurrentConsumers")).isEqualTo(10);
|
||||
assertThat(dfa.getPropertyValue("txSize")).isEqualTo(20);
|
||||
checkCommonProps(context, dfa);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectRabbitListenerContainerFactoryWithCustomSettings() {
|
||||
load(new Class<?>[] { MessageConvertersConfiguration.class,
|
||||
MessageRecoverersConfiguration.class },
|
||||
this.contextRunner.withUserConfiguration(MessageConvertersConfiguration.class,
|
||||
MessageRecoverersConfiguration.class).withPropertyValues(
|
||||
"spring.rabbitmq.listener.type:direct",
|
||||
"spring.rabbitmq.listener.direct.retry.enabled:true",
|
||||
"spring.rabbitmq.listener.direct.retry.maxAttempts:4",
|
||||
|
@ -335,71 +370,73 @@ public class RabbitAutoConfigurationTests {
|
|||
"spring.rabbitmq.listener.direct.consumers-per-queue:5",
|
||||
"spring.rabbitmq.listener.direct.prefetch:40",
|
||||
"spring.rabbitmq.listener.direct.defaultRequeueRejected:false",
|
||||
"spring.rabbitmq.listener.direct.idleEventInterval:5");
|
||||
DirectRabbitListenerContainerFactory rabbitListenerContainerFactory = this.context
|
||||
.getBean("rabbitListenerContainerFactory",
|
||||
DirectRabbitListenerContainerFactory.class);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitListenerContainerFactory);
|
||||
assertThat(dfa.getPropertyValue("consumersPerQueue")).isEqualTo(5);
|
||||
checkCommonProps(dfa);
|
||||
"spring.rabbitmq.listener.direct.idleEventInterval:5").run((context) -> {
|
||||
DirectRabbitListenerContainerFactory rabbitListenerContainerFactory = context
|
||||
.getBean("rabbitListenerContainerFactory",
|
||||
DirectRabbitListenerContainerFactory.class);
|
||||
DirectFieldAccessor dfa = new DirectFieldAccessor(rabbitListenerContainerFactory);
|
||||
assertThat(dfa.getPropertyValue("consumersPerQueue")).isEqualTo(5);
|
||||
checkCommonProps(context, dfa);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitListenerContainerFactoryConfigurersAreAvailable() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.listener.simple.concurrency:5",
|
||||
"spring.rabbitmq.listener.simple.maxConcurrency:10",
|
||||
"spring.rabbitmq.listener.simple.prefetch:40",
|
||||
"spring.rabbitmq.listener.direct.consumers-per-queue:5",
|
||||
"spring.rabbitmq.listener.direct.prefetch:40");
|
||||
assertThat(this.context
|
||||
.getBeansOfType(SimpleRabbitListenerContainerFactoryConfigurer.class))
|
||||
.hasSize(1);
|
||||
assertThat(this.context
|
||||
.getBeansOfType(DirectRabbitListenerContainerFactoryConfigurer.class))
|
||||
.hasSize(1);
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.listener.simple.concurrency:5",
|
||||
"spring.rabbitmq.listener.simple.maxConcurrency:10",
|
||||
"spring.rabbitmq.listener.simple.prefetch:40",
|
||||
"spring.rabbitmq.listener.direct.consumers-per-queue:5",
|
||||
"spring.rabbitmq.listener.direct.prefetch:40").run((context) -> {
|
||||
assertThat(context).hasSingleBean(
|
||||
SimpleRabbitListenerContainerFactoryConfigurer.class);
|
||||
assertThat(context).hasSingleBean(
|
||||
DirectRabbitListenerContainerFactoryConfigurer.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleRabbitListenerContainerFactoryConfigurerUsesConfig() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.listener.type:direct", // listener
|
||||
// type is
|
||||
// irrelevant
|
||||
"spring.rabbitmq.listener.simple.concurrency:5",
|
||||
"spring.rabbitmq.listener.simple.maxConcurrency:10",
|
||||
"spring.rabbitmq.listener.simple.prefetch:40");
|
||||
SimpleRabbitListenerContainerFactoryConfigurer configurer = this.context
|
||||
.getBean(SimpleRabbitListenerContainerFactoryConfigurer.class);
|
||||
SimpleRabbitListenerContainerFactory factory = mock(
|
||||
SimpleRabbitListenerContainerFactory.class);
|
||||
configurer.configure(factory, mock(ConnectionFactory.class));
|
||||
verify(factory).setConcurrentConsumers(5);
|
||||
verify(factory).setMaxConcurrentConsumers(10);
|
||||
verify(factory).setPrefetchCount(40);
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.listener.type:direct",
|
||||
"spring.rabbitmq.listener.simple.concurrency:5",
|
||||
"spring.rabbitmq.listener.simple.maxConcurrency:10",
|
||||
"spring.rabbitmq.listener.simple.prefetch:40").run((context) -> {
|
||||
SimpleRabbitListenerContainerFactoryConfigurer configurer = context
|
||||
.getBean(SimpleRabbitListenerContainerFactoryConfigurer.class);
|
||||
SimpleRabbitListenerContainerFactory factory = mock(
|
||||
SimpleRabbitListenerContainerFactory.class);
|
||||
configurer.configure(factory, mock(ConnectionFactory.class));
|
||||
verify(factory).setConcurrentConsumers(5);
|
||||
verify(factory).setMaxConcurrentConsumers(10);
|
||||
verify(factory).setPrefetchCount(40);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDirectRabbitListenerContainerFactoryConfigurerUsesConfig() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.listener.type:simple", // listener
|
||||
// type is
|
||||
// irrelevant
|
||||
"spring.rabbitmq.listener.direct.consumers-per-queue:5",
|
||||
"spring.rabbitmq.listener.direct.prefetch:40");
|
||||
DirectRabbitListenerContainerFactoryConfigurer configurer = this.context
|
||||
.getBean(DirectRabbitListenerContainerFactoryConfigurer.class);
|
||||
DirectRabbitListenerContainerFactory factory = mock(
|
||||
DirectRabbitListenerContainerFactory.class);
|
||||
configurer.configure(factory, mock(ConnectionFactory.class));
|
||||
verify(factory).setConsumersPerQueue(5);
|
||||
verify(factory).setPrefetchCount(40);
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.listener.type:simple",
|
||||
"spring.rabbitmq.listener.direct.consumers-per-queue:5",
|
||||
"spring.rabbitmq.listener.direct.prefetch:40").run((context) -> {
|
||||
DirectRabbitListenerContainerFactoryConfigurer configurer = context
|
||||
.getBean(DirectRabbitListenerContainerFactoryConfigurer.class);
|
||||
DirectRabbitListenerContainerFactory factory = mock(
|
||||
DirectRabbitListenerContainerFactory.class);
|
||||
configurer.configure(factory, mock(ConnectionFactory.class));
|
||||
verify(factory).setConsumersPerQueue(5);
|
||||
verify(factory).setPrefetchCount(40);
|
||||
});
|
||||
}
|
||||
|
||||
private void checkCommonProps(DirectFieldAccessor dfa) {
|
||||
private void checkCommonProps(AssertableApplicationContext context,
|
||||
DirectFieldAccessor dfa) {
|
||||
assertThat(dfa.getPropertyValue("autoStartup")).isEqualTo(Boolean.FALSE);
|
||||
assertThat(dfa.getPropertyValue("acknowledgeMode"))
|
||||
.isEqualTo(AcknowledgeMode.MANUAL);
|
||||
assertThat(dfa.getPropertyValue("prefetchCount")).isEqualTo(40);
|
||||
assertThat(dfa.getPropertyValue("messageConverter"))
|
||||
.isSameAs(this.context.getBean("myMessageConverter"));
|
||||
.isSameAs(context.getBean("myMessageConverter"));
|
||||
assertThat(dfa.getPropertyValue("defaultRequeueRejected"))
|
||||
.isEqualTo(Boolean.FALSE);
|
||||
assertThat(dfa.getPropertyValue("idleEventInterval")).isEqualTo(5L);
|
||||
|
@ -407,7 +444,7 @@ public class RabbitAutoConfigurationTests {
|
|||
assertThat(adviceChain).isNotNull();
|
||||
assertThat(adviceChain.length).isEqualTo(1);
|
||||
dfa = new DirectFieldAccessor(adviceChain[0]);
|
||||
MessageRecoverer messageRecoverer = this.context.getBean("myMessageRecoverer",
|
||||
MessageRecoverer messageRecoverer = context.getBean("myMessageRecoverer",
|
||||
MessageRecoverer.class);
|
||||
MethodInvocationRecoverer<?> mir = (MethodInvocationRecoverer<?>) dfa
|
||||
.getPropertyValue("recoverer");
|
||||
|
@ -431,52 +468,65 @@ public class RabbitAutoConfigurationTests {
|
|||
|
||||
@Test
|
||||
public void enableRabbitAutomatically() throws Exception {
|
||||
load(NoEnableRabbitConfiguration.class);
|
||||
AnnotationConfigApplicationContext ctx = this.context;
|
||||
ctx.getBean(
|
||||
RabbitListenerConfigUtils.RABBIT_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME);
|
||||
ctx.getBean(
|
||||
RabbitListenerConfigUtils.RABBIT_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME);
|
||||
this.contextRunner.withUserConfiguration(
|
||||
NoEnableRabbitConfiguration.class).run((context) -> {
|
||||
assertThat(context).hasBean(
|
||||
RabbitListenerConfigUtils.RABBIT_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME);
|
||||
assertThat(context).hasBean(
|
||||
RabbitListenerConfigUtils.RABBIT_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeRequestedHeartBeat() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.requestedHeartbeat:20");
|
||||
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory();
|
||||
assertThat(rabbitConnectionFactory.getRequestedHeartbeat()).isEqualTo(20);
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.requestedHeartbeat:20")
|
||||
.run((context) -> {
|
||||
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context);
|
||||
assertThat(rabbitConnectionFactory.getRequestedHeartbeat())
|
||||
.isEqualTo(20);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noSslByDefault() {
|
||||
load(TestConfiguration.class);
|
||||
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory();
|
||||
assertThat(rabbitConnectionFactory.getSocketFactory())
|
||||
.as("Must use default SocketFactory")
|
||||
.isEqualTo(SocketFactory.getDefault());
|
||||
this.contextRunner.withUserConfiguration(
|
||||
TestConfiguration.class).run((context) -> {
|
||||
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context);
|
||||
assertThat(rabbitConnectionFactory.getSocketFactory())
|
||||
.as("Must use default SocketFactory")
|
||||
.isEqualTo(SocketFactory.getDefault());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void enableSsl() {
|
||||
load(TestConfiguration.class, "spring.rabbitmq.ssl.enabled:true");
|
||||
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory();
|
||||
assertThat(rabbitConnectionFactory.getSocketFactory())
|
||||
.as("SocketFactory must use SSL").isInstanceOf(SSLSocketFactory.class);
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.ssl.enabled:true").run((context) -> {
|
||||
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context);
|
||||
assertThat(rabbitConnectionFactory.getSocketFactory())
|
||||
.as("SocketFactory must use SSL").isInstanceOf(SSLSocketFactory.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
// Make sure that we at least attempt to load the store
|
||||
public void enableSslWithExtraConfig() {
|
||||
this.thrown.expectMessage("foo");
|
||||
this.thrown.expectMessage("does not exist");
|
||||
load(TestConfiguration.class, "spring.rabbitmq.ssl.enabled:true",
|
||||
"spring.rabbitmq.ssl.keyStore=foo",
|
||||
"spring.rabbitmq.ssl.keyStorePassword=secret",
|
||||
"spring.rabbitmq.ssl.trustStore=bar",
|
||||
"spring.rabbitmq.ssl.trustStorePassword=secret");
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.ssl.enabled:true",
|
||||
"spring.rabbitmq.ssl.keyStore=foo",
|
||||
"spring.rabbitmq.ssl.keyStorePassword=secret",
|
||||
"spring.rabbitmq.ssl.trustStore=bar",
|
||||
"spring.rabbitmq.ssl.trustStorePassword=secret").run((context) -> {
|
||||
assertThat(context).hasFailed();
|
||||
assertThat(context).getFailure().hasMessageContaining("foo");
|
||||
assertThat(context).getFailure().hasMessageContaining("does not exist");
|
||||
});
|
||||
}
|
||||
|
||||
private com.rabbitmq.client.ConnectionFactory getTargetConnectionFactory() {
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
private com.rabbitmq.client.ConnectionFactory getTargetConnectionFactory(
|
||||
AssertableApplicationContext context) {
|
||||
CachingConnectionFactory connectionFactory = context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
return (com.rabbitmq.client.ConnectionFactory) new DirectFieldAccessor(
|
||||
connectionFactory).getPropertyValue("rabbitConnectionFactory");
|
||||
|
@ -489,19 +539,6 @@ public class RabbitAutoConfigurationTests {
|
|||
return expression.getValue();
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
load(new Class<?>[] { config }, environment);
|
||||
}
|
||||
|
||||
private void load(Class<?>[] configs, String... environment) {
|
||||
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
|
||||
applicationContext.register(configs);
|
||||
applicationContext.register(RabbitAutoConfiguration.class);
|
||||
TestPropertyValues.of(environment).applyTo(applicationContext);
|
||||
applicationContext.refresh();
|
||||
this.context = applicationContext;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class TestConfiguration {
|
||||
|
||||
|
|
|
@ -18,11 +18,12 @@ package org.springframework.boot.autoconfigure.aop;
|
|||
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
|
@ -38,73 +39,66 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*/
|
||||
public class AopAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(AopAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
public void aopDisabled() {
|
||||
load(TestConfiguration.class, "spring.aop.auto:false");
|
||||
TestAspect aspect = this.context.getBean(TestAspect.class);
|
||||
assertThat(aspect.isCalled()).isFalse();
|
||||
TestBean bean = this.context.getBean(TestBean.class);
|
||||
bean.foo();
|
||||
assertThat(aspect.isCalled()).isFalse();
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.aop.auto:false").run((context) -> {
|
||||
TestAspect aspect = context.getBean(TestAspect.class);
|
||||
assertThat(aspect.isCalled()).isFalse();
|
||||
TestBean bean = context.getBean(TestBean.class);
|
||||
bean.foo();
|
||||
assertThat(aspect.isCalled()).isFalse();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aopWithDefaultSettings() {
|
||||
load(TestConfiguration.class);
|
||||
testProxyTargetClassEnabled();
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.run(proxyTargetClassEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aopWithEnabledProxyTargetClass() {
|
||||
load(TestConfiguration.class, "spring.aop.proxy-target-class:true");
|
||||
testProxyTargetClassEnabled();
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.aop.proxy-target-class:true")
|
||||
.run(proxyTargetClassEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aopWithDisabledProxyTargetClass() {
|
||||
load(TestConfiguration.class, "spring.aop.proxy-target-class:false");
|
||||
testProxyTargetClassDisabled();
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.aop.proxy-target-class:false")
|
||||
.run(proxyTargetClassDisabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aopWithCustomConfiguration() {
|
||||
load(CustomTestConfiguration.class);
|
||||
testProxyTargetClassEnabled();
|
||||
this.contextRunner.withUserConfiguration(CustomTestConfiguration.class)
|
||||
.run(proxyTargetClassEnabled());
|
||||
}
|
||||
|
||||
private void testProxyTargetClassEnabled() {
|
||||
TestAspect aspect = this.context.getBean(TestAspect.class);
|
||||
assertThat(aspect.isCalled()).isFalse();
|
||||
TestBean bean = this.context.getBean(TestBean.class);
|
||||
bean.foo();
|
||||
assertThat(aspect.isCalled()).isTrue();
|
||||
private ContextConsumer<AssertableApplicationContext> proxyTargetClassEnabled() {
|
||||
return (context) -> {
|
||||
TestAspect aspect = context.getBean(TestAspect.class);
|
||||
assertThat(aspect.isCalled()).isFalse();
|
||||
TestBean bean = context.getBean(TestBean.class);
|
||||
bean.foo();
|
||||
assertThat(aspect.isCalled()).isTrue();
|
||||
};
|
||||
}
|
||||
|
||||
private void testProxyTargetClassDisabled() {
|
||||
TestAspect aspect = this.context.getBean(TestAspect.class);
|
||||
assertThat(aspect.isCalled()).isFalse();
|
||||
TestInterface bean = this.context.getBean(TestInterface.class);
|
||||
bean.foo();
|
||||
assertThat(aspect.isCalled()).isTrue();
|
||||
assertThat(this.context.getBeansOfType(TestBean.class)).isEmpty();
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(config);
|
||||
TestPropertyValues.of(environment).applyTo(ctx);
|
||||
ctx.register(AopAutoConfiguration.class);
|
||||
ctx.refresh();
|
||||
this.context = ctx;
|
||||
private ContextConsumer<AssertableApplicationContext> proxyTargetClassDisabled() {
|
||||
return (context) -> {
|
||||
TestAspect aspect = context.getBean(TestAspect.class);
|
||||
assertThat(aspect.isCalled()).isFalse();
|
||||
TestInterface bean = context.getBean(TestInterface.class);
|
||||
bean.foo();
|
||||
assertThat(aspect.isCalled()).isTrue();
|
||||
assertThat(context).doesNotHaveBean(TestBean.class);
|
||||
};
|
||||
}
|
||||
|
||||
@EnableAspectJAutoProxy(proxyTargetClass = true)
|
||||
|
|
|
@ -22,7 +22,6 @@ import java.util.Collections;
|
|||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
@ -47,14 +46,13 @@ import org.springframework.batch.core.repository.support.MapJobRepositoryFactory
|
|||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.test.City;
|
||||
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
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.jdbc.BadSqlGrammarException;
|
||||
|
@ -78,199 +76,187 @@ public class BatchAutoConfigurationTests {
|
|||
@Rule
|
||||
public ExpectedException expected = ExpectedException.none();
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(BatchAutoConfiguration.class,
|
||||
TransactionAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
public void testDefaultContext() throws Exception {
|
||||
load(new Class<?>[] { TestConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class });
|
||||
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
|
||||
assertThat(this.context.getBean(JobExplorer.class)).isNotNull();
|
||||
assertThat(
|
||||
this.context.getBean(BatchProperties.class).getInitializer().isEnabled())
|
||||
.isTrue();
|
||||
assertThat(new JdbcTemplate(this.context.getBean(DataSource.class))
|
||||
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class).run((context) -> {
|
||||
assertThat(context).hasSingleBean(JobLauncher.class);
|
||||
assertThat(context).hasSingleBean(JobExplorer.class);
|
||||
assertThat(context.getBean(BatchProperties.class)
|
||||
.getInitializer().isEnabled()).isTrue();
|
||||
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
|
||||
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoDatabase() throws Exception {
|
||||
load(new Class<?>[] { TestCustomConfiguration.class });
|
||||
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
|
||||
JobExplorer explorer = this.context.getBean(JobExplorer.class);
|
||||
assertThat(explorer).isNotNull();
|
||||
assertThat(explorer.getJobInstances("job", 0, 100)).isEmpty();
|
||||
this.contextRunner.withUserConfiguration(
|
||||
TestCustomConfiguration.class).run((context) -> {
|
||||
assertThat(context).hasSingleBean(JobLauncher.class);
|
||||
JobExplorer explorer = context.getBean(JobExplorer.class);
|
||||
assertThat(explorer.getJobInstances("job", 0, 100)).isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoBatchConfiguration() throws Exception {
|
||||
load(new Class<?>[] { EmptyConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class });
|
||||
assertThat(this.context.getBeanNamesForType(JobLauncher.class).length)
|
||||
.isEqualTo(0);
|
||||
assertThat(this.context.getBeanNamesForType(JobRepository.class).length)
|
||||
.isEqualTo(0);
|
||||
this.contextRunner.withUserConfiguration(EmptyConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class).run((context) -> {
|
||||
assertThat(context).doesNotHaveBean(JobLauncher.class);
|
||||
assertThat(context).doesNotHaveBean(JobRepository.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefinesAndLaunchesJob() throws Exception {
|
||||
load(new Class<?>[] { JobConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class });
|
||||
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
|
||||
this.context.getBean(JobLauncherCommandLineRunner.class).run();
|
||||
assertThat(this.context.getBean(JobRepository.class).getLastJobExecution("job",
|
||||
new JobParameters())).isNotNull();
|
||||
this.contextRunner.withUserConfiguration(JobConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class).run((context) -> {
|
||||
assertThat(context).hasSingleBean(JobLauncher.class);
|
||||
context.getBean(JobLauncherCommandLineRunner.class).run();
|
||||
assertThat(context.getBean(JobRepository.class).getLastJobExecution("job",
|
||||
new JobParameters())).isNotNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefinesAndLaunchesNamedJob() throws Exception {
|
||||
load(new Class<?>[] { NamedJobConfigurationWithRegisteredJob.class,
|
||||
EmbeddedDataSourceConfiguration.class },
|
||||
"spring.batch.job.names:discreteRegisteredJob");
|
||||
JobRepository repository = this.context.getBean(JobRepository.class);
|
||||
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
|
||||
this.context.getBean(JobLauncherCommandLineRunner.class).run();
|
||||
assertThat(repository.getLastJobExecution("discreteRegisteredJob",
|
||||
new JobParameters())).isNotNull();
|
||||
this.contextRunner.withUserConfiguration(
|
||||
NamedJobConfigurationWithRegisteredJob.class,
|
||||
EmbeddedDataSourceConfiguration.class).withPropertyValues(
|
||||
"spring.batch.job.names:discreteRegisteredJob").run((context) -> {
|
||||
assertThat(context).hasSingleBean(JobLauncher.class);
|
||||
context.getBean(JobLauncherCommandLineRunner.class).run();
|
||||
assertThat(context.getBean(JobRepository.class).getLastJobExecution(
|
||||
"discreteRegisteredJob", new JobParameters())).isNotNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefinesAndLaunchesLocalJob() throws Exception {
|
||||
load(new Class<?>[] { NamedJobConfigurationWithLocalJob.class,
|
||||
EmbeddedDataSourceConfiguration.class },
|
||||
"spring.batch.job.names:discreteLocalJob");
|
||||
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
|
||||
this.context.getBean(JobLauncherCommandLineRunner.class).run();
|
||||
assertThat(this.context.getBean(JobRepository.class)
|
||||
.getLastJobExecution("discreteLocalJob", new JobParameters()))
|
||||
.isNotNull();
|
||||
this.contextRunner.withUserConfiguration(NamedJobConfigurationWithLocalJob.class,
|
||||
EmbeddedDataSourceConfiguration.class).withPropertyValues(
|
||||
"spring.batch.job.names:discreteLocalJob").run((context) -> {
|
||||
assertThat(context).hasSingleBean(JobLauncher.class);
|
||||
context.getBean(JobLauncherCommandLineRunner.class).run();
|
||||
assertThat(context.getBean(JobRepository.class).getLastJobExecution(
|
||||
"discreteLocalJob", new JobParameters())).isNotNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisableLaunchesJob() throws Exception {
|
||||
load(new Class<?>[] { JobConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class },
|
||||
"spring.batch.job.enabled:false");
|
||||
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
|
||||
assertThat(this.context.getBeanNamesForType(CommandLineRunner.class).length)
|
||||
.isEqualTo(0);
|
||||
this.contextRunner.withUserConfiguration(JobConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class).withPropertyValues(
|
||||
"spring.batch.job.enabled:false").run((context) -> {
|
||||
assertThat(context).hasSingleBean(JobLauncher.class);
|
||||
assertThat(context).doesNotHaveBean(CommandLineRunner.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisableSchemaLoader() throws Exception {
|
||||
load(new Class<?>[] { TestConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class },
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class).withPropertyValues(
|
||||
"spring.datasource.generate-unique-name=true",
|
||||
"spring.batch.initializer.enabled:false");
|
||||
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
|
||||
assertThat(
|
||||
this.context.getBean(BatchProperties.class).getInitializer().isEnabled())
|
||||
.isFalse();
|
||||
this.expected.expect(BadSqlGrammarException.class);
|
||||
new JdbcTemplate(this.context.getBean(DataSource.class))
|
||||
.queryForList("select * from BATCH_JOB_EXECUTION");
|
||||
"spring.batch.initializer.enabled:false").run((context) -> {
|
||||
assertThat(context).hasSingleBean(JobLauncher.class);
|
||||
assertThat(context.getBean(BatchProperties.class).getInitializer()
|
||||
.isEnabled()).isFalse();
|
||||
this.expected.expect(BadSqlGrammarException.class);
|
||||
new JdbcTemplate(context.getBean(DataSource.class)).queryForList(
|
||||
"select * from BATCH_JOB_EXECUTION");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsingJpa() throws Exception {
|
||||
load(new Class<?>[] { TestConfiguration.class,
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class,
|
||||
HibernateJpaAutoConfiguration.class });
|
||||
PlatformTransactionManager transactionManager = this.context
|
||||
.getBean(PlatformTransactionManager.class);
|
||||
// It's a lazy proxy, but it does render its target if you ask for toString():
|
||||
assertThat(transactionManager.toString().contains("JpaTransactionManager"))
|
||||
.isTrue();
|
||||
assertThat(this.context.getBean(EntityManagerFactory.class)).isNotNull();
|
||||
// Ensure the JobRepository can be used (no problem with isolation level)
|
||||
assertThat(this.context.getBean(JobRepository.class).getLastJobExecution("job",
|
||||
new JobParameters())).isNull();
|
||||
HibernateJpaAutoConfiguration.class).run((context) -> {
|
||||
PlatformTransactionManager transactionManager = context
|
||||
.getBean(PlatformTransactionManager.class);
|
||||
// It's a lazy proxy, but it does render its target if you ask for toString():
|
||||
assertThat(transactionManager.toString().contains("JpaTransactionManager"))
|
||||
.isTrue();
|
||||
assertThat(context).hasSingleBean(EntityManagerFactory.class);
|
||||
// Ensure the JobRepository can be used (no problem with isolation level)
|
||||
assertThat(context.getBean(JobRepository.class).getLastJobExecution("job",
|
||||
new JobParameters())).isNull();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRenamePrefix() throws Exception {
|
||||
load(new Class<?>[] { TestConfiguration.class,
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class,
|
||||
HibernateJpaAutoConfiguration.class },
|
||||
HibernateJpaAutoConfiguration.class).withPropertyValues(
|
||||
"spring.datasource.generate-unique-name=true",
|
||||
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
|
||||
"spring.batch.tablePrefix:PREFIX_");
|
||||
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
|
||||
assertThat(
|
||||
this.context.getBean(BatchProperties.class).getInitializer().isEnabled())
|
||||
.isTrue();
|
||||
assertThat(new JdbcTemplate(this.context.getBean(DataSource.class))
|
||||
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
|
||||
JobExplorer jobExplorer = this.context.getBean(JobExplorer.class);
|
||||
assertThat(jobExplorer.findRunningJobExecutions("test")).isEmpty();
|
||||
JobRepository jobRepository = this.context.getBean(JobRepository.class);
|
||||
assertThat(jobRepository.getLastJobExecution("test", new JobParameters()))
|
||||
.isNull();
|
||||
"spring.batch.tablePrefix:PREFIX_").run((context) -> {
|
||||
assertThat(context).hasSingleBean(JobLauncher.class);
|
||||
assertThat(context.getBean(BatchProperties.class).getInitializer()
|
||||
.isEnabled()).isTrue();
|
||||
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
|
||||
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
|
||||
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
|
||||
assertThat(jobExplorer.findRunningJobExecutions("test")).isEmpty();
|
||||
JobRepository jobRepository = context.getBean(JobRepository.class);
|
||||
assertThat(jobRepository.getLastJobExecution("test", new JobParameters()))
|
||||
.isNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomTablePrefixWithDefaultSchemaDisablesInitializer()
|
||||
throws Exception {
|
||||
load(new Class<?>[] { TestConfiguration.class,
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class,
|
||||
HibernateJpaAutoConfiguration.class },
|
||||
HibernateJpaAutoConfiguration.class).withPropertyValues(
|
||||
"spring.datasource.generate-unique-name=true",
|
||||
"spring.batch.tablePrefix:PREFIX_");
|
||||
assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
|
||||
assertThat(
|
||||
this.context.getBean(BatchProperties.class).getInitializer().isEnabled())
|
||||
.isFalse();
|
||||
this.expected.expect(BadSqlGrammarException.class);
|
||||
new JdbcTemplate(this.context.getBean(DataSource.class))
|
||||
.queryForList("select * from BATCH_JOB_EXECUTION");
|
||||
"spring.batch.tablePrefix:PREFIX_").run((context) -> {
|
||||
assertThat(context).hasSingleBean(JobLauncher.class);
|
||||
assertThat(context.getBean(BatchProperties.class).getInitializer()
|
||||
.isEnabled()).isFalse();
|
||||
this.expected.expect(BadSqlGrammarException.class);
|
||||
new JdbcTemplate(context.getBean(DataSource.class)).queryForList(
|
||||
"select * from BATCH_JOB_EXECUTION");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeJpaTransactionManagerUsingProperties() throws Exception {
|
||||
load(new Class<?>[] { TestConfiguration.class,
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class,
|
||||
HibernateJpaAutoConfiguration.class },
|
||||
HibernateJpaAutoConfiguration.class).withPropertyValues(
|
||||
"spring.transaction.default-timeout:30",
|
||||
"spring.transaction.rollback-on-commit-failure:true");
|
||||
this.context.getBean(BatchConfigurer.class);
|
||||
JpaTransactionManager transactionManager = JpaTransactionManager.class.cast(
|
||||
this.context.getBean(BatchConfigurer.class).getTransactionManager());
|
||||
assertThat(transactionManager.getDefaultTimeout()).isEqualTo(30);
|
||||
assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue();
|
||||
"spring.transaction.rollback-on-commit-failure:true").run((context) -> {
|
||||
assertThat(context).hasSingleBean(BatchConfigurer.class);
|
||||
JpaTransactionManager transactionManager = JpaTransactionManager.class.cast(
|
||||
context.getBean(BatchConfigurer.class).getTransactionManager());
|
||||
assertThat(transactionManager.getDefaultTimeout()).isEqualTo(30);
|
||||
assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeDataSourceTransactionManagerUsingProperties()
|
||||
throws Exception {
|
||||
load(new Class<?>[] { TestConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class },
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class).withPropertyValues(
|
||||
"spring.transaction.default-timeout:30",
|
||||
"spring.transaction.rollback-on-commit-failure:true");
|
||||
this.context.getBean(BatchConfigurer.class);
|
||||
DataSourceTransactionManager transactionManager = DataSourceTransactionManager.class
|
||||
.cast(this.context.getBean(BatchConfigurer.class)
|
||||
.getTransactionManager());
|
||||
assertThat(transactionManager.getDefaultTimeout()).isEqualTo(30);
|
||||
assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue();
|
||||
}
|
||||
|
||||
private void load(Class<?>[] configs, String... environment) {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
TestPropertyValues.of(environment).applyTo(ctx);
|
||||
if (configs != null) {
|
||||
ctx.register(configs);
|
||||
}
|
||||
ctx.register(BatchAutoConfiguration.class, TransactionAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
ctx.refresh();
|
||||
this.context = ctx;
|
||||
"spring.transaction.rollback-on-commit-failure:true").run((context) -> {
|
||||
assertThat(context).hasSingleBean(BatchConfigurer.class);
|
||||
DataSourceTransactionManager transactionManager = DataSourceTransactionManager.class
|
||||
.cast(context.getBean(BatchConfigurer.class).getTransactionManager());
|
||||
assertThat(transactionManager.getDefaultTimeout()).isEqualTo(30);
|
||||
assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
|
|
@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.batch;
|
|||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
|
@ -27,15 +26,14 @@ import org.springframework.batch.core.configuration.annotation.EnableBatchProces
|
|||
import org.springframework.batch.core.explore.JobExplorer;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.test.City;
|
||||
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
|
||||
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
|
@ -50,66 +48,49 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
@ClassPathExclusions("hibernate-jpa-*.jar")
|
||||
public class BatchAutoConfigurationWithoutJpaTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(BatchAutoConfiguration.class,
|
||||
TransactionAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
public void jdbcWithDefaultSettings() throws Exception {
|
||||
load(new Class<?>[] { DefaultConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class },
|
||||
"spring.datasource.generate-unique-name=true");
|
||||
assertThat(this.context.getBeansOfType(JobLauncher.class)).hasSize(1);
|
||||
assertThat(this.context.getBeansOfType(JobExplorer.class)).hasSize(1);
|
||||
assertThat(this.context.getBeansOfType(JobRepository.class)).hasSize(1);
|
||||
assertThat(this.context.getBeansOfType(PlatformTransactionManager.class))
|
||||
.hasSize(1);
|
||||
assertThat(this.context.getBean(PlatformTransactionManager.class).toString())
|
||||
.contains("DataSourceTransactionManager");
|
||||
assertThat(
|
||||
this.context.getBean(BatchProperties.class).getInitializer().isEnabled())
|
||||
.isTrue();
|
||||
assertThat(new JdbcTemplate(this.context.getBean(DataSource.class))
|
||||
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
|
||||
assertThat(
|
||||
this.context.getBean(JobExplorer.class).findRunningJobExecutions("test"))
|
||||
.isEmpty();
|
||||
assertThat(this.context.getBean(JobRepository.class).getLastJobExecution("test",
|
||||
new JobParameters())).isNull();
|
||||
this.contextRunner.withUserConfiguration(DefaultConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class).withPropertyValues(
|
||||
"spring.datasource.generate-unique-name=true").run((context) -> {
|
||||
assertThat(context).hasSingleBean(JobLauncher.class);
|
||||
assertThat(context).hasSingleBean(JobExplorer.class);
|
||||
assertThat(context).hasSingleBean(JobRepository.class);
|
||||
assertThat(context).hasSingleBean(PlatformTransactionManager.class);
|
||||
assertThat(context.getBean(PlatformTransactionManager.class).toString())
|
||||
.contains("DataSourceTransactionManager");
|
||||
assertThat(
|
||||
context.getBean(BatchProperties.class).getInitializer().isEnabled())
|
||||
.isTrue();
|
||||
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
|
||||
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
|
||||
assertThat(
|
||||
context.getBean(JobExplorer.class).findRunningJobExecutions("test"))
|
||||
.isEmpty();
|
||||
assertThat(context.getBean(JobRepository.class).getLastJobExecution("test",
|
||||
new JobParameters())).isNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jdbcWithCustomPrefix() throws Exception {
|
||||
load(new Class<?>[] { DefaultConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class },
|
||||
this.contextRunner.withUserConfiguration(DefaultConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class).withPropertyValues(
|
||||
"spring.datasource.generate-unique-name=true",
|
||||
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
|
||||
"spring.batch.tablePrefix:PREFIX_");
|
||||
assertThat(new JdbcTemplate(this.context.getBean(DataSource.class))
|
||||
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
|
||||
assertThat(
|
||||
this.context.getBean(JobExplorer.class).findRunningJobExecutions("test"))
|
||||
.isEmpty();
|
||||
assertThat(this.context.getBean(JobRepository.class).getLastJobExecution("test",
|
||||
new JobParameters())).isNull();
|
||||
|
||||
}
|
||||
|
||||
private void load(Class<?>[] configs, String... environment) {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
TestPropertyValues.of(environment).applyTo(ctx);
|
||||
if (configs != null) {
|
||||
ctx.register(configs);
|
||||
}
|
||||
ctx.register(BatchAutoConfiguration.class, TransactionAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
ctx.refresh();
|
||||
this.context = ctx;
|
||||
"spring.batch.tablePrefix:PREFIX_").run((context) -> {
|
||||
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
|
||||
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
|
||||
assertThat(
|
||||
context.getBean(JobExplorer.class).findRunningJobExecutions("test"))
|
||||
.isEmpty();
|
||||
assertThat(context.getBean(JobRepository.class).getLastJobExecution("test",
|
||||
new JobParameters())).isNull();
|
||||
});
|
||||
}
|
||||
|
||||
@EnableBatchProcessing
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.batch;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -51,13 +52,11 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*/
|
||||
public class JobLauncherCommandLineRunnerTests {
|
||||
|
||||
private JobLauncherCommandLineRunner runner;
|
||||
|
||||
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
||||
private JobExplorer jobExplorer;
|
||||
private JobLauncherCommandLineRunner runner;
|
||||
|
||||
private JobLauncher jobLauncher;
|
||||
private JobExplorer jobExplorer;
|
||||
|
||||
private JobBuilderFactory jobs;
|
||||
|
||||
|
@ -72,7 +71,7 @@ public class JobLauncherCommandLineRunnerTests {
|
|||
this.context.register(BatchConfiguration.class);
|
||||
this.context.refresh();
|
||||
JobRepository jobRepository = this.context.getBean(JobRepository.class);
|
||||
this.jobLauncher = this.context.getBean(JobLauncher.class);
|
||||
JobLauncher jobLauncher = this.context.getBean(JobLauncher.class);
|
||||
this.jobs = new JobBuilderFactory(jobRepository);
|
||||
PlatformTransactionManager transactionManager = this.context
|
||||
.getBean(PlatformTransactionManager.class);
|
||||
|
@ -81,11 +80,16 @@ public class JobLauncherCommandLineRunnerTests {
|
|||
this.step = this.steps.get("step").tasklet(tasklet).build();
|
||||
this.job = this.jobs.get("job").start(this.step).build();
|
||||
this.jobExplorer = this.context.getBean(JobExplorer.class);
|
||||
this.runner = new JobLauncherCommandLineRunner(this.jobLauncher,
|
||||
this.runner = new JobLauncherCommandLineRunner(jobLauncher,
|
||||
this.jobExplorer);
|
||||
this.context.getBean(BatchConfiguration.class).clear();
|
||||
}
|
||||
|
||||
@After
|
||||
public void closeContext() {
|
||||
this.context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basicExecution() throws Exception {
|
||||
this.runner.execute(this.job, new JobParameters());
|
||||
|
|
|
@ -144,7 +144,7 @@ public class CacheAutoConfigurationTests {
|
|||
this.contextRunner.withUserConfiguration(SpecificCacheResolverConfiguration.class)
|
||||
.withPropertyValues("spring.cache.type=simple").run((context) -> {
|
||||
getCacheManager(context, ConcurrentMapCacheManager.class);
|
||||
assertThat(context).getBeans(CacheResolver.class).hasSize(1);
|
||||
assertThat(context).hasSingleBean(CacheResolver.class);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -656,8 +656,7 @@ public class CacheAutoConfigurationTests {
|
|||
.getCacheManager();
|
||||
assertThat(jCacheManager).isInstanceOf(
|
||||
com.hazelcast.cache.HazelcastCacheManager.class);
|
||||
assertThat(context.getBeansOfType(HazelcastInstance.class))
|
||||
.hasSize(1);
|
||||
assertThat(context).hasSingleBean(HazelcastInstance.class);
|
||||
HazelcastInstance hazelcastInstance = context
|
||||
.getBean(HazelcastInstance.class);
|
||||
assertThat(((com.hazelcast.cache.HazelcastCacheManager) jCacheManager)
|
||||
|
|
|
@ -18,12 +18,10 @@ package org.springframework.boot.autoconfigure.cassandra;
|
|||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.PoolingOptions;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
@ -38,95 +36,81 @@ import static org.mockito.Mockito.mock;
|
|||
*/
|
||||
public class CassandraAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(CassandraAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
public void createClusterWithDefault() {
|
||||
load();
|
||||
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
|
||||
Cluster cluster = this.context.getBean(Cluster.class);
|
||||
assertThat(cluster.getClusterName()).startsWith("cluster");
|
||||
this.contextRunner.run((context) -> {
|
||||
assertThat(context).hasSingleBean(Cluster.class);
|
||||
assertThat(context.getBean(Cluster.class).getClusterName())
|
||||
.startsWith("cluster");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createClusterWithOverrides() {
|
||||
load("spring.data.cassandra.cluster-name=testcluster");
|
||||
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
|
||||
Cluster cluster = this.context.getBean(Cluster.class);
|
||||
assertThat(cluster.getClusterName()).isEqualTo("testcluster");
|
||||
this.contextRunner.withPropertyValues(
|
||||
"spring.data.cassandra.cluster-name=testcluster").run((context) -> {
|
||||
assertThat(context).hasSingleBean(Cluster.class);
|
||||
assertThat(context.getBean(Cluster.class).getClusterName())
|
||||
.isEqualTo("testcluster");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createCustomizeCluster() {
|
||||
load(MockCustomizerConfig.class);
|
||||
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
|
||||
assertThat(
|
||||
this.context.getBeanNamesForType(ClusterBuilderCustomizer.class).length)
|
||||
.isEqualTo(1);
|
||||
this.contextRunner.withUserConfiguration(
|
||||
MockCustomizerConfig.class).run((context) -> {
|
||||
assertThat(context).hasSingleBean(Cluster.class);
|
||||
assertThat(context).hasSingleBean(ClusterBuilderCustomizer.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizerOverridesAutoConfig() {
|
||||
load(SimpleCustomizerConfig.class,
|
||||
"spring.data.cassandra.cluster-name=testcluster");
|
||||
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
|
||||
Cluster cluster = this.context.getBean(Cluster.class);
|
||||
assertThat(cluster.getClusterName()).isEqualTo("overridden-name");
|
||||
this.contextRunner.withUserConfiguration(SimpleCustomizerConfig.class)
|
||||
.withPropertyValues("spring.data.cassandra.cluster-name=testcluster").run((context) -> {
|
||||
assertThat(context).hasSingleBean(Cluster.class);
|
||||
assertThat(context.getBean(Cluster.class).getClusterName())
|
||||
.isEqualTo("overridden-name");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultPoolOptions() {
|
||||
load();
|
||||
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
|
||||
PoolingOptions poolingOptions = this.context.getBean(Cluster.class)
|
||||
.getConfiguration().getPoolingOptions();
|
||||
assertThat(poolingOptions.getIdleTimeoutSeconds())
|
||||
.isEqualTo(PoolingOptions.DEFAULT_IDLE_TIMEOUT_SECONDS);
|
||||
assertThat(poolingOptions.getPoolTimeoutMillis())
|
||||
.isEqualTo(PoolingOptions.DEFAULT_POOL_TIMEOUT_MILLIS);
|
||||
assertThat(poolingOptions.getHeartbeatIntervalSeconds())
|
||||
.isEqualTo(PoolingOptions.DEFAULT_HEARTBEAT_INTERVAL_SECONDS);
|
||||
assertThat(poolingOptions.getMaxQueueSize())
|
||||
.isEqualTo(PoolingOptions.DEFAULT_MAX_QUEUE_SIZE);
|
||||
this.contextRunner.run((context) -> {
|
||||
assertThat(context).hasSingleBean(Cluster.class);
|
||||
PoolingOptions poolingOptions = context.getBean(Cluster.class)
|
||||
.getConfiguration().getPoolingOptions();
|
||||
assertThat(poolingOptions.getIdleTimeoutSeconds())
|
||||
.isEqualTo(PoolingOptions.DEFAULT_IDLE_TIMEOUT_SECONDS);
|
||||
assertThat(poolingOptions.getPoolTimeoutMillis())
|
||||
.isEqualTo(PoolingOptions.DEFAULT_POOL_TIMEOUT_MILLIS);
|
||||
assertThat(poolingOptions.getHeartbeatIntervalSeconds())
|
||||
.isEqualTo(PoolingOptions.DEFAULT_HEARTBEAT_INTERVAL_SECONDS);
|
||||
assertThat(poolingOptions.getMaxQueueSize())
|
||||
.isEqualTo(PoolingOptions.DEFAULT_MAX_QUEUE_SIZE);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizePoolOptions() {
|
||||
load("spring.data.cassandra.pool.idle-timeout=42",
|
||||
this.contextRunner.withPropertyValues(
|
||||
"spring.data.cassandra.pool.idle-timeout=42",
|
||||
"spring.data.cassandra.pool.pool-timeout=52",
|
||||
"spring.data.cassandra.pool.heartbeat-interval=62",
|
||||
"spring.data.cassandra.pool.max-queue-size=72");
|
||||
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
|
||||
PoolingOptions poolingOptions = this.context.getBean(Cluster.class)
|
||||
.getConfiguration().getPoolingOptions();
|
||||
assertThat(poolingOptions.getIdleTimeoutSeconds()).isEqualTo(42);
|
||||
assertThat(poolingOptions.getPoolTimeoutMillis()).isEqualTo(52);
|
||||
assertThat(poolingOptions.getHeartbeatIntervalSeconds()).isEqualTo(62);
|
||||
assertThat(poolingOptions.getMaxQueueSize()).isEqualTo(72);
|
||||
"spring.data.cassandra.pool.max-queue-size=72").run((context) -> {
|
||||
assertThat(context).hasSingleBean(Cluster.class);
|
||||
PoolingOptions poolingOptions = context.getBean(Cluster.class)
|
||||
.getConfiguration().getPoolingOptions();
|
||||
assertThat(poolingOptions.getIdleTimeoutSeconds()).isEqualTo(42);
|
||||
assertThat(poolingOptions.getPoolTimeoutMillis()).isEqualTo(52);
|
||||
assertThat(poolingOptions.getHeartbeatIntervalSeconds()).isEqualTo(62);
|
||||
assertThat(poolingOptions.getMaxQueueSize()).isEqualTo(72);
|
||||
});
|
||||
}
|
||||
|
||||
private void load(String... environment) {
|
||||
load(null, environment);
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
if (config != null) {
|
||||
ctx.register(config);
|
||||
}
|
||||
ctx.register(PropertyPlaceholderAutoConfiguration.class,
|
||||
CassandraAutoConfiguration.class);
|
||||
TestPropertyValues.of(environment).applyTo(ctx);
|
||||
ctx.refresh();
|
||||
this.context = ctx;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class MockCustomizerConfig {
|
||||
|
|
|
@ -18,8 +18,9 @@ package org.springframework.boot.autoconfigure.condition;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
|
@ -34,40 +35,40 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*/
|
||||
public class AllNestedConditionsTests {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void neither() throws Exception {
|
||||
AnnotationConfigApplicationContext context = load(Config.class);
|
||||
assertThat(context.containsBean("myBean")).isFalse();
|
||||
context.close();
|
||||
this.contextRunner.withUserConfiguration(Config.class).run(match(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertyA() throws Exception {
|
||||
AnnotationConfigApplicationContext context = load(Config.class, "a:a");
|
||||
assertThat(context.containsBean("myBean")).isFalse();
|
||||
context.close();
|
||||
this.contextRunner.withUserConfiguration(Config.class).withPropertyValues("a:a")
|
||||
.run(match(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertyB() throws Exception {
|
||||
AnnotationConfigApplicationContext context = load(Config.class, "b:b");
|
||||
assertThat(context.containsBean("myBean")).isFalse();
|
||||
context.close();
|
||||
this.contextRunner.withUserConfiguration(Config.class).withPropertyValues("b:b")
|
||||
.run(match(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void both() throws Exception {
|
||||
AnnotationConfigApplicationContext context = load(Config.class, "a:a", "b:b");
|
||||
assertThat(context.containsBean("myBean")).isTrue();
|
||||
context.close();
|
||||
this.contextRunner.withUserConfiguration(Config.class)
|
||||
.withPropertyValues("a:a", "b:b").run(match(true));
|
||||
}
|
||||
|
||||
private AnnotationConfigApplicationContext load(Class<?> config, String... env) {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
TestPropertyValues.of(env).applyTo(context);
|
||||
context.register(config);
|
||||
context.refresh();
|
||||
return context;
|
||||
private ContextConsumer<AssertableApplicationContext> match(boolean expected) {
|
||||
return (context) -> {
|
||||
if (expected) {
|
||||
assertThat(context).hasBean("myBean");
|
||||
}
|
||||
else {
|
||||
assertThat(context).doesNotHaveBean("myBean");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
|
|
@ -18,8 +18,9 @@ package org.springframework.boot.autoconfigure.condition;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
|
@ -37,40 +38,40 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*/
|
||||
public class AnyNestedConditionTests {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void neither() throws Exception {
|
||||
AnnotationConfigApplicationContext context = load(Config.class);
|
||||
assertThat(context.containsBean("myBean")).isFalse();
|
||||
context.close();
|
||||
this.contextRunner.withUserConfiguration(Config.class).run(match(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertyA() throws Exception {
|
||||
AnnotationConfigApplicationContext context = load(Config.class, "a:a");
|
||||
assertThat(context.containsBean("myBean")).isTrue();
|
||||
context.close();
|
||||
this.contextRunner.withUserConfiguration(Config.class).withPropertyValues("a:a")
|
||||
.run(match(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertyB() throws Exception {
|
||||
AnnotationConfigApplicationContext context = load(Config.class, "b:b");
|
||||
assertThat(context.containsBean("myBean")).isTrue();
|
||||
context.close();
|
||||
this.contextRunner.withUserConfiguration(Config.class).withPropertyValues("b:b")
|
||||
.run(match(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void both() throws Exception {
|
||||
AnnotationConfigApplicationContext context = load(Config.class, "a:a", "b:b");
|
||||
assertThat(context.containsBean("myBean")).isTrue();
|
||||
context.close();
|
||||
this.contextRunner.withUserConfiguration(Config.class)
|
||||
.withPropertyValues("a:a", "b:b").run(match(true));
|
||||
}
|
||||
|
||||
private AnnotationConfigApplicationContext load(Class<?> config, String... env) {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
TestPropertyValues.of(env).applyTo(context);
|
||||
context.register(config);
|
||||
context.refresh();
|
||||
return context;
|
||||
private ContextConsumer<AssertableApplicationContext> match(boolean expected) {
|
||||
return (context) -> {
|
||||
if (expected) {
|
||||
assertThat(context).hasBean("myBean");
|
||||
}
|
||||
else {
|
||||
assertThat(context).doesNotHaveBean("myBean");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
|
|
@ -28,8 +28,9 @@ import org.junit.Test;
|
|||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
@ -45,100 +46,98 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
* Tests for {@link ConditionalOnBean}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class ConditionalOnBeanTests {
|
||||
|
||||
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void testNameOnBeanCondition() {
|
||||
this.context.register(FooConfiguration.class, OnBeanNameConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("bar")).isTrue();
|
||||
assertThat(this.context.getBean("bar")).isEqualTo("bar");
|
||||
this.contextRunner.withUserConfiguration(FooConfiguration.class,
|
||||
OnBeanNameConfiguration.class).run(hasBarBean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNameAndTypeOnBeanCondition() {
|
||||
this.context.register(FooConfiguration.class,
|
||||
OnBeanNameAndTypeConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("bar")).isFalse();
|
||||
this.contextRunner.withUserConfiguration(FooConfiguration.class,
|
||||
OnBeanNameAndTypeConfiguration.class).run((context) ->
|
||||
assertThat(context).doesNotHaveBean("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNameOnBeanConditionReverseOrder() {
|
||||
this.context.register(OnBeanNameConfiguration.class, FooConfiguration.class);
|
||||
this.context.refresh();
|
||||
// Ideally this should be true
|
||||
assertThat(this.context.containsBean("bar")).isFalse();
|
||||
this.contextRunner.withUserConfiguration(OnBeanNameConfiguration.class,
|
||||
FooConfiguration.class).run((context) ->
|
||||
assertThat(context).doesNotHaveBean("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassOnBeanCondition() {
|
||||
this.context.register(FooConfiguration.class, OnBeanClassConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("bar")).isTrue();
|
||||
assertThat(this.context.getBean("bar")).isEqualTo("bar");
|
||||
this.contextRunner.withUserConfiguration(FooConfiguration.class,
|
||||
OnBeanClassConfiguration.class).run(hasBarBean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassOnBeanClassNameCondition() {
|
||||
this.context.register(FooConfiguration.class, OnBeanClassNameConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("bar")).isTrue();
|
||||
assertThat(this.context.getBean("bar")).isEqualTo("bar");
|
||||
this.contextRunner.withUserConfiguration(FooConfiguration.class,
|
||||
OnBeanClassNameConfiguration.class).run(hasBarBean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnBeanConditionWithXml() {
|
||||
this.context.register(XmlConfiguration.class, OnBeanNameConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("bar")).isTrue();
|
||||
assertThat(this.context.getBean("bar")).isEqualTo("bar");
|
||||
this.contextRunner.withUserConfiguration(XmlConfiguration.class,
|
||||
OnBeanNameConfiguration.class).run(hasBarBean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnBeanConditionWithCombinedXml() {
|
||||
this.context.register(CombinedXmlConfiguration.class);
|
||||
this.context.refresh();
|
||||
// Ideally this should be true
|
||||
assertThat(this.context.containsBean("bar")).isFalse();
|
||||
this.contextRunner.withUserConfiguration(CombinedXmlConfiguration.class)
|
||||
.run((context) -> assertThat(context).doesNotHaveBean("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnnotationOnBeanCondition() {
|
||||
this.context.register(FooConfiguration.class, OnAnnotationConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("bar")).isTrue();
|
||||
assertThat(this.context.getBean("bar")).isEqualTo("bar");
|
||||
this.contextRunner.withUserConfiguration(FooConfiguration.class,
|
||||
OnAnnotationConfiguration.class).run(hasBarBean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnMissingBeanType() throws Exception {
|
||||
this.context.register(FooConfiguration.class,
|
||||
OnBeanMissingClassConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("bar")).isFalse();
|
||||
this.contextRunner.withUserConfiguration(FooConfiguration.class,
|
||||
OnBeanMissingClassConfiguration.class).run((context) ->
|
||||
assertThat(context).doesNotHaveBean("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withPropertyPlaceholderClassName() throws Exception {
|
||||
TestPropertyValues.of("mybeanclass=java.lang.String").applyTo(this.context);
|
||||
this.context.register(PropertySourcesPlaceholderConfigurer.class,
|
||||
WithPropertyPlaceholderClassName.class, OnBeanClassConfiguration.class);
|
||||
this.context.refresh();
|
||||
this.contextRunner.withUserConfiguration(
|
||||
PropertySourcesPlaceholderConfigurer.class,
|
||||
WithPropertyPlaceholderClassName.class,
|
||||
OnBeanClassConfiguration.class)
|
||||
.withPropertyValues("mybeanclass=java.lang.String")
|
||||
.run(context -> assertThat(context).hasNotFailed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanProducedByFactoryBeanIsConsideredWhenMatchingOnAnnotation() {
|
||||
this.context.register(FactoryBeanConfiguration.class,
|
||||
OnAnnotationWithFactoryBeanConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("bar")).isTrue();
|
||||
assertThat(this.context.getBeansOfType(ExampleBean.class)).hasSize(1);
|
||||
this.contextRunner.withUserConfiguration(FactoryBeanConfiguration.class,
|
||||
OnAnnotationWithFactoryBeanConfiguration.class).run((context) -> {
|
||||
assertThat(context).hasBean("bar");
|
||||
assertThat(context).hasSingleBean(ExampleBean.class);
|
||||
});
|
||||
}
|
||||
|
||||
private ContextConsumer<AssertableApplicationContext> hasBarBean() {
|
||||
return (context) -> {
|
||||
assertThat(context).hasBean("bar");
|
||||
assertThat(context.getBean("bar")).isEqualTo("bar");
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnBean(name = "foo")
|
||||
protected static class OnBeanNameConfiguration {
|
||||
|
|
|
@ -18,7 +18,9 @@ package org.springframework.boot.autoconfigure.condition;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
@ -30,41 +32,46 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
* Tests for {@link ConditionalOnClass}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class ConditionalOnClassTests {
|
||||
|
||||
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void testVanillaOnClassCondition() {
|
||||
this.context.register(BasicConfiguration.class, FooConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("bar")).isTrue();
|
||||
assertThat(this.context.getBean("bar")).isEqualTo("bar");
|
||||
this.contextRunner.withUserConfiguration(BasicConfiguration.class,
|
||||
FooConfiguration.class).run(hasBarBean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingOnClassCondition() {
|
||||
this.context.register(MissingConfiguration.class, FooConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("bar")).isFalse();
|
||||
assertThat(this.context.getBean("foo")).isEqualTo("foo");
|
||||
this.contextRunner.withUserConfiguration(MissingConfiguration.class,
|
||||
FooConfiguration.class).run((context) -> {
|
||||
assertThat(context).doesNotHaveBean("bar");
|
||||
assertThat(context).hasBean("foo");
|
||||
assertThat(context.getBean("foo")).isEqualTo("foo");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testOnClassConditionWithXml() {
|
||||
this.context.register(BasicConfiguration.class, XmlConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("bar")).isTrue();
|
||||
assertThat(this.context.getBean("bar")).isEqualTo("bar");
|
||||
this.contextRunner.withUserConfiguration(BasicConfiguration.class,
|
||||
XmlConfiguration.class).run(hasBarBean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnClassConditionWithCombinedXml() {
|
||||
this.context.register(CombinedXmlConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("bar")).isTrue();
|
||||
assertThat(this.context.getBean("bar")).isEqualTo("bar");
|
||||
this.contextRunner.withUserConfiguration(CombinedXmlConfiguration.class)
|
||||
.run(hasBarBean());
|
||||
}
|
||||
|
||||
private ContextConsumer<AssertableApplicationContext> hasBarBean() {
|
||||
return (context) -> {
|
||||
assertThat(context).hasBean("bar");
|
||||
assertThat(context.getBean("bar")).isEqualTo("bar");
|
||||
};
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
|
|
@ -16,12 +16,12 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.cloud.CloudPlatform;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
@ -32,39 +32,37 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*/
|
||||
public class ConditionalOnCloudPlatformTests {
|
||||
|
||||
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
@Test
|
||||
public void outcomeWhenCloudfoundryPlatformNotPresentShouldNotMatch() {
|
||||
this.contextRunner.withUserConfiguration(CloudFoundryPlatformConfig.class)
|
||||
.run(match(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void outcomeWhenCloudfoundryPlatformNotPresentShouldNotMatch()
|
||||
throws Exception {
|
||||
load(CloudFoundryPlatformConfig.class, "");
|
||||
assertThat(this.context.containsBean("foo")).isFalse();
|
||||
public void outcomeWhenCloudfoundryPlatformPresentShouldMatch() {
|
||||
this.contextRunner.withUserConfiguration(CloudFoundryPlatformConfig.class)
|
||||
.withPropertyValues("VCAP_APPLICATION:---")
|
||||
.run(match(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void outcomeWhenCloudfoundryPlatformPresentShouldMatch() throws Exception {
|
||||
load(CloudFoundryPlatformConfig.class, "VCAP_APPLICATION:---");
|
||||
assertThat(this.context.containsBean("foo")).isTrue();
|
||||
public void outcomeWhenCloudfoundryPlatformPresentAndMethodTargetShouldMatch() {
|
||||
this.contextRunner.withUserConfiguration(CloudFoundryPlatformOnMethodConfig.class)
|
||||
.withPropertyValues("VCAP_APPLICATION:---")
|
||||
.run(match(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void outcomeWhenCloudfoundryPlatformPresentAndMethodTargetShouldMatch()
|
||||
throws Exception {
|
||||
load(CloudFoundryPlatformOnMethodConfig.class, "VCAP_APPLICATION:---");
|
||||
assertThat(this.context.containsBean("foo")).isTrue();
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
TestPropertyValues.of(environment).applyTo(this.context);
|
||||
this.context.register(config);
|
||||
this.context.refresh();
|
||||
private ContextConsumer<AssertableApplicationContext> match(boolean expected) {
|
||||
return (context) -> {
|
||||
if (expected) {
|
||||
assertThat(context).hasBean("foo");
|
||||
}
|
||||
else {
|
||||
assertThat(context).doesNotHaveBean("foo");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
|
|
@ -18,7 +18,9 @@ package org.springframework.boot.autoconfigure.condition;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
@ -32,28 +34,36 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*/
|
||||
public class ConditionalOnExpressionTests {
|
||||
|
||||
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void expressionIsTrue() {
|
||||
this.context.register(BasicConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("foo")).isTrue();
|
||||
assertThat(this.context.getBean("foo")).isEqualTo("foo");
|
||||
this.contextRunner.withUserConfiguration(BasicConfiguration.class)
|
||||
.run(match(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expressionIsFalse() {
|
||||
this.context.register(MissingConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("foo")).isFalse();
|
||||
this.contextRunner.withUserConfiguration(MissingConfiguration.class)
|
||||
.run(match(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expressionIsNull() {
|
||||
this.context.register(NullConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.containsBean("foo")).isFalse();
|
||||
this.contextRunner.withUserConfiguration(NullConfiguration.class)
|
||||
.run(match(false));
|
||||
}
|
||||
|
||||
private ContextConsumer<AssertableApplicationContext> match(boolean expected) {
|
||||
return (context) -> {
|
||||
if (expected) {
|
||||
assertThat(context).hasBean("foo");
|
||||
assertThat(context.getBean("foo")).isEqualTo("foo");
|
||||
}
|
||||
else {
|
||||
assertThat(context).doesNotHaveBean("foo");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
|
|
@ -29,7 +29,9 @@ import org.junit.Test;
|
|||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.Range;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
@ -44,26 +46,23 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*/
|
||||
public class ConditionalOnJavaTests {
|
||||
|
||||
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
private final OnJavaCondition condition = new OnJavaCondition();
|
||||
|
||||
@Test
|
||||
public void doesNotMatchIfBetterVersionIsRequired() {
|
||||
registerAndRefresh(Java9Required.class);
|
||||
assertPresent(false);
|
||||
this.contextRunner.withUserConfiguration(Java9Required.class).run(match(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotMatchIfLowerIsRequired() {
|
||||
registerAndRefresh(Java7Required.class);
|
||||
assertPresent(false);
|
||||
this.contextRunner.withUserConfiguration(Java7Required.class).run(match(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesIfVersionIsInRange() {
|
||||
registerAndRefresh(Java8Required.class);
|
||||
assertPresent(true);
|
||||
this.contextRunner.withUserConfiguration(Java8Required.class).run(match(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -124,13 +123,15 @@ public class ConditionalOnJavaTests {
|
|||
assertThat(outcome.isMatch()).as(outcome.getMessage()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
private void registerAndRefresh(Class<?> annotatedClasses) {
|
||||
this.context.register(annotatedClasses);
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
private void assertPresent(boolean expected) {
|
||||
assertThat(this.context.getBeansOfType(String.class)).hasSize(expected ? 1 : 0);
|
||||
private ContextConsumer<AssertableApplicationContext> match(boolean expected) {
|
||||
return (context) -> {
|
||||
if (expected) {
|
||||
assertThat(context).hasSingleBean(String.class);
|
||||
}
|
||||
else {
|
||||
assertThat(context).doesNotHaveBean(String.class);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private final class ClassHidingClassLoader extends URLClassLoader {
|
||||
|
|
|
@ -27,9 +27,9 @@ import org.junit.Test;
|
|||
|
||||
import org.springframework.boot.autoconfigure.jndi.JndiPropertiesHidingClassLoader;
|
||||
import org.springframework.boot.autoconfigure.jndi.TestableInitialContextFactory;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.context.runner.ContextConsumer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
@ -51,7 +51,7 @@ public class ConditionalOnJndiTests {
|
|||
|
||||
private String initialContextFactory;
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
private MockableOnJndi condition = new MockableOnJndi();
|
||||
|
||||
|
@ -72,38 +72,35 @@ public class ConditionalOnJndiTests {
|
|||
else {
|
||||
System.clearProperty(Context.INITIAL_CONTEXT_FACTORY);
|
||||
}
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
Thread.currentThread().setContextClassLoader(this.threadContextClassLoader);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jndiNotAvailable() {
|
||||
load(JndiAvailableConfiguration.class);
|
||||
assertPresent(false);
|
||||
this.contextRunner.withUserConfiguration(JndiAvailableConfiguration.class,
|
||||
JndiConditionConfiguration.class).run(match(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jndiAvailable() {
|
||||
setupJndi();
|
||||
load(JndiAvailableConfiguration.class);
|
||||
assertPresent(true);
|
||||
this.contextRunner.withUserConfiguration(JndiAvailableConfiguration.class,
|
||||
JndiConditionConfiguration.class).run(match(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jndiLocationNotBound() {
|
||||
setupJndi();
|
||||
load(JndiConditionConfiguration.class);
|
||||
assertPresent(false);
|
||||
this.contextRunner.withUserConfiguration(JndiConditionConfiguration.class)
|
||||
.run(match(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jndiLocationBound() {
|
||||
setupJndi();
|
||||
TestableInitialContextFactory.bind("java:/FooManager", new Object());
|
||||
load(JndiConditionConfiguration.class);
|
||||
assertPresent(true);
|
||||
this.contextRunner.withUserConfiguration(JndiConditionConfiguration.class)
|
||||
.run(match(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -127,17 +124,15 @@ public class ConditionalOnJndiTests {
|
|||
TestableInitialContextFactory.class.getName());
|
||||
}
|
||||
|
||||
private void assertPresent(boolean expected) {
|
||||
assertThat(this.context.getBeansOfType(String.class)).hasSize(expected ? 1 : 0);
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
|
||||
TestPropertyValues.of(environment).applyTo(applicationContext);
|
||||
applicationContext.register(config);
|
||||
applicationContext.register(JndiConditionConfiguration.class);
|
||||
applicationContext.refresh();
|
||||
this.context = applicationContext;
|
||||
private ContextConsumer<AssertableApplicationContext> match(boolean expected) {
|
||||
return (context) -> {
|
||||
if (expected) {
|
||||
assertThat(context).hasSingleBean(String.class);
|
||||
}
|
||||
else {
|
||||
assertThat(context).doesNotHaveBean(String.class);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private AnnotatedTypeMetadata mockMetaData(String... value) {
|
||||
|
|
Loading…
Reference in New Issue