[jboss-svn-commits] JBL Code SVN: r22536 - in labs/jbossesb/workspace/skeagh/routing/jms/src: test/java/org/jboss/esb/jms and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Sep 9 05:09:00 EDT 2008
Author: beve
Date: 2008-09-09 05:08:59 -0400 (Tue, 09 Sep 2008)
New Revision: 22536
Added:
labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsInformationExtractor.java
labs/jbossesb/workspace/skeagh/routing/jms/src/test/java/org/jboss/esb/jms/JmsInformationExtractorTest.java
Removed:
labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsPayloadExtractor.java
labs/jbossesb/workspace/skeagh/routing/jms/src/test/java/org/jboss/esb/jms/JmsPayloadExtractorTest.java
Modified:
labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsConstants.java
labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsInboundRouter.java
labs/jbossesb/workspace/skeagh/routing/jms/src/test/java/org/jboss/esb/jms/JmsInboundRouterTest.java
Log:
Updated for JmsInBoundRouter.
Modified: labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsConstants.java
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsConstants.java 2008-09-09 07:05:09 UTC (rev 22535)
+++ labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsConstants.java 2008-09-09 09:08:59 UTC (rev 22536)
@@ -15,7 +15,8 @@
package org.jboss.esb.jms;
/**
- * Contants used for the JMS Routers.
+ * Contants uses for JMS properties and configuration names.
+ * <p/>
*
* @author <a href="mailto:dbevenius at redhat.com">Daniel Bevenius</a>
*
@@ -27,6 +28,7 @@
*/
private JmsConstants()
{
+ throw new AssertionError("Should not be called");
}
/**
@@ -35,18 +37,53 @@
public static final String CONNECTION_FACTORY = "org.jboss.esb.jms.connectionFactory";
/**
- * DeliveryMode property name used in configuration files.
+ * DeliveryMode property name.
*/
public static final String DELIVERY_MODE = "org.jboss.esb.jms.deliveryMode";
/**
- * Priority property name used in configuration files.
+ * Time-to-Live property name.
*/
+ public static final String TIME_TO_LIVE = "org.jboss.esb.jms.timeToLive";
+
+ /**
+ * CorrelationId property name.
+ */
+ public static final String CORRELATION_ID = "org.jboss.esb.jms.correlationId";
+
+ /**
+ * Expiration property name.
+ */
+ public static final String EXPIRATION = "org.jboss.esb.jms.expiration";
+
+ /**
+ * MessageId property name.
+ */
+ public static final String MESSAGE_ID = "org.jboss.esb.jms.messageId";
+
+ /**
+ * Redelivered property name.
+ */
+ public static final String REDELIVERED = "org.jboss.esb.jms.redelivered";
+
+ /**
+ * Timestamp property name.
+ */
+ public static final String TIMESTAMP = "org.jboss.esb.jms.timestamp";
+
+ /**
+ * ReplyTo property name.
+ */
+ public static final String REPLY_TO = "org.jboss.esb.jms.replyTo";
+
+ /**
+ * Priority property name.
+ */
public static final String PRIORITY = "org.jboss.esb.jms.priority";
/**
- * Time-to-Live property name used in configuration files.
+ * Priority property name.
*/
- public static final String TIME_TO_LIVE = "org.jboss.esb.jms.timeToLive";
+ public static final String PROPERTIES = "org.jboss.esb.jms.properties";
}
Modified: labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsInboundRouter.java
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsInboundRouter.java 2008-09-09 07:05:09 UTC (rev 22535)
+++ labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsInboundRouter.java 2008-09-09 09:08:59 UTC (rev 22536)
@@ -50,21 +50,31 @@
* Logger.
*/
private final Logger log = Logger.getLogger(JmsInboundRouter.class);
+
/**
* MessageDispatcher used to dipatch the message to the ESB.
*/
private MessageDispatcher dispatcher;
+
/**
* JMS destination name.
*/
@Property(use = Use.REQUIRED, name = "jmsDestination")
private String destination;
+
/**
* JMS properties.
*/
@org.jboss.esb.annotations.Properties
private final Properties jmsProperties = getDefaultProperties();
+
/**
+ * Should JMS Properties and headers be extrated from the JMS Message
+ */
+ @Property(use = Use.REQUIRED, name = "extractProperties" )
+ private boolean extractProperties;
+
+ /**
* JmsMessageListener that takes care of the JMS connection.
*/
private JmsMessageListener messageListener;
@@ -184,16 +194,28 @@
*/
public void onMessage(final javax.jms.Message jmsMessage)
{
- log.debug("jmsMessage recieved.....");
+ log.info("jmsMessage recieved.....");
try
{
- Message esbMessage = JmsPayloadExtractor.extractPayload(jmsMessage);
- dispatcher.dispatch(esbMessage, new InvocationContext());
+ Message esbMessage = JmsInformationExtractor.extractPayload(jmsMessage);
+
+ final InvocationContext context = new InvocationContext();
+ if (extractProperties)
+ {
+ Properties properties = JmsInformationExtractor.extractJmsProperties(jmsMessage);
+ context.set(JmsConstants.PROPERTIES, properties);
+ }
+
+ dispatcher.dispatch(esbMessage, context);
}
catch (RoutingException e)
{
- e.printStackTrace();
+ log.error("RoutingException in onMessage", e);
}
+ catch (JMSException e)
+ {
+ log.error("JMSException in onMessage", e);
+ }
}
}
Added: labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsInformationExtractor.java
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsInformationExtractor.java (rev 0)
+++ labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsInformationExtractor.java 2008-09-09 09:08:59 UTC (rev 22536)
@@ -0,0 +1,257 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2008, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY 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
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.esb.jms;
+
+import static org.jboss.esb.jms.JmsConstants.*;
+import java.io.Serializable;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.jms.BytesMessage;
+import javax.jms.Destination;
+import javax.jms.JMSException;
+import javax.jms.MapMessage;
+import javax.jms.ObjectMessage;
+import javax.jms.StreamMessage;
+import javax.jms.TextMessage;
+
+import org.jboss.esb.message.Message;
+import org.jboss.esb.routing.RoutingException;
+
+/**
+ *
+ * @author <a href="mailto:dbevenius at redhat.com">Daniel Bevenius</a>
+ *
+ */
+public final class JmsInformationExtractor
+{
+ /**
+ * Private constructor.
+ */
+ private JmsInformationExtractor()
+ {
+ throw new AssertionError("Private constructor should not be called. Not even reflectively");
+ }
+
+ /**
+ * Extracts the content from the passed in JMSMessage object.
+ * <br>
+ * The following conversions are done bases on the message type:
+ * <lu>
+ * <li>TestMessage -> java.lang.String</li>
+ * <li>ObjectMessage -> java.lang.Object</li>
+ * <li>BytesMessage -> byte[]</li>
+ * <li>MapMessage -> java.util.Map</li>
+ * <li>StreamMessage -> org.jboss.esb.jms.StreamMessageInputStream</li>
+ * </lu>
+ *
+ * @param jmsMessage the JMS Message object instance
+ * @return Message the populated ESB Message object instance.
+ * @throws RoutingException if an error occurs while trying to extract the JMS messages contents.
+ */
+ public static Message extractPayload(final javax.jms.Message jmsMessage) throws JMSException, RoutingException
+ {
+ final Message esbMessage = new Message();
+ try
+ {
+ if (jmsMessage instanceof TextMessage)
+ {
+ final String text = ((TextMessage) jmsMessage).getText();
+ if (text == null)
+ {
+ throw new RoutingException("Content in JMSMessage [" + jmsMessage + "] was null");
+ }
+ esbMessage.setPayload(text);
+ }
+ else if (jmsMessage instanceof ObjectMessage)
+ {
+ final Serializable object = ((ObjectMessage)jmsMessage).getObject();
+ if (object == null)
+ {
+ throw new RoutingException("Content in JMSMessage [" + jmsMessage + "] was null");
+ }
+ esbMessage.setPayload(object);
+ }
+ else if (jmsMessage instanceof BytesMessage)
+ {
+ byte[] content = readBytes(jmsMessage);
+ esbMessage.setPayload(content);
+ }
+ else if (jmsMessage instanceof MapMessage)
+ {
+ final MapMessage jmsMap = (MapMessage)jmsMessage;
+ final Map<String,Object> esbMap = new HashMap<String,Object>();
+
+ final Enumeration<?> mapNames = jmsMap.getMapNames();
+ while (mapNames.hasMoreElements())
+ {
+ final String key = (String)mapNames.nextElement();
+ final Object value = jmsMap.getObject(key);
+ esbMap.put(key, value);
+ }
+ if (esbMap.isEmpty())
+ {
+ throw new RoutingException("Map in JMSMessage [" + jmsMessage + "] contained zero key value pairs.");
+ }
+ esbMessage.setPayload(esbMap);
+ }
+ else if (jmsMessage instanceof StreamMessage)
+ {
+ StreamMessage streamMessage = (StreamMessage)jmsMessage;
+ esbMessage.setPayload(new StreamMessageInputStream(streamMessage));
+ }
+ else
+ {
+ throw new RoutingException("Could not determine the type of JMSMessage:" + jmsMessage);
+ }
+ }
+ catch (final JMSException e)
+ {
+ throw new RoutingException("Could not extract content from JMSMessage: " + jmsMessage, e);
+ }
+ return esbMessage;
+ }
+
+ /**
+ * Will extract all the properties that have been set on the JMS Message and
+ * also extract all the set JMS Headers
+ * The following JMS Headers are extracted:
+ * <lu>
+ * <li>JMSCorrelationID -> get(JmsConstants.CORRELATION_ID</li>
+ * <li>JMSExpiration -> get(JmsConstants.EXPIRATOIN</li>
+ * <li>JMSRedelivered -> get(JmsConstants.REDELIVERED</li>
+ * <li>JMSMessageID -> get(JmsConstants.MESSAGE_ID</li>
+ * <li>JMSTimestamp -> get(JmsConstants.TIMESTAMP</li>
+ * <li>JMSReplyTo -> get(JmsConstants.REPLY_TO</li>
+ * <li>JMSPriority -> get(JmsConstants.PRIORITY</li>
+ * <li></li>
+ *
+ * @param jmsMessage
+ * @return
+ * @throws JMSException
+ */
+ public static Properties extractJmsProperties(final javax.jms.Message jmsMessage) throws JMSException
+ {
+ final Properties properties = new Properties();
+
+ // extract properties
+ final Enumeration<String> propertyNames = jmsMessage.getPropertyNames();
+ while (propertyNames.hasMoreElements())
+ {
+ final String propertyName = propertyNames.nextElement();
+ final Object value = jmsMessage.getObjectProperty(propertyName);
+ properties.put(propertyName, value);
+ }
+
+ final String correlationID = jmsMessage.getJMSCorrelationID();
+ if (correlationID != null)
+ {
+ properties.put(CORRELATION_ID, correlationID);
+ }
+
+ final long expiration = jmsMessage.getJMSExpiration();
+ if (expiration > 0l)
+ {
+ properties.put(EXPIRATION, expiration);
+ }
+
+ final boolean redelivered = jmsMessage.getJMSRedelivered();
+ properties.put(REDELIVERED, redelivered);
+
+ /*
+ * Extract messageId header. Need to check for null as a client
+ * may have called MessageProducer.setDisableMessageID.
+ */
+ final String messageId = jmsMessage.getJMSMessageID();
+ if (messageId != null)
+ {
+ properties.put(MESSAGE_ID, messageId);
+ }
+
+ /*
+ * Extract redelivered header if one exist. The JMS client may
+ * have called MessageProducer.setDisableMessageTimestamp, in which
+ * case this timestamp might have been set to 0.
+ */
+ final long timestamp = jmsMessage.getJMSTimestamp();
+ if (timestamp > 0l)
+ {
+ properties.put(TIMESTAMP, timestamp);
+ }
+
+ final Destination replyTo = jmsMessage.getJMSReplyTo();
+ if (replyTo != null)
+ {
+ properties.put(REPLY_TO, replyTo);
+ }
+
+ int priority = jmsMessage.getJMSPriority();
+ if ( priority >= 0 && priority <= 9 )
+ {
+ properties.put(PRIORITY, priority);
+ }
+
+ int deliveryMode = jmsMessage.getJMSDeliveryMode();
+ properties.put(DELIVERY_MODE, deliveryMode);
+
+ return properties;
+ }
+
+ private static byte[] readBytes(final javax.jms.Message jmsMessage) throws JMSException, RoutingException
+ {
+ final BytesMessage bytesMessage = (BytesMessage)jmsMessage;
+ bytesMessage.reset();
+ int msgLength = 0;
+ final int buffSize = 50000;
+ byte[] data = new byte[buffSize];
+ // calculate the nr of bytes in the message
+ while (true)
+ {
+ int len = bytesMessage.readBytes(data);
+ if (len > 0)
+ {
+ msgLength += len;
+ }
+ else
+ {
+ break;
+ }
+ }
+ if (msgLength == 0)
+ {
+ throw new RoutingException("Content in JMSMessage [" + jmsMessage + "] was of zero length");
+ }
+ byte[] content = new byte[msgLength];
+ if (msgLength <= buffSize)
+ {
+ System.arraycopy(data, 0, content, 0, msgLength);
+ }
+ else
+ {
+ bytesMessage.reset();
+ bytesMessage.readBytes(content);
+ }
+ return content;
+ }
+
+}
Deleted: labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsPayloadExtractor.java
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsPayloadExtractor.java 2008-09-09 07:05:09 UTC (rev 22535)
+++ labs/jbossesb/workspace/skeagh/routing/jms/src/main/java/org/jboss/esb/jms/JmsPayloadExtractor.java 2008-09-09 09:08:59 UTC (rev 22536)
@@ -1,170 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source Copyright 2008, Red Hat Middleware
- * LLC, and individual contributors by the @authors tag. See the copyright.txt
- * in the distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it under the
- * terms of the GNU Lesser General Public License as published by the Free
- * Software Foundation; either version 2.1 of the License, or (at your option)
- * any later version.
- *
- * This software is distributed in the hope that it will be useful, but WITHOUT
- * ANY 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
- * along with this software; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
- * site: http://www.fsf.org.
- */
-package org.jboss.esb.jms;
-
-import java.io.Serializable;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.jms.BytesMessage;
-import javax.jms.JMSException;
-import javax.jms.MapMessage;
-import javax.jms.ObjectMessage;
-import javax.jms.StreamMessage;
-import javax.jms.TextMessage;
-
-import org.apache.log4j.Logger;
-import org.jboss.esb.message.Message;
-import org.jboss.esb.routing.RoutingException;
-
-/**
- *
- * @author <a href="mailto:dbevenius at redhat.com">Daniel Bevenius</a>
- *
- */
-public final class JmsPayloadExtractor
-{
- /**
- * Private constructor.
- */
- private JmsPayloadExtractor()
- {
- throw new AssertionError("Private constructor should not be called. Not even reflectively");
- }
-
- /**
- * Extracts the content from the passed in JMSMessage object.
- * <br>
- * The following conversions are done bases on the message type:
- * <lu>
- * <li>TestMessage -> java.lang.String</li>
- * <li>ObjectMessage -> java.lang.Object</li>
- * <li>BytesMessage -> byte[]</li>
- * <li>MapMessage -> java.util.Map</li>
- * <li>StreamMessage -> org.jboss.esb.jms.StreamMessageInputStream</li>
- * </lu>
- *
- * @param jmsMessage the JMS Message object instance
- * @return Message the populated ESB Message object instance.
- * @throws RoutingException if an error occurs while trying to extract the JMS messages contents.
- */
- public static Message extractPayload(final javax.jms.Message jmsMessage) throws RoutingException
- {
- final Message esbMessage = new Message();
- try
- {
- if (jmsMessage instanceof TextMessage)
- {
- final String text = ((TextMessage) jmsMessage).getText();
- if (text == null)
- {
- throw new RoutingException("Content in JMSMessage [" + jmsMessage + "] was null");
- }
- esbMessage.setPayload(text);
- }
- else if (jmsMessage instanceof ObjectMessage)
- {
- final Serializable object = ((ObjectMessage)jmsMessage).getObject();
- if (object == null)
- {
- throw new RoutingException("Content in JMSMessage [" + jmsMessage + "] was null");
- }
- esbMessage.setPayload(object);
- }
- else if (jmsMessage instanceof BytesMessage)
- {
- byte[] content = readBytes(jmsMessage);
- esbMessage.setPayload(content);
- }
- else if (jmsMessage instanceof MapMessage)
- {
- final MapMessage jmsMap = (MapMessage)jmsMessage;
- final Map<String,Object> esbMap = new HashMap<String,Object>();
-
- final Enumeration<?> mapNames = jmsMap.getMapNames();
- while (mapNames.hasMoreElements())
- {
- final String key = (String)mapNames.nextElement();
- final Object value = jmsMap.getObject(key);
- esbMap.put(key, value);
- }
- if (esbMap.isEmpty())
- {
- throw new RoutingException("Map in JMSMessage [" + jmsMessage + "] contained zero key value pairs.");
- }
- esbMessage.setPayload(esbMap);
- }
- else if (jmsMessage instanceof StreamMessage)
- {
- StreamMessage streamMessage = (StreamMessage)jmsMessage;
- esbMessage.setPayload(new StreamMessageInputStream(streamMessage));
- }
- else
- {
- throw new RoutingException("Could not determine the type of JMSMessage:" + jmsMessage);
- }
- }
- catch (final JMSException e)
- {
- throw new RoutingException("Could not extract content from JMSMessage: " + jmsMessage, e);
- }
- return esbMessage;
- }
-
- private static byte[] readBytes(final javax.jms.Message jmsMessage) throws JMSException, RoutingException
- {
- final BytesMessage bytesMessage = (BytesMessage)jmsMessage;
- bytesMessage.reset();
- int msgLength = 0;
- final int buffSize = 50000;
- byte[] data = new byte[buffSize];
- // calculate the nr of bytes in the message
- while (true)
- {
- int len = bytesMessage.readBytes(data);
- if (len > 0)
- {
- msgLength += len;
- }
- else
- {
- break;
- }
- }
- if (msgLength == 0)
- {
- throw new RoutingException("Content in JMSMessage [" + jmsMessage + "] was of zero length");
- }
- byte[] content = new byte[msgLength];
- if (msgLength <= buffSize)
- {
- System.arraycopy(data, 0, content, 0, msgLength);
- }
- else
- {
- bytesMessage.reset();
- bytesMessage.readBytes(content);
- }
- return content;
- }
-
-}
Modified: labs/jbossesb/workspace/skeagh/routing/jms/src/test/java/org/jboss/esb/jms/JmsInboundRouterTest.java
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/jms/src/test/java/org/jboss/esb/jms/JmsInboundRouterTest.java 2008-09-09 07:05:09 UTC (rev 22535)
+++ labs/jbossesb/workspace/skeagh/routing/jms/src/test/java/org/jboss/esb/jms/JmsInboundRouterTest.java 2008-09-09 09:08:59 UTC (rev 22536)
@@ -48,32 +48,11 @@
private static final String TEST_PROVIDER_URL = "tcp://localhost:61616";
private static final String DESTINATION_NAME = "jbossesb.TestQueue";
- private final ServiceName serviceName = new ServiceName("service-cat", "service-a");
-
@Test
- @Ignore
- public final void initializeDefaults() throws DeploymentException, IOException, JMSException
- {
- DeploymentRuntime runtime = DigestUtil.digestConfig(getClass().getResourceAsStream("jms-inbound-router_01.xml"));
- InboundRouter inboundRouter = runtime.getDeploymentUnits().get(0).getInboundRouter(serviceName, "noproperties").getRouter();
-
- assertTrue(inboundRouter instanceof JmsInboundRouter);
- runtime.deploy();
- try {
- final Properties properties = ((JmsInboundRouter) inboundRouter).getProperties();
- assertEquals("org.jnp.interfaces.NamingContextFactory", properties .get(Context.INITIAL_CONTEXT_FACTORY));
- assertEquals("org.jboss.naming,org.jnp.interfaces", properties .get(Context.URL_PKG_PREFIXES));
- assertEquals("jnp://localhost:1099", properties .get(Context.PROVIDER_URL));
- assertEquals("ConnectionFactory", properties .get(JmsConstants.CONNECTION_FACTORY));
- } finally {
- runtime.undeploy();
- }
- }
-
- @Test
public final void initializeConfOverride() throws Exception
{
new JMSTestRunner(TEST_PROVIDER_URL) {
+ @Override
public void test() throws Exception
{
DeploymentRuntime runtime = DigestUtil.digestConfig(getClass().getResourceAsStream("jms-inbound-router_01.xml"));
Added: labs/jbossesb/workspace/skeagh/routing/jms/src/test/java/org/jboss/esb/jms/JmsInformationExtractorTest.java
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/jms/src/test/java/org/jboss/esb/jms/JmsInformationExtractorTest.java (rev 0)
+++ labs/jbossesb/workspace/skeagh/routing/jms/src/test/java/org/jboss/esb/jms/JmsInformationExtractorTest.java 2008-09-09 09:08:59 UTC (rev 22536)
@@ -0,0 +1,317 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2008, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY 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
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.esb.jms;
+
+import static org.jboss.esb.jms.JmsConstants.CORRELATION_ID;
+import static org.jboss.esb.jms.JmsConstants.EXPIRATION;
+import static org.jboss.esb.jms.JmsConstants.MESSAGE_ID;
+import static org.jboss.esb.jms.JmsConstants.PRIORITY;
+import static org.jboss.esb.jms.JmsConstants.REDELIVERED;
+import static org.jboss.esb.jms.JmsConstants.REPLY_TO;
+import static org.jboss.esb.jms.JmsConstants.TIMESTAMP;
+import static org.jboss.esb.jms.JmsConstants.DELIVERY_MODE;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.jms.DeliveryMode;
+import javax.jms.Destination;
+import javax.jms.JMSException;
+
+import org.jboss.esb.message.Message;
+import org.jboss.esb.routing.RoutingException;
+import org.junit.Test;
+
+import com.mockrunner.mock.jms.MockBytesMessage;
+import com.mockrunner.mock.jms.MockMapMessage;
+import com.mockrunner.mock.jms.MockObjectMessage;
+import com.mockrunner.mock.jms.MockQueue;
+import com.mockrunner.mock.jms.MockStreamMessage;
+import com.mockrunner.mock.jms.MockTextMessage;
+
+
+/**
+ * Test for {@link JmsInformationExtractor}.
+ *
+ * @author <a href="mailto:dbevenius at redhat.com">Daniel Bevenius</a>
+ *
+ */
+public class JmsInformationExtractorTest
+{
+ @Test
+ public void extractContentTextMessage() throws RoutingException, JMSException
+ {
+ final String content = "testing text message content";
+ final MockTextMessage textMessage = new MockTextMessage();
+ textMessage.setText(content);
+ final Message esbMessage = JmsInformationExtractor.extractPayload(textMessage);
+ assertEquals( content, esbMessage.getPayload());
+ }
+
+ @Test ( expected = RoutingException.class )
+ public void shouldThrowIfTextMessageIsNull() throws RoutingException, JMSException
+ {
+ final MockTextMessage textMessage = new MockTextMessage();
+ textMessage.setText(null);
+ JmsInformationExtractor.extractPayload(textMessage);
+ }
+
+ @Test
+ public void extractContentObjectMessage() throws RoutingException, JMSException
+ {
+ final String content = "testing text message content";
+ final MockObjectMessage objectMessage = new MockObjectMessage();
+ objectMessage.setObject(content);
+ final Message esbMessage = JmsInformationExtractor.extractPayload(objectMessage);
+ assertEquals( content, esbMessage.getPayload());
+ }
+
+ @Test ( expected = RoutingException.class )
+ public void shouldThrowIfObjectMessagePayloadIsNull() throws RoutingException, JMSException
+ {
+ final MockObjectMessage objectMessage = new MockObjectMessage();
+ objectMessage.setObject(null);
+ JmsInformationExtractor.extractPayload(objectMessage);
+ }
+
+ @Test
+ public void extractContentBytesMessage() throws RoutingException, JMSException
+ {
+ final byte[] content = "testing text message content".getBytes();
+ final MockBytesMessage bytesMessage = new MockBytesMessage();
+ bytesMessage.writeObject(content);
+ final Message esbMessage = JmsInformationExtractor.extractPayload(bytesMessage);
+ Object payload = esbMessage.getPayload();
+ assertTrue(payload instanceof byte[]);
+ assertEquals( new String(content), new String((byte[])esbMessage.getPayload()));
+ }
+
+ @Test ( expected = RoutingException.class )
+ public void shouldThrowBytesMessagePayloadIsNull() throws RoutingException, JMSException
+ {
+ final MockBytesMessage bytesMessage = new MockBytesMessage();
+ JmsInformationExtractor.extractPayload(bytesMessage);
+ }
+
+ @Test
+ public void extractContentMapMessage() throws RoutingException, JMSException
+ {
+ final MockMapMessage mapMessage = new MockMapMessage();
+ mapMessage.setObject("key1", "value1");
+ mapMessage.setObject("key2", "value2");
+ final Message esbMessage = JmsInformationExtractor.extractPayload(mapMessage);
+ Object payload = esbMessage.getPayload();
+ assertTrue(payload instanceof Map);
+
+ Map esbMap = (Map)payload;
+ assertTrue(esbMap.containsKey("key1"));
+ assertTrue(esbMap.containsKey("key2"));
+ assertEquals("value1", esbMap.get("key1"));
+ assertEquals("value2", esbMap.get("key2"));
+ }
+
+ @Test ( expected = RoutingException.class )
+ public void shouldThrowIfMapMessageIsEmptyMap() throws RoutingException, JMSException
+ {
+ final MockMapMessage mapMessage = new MockMapMessage();
+ JmsInformationExtractor.extractPayload(mapMessage);
+ }
+
+ @Test
+ public void extractContentStreamMessageStreamAdapter() throws RoutingException, JMSException, ClassNotFoundException, IOException
+ {
+ final byte[] content = "testing text message content".getBytes();
+ final MockStreamMessage streamMessage = new MockStreamMessage();
+ streamMessage.writeObject(content);
+
+ final Message esbMessage = JmsInformationExtractor.extractPayload(streamMessage);
+
+ final Object payload = esbMessage.getPayload();
+ assertTrue(payload instanceof InputStream);
+ final StreamMessageInputStream adapter = (StreamMessageInputStream)payload;
+ adapter.reset();
+ final Object readObject = adapter.getStreamMessage().readObject();
+ assertTrue(readObject instanceof byte[]);
+ assertEquals(new String(content), new String((byte[])readObject));
+ }
+
+ @Test
+ public void extractContentStreamMessageInputStream() throws RoutingException, JMSException, ClassNotFoundException, IOException
+ {
+ final byte[] content = "testing text message content".getBytes();
+ final MockStreamMessage streamMessage = new MockStreamMessage();
+ streamMessage.writeObject(content);
+
+ final Message esbMessage = JmsInformationExtractor.extractPayload(streamMessage);
+
+ assertTrue(esbMessage.getPayload() instanceof InputStream);
+ final InputStream in = (InputStream)esbMessage.getPayload();
+ in.reset();
+
+ // read from InputStream
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ final byte[] buf = new byte[1024];
+ int len;
+ while ((len = in.read(buf)) > 0)
+ {
+ bos.write(buf, 0, len);
+ }
+ byte[] data = bos.toByteArray();
+ assertEquals(new String(content), new String(data));
+ }
+
+ @Test
+ public void extractProperties() throws JMSException
+ {
+ final MockTextMessage textMessage = new MockTextMessage();
+ textMessage.setObjectProperty("testKey", "testValue");
+ textMessage.setJMSCorrelationID("1234");
+
+ final Properties jmsProperties = JmsInformationExtractor.extractJmsProperties(textMessage);
+ assertTrue("User property should have been set", jmsProperties.containsKey("testKey"));
+ assertEquals("testValue", jmsProperties.get("testKey"));
+ }
+
+ @Test
+ public void extractPropertiesJMSCorrelationID() throws JMSException
+ {
+ final MockTextMessage textMessage = new MockTextMessage();
+ textMessage.setJMSCorrelationID("1234");
+
+ final Properties jmsProperties = JmsInformationExtractor.extractJmsProperties(textMessage);
+ assertTrue("CorrelationId property should have been set", jmsProperties.containsKey(CORRELATION_ID));
+ assertEquals("1234", jmsProperties.get(CORRELATION_ID));
+ }
+
+ @Test
+ public void extractPropertiesJMSExpiration() throws JMSException
+ {
+ final MockTextMessage textMessage = new MockTextMessage();
+ textMessage.setJMSExpiration(1000l);
+
+ final Properties jmsProperties = JmsInformationExtractor.extractJmsProperties(textMessage);
+ assertTrue("Expiration property should have been set", jmsProperties.containsKey(EXPIRATION));
+ assertEquals(1000l, jmsProperties.get(EXPIRATION));
+ }
+
+ @Test
+ public void extractPropertiesJMSMessageID() throws JMSException
+ {
+ final MockTextMessage textMessage = new MockTextMessage();
+ final String messageId = "jmsMessageID1133";
+ textMessage.setJMSMessageID(messageId);
+
+ final Properties jmsProperties = JmsInformationExtractor.extractJmsProperties(textMessage);
+ assertTrue("MessageId property should have been set", jmsProperties.containsKey(MESSAGE_ID));
+ assertEquals(messageId, jmsProperties.get(MESSAGE_ID));
+ }
+
+ @Test
+ public void extractPropertiesJMSRedelivered() throws JMSException
+ {
+ final MockTextMessage textMessage = new MockTextMessage();
+ final boolean redelivered = true;
+ textMessage.setJMSRedelivered(redelivered);
+
+ final Properties jmsProperties = JmsInformationExtractor.extractJmsProperties(textMessage);
+ assertTrue("Redelivered property should have been set", jmsProperties.containsKey(REDELIVERED));
+ assertEquals(redelivered, jmsProperties.get(REDELIVERED));
+ }
+
+ @Test
+ public void extractPropertiesJMSTimestamp() throws JMSException
+ {
+ final MockTextMessage textMessage = new MockTextMessage();
+ final long timestamp = System.currentTimeMillis();
+ textMessage.setJMSTimestamp(timestamp);
+
+ final Properties jmsProperties = JmsInformationExtractor.extractJmsProperties(textMessage);
+ assertTrue("Timestamp property should have been set", jmsProperties.containsKey(TIMESTAMP));
+ assertEquals(timestamp, jmsProperties.get(TIMESTAMP));
+ }
+
+ @Test
+ public void extractPropertiesJMSReplyTo() throws JMSException
+ {
+ final MockTextMessage textMessage = new MockTextMessage();
+ final Destination destination = new MockQueue("testing");
+ textMessage.setJMSReplyTo(destination);
+
+ final Properties jmsProperties = JmsInformationExtractor.extractJmsProperties(textMessage);
+ assertTrue("ReplyTo property should have been set", jmsProperties.containsKey(REPLY_TO));
+ assertEquals(destination, jmsProperties.get(REPLY_TO));
+ }
+
+ @Test
+ public void extractPropertiesJMSPriority() throws JMSException
+ {
+ final MockTextMessage textMessage = new MockTextMessage();
+ final int priority = 0;
+ textMessage.setJMSPriority(priority);
+
+ final Properties jmsProperties = JmsInformationExtractor.extractJmsProperties(textMessage);
+ assertTrue("Priority property should have been set", jmsProperties.containsKey(PRIORITY));
+ assertEquals(priority, jmsProperties.get(PRIORITY));
+ }
+
+ @Test
+ public void extractPropertiesJMSPriorityNegative() throws JMSException
+ {
+ final MockTextMessage textMessage = new MockTextMessage();
+ final int priority = -1;
+ textMessage.setJMSPriority(priority);
+
+ final Properties jmsProperties = JmsInformationExtractor.extractJmsProperties(textMessage);
+ assertFalse("Priority property should not have been set", jmsProperties.containsKey(PRIORITY));
+ }
+
+ @Test
+ public void extractPropertiesJMSPriorityTooLarge() throws JMSException
+ {
+ final MockTextMessage textMessage = new MockTextMessage();
+ final int priority = 10;
+ textMessage.setJMSPriority(priority);
+
+ final Properties jmsProperties = JmsInformationExtractor.extractJmsProperties(textMessage);
+ assertFalse("Priority property should not have been set", jmsProperties.containsKey(PRIORITY));
+ }
+
+ @Test
+ public void extractPropertiesJMSDeliveryMode() throws JMSException
+ {
+ final MockTextMessage textMessage = new MockTextMessage();
+ textMessage.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
+
+ Properties jmsProperties = JmsInformationExtractor.extractJmsProperties(textMessage);
+ assertEquals(DeliveryMode.NON_PERSISTENT, jmsProperties.get(DELIVERY_MODE));
+
+ textMessage.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
+ jmsProperties = JmsInformationExtractor.extractJmsProperties(textMessage);
+ assertEquals(DeliveryMode.PERSISTENT, jmsProperties.get(DELIVERY_MODE));
+ }
+
+}
Deleted: labs/jbossesb/workspace/skeagh/routing/jms/src/test/java/org/jboss/esb/jms/JmsPayloadExtractorTest.java
===================================================================
--- labs/jbossesb/workspace/skeagh/routing/jms/src/test/java/org/jboss/esb/jms/JmsPayloadExtractorTest.java 2008-09-09 07:05:09 UTC (rev 22535)
+++ labs/jbossesb/workspace/skeagh/routing/jms/src/test/java/org/jboss/esb/jms/JmsPayloadExtractorTest.java 2008-09-09 09:08:59 UTC (rev 22536)
@@ -1,178 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source Copyright 2008, Red Hat Middleware
- * LLC, and individual contributors by the @authors tag. See the copyright.txt
- * in the distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it under the
- * terms of the GNU Lesser General Public License as published by the Free
- * Software Foundation; either version 2.1 of the License, or (at your option)
- * any later version.
- *
- * This software is distributed in the hope that it will be useful, but WITHOUT
- * ANY 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
- * along with this software; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
- * site: http://www.fsf.org.
- */
-package org.jboss.esb.jms;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Map;
-
-import javax.jms.JMSException;
-
-import org.jboss.esb.message.Message;
-import org.jboss.esb.routing.RoutingException;
-import org.junit.Test;
-import com.mockrunner.mock.jms.MockBytesMessage;
-import com.mockrunner.mock.jms.MockMapMessage;
-import com.mockrunner.mock.jms.MockObjectMessage;
-import com.mockrunner.mock.jms.MockStreamMessage;
-import com.mockrunner.mock.jms.MockTextMessage;
-
-import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
-
-/**
- * Test for {@link JmsPayloadExtractor}.
- *
- * @author <a href="mailto:dbevenius at redhat.com">Daniel Bevenius</a>
- *
- */
-public class JmsPayloadExtractorTest
-{
- @Test
- public void extractContentTextMessage() throws RoutingException, JMSException
- {
- final String content = "testing text message content";
- final MockTextMessage textMessage = new MockTextMessage();
- textMessage.setText(content);
- final Message esbMessage = JmsPayloadExtractor.extractPayload(textMessage);
- assertEquals( content, esbMessage.getPayload());
- }
-
- @Test ( expected = RoutingException.class )
- public void extractContentTextMessageNullPayload() throws RoutingException, JMSException
- {
- final MockTextMessage textMessage = new MockTextMessage();
- textMessage.setText(null);
- JmsPayloadExtractor.extractPayload(textMessage);
- }
-
- @Test
- public void extractContentObjectMessage() throws RoutingException, JMSException
- {
- final String content = "testing text message content";
- final MockObjectMessage objectMessage = new MockObjectMessage();
- objectMessage.setObject(content);
- final Message esbMessage = JmsPayloadExtractor.extractPayload(objectMessage);
- assertEquals( content, esbMessage.getPayload());
- }
-
- @Test ( expected = RoutingException.class )
- public void extractContentObjectMessageNullPayload() throws RoutingException, JMSException
- {
- final MockObjectMessage objectMessage = new MockObjectMessage();
- objectMessage.setObject(null);
- JmsPayloadExtractor.extractPayload(objectMessage);
- }
-
- @Test
- public void extractContentBytesMessage() throws RoutingException, JMSException
- {
- final byte[] content = "testing text message content".getBytes();
- final MockBytesMessage bytesMessage = new MockBytesMessage();
- bytesMessage.writeObject(content);
- final Message esbMessage = JmsPayloadExtractor.extractPayload(bytesMessage);
- Object payload = esbMessage.getPayload();
- assertTrue(payload instanceof byte[]);
- assertEquals( new String(content), new String((byte[])esbMessage.getPayload()));
- }
-
- @Test ( expected = RoutingException.class )
- public void extractContentBytesMessageNullPayload() throws RoutingException, JMSException
- {
- final MockBytesMessage bytesMessage = new MockBytesMessage();
- JmsPayloadExtractor.extractPayload(bytesMessage);
- }
-
- @Test
- public void extractContentMapMessage() throws RoutingException, JMSException
- {
- final MockMapMessage mapMessage = new MockMapMessage();
- mapMessage.setObject("key1", "value1");
- mapMessage.setObject("key2", "value2");
- final Message esbMessage = JmsPayloadExtractor.extractPayload(mapMessage);
- Object payload = esbMessage.getPayload();
- assertTrue(payload instanceof Map);
-
- Map esbMap = (Map)payload;
- assertTrue(esbMap.containsKey("key1"));
- assertTrue(esbMap.containsKey("key2"));
- assertEquals("value1", esbMap.get("key1"));
- assertEquals("value2", esbMap.get("key2"));
- }
-
- @Test ( expected = RoutingException.class )
- public void extractContentMapMessageEmptyMap() throws RoutingException, JMSException
- {
- final MockMapMessage mapMessage = new MockMapMessage();
- JmsPayloadExtractor.extractPayload(mapMessage);
- }
-
- @Test
- public void extractContentStreamMessageStreamAdapter() throws RoutingException, JMSException, ClassNotFoundException, IOException
- {
- final byte[] content = "testing text message content".getBytes();
- final MockStreamMessage streamMessage = new MockStreamMessage();
- streamMessage.writeObject(content);
-
- final Message esbMessage = JmsPayloadExtractor.extractPayload(streamMessage);
-
- Object payload = esbMessage.getPayload();
- assertTrue(payload instanceof InputStream);
- StreamMessageInputStream adapter = (StreamMessageInputStream)payload;
- adapter.reset();
- Object readObject = adapter.getStreamMessage().readObject();
- assertTrue( readObject instanceof byte[]);
- assertEquals( new String(content), new String((byte[])readObject));
- }
-
- @Test
- public void extractContentStreamMessageInputStream() throws RoutingException, JMSException, ClassNotFoundException, IOException
- {
- final byte[] content = "testing text message content".getBytes();
- final MockStreamMessage streamMessage = new MockStreamMessage();
- streamMessage.writeObject(content);
-
- long start = System.nanoTime();
- final Message esbMessage = JmsPayloadExtractor.extractPayload(streamMessage);
- long end = System.nanoTime();
- long millis = TimeUnit.MILLISECONDS.toMillis(end - start);
- System.out.println(millis);
-
- assertTrue(esbMessage.getPayload() instanceof InputStream);
- InputStream in = (InputStream)esbMessage.getPayload();
- in.reset();
-
- // read from InputStream
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- final byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) > 0)
- {
- bos.write(buf, 0, len);
- }
- byte[] data = bos.toByteArray();
- assertEquals(new String(content), new String(data));
- }
-
-}
More information about the jboss-svn-commits
mailing list