[jboss-svn-commits] JBL Code SVN: r22463 - in labs/jbossesb/workspace/skeagh/commons/src: test/java/org/jboss/esb and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sat Sep 6 04:55:27 EDT 2008


Author: tfennelly
Date: 2008-09-06 04:55:27 -0400 (Sat, 06 Sep 2008)
New Revision: 22463

Added:
   labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/jms/
   labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/jms/MessageSendAndListenTest.java
Modified:
   labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/jms/AbstractMessageHandler.java
   labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/jms/AbstractMessageListener.java
   labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/jms/MessageSender.java
Log:
Added tests for JMS utils

Modified: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/jms/AbstractMessageHandler.java
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/jms/AbstractMessageHandler.java	2008-09-06 07:28:14 UTC (rev 22462)
+++ labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/jms/AbstractMessageHandler.java	2008-09-06 08:55:27 UTC (rev 22463)
@@ -20,6 +20,7 @@
 package org.jboss.esb.jms;
 
 import org.apache.log4j.Logger;
+import org.jboss.esb.util.AssertArgument;
 
 import javax.jms.*;
 import javax.naming.Context;
@@ -67,6 +68,9 @@
      */
     public AbstractMessageHandler(final String destinationName, final Properties jndiProperties)
     {
+        AssertArgument.isNotNullAndNotEmpty(destinationName, "destinationName");
+        AssertArgument.isNotNull(jndiProperties, "jndiProperties");
+
         logger = Logger.getLogger(getClass());
         this.jndiProperties = jndiProperties;
         this.destinationName = destinationName;
@@ -111,6 +115,22 @@
     {
         ConnectionFactory connectionFactory;
 
+        // Lookup the destination...
+        try
+        {
+            Context context = getNamingContext();
+            try
+            {
+                destination = (Destination) context.lookup(destinationName);
+            } finally
+            {
+                context.close();
+            }
+        } catch (NamingException e)
+        {
+            throw (JMSException) (new JMSException("Destination lookup failed.  Destination name '" + destinationName + "'.").initCause(e));
+        }
+
         // Get the Destination ConnectionFactory...
         try
         {
@@ -121,7 +141,7 @@
         }
 
         // Create the destination connection...
-        if (connectionFactory instanceof TopicConnectionFactory)
+        if (destination instanceof Topic)
         {
             conn = ((TopicConnectionFactory) connectionFactory).createTopicConnection();
         } else
@@ -129,24 +149,8 @@
             conn = ((QueueConnectionFactory) connectionFactory).createQueueConnection();
         }
 
-        // Lookup the destination...
-        try
-        {
-            Context context = getNamingContext();
-            try
-            {
-                destination = (Destination) context.lookup(destinationName);
-            } finally
-            {
-                context.close();
-            }
-        } catch (NamingException e)
-        {
-            throw (JMSException) (new JMSException("Destination lookup failed.  Destination name '" + destinationName + "'.").initCause(e));
-        }
-
         // Create the Destination Session...
-        if (conn instanceof TopicConnection)
+        if (destination instanceof Topic)
         {
             session = ((TopicConnection) conn).createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
         } else

Modified: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/jms/AbstractMessageListener.java
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/jms/AbstractMessageListener.java	2008-09-06 07:28:14 UTC (rev 22462)
+++ labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/jms/AbstractMessageListener.java	2008-09-06 08:55:27 UTC (rev 22463)
@@ -67,11 +67,8 @@
         {
             logger.debug("Attempting to connect listener '" + getClass().getName() + "' to JMS Destination '" + getDestinationName() + "'.");
 
-            // Call super first to connect etc...
-            super.connect();
-
             // Bind "this" listener to the destination...
-            if (getSession() instanceof TopicSession)
+            if (getDestination() instanceof Topic)
             {
                 messageConsumer = ((TopicSession) getSession()).createSubscriber((Topic) getDestination());
                 messageConsumer.setMessageListener(this);
@@ -99,23 +96,17 @@
     {
         try
         {
-            try
+            if (messageConsumer != null)
             {
-                if (messageConsumer != null)
-                {
-                    messageConsumer.close();
-                    logger.debug("Closed JMS MessageConsumer for connected listener '" + getClass().getName() + "' on JMS destination '" + getDestinationName() + "'.");
-                }
-            } catch (Throwable e)
-            {
-                logger.error("Failed to close JMS Consumer.", e);
-            } finally
-            {
-                messageConsumer = null;
+                messageConsumer.close();
+                logger.debug("Closed JMS MessageConsumer for connected listener '" + getClass().getName() + "' on JMS destination '" + getDestinationName() + "'.");
             }
+        } catch (Throwable e)
+        {
+            logger.error("Failed to close JMS Consumer.", e);
         } finally
         {
-            super.close();
+            messageConsumer = null;
         }
     }
 

Modified: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/jms/MessageSender.java
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/jms/MessageSender.java	2008-09-06 07:28:14 UTC (rev 22462)
+++ labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/jms/MessageSender.java	2008-09-06 08:55:27 UTC (rev 22463)
@@ -67,13 +67,10 @@
         {
             logger.debug("Attempting to connect sender '" + getClass().getName() + "' to JMS Destination '" + getDestinationName() + "'.");
 
-            // Call super first to connect etc...
-            super.connect();
-
             // Bind "this" sender to the destination...
             try
             {
-                if (getSession() instanceof TopicSession)
+                if (getDestination() instanceof Topic)
                 {
                     messageProducer = ((TopicSession) getSession()).createPublisher((Topic) getDestination());
                 } else
@@ -114,23 +111,17 @@
     {
         try
         {
-            try
+            if (messageProducer != null)
             {
-                if (messageProducer != null)
-                {
-                    messageProducer.close();
-                    logger.debug("Closed JMS MessageConsumer for connected sender '" + getClass().getName() + "' on JMS destination '" + getDestinationName() + "'.");
-                }
-            } catch (Throwable e)
-            {
-                logger.error("Failed to close JMS Consumer.", e);
-            } finally
-            {
-                messageProducer = null;
+                messageProducer.close();
+                logger.debug("Closed JMS MessageConsumer for connected sender '" + getClass().getName() + "' on JMS destination '" + getDestinationName() + "'.");
             }
+        } catch (Throwable e)
+        {
+            logger.error("Failed to close JMS Consumer.", e);
         } finally
         {
-            super.close();
+            messageProducer = null;
         }
     }
 

Added: labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/jms/MessageSendAndListenTest.java
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/jms/MessageSendAndListenTest.java	                        (rev 0)
+++ labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/jms/MessageSendAndListenTest.java	2008-09-06 08:55:27 UTC (rev 22463)
@@ -0,0 +1,117 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright XXXX, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2008, JBoss Inc.
+ */
+package org.jboss.esb.jms;
+
+import junit.framework.TestCase;
+import org.apache.activemq.broker.BrokerService;
+import org.apache.activemq.jndi.ActiveMQInitialContextFactory;
+
+import javax.naming.Context;
+import javax.jms.*;
+import java.util.Properties;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.io.File;
+
+/**
+ * JMS Send and listen tests.
+ *
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public class MessageSendAndListenTest extends TestCase
+{
+
+    private static final String TEST_PROVIDER_URL = "tcp://localhost:61717";    
+    private Properties jndiProperties;
+
+    protected void setUp() throws Exception
+    {
+        jndiProperties = new Properties();
+        jndiProperties.setProperty(Context.INITIAL_CONTEXT_FACTORY, ActiveMQInitialContextFactory.class.getName());
+        jndiProperties.setProperty(Context.PROVIDER_URL, TEST_PROVIDER_URL);
+    }
+
+    public void test_Topic() throws Exception
+    {
+        test_Destination(Topic.class, "jbossesb.testTopic");
+    }
+
+    public void test_Queue() throws Exception
+    {
+        test_Destination(Queue.class, "jbossesb.testQueue");
+    }
+
+    public void test_Destination(Class<? extends Destination> destType, String destName) throws Exception
+    {
+        BrokerService broker = new BrokerService();
+
+        // configure the broker
+        broker.setDataDirectory(new File("./target/activeMQData"));
+        broker.addConnector(TEST_PROVIDER_URL);
+
+        broker.start();
+
+        try{
+            final List<TextMessage> messagesReceived = new ArrayList<TextMessage>();
+
+            if(destType == Topic.class) {
+                jndiProperties.setProperty("topic." + destName, destName);
+            } else {
+                jndiProperties.setProperty("queue." + destName, destName);
+            }
+
+            AbstractMessageListener listener = new AbstractMessageListener(destName, jndiProperties) {
+                public void onMessage(Message message)
+                {
+                    messagesReceived.add((TextMessage) message);
+                }
+            };
+
+            listener.connect();
+            assertTrue(destType.isAssignableFrom(listener.getDestination().getClass()));
+            try {
+                MessageSender sender = new MessageSender(destName, jndiProperties);
+                List<String> messagesSent = Arrays.asList(new String[] {"message 1", "message 2", "message 2"});
+
+                sender.connect();
+                assertTrue(destType.isAssignableFrom(sender.getDestination().getClass()));
+                try {
+                    for (String message : messagesSent)
+                    {
+                        sender.send(sender.getSession().createTextMessage(message));
+                    }
+                } finally {
+                    sender.close();
+                }
+
+                Thread.sleep(1000);
+                assertEquals(3, messagesReceived.size());
+                assertTrue(messagesSent.contains(messagesReceived.get(0).getText()));
+                assertTrue(messagesSent.contains(messagesReceived.get(1).getText()));
+                assertTrue(messagesSent.contains(messagesReceived.get(2).getText()));
+            } finally {
+                listener.close();
+            }
+        } finally {
+            broker.stop();
+        }
+    }
+}


Property changes on: labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/jms/MessageSendAndListenTest.java
___________________________________________________________________
Name: svn:eol-style
   + native




More information about the jboss-svn-commits mailing list