From 9bb6e0f497d8760e31607fb0c199c3bb5679a3b4 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 26 Sep 2013 12:08:11 -0400 Subject: [PATCH] Fix test --- .../amqp/RabbitTemplateAutoConfiguration.java | 150 +++++++++--------- .../RabbitTemplateAutoconfigurationTests.java | 134 ++++++++-------- 2 files changed, 148 insertions(+), 136 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateAutoConfiguration.java index e05aee5698..67f744b713 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateAutoConfiguration.java @@ -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; + } - } + } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateAutoconfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateAutoconfigurationTests.java index 4e0ebdf394..08170f94f9 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateAutoconfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateAutoconfigurationTests.java @@ -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); + } + } }