[hornetq-commits] JBoss hornetq SVN: r8007 - in trunk: tests/src/org/hornetq/tests/integration/jms/client and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Sep 29 09:34:52 EDT 2009


Author: timfox
Date: 2009-09-29 09:34:52 -0400 (Tue, 29 Sep 2009)
New Revision: 8007

Added:
   trunk/tests/src/org/hornetq/tests/integration/jms/client/MessageTest.java
Modified:
   trunk/src/main/org/hornetq/jms/client/HornetQMessage.java
Log:
added message test

Modified: trunk/src/main/org/hornetq/jms/client/HornetQMessage.java
===================================================================
--- trunk/src/main/org/hornetq/jms/client/HornetQMessage.java	2009-09-29 01:14:33 UTC (rev 8006)
+++ trunk/src/main/org/hornetq/jms/client/HornetQMessage.java	2009-09-29 13:34:52 UTC (rev 8007)
@@ -11,7 +11,6 @@
  * permissions and limitations under the License.
  */
 
-
 package org.hornetq.jms.client;
 
 import java.io.InputStream;
@@ -246,7 +245,7 @@
 
    }
 
-   public HornetQMessage(byte type)
+   public HornetQMessage(final byte type)
    {
       message = new ClientMessageImpl(type,
                                       true,
@@ -274,13 +273,13 @@
    /**
     * Constructor for when receiving a message from the server
     */
-   public HornetQMessage(final ClientMessage message, ClientSession session)
+   public HornetQMessage(final ClientMessage message, final ClientSession session)
    {
       this.message = message;
 
-      this.readOnly = true;
+      readOnly = true;
 
-      this.propertiesReadOnly = true;
+      propertiesReadOnly = true;
 
       this.session = session;
    }
@@ -324,11 +323,11 @@
       // We can't avoid a cast warning here since getPropertyNames() is on the JMS API
       for (Enumeration<String> props = foreign.getPropertyNames(); props.hasMoreElements();)
       {
-         String name = (String)props.nextElement();
+         String name = props.nextElement();
 
          Object prop = foreign.getObjectProperty(name);
 
-         this.setObjectProperty(name, prop);
+         setObjectProperty(name, prop);
       }
    }
 
@@ -477,7 +476,7 @@
 
    public void setJMSDestination(final Destination destination) throws JMSException
    {
-      this.dest = destination;
+      dest = destination;
    }
 
    public int getJMSDeliveryMode() throws JMSException
@@ -599,51 +598,78 @@
    public boolean propertyExists(final String name) throws JMSException
    {
       return message.containsProperty(new SimpleString(name)) || name.equals(JMSXDELIVERYCOUNT) ||
-             (JMSXGROUPID.equals(name) && message.containsProperty(MessageImpl.HDR_GROUP_ID));
+             JMSXGROUPID.equals(name) &&
+             message.containsProperty(MessageImpl.HDR_GROUP_ID);
    }
 
    public boolean getBooleanProperty(final String name) throws JMSException
    {
       Object value = message.getProperty(new SimpleString(name));
       if (value == null)
+      {
          return Boolean.valueOf(null).booleanValue();
+      }
 
       if (value instanceof Boolean)
+      {
          return ((Boolean)value).booleanValue();
+      }
       else if (value instanceof SimpleString)
+      {
          return Boolean.valueOf(((SimpleString)value).toString()).booleanValue();
+      }
       else
+      {
          throw new MessageFormatException("Invalid conversion");
+      }
    }
 
    public byte getByteProperty(final String name) throws JMSException
    {
       Object value = message.getProperty(new SimpleString(name));
       if (value == null)
+      {
          throw new NumberFormatException("Message property '" + name + "' not set.");
+      }
 
       if (value instanceof Byte)
+      {
          return ((Byte)value).byteValue();
+      }
       else if (value instanceof SimpleString)
+      {
          return Byte.parseByte(((SimpleString)value).toString());
+      }
       else
+      {
          throw new MessageFormatException("Invalid conversion");
+      }
    }
 
    public short getShortProperty(final String name) throws JMSException
    {
       Object value = message.getProperty(new SimpleString(name));
       if (value == null)
+      {
          throw new NumberFormatException("Message property '" + name + "' not set.");
+      }
 
       if (value instanceof Byte)
+      {
          return ((Byte)value).shortValue();
+      }
       else if (value instanceof Short)
+      {
          return ((Short)value).shortValue();
+      }
       else if (value instanceof SimpleString)
+      {
          return Short.parseShort(((SimpleString)value).toString());
+      }
       else
+      {
          throw new MessageFormatException("Invalid conversion");
+      }
    }
 
    public int getIntProperty(final String name) throws JMSException
@@ -657,7 +683,7 @@
 
       if (value == null)
       {
-         throw new NumberFormatException("Message property '" + name + "' not set.");
+         return Integer.valueOf(null);
       }
 
       if (value instanceof Byte)
@@ -693,7 +719,7 @@
 
       if (value == null)
       {
-         throw new NumberFormatException("Message property '" + name + "' not set.");
+         return Long.valueOf(null);
       }
 
       if (value instanceof Byte)
@@ -726,30 +752,48 @@
    {
       Object value = message.getProperty(new SimpleString(name));
       if (value == null)
+      {
          return Float.valueOf(null).floatValue();
+      }
 
       if (value instanceof Float)
+      {
          return ((Float)value).floatValue();
+      }
       else if (value instanceof SimpleString)
+      {
          return Float.parseFloat(((SimpleString)value).toString());
+      }
       else
+      {
          throw new MessageFormatException("Invalid conversion");
+      }
    }
 
    public double getDoubleProperty(final String name) throws JMSException
    {
       Object value = message.getProperty(new SimpleString(name));
       if (value == null)
+      {
          return Double.valueOf(null).doubleValue();
+      }
 
       if (value instanceof Float)
+      {
          return ((Float)value).doubleValue();
+      }
       else if (value instanceof Double)
+      {
          return ((Double)value).doubleValue();
+      }
       else if (value instanceof SimpleString)
+      {
          return Double.parseDouble(((SimpleString)value).toString());
+      }
       else
+      {
          throw new MessageFormatException("Invalid conversion");
+      }
    }
 
    public String getStringProperty(final String name) throws JMSException
@@ -768,7 +812,9 @@
          value = message.getProperty(new SimpleString(name));
       }
       if (value == null)
