[jboss-cvs] JBoss Messaging SVN: r4402 - in trunk: tests/src/org/jboss/messaging/tests/unit/core/util and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Jun 6 09:02:50 EDT 2008


Author: jmesnil
Date: 2008-06-06 09:02:50 -0400 (Fri, 06 Jun 2008)
New Revision: 4402

Modified:
   trunk/src/main/org/jboss/messaging/util/TypedProperties.java
   trunk/tests/src/org/jboss/messaging/tests/unit/core/util/TypedPropertiesTest.java
   trunk/tests/src/org/jboss/messaging/tests/util/RandomUtil.java
Log:
added unit tests for TypedProperties + bug fixes

Modified: trunk/src/main/org/jboss/messaging/util/TypedProperties.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/TypedProperties.java	2008-06-06 11:23:39 UTC (rev 4401)
+++ trunk/src/main/org/jboss/messaging/util/TypedProperties.java	2008-06-06 13:02:50 UTC (rev 4402)
@@ -42,6 +42,7 @@
 import static org.jboss.messaging.util.DataConstants.SIZE_SHORT;
 import static org.jboss.messaging.util.DataConstants.STRING;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
@@ -148,12 +149,18 @@
 	
 	public boolean containsProperty(final SimpleString key)
 	{
-		return properties.containsKey(key);
+	   if (properties != null)
+	      return properties.containsKey(key);
+	   else
+	      return false;
 	}
 	
 	public Set<SimpleString> getPropertyNames()
 	{
-		return properties.keySet();
+	   if (properties != null)
+	      return properties.keySet();
+	   else
+	      return Collections.EMPTY_SET;
 	}
 			
 	public void decode(final MessagingBuffer buffer)
@@ -376,8 +383,6 @@
 		void write(MessagingBuffer buffer);
 		
 		int encodeSize();
-		
-		byte getType();
 	}
    
    private static final class NullValue implements PropertyValue
@@ -396,11 +401,6 @@
          buffer.putByte(NULL);
       }
       
-      public byte getType()
-      {
-         return NULL;
-      }
-
       public int encodeSize()
       {
          return SIZE_BYTE;
@@ -432,11 +432,6 @@
 			buffer.putByte(BOOLEAN);
 			buffer.putBoolean(val);
 		}
-		
-		public byte getType()
-		{
-			return BOOLEAN;
-		}
 
       public int encodeSize()
       {
@@ -470,11 +465,6 @@
 			buffer.putByte(val);
 		}
 		
-		public byte getType()
-		{
-			return BYTE;
-		}
-		
 		public int encodeSize()
 		{
 		   return SIZE_BYTE + SIZE_BYTE;
@@ -509,11 +499,6 @@
 			buffer.putBytes(val);
 		}
 		
-		public byte getType()
-		{
-			return BYTES;
-		}
-		
       public int encodeSize()
       {
          return SIZE_BYTE + SIZE_INT + val.length;
@@ -546,11 +531,6 @@
 			buffer.putShort(val);
 		}
 		
-		public byte getType()
-		{
-			return SHORT;
-		}
-		
 		public int encodeSize()
 		{
 		   return SIZE_BYTE + SIZE_SHORT;
@@ -581,11 +561,6 @@
 			buffer.putByte(INT);
 			buffer.putInt(val);
 		}
-		
-		public byte getType()
-		{
-			return INT;
-		}
 
 		public int encodeSize()
       {
@@ -617,11 +592,6 @@
 			buffer.putByte(LONG);
 			buffer.putLong(val);
 		}
-		
-		public byte getType()
-		{
-			return LONG;
-		}
 
 		public int encodeSize()
       {
@@ -654,11 +624,6 @@
 			buffer.putFloat(val);
 		}
 		
-		public byte getType()
-		{
-			return FLOAT;
-		}
-		
       public int encodeSize()
       {
          return SIZE_BYTE + SIZE_FLOAT;
@@ -690,11 +655,6 @@
 			buffer.putByte(DOUBLE);
 			buffer.putDouble(val);
 		}
