pull/64/head
Dave Syer 11 years ago
parent 941d163709
commit 9bb6e0f497

@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.amqp;
import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; 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.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -62,7 +63,7 @@ public class RabbitTemplateAutoConfiguration {
} }
@Configuration @Configuration
@ConditionalOnMissingBean(CachingConnectionFactory.class) @ConditionalOnMissingBean(ConnectionFactory.class)
@EnableConfigurationProperties(RabbitConnectionFactoryProperties.class) @EnableConfigurationProperties(RabbitConnectionFactoryProperties.class)
protected static class RabbitConnectionFactoryCreator { protected static class RabbitConnectionFactoryCreator {
@ -71,7 +72,8 @@ public class RabbitTemplateAutoConfiguration {
@Bean @Bean
public CachingConnectionFactory connectionFactory() { public CachingConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.config.getHost()); CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
this.config.getHost());
connectionFactory.setPort(this.config.getPort()); connectionFactory.setPort(this.config.getPort());
if (this.config.getUsername() != null) { if (this.config.getUsername() != null) {
connectionFactory.setUsername(this.config.getUsername()); connectionFactory.setUsername(this.config.getUsername());
@ -97,7 +99,7 @@ public class RabbitTemplateAutoConfiguration {
private boolean dynamic = true; private boolean dynamic = true;
public String getHost() { public String getHost() {
return host; return this.host;
} }
public void setHost(String host) { public void setHost(String host) {
@ -105,7 +107,7 @@ public class RabbitTemplateAutoConfiguration {
} }
public int getPort() { public int getPort() {
return port; return this.port;
} }
public void setPort(int port) { public void setPort(int port) {
@ -113,7 +115,7 @@ public class RabbitTemplateAutoConfiguration {
} }
public String getUsername() { public String getUsername() {
return username; return this.username;
} }
public void setUsername(String username) { public void setUsername(String username) {
@ -121,7 +123,7 @@ public class RabbitTemplateAutoConfiguration {
} }
public String getPassword() { public String getPassword() {
return password; return this.password;
} }
public void setPassword(String password) { public void setPassword(String password) {
@ -129,7 +131,7 @@ public class RabbitTemplateAutoConfiguration {
} }
public boolean isDynamic() { public boolean isDynamic() {
return dynamic; return this.dynamic;
} }
public void setDynamic(boolean dynamic) { public void setDynamic(boolean dynamic) {

@ -27,9 +27,9 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import static junit.framework.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static junit.framework.Assert.fail; import static org.junit.Assert.fail;
/** /**
* Tests for {@link RabbitTemplateAutoConfiguration}. * Tests for {@link RabbitTemplateAutoConfiguration}.
@ -43,10 +43,12 @@ public class RabbitTemplateAutoconfigurationTests {
@Test @Test
public void testDefaultRabbitTemplate() { public void testDefaultRabbitTemplate() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, RabbitTemplateAutoConfiguration.class); this.context.register(TestConfiguration.class,
RabbitTemplateAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class); RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
CachingConnectionFactory connectionFactory = this.context.getBean(CachingConnectionFactory.class); CachingConnectionFactory connectionFactory = this.context
.getBean(CachingConnectionFactory.class);
RabbitAdmin amqpAdmin = this.context.getBean(RabbitAdmin.class); RabbitAdmin amqpAdmin = this.context.getBean(RabbitAdmin.class);
assertNotNull(rabbitTemplate); assertNotNull(rabbitTemplate);
assertNotNull(connectionFactory); assertNotNull(connectionFactory);
@ -58,11 +60,14 @@ public class RabbitTemplateAutoconfigurationTests {
@Test @Test
public void testRabbitTemplateWithOverrides() { public void testRabbitTemplateWithOverrides() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, RabbitTemplateAutoConfiguration.class); this.context.register(TestConfiguration.class,
RabbitTemplateAutoConfiguration.class);
TestUtils.addEnviroment(this.context, "spring.rabbitmq.host:remote-server", TestUtils.addEnviroment(this.context, "spring.rabbitmq.host:remote-server",
"spring.rabbitmq.port:9000", "spring.rabbitmq.username:alice", "spring.rabbitmq.password:secret"); "spring.rabbitmq.port:9000", "spring.rabbitmq.username:alice",
"spring.rabbitmq.password:secret");
this.context.refresh(); this.context.refresh();
CachingConnectionFactory connectionFactory = this.context.getBean(CachingConnectionFactory.class); CachingConnectionFactory connectionFactory = this.context
.getBean(CachingConnectionFactory.class);
assertEquals(connectionFactory.getHost(), "remote-server"); assertEquals(connectionFactory.getHost(), "remote-server");
assertEquals(connectionFactory.getPort(), 9000); assertEquals(connectionFactory.getPort(), 9000);
} }
@ -70,10 +75,12 @@ public class RabbitTemplateAutoconfigurationTests {
@Test @Test
public void testConnectionFactoryBackoff() { public void testConnectionFactoryBackoff() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration2.class, RabbitTemplateAutoConfiguration.class); this.context.register(TestConfiguration2.class,
RabbitTemplateAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class); RabbitTemplate rabbitTemplate = this.context.getBean(RabbitTemplate.class);
CachingConnectionFactory connectionFactory = this.context.getBean(CachingConnectionFactory.class); CachingConnectionFactory connectionFactory = this.context
.getBean(CachingConnectionFactory.class);
assertEquals(rabbitTemplate.getConnectionFactory(), connectionFactory); assertEquals(rabbitTemplate.getConnectionFactory(), connectionFactory);
assertEquals(connectionFactory.getHost(), "otherserver"); assertEquals(connectionFactory.getHost(), "otherserver");
assertEquals(connectionFactory.getPort(), 8001); assertEquals(connectionFactory.getPort(), 8001);
@ -82,13 +89,16 @@ public class RabbitTemplateAutoconfigurationTests {
@Test @Test
public void testStaticQueues() { public void testStaticQueues() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, RabbitTemplateAutoConfiguration.class); this.context.register(TestConfiguration.class,
RabbitTemplateAutoConfiguration.class);
TestUtils.addEnviroment(this.context, "spring.rabbitmq.dynamic:false"); TestUtils.addEnviroment(this.context, "spring.rabbitmq.dynamic:false");
this.context.refresh(); this.context.refresh();
try { try {
this.context.getBean(AmqpAdmin.class); this.context.getBean(AmqpAdmin.class);
fail("There should NOT be an AmqpAdmin bean when dynamic is switch to false"); fail("There should NOT be an AmqpAdmin bean when dynamic is switch to false");
} catch (Exception e) {} }
catch (Exception e) {
}
} }
@Configuration @Configuration

Loading…
Cancel
Save