Fix test
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.amqp;
|
||||
|
||||
import org.springframework.amqp.core.AmqpAdmin;
|
||||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -32,7 +33,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for {@link RabbitTemplate}.
|
||||
*
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
@Configuration
|
||||
@@ -40,101 +41,102 @@ import org.springframework.context.annotation.Configuration;
|
||||
@EnableConfigurationProperties
|
||||
public class RabbitTemplateAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnExpression("${spring.rabbitmq.dynamic:true}")
|
||||
@ConditionalOnMissingBean(AmqpAdmin.class)
|
||||
public AmqpAdmin amqpAdmin(CachingConnectionFactory connectionFactory) {
|
||||
return new RabbitAdmin(connectionFactory);
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnExpression("${spring.rabbitmq.dynamic:true}")
|
||||
@ConditionalOnMissingBean(AmqpAdmin.class)
|
||||
public AmqpAdmin amqpAdmin(CachingConnectionFactory connectionFactory) {
|
||||
return new RabbitAdmin(connectionFactory);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(RabbitTemplate.class)
|
||||
protected static class RabbitTemplateCreator {
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(RabbitTemplate.class)
|
||||
protected static class RabbitTemplateCreator {
|
||||
|
||||
@Autowired
|
||||
CachingConnectionFactory connectionFactory;
|
||||
@Autowired
|
||||
CachingConnectionFactory connectionFactory;
|
||||
|
||||
@Bean
|
||||
public RabbitTemplate rabbitTemplate() {
|
||||
return new RabbitTemplate(this.connectionFactory);
|
||||
}
|
||||
@Bean
|
||||
public RabbitTemplate rabbitTemplate() {
|
||||
return new RabbitTemplate(this.connectionFactory);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(CachingConnectionFactory.class)
|
||||
@EnableConfigurationProperties(RabbitConnectionFactoryProperties.class)
|
||||
protected static class RabbitConnectionFactoryCreator {
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(ConnectionFactory.class)
|
||||
@EnableConfigurationProperties(RabbitConnectionFactoryProperties.class)
|
||||
protected static class RabbitConnectionFactoryCreator {
|
||||
|
||||
@Autowired
|
||||
private RabbitConnectionFactoryProperties config;
|
||||
@Autowired
|
||||
private RabbitConnectionFactoryProperties config;
|
||||
|
||||
@Bean
|
||||
public CachingConnectionFactory connectionFactory() {
|
||||
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.config.getHost());
|
||||
connectionFactory.setPort(this.config.getPort());
|
||||
if (this.config.getUsername() != null) {
|
||||
connectionFactory.setUsername(this.config.getUsername());
|
||||
}
|
||||
if (this.config.getPassword() != null) {
|
||||
connectionFactory.setPassword(this.config.getPassword());
|
||||
}
|
||||
return connectionFactory;
|
||||
}
|
||||
}
|
||||
@Bean
|
||||
public CachingConnectionFactory connectionFactory() {
|
||||
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
|
||||
this.config.getHost());
|
||||
connectionFactory.setPort(this.config.getPort());
|
||||
if (this.config.getUsername() != null) {
|
||||
connectionFactory.setUsername(this.config.getUsername());
|
||||
}
|
||||
if (this.config.getPassword() != null) {
|
||||
connectionFactory.setPassword(this.config.getPassword());
|
||||
}
|
||||
return connectionFactory;
|
||||
}
|
||||
}
|
||||
|
||||
@ConfigurationProperties(name = "spring.rabbitmq")
|
||||
public static class RabbitConnectionFactoryProperties {
|
||||
@ConfigurationProperties(name = "spring.rabbitmq")
|
||||
public static class RabbitConnectionFactoryProperties {
|
||||
|
||||
private String host = "localhost";
|
||||
private String host = "localhost";
|
||||
|
||||
private int port = 5672;
|
||||
private int port = 5672;
|
||||
|
||||
private String username;
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
private String password;
|
||||
|
||||
private boolean dynamic = true;
|
||||
private boolean dynamic = true;
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
public String getHost() {
|
||||
return this.host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
public String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public boolean isDynamic() {
|
||||
return dynamic;
|
||||
}
|
||||
public boolean isDynamic() {
|
||||
return this.dynamic;
|
||||
}
|
||||
|
||||
public void setDynamic(boolean dynamic) {
|
||||
this.dynamic = dynamic;
|
||||
}
|
||||
public void setDynamic(boolean dynamic) {
|
||||
this.dynamic = dynamic;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,80 +27,90 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static junit.framework.Assert.fail;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Tests for {@link RabbitTemplateAutoConfiguration}.
|
||||
*
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
public class RabbitTemplateAutoconfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void testDefaultRabbitTemplate() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class, RabbitTemplateAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
CachingConnectionFactory connectionFactory = this.context.getBean(CachingConnectionFactory.class);
|
||||
RabbitAdmin amqpAdmin = this.context.getBean(RabbitAdmin.class);
|
||||
assertNotNull(rabbitTemplate);
|
||||
assertNotNull(connectionFactory);
|
||||
assertNotNull(amqpAdmin);
|
||||
assertEquals(rabbitTemplate.getConnectionFactory(), connectionFactory);
|
||||
assertEquals(connectionFactory.getHost(), "localhost");
|
||||
}
|
||||
@Test
|
||||
public void testDefaultRabbitTemplate() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class,
|
||||
RabbitTemplateAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
RabbitAdmin amqpAdmin = this.context.getBean(RabbitAdmin.class);
|
||||
assertNotNull(rabbitTemplate);
|
||||
assertNotNull(connectionFactory);
|
||||
assertNotNull(amqpAdmin);
|
||||
assertEquals(rabbitTemplate.getConnectionFactory(), connectionFactory);
|
||||
assertEquals(connectionFactory.getHost(), "localhost");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRabbitTemplateWithOverrides() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class, RabbitTemplateAutoConfiguration.class);
|
||||
TestUtils.addEnviroment(this.context, "spring.rabbitmq.host:remote-server",
|
||||
"spring.rabbitmq.port:9000", "spring.rabbitmq.username:alice", "spring.rabbitmq.password:secret");
|
||||
this.context.refresh();
|
||||
CachingConnectionFactory connectionFactory = this.context.getBean(CachingConnectionFactory.class);
|
||||
assertEquals(connectionFactory.getHost(), "remote-server");
|
||||
assertEquals(connectionFactory.getPort(), 9000);
|
||||
}
|
||||
@Test
|
||||
public void testRabbitTemplateWithOverrides() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class,
|
||||
RabbitTemplateAutoConfiguration.class);
|
||||
TestUtils.addEnviroment(this.context, "spring.rabbitmq.host:remote-server",
|
||||
"spring.rabbitmq.port:9000", "spring.rabbitmq.username:alice",
|
||||
"spring.rabbitmq.password:secret");
|
||||
this.context.refresh();
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertEquals(connectionFactory.getHost(), "remote-server");
|
||||
assertEquals(connectionFactory.getPort(), 9000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionFactoryBackoff() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration2.class, RabbitTemplateAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
CachingConnectionFactory connectionFactory = this.context.getBean(CachingConnectionFactory.class);
|
||||
assertEquals(rabbitTemplate.getConnectionFactory(), connectionFactory);
|
||||
assertEquals(connectionFactory.getHost(), "otherserver");
|
||||
assertEquals(connectionFactory.getPort(), 8001);
|
||||
}
|
||||
@Test
|
||||
public void testConnectionFactoryBackoff() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration2.class,
|
||||
RabbitTemplateAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
|
||||
CachingConnectionFactory connectionFactory = this.context
|
||||
.getBean(CachingConnectionFactory.class);
|
||||
assertEquals(rabbitTemplate.getConnectionFactory(), connectionFactory);
|
||||
assertEquals(connectionFactory.getHost(), "otherserver");
|
||||
assertEquals(connectionFactory.getPort(), 8001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticQueues() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class, RabbitTemplateAutoConfiguration.class);
|
||||
TestUtils.addEnviroment(this.context, "spring.rabbitmq.dynamic:false");
|
||||
this.context.refresh();
|
||||
try {
|
||||
this.context.getBean(AmqpAdmin.class);
|
||||
fail("There should NOT be an AmqpAdmin bean when dynamic is switch to false");
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
@Test
|
||||
public void testStaticQueues() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class,
|
||||
RabbitTemplateAutoConfiguration.class);
|
||||
TestUtils.addEnviroment(this.context, "spring.rabbitmq.dynamic:false");
|
||||
this.context.refresh();
|
||||
try {
|
||||
this.context.getBean(AmqpAdmin.class);
|
||||
fail("There should NOT be an AmqpAdmin bean when dynamic is switch to false");
|
||||
}
|
||||
catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class TestConfiguration {
|
||||
@Configuration
|
||||
protected static class TestConfiguration {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class TestConfiguration2 {
|
||||
@Bean
|
||||
ConnectionFactory aDifferentConnectionFactory() {
|
||||
return new CachingConnectionFactory("otherserver", 8001);
|
||||
}
|
||||
}
|
||||
@Configuration
|
||||
protected static class TestConfiguration2 {
|
||||
@Bean
|
||||
ConnectionFactory aDifferentConnectionFactory() {
|
||||
return new CachingConnectionFactory("otherserver", 8001);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user