-			
-		public byte getType()
-		{
-			return DOUBLE;
-		}
 
 		public int encodeSize()
       {
@@ -726,12 +686,7 @@
 			buffer.putByte(CHAR);
 			buffer.putChar(val);
 		}
-			
-		public byte getType()
-		{
-			return CHAR;
-		}
-		
+
 		public int size()
 		{
 			return SIZE_CHAR;
@@ -767,17 +722,7 @@
 			buffer.putByte(STRING);
 			buffer.putSimpleString(val);
 		}
-			
-		public byte getType()
-		{
-			return STRING;
-		}
 		
-		public int size()
-		{
-			return SimpleString.sizeofString(val);
-		}
-		
 		public int encodeSize()
 		{
 		   return SIZE_BYTE + SimpleString.sizeofString(val);

Modified: trunk/tests/src/org/jboss/messaging/tests/unit/core/util/TypedPropertiesTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/core/util/TypedPropertiesTest.java	2008-06-06 11:23:39 UTC (rev 4401)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/core/util/TypedPropertiesTest.java	2008-06-06 13:02:50 UTC (rev 4402)
@@ -6,17 +6,33 @@
  */
 package org.jboss.messaging.tests.unit.core.util;
 
-import static org.jboss.messaging.tests.util.RandomUtil.randomString;
+import static org.jboss.messaging.tests.unit.core.remoting.impl.wireformat.CodecAssert.assertEqualsByteArrays;
+import static org.jboss.messaging.tests.util.RandomUtil.randomBoolean;
+import static org.jboss.messaging.tests.util.RandomUtil.randomByte;
+import static org.jboss.messaging.tests.util.RandomUtil.randomBytes;
+import static org.jboss.messaging.tests.util.RandomUtil.randomChar;
+import static org.jboss.messaging.tests.util.RandomUtil.randomDouble;
+import static org.jboss.messaging.tests.util.RandomUtil.randomFloat;
+import static org.jboss.messaging.tests.util.RandomUtil.randomInt;
+import static org.jboss.messaging.tests.util.RandomUtil.randomLong;
+import static org.jboss.messaging.tests.util.RandomUtil.randomShort;
+import static org.jboss.messaging.tests.util.RandomUtil.randomSimpleString;
+
+import java.util.Iterator;
+
 import junit.framework.TestCase;
 
+import org.jboss.messaging.core.remoting.impl.mina.IoBufferWrapper;
+import org.jboss.messaging.tests.util.RandomUtil;
+import org.jboss.messaging.util.MessagingBuffer;
 import org.jboss.messaging.util.SimpleString;
 import org.jboss.messaging.util.TypedProperties;
 
 /**
  * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
- *
+ * 
  * @version <tt>$Revision$</tt>
- *
+ * 
  */
 public class TypedPropertiesTest extends TestCase
 {
@@ -26,6 +42,32 @@
 
    // Static --------------------------------------------------------
 
+   private static void assertEqualsTypeProperties(TypedProperties expected,
+         TypedProperties actual)
+   {
+      assertNotNull(expected);
+      assertNotNull(actual);
+      assertEquals(expected.encodeSize(), actual.encodeSize());
+      assertEquals(expected.getPropertyNames(), actual.getPropertyNames());
+      Iterator<SimpleString> iterator = actual.getPropertyNames().iterator();
+      while (iterator.hasNext())
+      {
+         SimpleString key = (SimpleString) iterator.next();
+         Object expectedValue = expected.getProperty(key);
+         Object actualValue = actual.getProperty(key);
+         if ((expectedValue instanceof byte[])
+               && (actualValue instanceof byte[]))
+         {
+            byte[] expectedBytes = (byte[]) expectedValue;
+            byte[] actualBytes = (byte[]) actualValue;
+            assertEqualsByteArrays(expectedBytes, actualBytes);
+         } else
+         {
+            assertEquals(expectedValue, actualValue);
+         }
+      }
+   }
+
    // Constructors --------------------------------------------------
 
    // Public --------------------------------------------------------
@@ -35,30 +77,43 @@
 
    public void testCopyContructor() throws Exception
    {
-      props.putStringProperty(key, new SimpleString(randomString()));
-      
+      props.putStringProperty(key, randomSimpleString());
+
       TypedProperties copy = new TypedProperties(props);
-      
+
       assertEquals(props.encodeSize(), copy.encodeSize());
       assertEquals(props.getPropertyNames(), copy.getPropertyNames());
-      
+
       assertTrue(copy.containsProperty(key));
       assertEquals(props.getProperty(key), copy.getProperty(key));
    }
-   
+
+   public void testRemove() throws Exception
+   {
+      props.putStringProperty(key, randomSimpleString());
+
+      assertTrue(props.containsProperty(key));
+      assertNotNull(props.getProperty(key));
+
+      props.removeProperty(key);
+
+      assertFalse(props.containsProperty(key));
+      assertNull(props.getProperty(key));
+   }
+
    public void testClear() throws Exception
    {
-      props.putStringProperty(key, new SimpleString(randomString()));
+      props.putStringProperty(key, randomSimpleString());
 
       assertTrue(props.containsProperty(key));
       assertNotNull(props.getProperty(key));
-      
+
       props.clear();
 
       assertFalse(props.containsProperty(key));
-      assertNull(props.getProperty(key));      
+      assertNull(props.getProperty(key));
    }
-   
+
    public void testKey() throws Exception
    {
       props.putBooleanProperty(key, true);
@@ -69,34 +124,160 @@
       char c = (Character) props.getProperty(key);
       assertEquals('a', c);
    }
+
+   public void testGetPropertyOnEmptyProperties() throws Exception
+   {
+      assertFalse(props.containsProperty(key));
+      assertNull(props.getProperty(key));
+   }
    
+   public void testRemovePropertyOnEmptyProperties() throws Exception
+   {
+      assertFalse(props.containsProperty(key));
+      assertNull(props.removeProperty(key));
+   }
+   
    public void testNullProperty() throws Exception
    {
       props.putStringProperty(key, null);
       assertTrue(props.containsProperty(key));
-      assertNull(props.getProperty(key));            
+      assertNull(props.getProperty(key));
    }
-   
+
    public void testBooleanProperty() throws Exception
    {
       props.putBooleanProperty(key, true);
       boolean bool = (Boolean) props.getProperty(key);
       assertEquals(true, bool);
-      
+
       props.putBooleanProperty(key, false);
       bool = (Boolean) props.getProperty(key);
       assertEquals(false, bool);
    }
+
+   public void testByteProperty() throws Exception
+   {
+      byte b = randomByte();
+      props.putByteProperty(key, b);
+      byte bb = (Byte) props.getProperty(key);
+      assertEquals(b, bb);
+   }
+
+   public void testBytesProperty() throws Exception
+   {
+      byte[] b = RandomUtil.randomBytes();
+      props.putBytesProperty(key, b);
+      byte[] bb = (byte[]) props.getProperty(key);
+      assertEqualsByteArrays(b, bb);
+   }
+
+   public void testBytesPropertyWithNull() throws Exception
+   {
+      props.putBytesProperty(key, null);
+
+      assertTrue(props.containsProperty(key));
+      byte[] bb = (byte[]) props.getProperty(key);
+      assertNull(bb);
+   }
+
+   public void testFloatProperty() throws Exception
+   {
+      float f = randomFloat();
+      props.putFloatProperty(key, f);
+      float ff = (Float) props.getProperty(key);
+      assertEquals(f, ff);
+   }
+
+   public void testDoubleProperty() throws Exception
+   {
+      double d = randomDouble();
+      props.putDoubleProperty(key, d);
+      double dd = (Double) props.getProperty(key);
+      assertEquals(d, dd);
+   }
+
+   public void testShortProperty() throws Exception
+   {
+      short s = randomShort();
+      props.putShortProperty(key, s);
+      short ss = (Short) props.getProperty(key);
+      assertEquals(s, ss);
+   }
+
+   public void testIntProperty() throws Exception
+   {
+      int i = randomInt();
+      props.putIntProperty(key, i);
+      int ii = (Integer) props.getProperty(key);
+      assertEquals(i, ii);
+   }
+
+   public void testLongProperty() throws Exception
+   {
+      long l = randomLong();
+      props.putLongProperty(key, l);
+      long ll = (Long) props.getProperty(key);
+      assertEquals(l, ll);
+   }
+
+   public void testCharProperty() throws Exception
+   {
+      char c = randomChar();
+      props.putCharProperty(key, c);
+      char cc = (Character) props.getProperty(key);
+      assertEquals(c, cc);
+   }
+
+   public void testEncodeDecode() throws Exception
+   {
+      props.putByteProperty(randomSimpleString(), randomByte());
+      props.putBytesProperty(randomSimpleString(), randomBytes());
+      props.putBytesProperty(randomSimpleString(), null);
+      props.putBooleanProperty(randomSimpleString(), randomBoolean());
+      props.putShortProperty(randomSimpleString(), randomShort());
+      props.putIntProperty(randomSimpleString(), randomInt());
+      props.putLongProperty(randomSimpleString(), randomLong());
+      props.putFloatProperty(randomSimpleString(), randomFloat());
+      props.putDoubleProperty(randomSimpleString(), randomDouble());
+      props.putCharProperty(randomSimpleString(), randomChar());
+      props.putStringProperty(randomSimpleString(), randomSimpleString());
+      props.putStringProperty(randomSimpleString(), null);
+
+      MessagingBuffer buffer = new IoBufferWrapper(1024);
+      props.encode(buffer);
+
+      buffer.flip();
+
+      TypedProperties decodedProps = new TypedProperties();
+      decodedProps.decode(buffer);
+
+      assertEqualsTypeProperties(props, decodedProps);
+   }
    
+   public void testEncodeDecodeEmpty() throws Exception
+   {
+      TypedProperties emptyProps = new TypedProperties();
+
+      MessagingBuffer buffer = new IoBufferWrapper(1024);
+      emptyProps.encode(buffer);
+
+      buffer.flip();
+
+      TypedProperties decodedProps = new TypedProperties();
+      decodedProps.decode(buffer);
+
+      assertEqualsTypeProperties(emptyProps, decodedProps);
+   }
+
    @Override
    protected void setUp() throws Exception
    {
       super.setUp();
 
       props = new TypedProperties();
-      key = new SimpleString(randomString());
+      key = randomSimpleString();
    }
-   
+
    @Override
    protected void tearDown() throws Exception
    {
@@ -105,7 +286,7 @@
 
       super.tearDown();
    }
-   
+
    // Package protected ---------------------------------------------
 
    // Protected -----------------------------------------------------

Modified: trunk/tests/src/org/jboss/messaging/tests/util/RandomUtil.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/util/RandomUtil.java	2008-06-06 11:23:39 UTC (rev 4401)
+++ trunk/tests/src/org/jboss/messaging/tests/util/RandomUtil.java	2008-06-06 13:02:50 UTC (rev 4402)
@@ -13,6 +13,7 @@
 import javax.transaction.xa.Xid;
 
 import org.jboss.messaging.core.transaction.impl.XidImpl;
+import org.jboss.messaging.util.SimpleString;
 
 /**
  * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
@@ -34,6 +35,16 @@
    {
       return randomUUID().toString();
    }
+   
+   public static SimpleString randomSimpleString()
+   {
+      return new SimpleString(randomString());
+   }
+   
+   public static char randomChar()
+   {
+      return randomString().charAt(0);
+   }
 
    public static long randomLong()
    {
@@ -44,6 +55,11 @@
    {
       return random.nextInt();
    }
+   
+   public static short randomShort()
+   {
+      return (short) random.nextInt(Short.MAX_VALUE);
+   }
 
    public static byte randomByte()
    {




More information about the jboss-cvs-commits mailing list