+      {
          return null;
+      }
 
       if (value instanceof SimpleString)
       {
@@ -896,6 +942,13 @@
    public void setStringProperty(final String name, final String value) throws JMSException
    {
       checkProperty(name, value);
+
+      if (value == null)
+      {
+         // This is ok - when we try to read the same key it will return null too
+         return;
+      }
+
       if (JMSXGROUPID.equals(name))
       {
          message.putStringProperty(MessageImpl.HDR_GROUP_ID, new SimpleString(value));
@@ -923,6 +976,12 @@
 
       checkProperty(name, value);
 
+      if (value == null)
+      {
+         // This is ok - when we try to read the same key it will return null too
+         return;
+      }
+
       if (JMS_HORNETQ_INPUT_STREAM.equals(name))
       {
          setInputStream((InputStream)value);
@@ -1058,7 +1117,7 @@
       }
    }
 
-   public boolean waitCompletionOnStream(long timeWait) throws JMSException
+   public boolean waitCompletionOnStream(final long timeWait) throws JMSException
    {
       checkStream();
       try
@@ -1071,6 +1130,7 @@
       }
    }
 
+   @Override
    public String toString()
    {
       StringBuffer sb = new StringBuffer("HornetQMessage[");
@@ -1123,7 +1183,8 @@
          {
             throw new MessageNotWriteableException("You cannot set the Input Stream on received messages. Did you mean " + JMS_HORNETQ_OUTPUT_STREAM +
                                                    " or " +
-                                                   JMS_HORNETQ_SAVE_STREAM + "?");
+                                                   JMS_HORNETQ_SAVE_STREAM +
+                                                   "?");
          }
          else
          {
@@ -1198,7 +1259,7 @@
       return true;
    }
 
-   private void checkPriority(int priority) throws JMSException
+   private void checkPriority(final int priority) throws JMSException
    {
       if (priority < 0 || priority > 9)
       {

Added: trunk/tests/src/org/hornetq/tests/integration/jms/client/MessageTest.java
===================================================================
--- trunk/tests/src/org/hornetq/tests/integration/jms/client/MessageTest.java	                        (rev 0)
+++ trunk/tests/src/org/hornetq/tests/integration/jms/client/MessageTest.java	2009-09-29 13:34:52 UTC (rev 8007)
@@ -0,0 +1,173 @@
+/*
+ * Copyright 2009 Red Hat, Inc.
+ * Red Hat 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.hornetq.tests.integration.jms.client;
+
+import javax.jms.Connection;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+
+import org.hornetq.core.logging.Logger;
+import org.hornetq.tests.util.JMSTestBase;
+
+/**
+ * 
+ * A MessageTest
+ *
+ * @author tim
+ *
+ *
+ */
+public class MessageTest extends JMSTestBase
+{
+   // Constants -----------------------------------------------------
+   
+   private static final Logger log = Logger.getLogger(MessageTest.class);
+   
+   private static final long TIMEOUT = 1000;
+
+   private static final String propName1 = "myprop1";
+   private static final String propName2 = "myprop2";
+   private static final String propName3 = "myprop3";
+   
+   // Attributes ----------------------------------------------------
+
+   // Static --------------------------------------------------------
+
+   // Constructors --------------------------------------------------
+
+   // Public --------------------------------------------------------
+
+   public void testNullProperties() throws Exception
+   {
+      Connection conn = cf.createConnection();
+
+      Queue queue = createQueue("testQueue");
+
+      try
+      {
+         Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+         MessageProducer prod = sess.createProducer(queue);
+
+         MessageConsumer cons = sess.createConsumer(queue);
+         
+         conn.start();
+
+         Message msg = sess.createMessage();
+                  
+         msg.setObjectProperty(propName1, null);
+         msg.setStringProperty(propName2, null);
+         
+         checkProperties(msg);
+         
+         Message received = sendAndConsumeMessage(msg, prod, cons);
+         
+         assertNotNull(received);
+         
+         checkProperties(received);
+      }
+      finally
+      {
+         try
+         {
+            conn.close();
+         }
+         catch (Throwable igonred)
+         {
+         }
+      }
+   }
+
+   // Package protected ---------------------------------------------
+
+   // Protected -----------------------------------------------------
+
+   // Private -------------------------------------------------------
+
+   private void checkProperties(final Message message) throws Exception
+   {
+      assertNull(message.getObjectProperty(propName1));
+      assertNull(message.getStringProperty(propName1));
+      assertNull(message.getStringProperty(propName2));
+      assertNull(message.getObjectProperty(propName2));
+      assertNull(message.getStringProperty(propName3));
+      assertNull(message.getObjectProperty(propName3));
+      
+      try
+      {
+         log.info(message.getIntProperty(propName1));
+         fail("Should throw exception");
+      }
+      catch (NumberFormatException e)
+      {
+         //Ok
+      }
+      
+      try
+      {
+         log.info(message.getShortProperty(propName1));
+      }
+      catch (NumberFormatException e)
+      {
+         //Ok
+      }
+      try
+      {
+         log.info(message.getByteProperty(propName1));
+      }
+      catch (NumberFormatException e)
+      {
+         //Ok
+      }
+      assertEquals(false, message.getBooleanProperty(propName1));
+      try
+      {
+         log.info(message.getLongProperty(propName1));
+      }
+      catch (NumberFormatException e)
+      {
+         //Ok
+      }
+      try
+      {
+         log.info(message.getFloatProperty(propName1));
+      }
+      catch (NullPointerException e)
+      {
+         //Ok
+      }
+      try
+      {
+         log.info(message.getDoubleProperty(propName1));
+      }
+      catch (NullPointerException e)
+      {
+         //Ok
+      }
+   }
+   
+   private Message sendAndConsumeMessage(final Message msg, final MessageProducer prod, final MessageConsumer cons) throws Exception
+   {
+      prod.send(msg);
+
+      Message received = cons.receive(TIMEOUT);
+
+      return received;
+   }
+
+   // Inner classes -------------------------------------------------
+}



More information about the hornetq-commits mailing list