From 1a01f96a7880d302f220dbc54aea1db192088bf0 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 31 Mar 2011 17:14:55 +0100 Subject: [PATCH] Add integration test for TTL, plus extra constructors for connection factories --- .../connection/CachingConnectionFactory.java | 18 ++++++ .../connection/SingleConnectionFactory.java | 18 ++++++ .../config/QueueParserIntegrationTests.java | 63 +++++++++++++++++++ .../QueueParserIntegrationTests-context.xml | 15 +++++ 4 files changed, 114 insertions(+) create mode 100644 spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/QueueParserIntegrationTests.java create mode 100644 spring-rabbit/src/test/resources/org/springframework/amqp/rabbit/config/QueueParserIntegrationTests-context.xml diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java index ab90f377..a4a9634f 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java @@ -71,6 +71,24 @@ public class CachingConnectionFactory extends SingleConnectionFactory implements super(); } + /** + * Create a new CachingConnectionFactory given a host name. + * + * @param hostName the host name to connect to + */ + public CachingConnectionFactory(String hostName, int port) { + super(hostName, port); + } + + /** + * Create a new CachingConnectionFactory given a host name. + * + * @param hostName the host name to connect to + */ + public CachingConnectionFactory(int port) { + super(port); + } + /** * Create a new CachingConnectionFactory given a host name. * diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactory.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactory.java index 59b5aa44..a8f82ec5 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactory.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactory.java @@ -60,16 +60,34 @@ public class SingleConnectionFactory implements ConnectionFactory, DisposableBea this((String) null); } + /** + * Create a new SingleConnectionFactory given a host name. + * @param port the port to connect to + */ + public SingleConnectionFactory(int port) { + this(null, port); + } + /** * Create a new SingleConnectionFactory given a host name. * @param hostname the host name to connect to */ public SingleConnectionFactory(String hostname) { + this(hostname, com.rabbitmq.client.ConnectionFactory.DEFAULT_AMQP_PORT); + } + + /** + * Create a new SingleConnectionFactory given a host name. + * @param hostname the host name to connect to + * @param port the port number to connect to + */ + public SingleConnectionFactory(String hostname, int port) { if (!StringUtils.hasText(hostname)) { hostname = getDefaultHostName(); } this.rabbitConnectionFactory = new com.rabbitmq.client.ConnectionFactory(); this.rabbitConnectionFactory.setHost(hostname); + this.rabbitConnectionFactory.setPort(port); } /** diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/QueueParserIntegrationTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/QueueParserIntegrationTests.java new file mode 100644 index 00000000..beb03ad7 --- /dev/null +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/QueueParserIntegrationTests.java @@ -0,0 +1,63 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package org.springframework.amqp.rabbit.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; +import org.springframework.amqp.rabbit.core.RabbitAdmin; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.amqp.rabbit.test.BrokerRunning; +import org.springframework.amqp.rabbit.test.BrokerTestUtils; +import org.springframework.beans.factory.xml.XmlBeanFactory; +import org.springframework.core.io.ClassPathResource; + +public final class QueueParserIntegrationTests { + + @Rule + public BrokerRunning brokerIsRunning = BrokerRunning.isRunning(); + + private XmlBeanFactory beanFactory; + + @Before + public void setUpDefaultBeanFactory() throws Exception { + beanFactory = new XmlBeanFactory(new ClassPathResource(getClass().getSimpleName() + "-context.xml", getClass())); + } + + @Test + public void testArgumentsQueue() throws Exception { + + Queue queue = beanFactory.getBean("arguments", Queue.class); + assertNotNull(queue); + + RabbitTemplate template = new RabbitTemplate(new CachingConnectionFactory(BrokerTestUtils.getPort())); + RabbitAdmin rabbitAdmin = new RabbitAdmin(template.getConnectionFactory()); + rabbitAdmin.deleteQueue(queue.getName()); + rabbitAdmin.declareQueue(queue); + + assertEquals(100L, queue.getArguments().get("x-message-ttl")); + template.convertAndSend(queue.getName(), "message"); + + Thread.sleep(200); + String result = (String) template.receiveAndConvert(queue.getName()); + assertEquals(null, result); + + } + +} diff --git a/spring-rabbit/src/test/resources/org/springframework/amqp/rabbit/config/QueueParserIntegrationTests-context.xml b/spring-rabbit/src/test/resources/org/springframework/amqp/rabbit/config/QueueParserIntegrationTests-context.xml new file mode 100644 index 00000000..2578b3ab --- /dev/null +++ b/spring-rabbit/src/test/resources/org/springframework/amqp/rabbit/config/QueueParserIntegrationTests-context.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + +