From 72b857230358580616a7dc118bf5b4639e387dcc Mon Sep 17 00:00:00 2001 From: Bosanac Dejan Date: Tue, 5 Oct 2010 11:38:22 +0000 Subject: [PATCH] spring message listener container tests git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1004616 13f79535-47bb-0310-9956-ffa450edef68 --- activemq-spring/pom.xml | 5 ++ .../org/apache/activemq/spring/Listener.java | 47 ++++++++++++ .../apache/activemq/spring/ListenerTest.java | 76 +++++++++++++++++++ .../src/test/resources/spring/spring.xml | 65 ++++++++++++++++ 4 files changed, 193 insertions(+) create mode 100644 activemq-spring/src/test/java/org/apache/activemq/spring/Listener.java create mode 100644 activemq-spring/src/test/java/org/apache/activemq/spring/ListenerTest.java create mode 100644 activemq-spring/src/test/resources/spring/spring.xml diff --git a/activemq-spring/pom.xml b/activemq-spring/pom.xml index 9d7f2a8767..f25367671d 100755 --- a/activemq-spring/pom.xml +++ b/activemq-spring/pom.xml @@ -83,6 +83,11 @@ org.springframework spring-beans + + org.springframework + spring-test + ${spring-version} + ${project.groupId} activemq-core diff --git a/activemq-spring/src/test/java/org/apache/activemq/spring/Listener.java b/activemq-spring/src/test/java/org/apache/activemq/spring/Listener.java new file mode 100644 index 0000000000..a3b490a86e --- /dev/null +++ b/activemq-spring/src/test/java/org/apache/activemq/spring/Listener.java @@ -0,0 +1,47 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.activemq.spring; + +import org.apache.activemq.command.ActiveMQTextMessage; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import java.util.ArrayList; +import java.util.List; + +public class Listener implements MessageListener { + + List messages = new ArrayList(); + long lastReceived = 0L; + + + public void onMessage(Message message) { + + try { + System.out.println("LISTENER received " + message.getJMSDestination() + " " + ((ActiveMQTextMessage)message).getText()); + lastReceived = System.currentTimeMillis(); + synchronized (messages) { + messages.add(message); + } + } catch (JMSException e) { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + + } + +} diff --git a/activemq-spring/src/test/java/org/apache/activemq/spring/ListenerTest.java b/activemq-spring/src/test/java/org/apache/activemq/spring/ListenerTest.java new file mode 100644 index 0000000000..d2a2e5ca3a --- /dev/null +++ b/activemq-spring/src/test/java/org/apache/activemq/spring/ListenerTest.java @@ -0,0 +1,76 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.activemq.spring; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.transaction.TransactionConfiguration; + +import javax.annotation.Resource; +import javax.jms.*; + +@RunWith(SpringJUnit4ClassRunner.class) + +@ContextConfiguration(locations = {"classpath:spring/spring.xml"}) +@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false) +public class ListenerTest { + + protected String bindAddress = "vm://localhost"; + + @Resource + Listener listener; + + @Test + @DirtiesContext + public void testSimple() throws Exception { + sendMessages("SIMPLE", 10); + + Thread.sleep(3000); + + System.out.println(listener.messages.size()); + Assert.assertEquals(listener.messages.size(), 10); + } + + + @Test + @DirtiesContext + public void testComposite() throws Exception { + sendMessages("TEST.1,TEST.2,TEST.3,TEST.4,TEST.5,TEST.6", 10); + + Thread.sleep(3000); + + System.out.println(listener.messages.size()); + Assert.assertEquals(listener.messages.size(), 60); + } + + public void sendMessages(String destName, int msgNum) throws Exception { + ConnectionFactory factory = new org.apache.activemq.ActiveMQConnectionFactory("tcp://localhost:61616"); + Connection conn = factory.createConnection(); + Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); + Destination dest = sess.createQueue(destName); + MessageProducer producer = sess.createProducer(dest); + for (int i = 0; i < msgNum; i++) { + producer.send(sess.createTextMessage("test")); + } + } + + +} diff --git a/activemq-spring/src/test/resources/spring/spring.xml b/activemq-spring/src/test/resources/spring/spring.xml new file mode 100644 index 0000000000..f79f6851e7 --- /dev/null +++ b/activemq-spring/src/test/resources/spring/spring.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file