diff options
| author | Martin Ritchie <ritchiem@apache.org> | 2006-12-15 08:33:10 +0000 |
|---|---|---|
| committer | Martin Ritchie <ritchiem@apache.org> | 2006-12-15 08:33:10 +0000 |
| commit | a96152eea7e7ce48bb9340f477f3935744ee5ea6 (patch) | |
| tree | ada0a6c314add42f32e5143e298c010ecc4b997c /java/common/src/test | |
| parent | b6e573a5889ff89ccceeab3d3dc0504157a8c285 (diff) | |
| download | qpid-python-a96152eea7e7ce48bb9340f477f3935744ee5ea6.tar.gz | |
QPID-181 QPID-180
AbstractJMSMessage.java - updated to use getJMSHeaders
JMSMapMessage.java - JMSPropertyFieldTable.java - Moved functionality of setting and retrieving a JMS property. Now shared by the Headers and MapMessageTest.java
MapMessageTest.java - Updated the exceptions that are caught as all methods should throw a JMSException i.e. MessageFormatException
TextMessageTest.java - Added tests for the Message Properties
common/pom.xml - Added JMS dependency for the JMSPropertyFieldTable.java and associated tests
EncodingUtils.java - changed comments and changed getencodedCharLength return to an int
PropertyFieldTable.java - Cleaned up and now uses enum for prefixs. Created comprehensive test of both PropertyFieldTable classes.
AMQPInvalidClassException.java - created to throw a runtime exception when trying to add a non-primative value. Forcing clients to handle this would break the Map usage of the FieldTable.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@487481 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/common/src/test')
| -rw-r--r-- | java/common/src/test/java/org/apache/qpid/framing/JMSPropertyFieldTableTest.java | 1020 | ||||
| -rw-r--r-- | java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java | 885 |
2 files changed, 1767 insertions, 138 deletions
diff --git a/java/common/src/test/java/org/apache/qpid/framing/JMSPropertyFieldTableTest.java b/java/common/src/test/java/org/apache/qpid/framing/JMSPropertyFieldTableTest.java new file mode 100644 index 0000000000..98202ddd1a --- /dev/null +++ b/java/common/src/test/java/org/apache/qpid/framing/JMSPropertyFieldTableTest.java @@ -0,0 +1,1020 @@ +/* + * 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.qpid.framing; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import java.util.Enumeration; +import java.util.Iterator; +import java.util.Map; +import java.util.HashMap; + +import org.apache.mina.common.ByteBuffer; +import org.apache.log4j.Logger; + +import javax.jms.JMSException; +import javax.jms.MessageFormatException; + +public class JMSPropertyFieldTableTest extends TestCase +{ + + private static final Logger _logger = Logger.getLogger(JMSPropertyFieldTableTest.class); + + + public void setUp() + { + System.getProperties().setProperty("strict-jms", "true"); + } + + public void tearDown() + { + System.getProperties().remove("strict-jms"); + } + + /** + * Test that setting a similar named value replaces any previous value set on that name + */ + public void testReplacement() throws JMSException + { + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + //Set a boolean value + table1.setBoolean("value", true); + + // reset value to an integer + table1.setInteger("value", Integer.MAX_VALUE); + + //Check boolean value is null + try + { + table1.getBoolean("value"); + } + catch (MessageFormatException mfe) + { + //normal execution + } + // ... and integer value is good + Assert.assertEquals(Integer.MAX_VALUE, table1.getInteger("value")); + } + + public void testRemoval() throws JMSException + { + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + //Set a boolean value + table1.setBoolean("value", true); + + Assert.assertTrue(table1.getBoolean("value")); + + table1.remove("value"); + + //Check boolean value is null + try + { + table1.getBoolean("value"); + } + catch (MessageFormatException mfe) + { + //normal execution + } + } + + + /** + * Set a boolean and check that we can only get it back as a boolean and a string + * Check that attempting to lookup a non existent value returns null + */ + public void testBoolean() throws JMSException + { + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + table1.setBoolean("value", true); + Assert.assertTrue(table1.propertyExists("value")); + + //Test Getting right value back + Assert.assertEquals(true, table1.getBoolean("value")); + + //Check we don't get anything back for other gets + try + { + table1.getByte("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + try + { + table1.getByte("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + try + { + table1.getShort("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + try + { + table1.getDouble("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + try + { + table1.getFloat("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + try + { + table1.getInteger("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + try + { + table1.getLong("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + //except value as a string + Assert.assertEquals("true", table1.getString("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.propertyExists("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value will return false + Assert.assertFalse(table1.getBoolean("Rubbish")); + } + + /** + * Set a byte and check that we can only get it back as a byte and a string + * Check that attempting to lookup a non existent value returns null + */ + public void testByte() throws JMSException + { + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + table1.setByte("value", Byte.MAX_VALUE); + Assert.assertTrue(table1.propertyExists("value")); + + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + try + { + table1.getBoolean("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + try + { + table1.getDouble("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getFloat("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + Assert.assertEquals(Byte.MAX_VALUE, (byte) table1.getShort("value")); + Assert.assertEquals(Byte.MAX_VALUE, (byte) table1.getInteger("value")); + Assert.assertEquals(Byte.MAX_VALUE, (byte) table1.getLong("value")); + Assert.assertEquals(Byte.MAX_VALUE, table1.getByte("value")); + //... and a the string value of it. + Assert.assertEquals("" + Byte.MAX_VALUE, table1.getString("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.propertyExists("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value returns null + try + { + table1.getByte("Rubbish"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + } + + + /** + * Set a short and check that we can only get it back as a short and a string + * Check that attempting to lookup a non existent value returns null + */ + public void testShort() throws JMSException + { + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + table1.setShort("value", Short.MAX_VALUE); + Assert.assertTrue(table1.propertyExists("value")); + + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + + try + { + table1.getBoolean("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getByte("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + try + { + table1.getDouble("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getFloat("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + + Assert.assertEquals(Short.MAX_VALUE, (short) table1.getLong("value")); + Assert.assertEquals(Short.MAX_VALUE, (short) table1.getInteger("value")); + Assert.assertEquals(Short.MAX_VALUE, table1.getShort("value")); + + //... and a the string value of it. + Assert.assertEquals("" + Short.MAX_VALUE, table1.getString("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.propertyExists("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value returns null + try + { + table1.getShort("Rubbish"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfe) + { + //normal path + } + } + + + /** + * Set a double and check that we can only get it back as a double + * Check that attempting to lookup a non existent value returns null + */ + public void testDouble() throws JMSException + { + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + table1.setDouble("value", Double.MAX_VALUE); + Assert.assertTrue(table1.propertyExists("value")); + + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + try + { + table1.getBoolean("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getByte("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getShort("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getFloat("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getInteger("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getLong("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + Assert.assertEquals(Double.MAX_VALUE, table1.getDouble("value")); + //... and a the string value of it. + Assert.assertEquals("" + Double.MAX_VALUE, table1.getString("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.propertyExists("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value returns null + try + { + table1.getDouble("Rubbish"); + fail("Should throw NullPointerException as float.valueOf will try sunreadJavaFormatString"); + } + catch (NullPointerException mfe) + { + //normal path + } + + } + + + /** + * Set a float and check that we can only get it back as a float + * Check that attempting to lookup a non existent value returns null + */ + public void testFloat() throws JMSException + { + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + table1.setFloat("value", Float.MAX_VALUE); + Assert.assertTrue(table1.propertyExists("value")); + + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + try + { + table1.getBoolean("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getByte("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getShort("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getInteger("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getLong("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + + Assert.assertEquals(Float.MAX_VALUE, table1.getFloat("value")); + Assert.assertEquals(Float.MAX_VALUE, (float) table1.getDouble("value")); + + //... and a the string value of it. + Assert.assertEquals("" + Float.MAX_VALUE, table1.getString("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.propertyExists("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value returns null + try + { + table1.getFloat("Rubbish"); + fail("Should throw NullPointerException as float.valueOf will try sunreadJavaFormatString"); + } + catch (NullPointerException mfe) + { + //normal path + } + } + + + /** + * Set an int and check that we can only get it back as an int + * Check that attempting to lookup a non existent value returns null + */ + public void testInt() throws JMSException + { + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + table1.setInteger("value", Integer.MAX_VALUE); + Assert.assertTrue(table1.propertyExists("value")); + + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + try + { + table1.getBoolean("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getByte("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getShort("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getDouble("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getFloat("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + + Assert.assertEquals(Integer.MAX_VALUE, table1.getLong("value")); + + Assert.assertEquals(Integer.MAX_VALUE, table1.getInteger("value")); + + //... and a the string value of it. + Assert.assertEquals("" + Integer.MAX_VALUE, table1.getString("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.propertyExists("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value returns null + try + { + table1.getInteger("Rubbish"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfe) + { + //normal path + } + } + + + /** + * Set a long and check that we can only get it back as a long + * Check that attempting to lookup a non existent value returns null + */ + public void testLong() throws JMSException + { + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + table1.setLong("value", Long.MAX_VALUE); + Assert.assertTrue(table1.propertyExists("value")); + + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + try + { + table1.getBoolean("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getByte("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getShort("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getDouble("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getFloat("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + try + { + table1.getInteger("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + + Assert.assertEquals(Long.MAX_VALUE, table1.getLong("value")); + + //... and a the string value of it. + Assert.assertEquals("" + Long.MAX_VALUE, table1.getString("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.propertyExists("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value + try + { + table1.getLong("Rubbish"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException mfs) + { + //normal Execution + } + + } + + + /** + * Calls all methods that can be used to check the table is empty + * - getEncodedSize + * - isEmpty + * - size + * + * @param table to check is empty + */ + private void checkEmpty(JMSPropertyFieldTable table) + { + Assert.assertFalse(table.getPropertyNames().hasMoreElements()); + } + + + /** + * Set a String and check that we can only get it back as a String + * Check that attempting to lookup a non existent value returns null + */ + public void testString() throws JMSException + { + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + table1.setString("value", "Hello"); + Assert.assertTrue(table1.propertyExists("value")); + + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + Assert.assertEquals(false, table1.getBoolean("value")); + + try + { + table1.getByte("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException nfs) + { + //normal Execution + } + try + { + table1.getShort("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException nfs) + { + //normal Execution + } + try + { + table1.getDouble("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException nfs) + { + //normal Execution + } + try + { + table1.getFloat("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException nfs) + { + //normal Execution + } + try + { + table1.getInteger("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException nfs) + { + //normal Execution + } + try + { + table1.getLong("value"); + fail("Should throw MessageFormatException"); + } + catch (MessageFormatException nfs) + { + //normal Execution + } + + Assert.assertEquals("Hello", table1.getString("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.propertyExists("value")); + + checkEmpty(table1); + + //Looking up an invalid value returns null + Assert.assertEquals(null, table1.getString("Rubbish")); + + //Additional Test that haven't been covered for string + table1.setObject("value", "Hello"); + //Check that it was set correctly + Assert.assertEquals("Hello", table1.getString("value")); + } + + + public void testValues() throws JMSException + { + JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + table.setBoolean("bool", true); + table.setDouble("double", Double.MAX_VALUE); + table.setFloat("float", Float.MAX_VALUE); + table.setInteger("int", Integer.MAX_VALUE); + table.setLong("long", Long.MAX_VALUE); + table.setShort("short", Short.MAX_VALUE); + table.setString("string", "Hello"); + table.setString("nullstring", null); + + table.setObject("objectbool", true); + table.setObject("objectdouble", Double.MAX_VALUE); + table.setObject("objectfloat", Float.MAX_VALUE); + table.setObject("objectint", Integer.MAX_VALUE); + table.setObject("objectlong", Long.MAX_VALUE); + table.setObject("objectshort", Short.MAX_VALUE); + table.setObject("objectstring", "Hello"); + + + Assert.assertEquals(true, table.getBoolean("bool")); + + Assert.assertEquals(Double.MAX_VALUE, table.getDouble("double")); + Assert.assertEquals(Float.MAX_VALUE, table.getFloat("float")); + Assert.assertEquals(Integer.MAX_VALUE, table.getInteger("int")); + Assert.assertEquals(Long.MAX_VALUE, table.getLong("long")); + Assert.assertEquals(Short.MAX_VALUE, table.getShort("short")); + Assert.assertEquals("Hello", table.getString("string")); + Assert.assertEquals(null, table.getString("null-string")); + + Assert.assertEquals(true, table.getObject("objectbool")); + Assert.assertEquals(Double.MAX_VALUE, table.getObject("objectdouble")); + Assert.assertEquals(Float.MAX_VALUE, table.getObject("objectfloat")); + Assert.assertEquals(Integer.MAX_VALUE, table.getObject("objectint")); + Assert.assertEquals(Long.MAX_VALUE, table.getObject("objectlong")); + Assert.assertEquals(Short.MAX_VALUE, table.getObject("objectshort")); + Assert.assertEquals("Hello", table.getObject("objectstring")); + } + + /** + * Additional test checkPropertyName doesn't accept Null + */ + public void testCheckPropertyNameasNull() throws JMSException + { + JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + + try + { + table.setObject(null, "String"); + fail("Null property name is not allowed"); + } + catch (IllegalArgumentException iae) + { + //normal path + } + checkEmpty(table); + } + + + /** + * Additional test checkPropertyName doesn't accept an empty String + */ + public void testCheckPropertyNameasEmptyString() throws JMSException + { + JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + + try + { + table.setObject("", "String"); + fail("empty property name is not allowed"); + } + catch (IllegalArgumentException iae) + { + //normal path + } + checkEmpty(table); + } + + + /** + * Additional test checkPropertyName doesn't accept an empty String + */ + public void testCheckPropertyNamehasMaxLength() throws JMSException + { + JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + + StringBuffer longPropertyName = new StringBuffer(129); + + for (int i = 0; i < 129; i++) + { + longPropertyName.append("x"); + } + + try + { + table.setObject(longPropertyName.toString(), "String"); + fail("property name must be < 128 characters"); + } + catch (IllegalArgumentException iae) + { + _logger.warn("JMS requires infinite property names AMQP limits us to 128 characters"); + } + + checkEmpty(table); + } + + + /** + * Additional test checkPropertyName starts with a letter + */ + public void testCheckPropertyNameStartCharacterIsLetter() throws JMSException + { + JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + + //Try a name that starts with a number + try + { + table.setObject("1", "String"); + fail("property name must start with a letter"); + } + catch (IllegalArgumentException iae) + { + //normal path + } + + checkEmpty(table); + } + + /** + * Additional test checkPropertyName starts with a letter + */ + public void testCheckPropertyNameContainsInvalidCharacter() throws JMSException + { + JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + + //Try a name that starts with a number + try + { + table.setObject("hello there", "String"); + fail("property name cannot contain spaces"); + } + catch (IllegalArgumentException iae) + { + //normal path + } + + checkEmpty(table); + } + + + /** + * Additional test checkPropertyName starts with a letter + */ + public void testCheckPropertyNameIsInvalid() throws JMSException + { + JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + + //Try a name that starts with a number + try + { + table.setObject("ESCAPE", "String"); + fail("property name must not contains spaces"); + } + catch (IllegalArgumentException iae) + { + //normal path + } + + checkEmpty(table); + } + + /** + * Additional test checkPropertyName starts with a hash or a dollar + */ + public void testCheckPropertyNameStartCharacterIsHashorDollar() throws JMSException + { + _logger.warn("Test:testCheckPropertyNameStartCharacterIsHashorDollar will fail JMS compilance as # and $ are not valid in a jms identifier"); +// JMSPropertyFieldTable table = new JMSPropertyFieldTable(); +// +// //Try a name that starts with a number +// try +// { +// table.setObject("#", "String"); +// table.setObject("$", "String"); +// } +// catch (IllegalArgumentException iae) +// { +// fail("property name are allowed to start with # and $s in AMQP"); +// } + } + + /** + * Test the contents of the sets + */ + public void testSets() + { + + JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + + table.put("n1", "1"); + table.put("n2", "2"); + table.put("n3", "3"); + + Enumeration enumerator = table.getPropertyNames(); + Assert.assertEquals("n1", enumerator.nextElement()); + Assert.assertEquals("n2", enumerator.nextElement()); + Assert.assertEquals("n3", enumerator.nextElement()); + Assert.assertFalse(enumerator.hasMoreElements()); + } + + public static junit.framework.Test suite() + { + return new junit.framework.TestSuite(JMSPropertyFieldTableTest.class); + } + +} diff --git a/java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java b/java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java index f2067a5649..5256c62054 100644 --- a/java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java +++ b/java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java @@ -24,14 +24,22 @@ import junit.framework.Assert; import junit.framework.TestCase; import java.util.Enumeration; +import java.util.Iterator; +import java.util.Map; +import java.util.HashMap; import org.apache.mina.common.ByteBuffer; +import org.apache.log4j.Logger; +import org.apache.qpid.AMQPInvalidClassException; public class PropertyFieldTableTest extends TestCase { - //Test byte modification + private static final Logger _logger = Logger.getLogger(PropertyFieldTableTest.class); + /** + * Test that modifying a byte[] after setting property doesn't change property + */ public void testByteModification() { PropertyFieldTable table = new PropertyFieldTable(); @@ -46,197 +54,518 @@ public class PropertyFieldTableTest extends TestCase assertBytesNotEqual(bytes, table.getBytes("bytes")); } - //Test replacement - + /** + * Test that setting a similar named value replaces any previous value set on that name + */ public void testReplacement() { PropertyFieldTable table1 = new PropertyFieldTable(); + //Set a boolean value table1.setBoolean("value", true); + //Check size of table is correct (<Value length> + <type> + <Boolean length>) + int size = EncodingUtils.encodedShortStringLength("value") + 1 + EncodingUtils.encodedBooleanLength(); + Assert.assertEquals(size, table1.getEncodedSize()); + + // reset value to an integer table1.setInteger("value", Integer.MAX_VALUE); + + // Check the size has changed accordingly (<Value length> + <type> + <Integer length>) + size = EncodingUtils.encodedShortStringLength("value") + 1 + EncodingUtils.encodedIntegerLength(); + Assert.assertEquals(size, table1.getEncodedSize()); + + //Check boolean value is null Assert.assertEquals(null, table1.getBoolean("value")); + // ... and integer value is good Assert.assertEquals((Integer) Integer.MAX_VALUE, table1.getInteger("value")); } - //Test Lookups - public void testBooleanLookup() + /** + * Set a boolean and check that we can only get it back as a boolean and a string + * Check that attempting to lookup a non existent value returns null + */ + public void testBoolean() { PropertyFieldTable table1 = new PropertyFieldTable(); table1.setBoolean("value", true); + Assert.assertTrue(table1.propertyExists("value")); + //Test Getting right value back Assert.assertEquals((Boolean) true, table1.getBoolean("value")); - //Looking up an invalid value returns null - Assert.assertTrue(table1.getBoolean("Rubbish") == null); - - //Try reading value as a string + //Check we don't get anything back for other gets + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + //except value as a string Assert.assertEquals("true", table1.getString("value")); + + //Try setting a null value and read it back + table1.put(PropertyFieldTable.Prefix.AMQP_BOOLEAN_PROPERTY_PREFIX, "value", null); + + // Should be able to get the null back + Assert.assertEquals(null, table1.getBoolean("value")); + //but still contains the value + Assert.assertTrue(table1.containsKey("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value returns null + Assert.assertEquals(null, table1.getBoolean("Rubbish")); } - public void testByteLookup() + /** + * Set a byte and check that we can only get it back as a byte and a string + * Check that attempting to lookup a non existent value returns null + */ + public void testByte() { PropertyFieldTable table1 = new PropertyFieldTable(); - table1.setByte("value", (byte) 1); - Assert.assertEquals((Byte) (byte) 1, table1.getByte("value")); + table1.setByte("value", Byte.MAX_VALUE); + Assert.assertTrue(table1.propertyExists("value")); - //Looking up an invalid value returns null - Assert.assertTrue(table1.getByte("Rubbish") == null); + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(Byte.MAX_VALUE, (byte) table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + //... and a the string value of it. + Assert.assertEquals("" + Byte.MAX_VALUE, table1.getString("value")); + + //Try setting a null value and read it back + table1.put(PropertyFieldTable.Prefix.AMQP_BYTE_PROPERTY_PREFIX, "value", null); + + // Should be able to get the null back + Assert.assertEquals(null, table1.getByte("value")); - //Try reading value as a string - Assert.assertEquals("1", table1.getString("value")); + //but still contains the value + Assert.assertTrue(table1.containsKey("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value returns null + Assert.assertEquals(null, table1.getByte("Rubbish")); } - public void testShortLookup() + /** + * Set a short and check that we can only get it back as a short and a string + * Check that attempting to lookup a non existent value returns null + */ + public void testShort() { PropertyFieldTable table1 = new PropertyFieldTable(); table1.setShort("value", Short.MAX_VALUE); - Assert.assertEquals((Short) Short.MAX_VALUE, table1.getShort("value")); + Assert.assertTrue(table1.propertyExists("value")); - //Looking up an invalid value returns null - Assert.assertTrue(table1.getShort("Rubbish") == null); - - //Try reading value as a string + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(Short.MAX_VALUE, (short) table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + //... and a the string value of it. Assert.assertEquals("" + Short.MAX_VALUE, table1.getString("value")); + + //Try setting a null value and read it back + table1.put(PropertyFieldTable.Prefix.AMQP_SHORT_PROPERTY_PREFIX, "value", null); + + // Should be able to get the null back + Assert.assertEquals(null, table1.getShort("value")); + + //but still contains the value + Assert.assertTrue(table1.containsKey("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value returns null + Assert.assertEquals(null, table1.getShort("Rubbish")); } - public void testCharLookup() + /** + * Set a char and check that we can only get it back as a char + * Check that attempting to lookup a non existent value returns null + */ + public void testChar() { PropertyFieldTable table1 = new PropertyFieldTable(); - table1.setChar("value", 'b'); - Assert.assertEquals((Character) 'b', table1.getCharacter("value")); + table1.setChar("value", 'c'); + Assert.assertTrue(table1.propertyExists("value")); - //Looking up an invalid value returns null - Assert.assertTrue(table1.getCharacter("Rubbish") == null); + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals('c', (char) table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + //... and a the string value of it. + Assert.assertEquals("c", table1.getString("value")); + + //Try setting a null value and read it back + table1.put(PropertyFieldTable.Prefix.AMQP_ASCII_CHARACTER_PROPERTY_PREFIX, "value", null); + + try + { + table1.getString("value"); + fail("Should throw NullPointerException"); + } + catch (NullPointerException npe) + { + //Normal Path + } + //but still contains the value + Assert.assertTrue(table1.containsKey("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); - //Try reading value as a string - Assert.assertEquals("b", table1.getString("value")); + //Looking up an invalid value returns null + Assert.assertEquals(null, table1.getCharacter("Rubbish")); } - public void testDoubleLookup() + + /** + * Set a double and check that we can only get it back as a double + * Check that attempting to lookup a non existent value returns null + */ + public void testDouble() { PropertyFieldTable table1 = new PropertyFieldTable(); table1.setDouble("value", Double.MAX_VALUE); - Assert.assertEquals(Double.MAX_VALUE, table1.getDouble("value")); - - //Looking up an invalid value returns null - Assert.assertTrue(table1.getDouble("Rubbish") == null); + Assert.assertTrue(table1.propertyExists("value")); - //Try reading value as a string + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(Double.MAX_VALUE, (double) table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + //... and a the string value of it. Assert.assertEquals("" + Double.MAX_VALUE, table1.getString("value")); + //Try setting a null value and read it back + table1.put(PropertyFieldTable.Prefix.AMQP_DOUBLE_PROPERTY_PREFIX, "value", null); + + Assert.assertEquals(null, table1.getDouble("value")); + + //but still contains the value + Assert.assertTrue(table1.containsKey("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value returns null + Assert.assertEquals(null, table1.getDouble("Rubbish")); } - public void testFloatLookup() + + /** + * Set a float and check that we can only get it back as a float + * Check that attempting to lookup a non existent value returns null + */ + public void testFloat() { PropertyFieldTable table1 = new PropertyFieldTable(); table1.setFloat("value", Float.MAX_VALUE); - Assert.assertEquals(Float.MAX_VALUE, table1.getFloat("value")); + Assert.assertTrue(table1.propertyExists("value")); - //Looking up an invalid value returns null - Assert.assertTrue(table1.getFloat("Rubbish") == null); - - //Try reading value as a string + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(Float.MAX_VALUE, (float) table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + //... and a the string value of it. Assert.assertEquals("" + Float.MAX_VALUE, table1.getString("value")); + //Try setting a null value and read it back + table1.put(PropertyFieldTable.Prefix.AMQP_FLOAT_PROPERTY_PREFIX, "value", null); + + Assert.assertEquals(null, table1.getFloat("value")); + + //but still contains the value + Assert.assertTrue(table1.containsKey("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value returns null + Assert.assertEquals(null, table1.getFloat("Rubbish")); } - public void testIntLookup() + + /** + * Set an int and check that we can only get it back as an int + * Check that attempting to lookup a non existent value returns null + */ + public void testInt() { PropertyFieldTable table1 = new PropertyFieldTable(); table1.setInteger("value", Integer.MAX_VALUE); - Assert.assertEquals((Integer) Integer.MAX_VALUE, table1.getInteger("value")); + Assert.assertTrue(table1.propertyExists("value")); - //Looking up an invalid value returns null - Assert.assertTrue(table1.getInteger("Rubbish") == null); - - //Try reading value as a string + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(Integer.MAX_VALUE, (int) table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + //... and a the string value of it. Assert.assertEquals("" + Integer.MAX_VALUE, table1.getString("value")); + //Try setting a null value and read it back + table1.put(PropertyFieldTable.Prefix.AMQP_INT_PROPERTY_PREFIX, "value", null); + + Assert.assertEquals(null, table1.getInteger("value")); + + //but still contains the value + Assert.assertTrue(table1.containsKey("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value returns null + Assert.assertEquals(null, table1.getInteger("Rubbish")); } - public void testLongLookup() + + /** + * Set a long and check that we can only get it back as a long + * Check that attempting to lookup a non existent value returns null + */ + public void testLong() { PropertyFieldTable table1 = new PropertyFieldTable(); table1.setLong("value", Long.MAX_VALUE); - Assert.assertEquals((Long) Long.MAX_VALUE, table1.getLong("value")); - - //Looking up an invalid value returns null - Assert.assertTrue(table1.getLong("Rubbish") == null); + Assert.assertTrue(table1.propertyExists("value")); - //Try reading value as a string + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(Long.MAX_VALUE, (long) table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + + //... and a the string value of it. Assert.assertEquals("" + Long.MAX_VALUE, table1.getString("value")); - } + //Try setting a null value and read it back + table1.put(PropertyFieldTable.Prefix.AMQP_LONG_PROPERTY_PREFIX, "value", null); - public void testBytesLookup() - { - PropertyFieldTable table1 = new PropertyFieldTable(); - byte[] bytes = {99, 98, 97, 96, 95}; - table1.setBytes("bytes", bytes); - assertBytesEqual(bytes, table1.getBytes("bytes")); + Assert.assertEquals(null, table1.getLong("value")); + + //but still contains the value + Assert.assertTrue(table1.containsKey("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); //Looking up an invalid value returns null - Assert.assertTrue(table1.getBytes("Rubbish") == null); + Assert.assertEquals(null, table1.getLong("Rubbish")); } - // Failed Lookups - public void testFailedBooleanLookup() + /** + * Set a double and check that we can only get it back as a double + * Check that attempting to lookup a non existent value returns null + */ + public void testBytes() { - PropertyFieldTable table1 = new PropertyFieldTable(); - Assert.assertEquals(null, table1.getBoolean("int")); - } + byte[] bytes = {99, 98, 97, 96, 95}; - public void testFailedByteLookup() - { PropertyFieldTable table1 = new PropertyFieldTable(); - Assert.assertEquals(null, table1.getByte("int")); - } + table1.setBytes("value", bytes); + Assert.assertTrue(table1.propertyExists("value")); - public void testFailedBytesLookup() - { - PropertyFieldTable table1 = new PropertyFieldTable(); - Assert.assertEquals(null, table1.getBytes("int")); - } + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + assertBytesEqual(bytes, table1.getBytes("value")); - public void testFailedCharLookup() - { - PropertyFieldTable table1 = new PropertyFieldTable(); - Assert.assertEquals(null, table1.getCharacter("int")); - } + //... and a the string value of it is null + Assert.assertEquals(null, table1.getString("value")); - public void testFailedDoubleLookup() - { - PropertyFieldTable table1 = new PropertyFieldTable(); - Assert.assertEquals(null, table1.getDouble("int")); - } + //Try setting a null value and read it back + table1.put(PropertyFieldTable.Prefix.AMQP_BINARY_PROPERTY_PREFIX, "value", null); - public void testFailedFloatLookup() - { - PropertyFieldTable table1 = new PropertyFieldTable(); - Assert.assertEquals(null, table1.getFloat("int")); - } + Assert.assertEquals(null, table1.getBytes("value")); - public void testFailedIntLookup() - { - PropertyFieldTable table1 = new PropertyFieldTable(); - Assert.assertEquals(null, table1.getInteger("int")); + //but still contains the value + Assert.assertTrue(table1.containsKey("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.containsKey("value")); + + // Table should now have zero size for encoding + checkEmpty(table1); + + //Looking up an invalid value returns null + Assert.assertEquals(null, table1.getBytes("Rubbish")); } - public void testFailedLongLookup() + /** + * Calls all methods that can be used to check the table is empty + * - getEncodedSize + * - isEmpty + * - size + * + * @param table to check is empty + */ + private void checkEmpty(PropertyFieldTable table) { - PropertyFieldTable table1 = new PropertyFieldTable(); - Assert.assertEquals(null, table1.getLong("int")); + Assert.assertEquals(0, table.getEncodedSize()); + Assert.assertTrue(table.isEmpty()); + Assert.assertEquals(0, table.size()); + + Assert.assertEquals(0, table.keySet().size()); + Assert.assertEquals(0, table.values().size()); + Assert.assertEquals(0, table.entrySet().size()); } - public void testFailedShortLookup() + + /** + * Set a String and check that we can only get it back as a String + * Check that attempting to lookup a non existent value returns null + */ + public void testString() { PropertyFieldTable table1 = new PropertyFieldTable(); - Assert.assertEquals(null, table1.getShort("int")); + table1.setString("value", "Hello"); + Assert.assertTrue(table1.propertyExists("value")); + + //Tets lookups we shouldn't get anything back for other gets + //we should get right value back for this type .... + Assert.assertEquals(null, table1.getBoolean("value")); + Assert.assertEquals(null, table1.getByte("value")); + Assert.assertEquals(null, table1.getShort("value")); + Assert.assertEquals(null, table1.getCharacter("value")); + Assert.assertEquals(null, table1.getDouble("value")); + Assert.assertEquals(null, table1.getFloat("value")); + Assert.assertEquals(null, table1.getInteger("value")); + Assert.assertEquals(null, table1.getLong("value")); + Assert.assertEquals(null, table1.getBytes("value")); + Assert.assertEquals("Hello", table1.getString("value")); + + //Try setting a null value and read it back + table1.setString("value", null); + + Assert.assertEquals(null, table1.getString("value")); + + //but still contains the value + Assert.assertTrue(table1.containsKey("value")); + + table1.remove("value"); + //but after a remove it doesn't + Assert.assertFalse(table1.containsKey("value")); + + checkEmpty(table1); + + //Looking up an invalid value returns null + Assert.assertEquals(null, table1.getString("Rubbish")); + + //Additional Test that haven't been covered for string + table1.setObject("value", "Hello"); + //Check that it was set correctly + Assert.assertEquals("Hello", table1.getString("value")); } - public void testXML() + + /** + * Test that the generated XML can be used to create a field table with the same values. + */ + public void testValidXML() { PropertyFieldTable table1 = new PropertyFieldTable(); table1.setBoolean("bool", true); @@ -249,6 +578,8 @@ public class PropertyFieldTableTest extends TestCase table1.setInteger("int", Integer.MAX_VALUE); table1.setLong("long", Long.MAX_VALUE); table1.setShort("short", Short.MAX_VALUE); + table1.setString("string", "Hello"); + table1.setString("null-string", null); table1.setObject("object-bool", true); table1.setObject("object-byte", Byte.MAX_VALUE); @@ -259,14 +590,48 @@ public class PropertyFieldTableTest extends TestCase table1.setObject("object-int", Integer.MAX_VALUE); table1.setObject("object-long", Long.MAX_VALUE); table1.setObject("object-short", Short.MAX_VALUE); + table1.setObject("object-string", "Hello"); + + Assert.assertEquals(21, table1.size()); String table1XML = table1.toString(); PropertyFieldTable table2 = new PropertyFieldTable(table1XML); Assert.assertEquals(table1XML, table2.toString()); + + //Check that when bytes is written out as a string with no new line between items that it is read in ok. + + } + + /** + * Test that invalid input throws the correct Exception + */ + public void testInvalidXML() + { + try + { + _logger.warn("Testing Invalid XML expecting IllegalArgumentException"); + new PropertyFieldTable("Rubbish"); + fail("IllegalArgumentException expected"); + } + catch (IllegalArgumentException iae) + { + //normal path + } + try + { + _logger.warn("Testing Invalid XML expecting IllegalArgumentException"); + new PropertyFieldTable(""); + fail("IllegalArgumentException expected"); + } + catch (IllegalArgumentException iae) + { + //normal path + } } + public void testKeyEnumeration() { PropertyFieldTable table = new PropertyFieldTable(); @@ -298,6 +663,8 @@ public class PropertyFieldTableTest extends TestCase table.setInteger("int", Integer.MAX_VALUE); table.setLong("long", Long.MAX_VALUE); table.setShort("short", Short.MAX_VALUE); + table.setString("string", "Hello"); + table.setString("null-string", null); table.setObject("object-bool", true); table.setObject("object-byte", Byte.MAX_VALUE); @@ -308,6 +675,7 @@ public class PropertyFieldTableTest extends TestCase table.setObject("object-int", Integer.MAX_VALUE); table.setObject("object-long", Long.MAX_VALUE); table.setObject("object-short", Short.MAX_VALUE); + table.setObject("object-string", "Hello"); Assert.assertEquals((Boolean) true, table.getBoolean("bool")); @@ -319,6 +687,8 @@ public class PropertyFieldTableTest extends TestCase Assert.assertEquals((Integer) Integer.MAX_VALUE, table.getInteger("int")); Assert.assertEquals((Long) Long.MAX_VALUE, table.getLong("long")); Assert.assertEquals((Short) Short.MAX_VALUE, table.getShort("short")); + Assert.assertEquals("Hello", table.getString("string")); + Assert.assertEquals(null, table.getString("null-string")); Assert.assertEquals(true, table.getObject("object-bool")); Assert.assertEquals(Byte.MAX_VALUE, table.getObject("object-byte")); @@ -329,6 +699,7 @@ public class PropertyFieldTableTest extends TestCase Assert.assertEquals(Integer.MAX_VALUE, table.getObject("object-int")); Assert.assertEquals(Long.MAX_VALUE, table.getObject("object-long")); Assert.assertEquals(Short.MAX_VALUE, table.getObject("object-short")); + Assert.assertEquals("Hello", table.getObject("object-string")); } @@ -347,6 +718,8 @@ public class PropertyFieldTableTest extends TestCase table.setInteger("int", Integer.MAX_VALUE); table.setLong("long", Long.MAX_VALUE); table.setShort("short", Short.MAX_VALUE); + table.setString("string", "hello"); + table.setString("null-string", null); final ByteBuffer buffer = ByteBuffer.allocate((int) table.getEncodedSize()); // FIXME XXX: Is cast a problem? @@ -370,6 +743,9 @@ public class PropertyFieldTableTest extends TestCase Assert.assertEquals((Integer) Integer.MAX_VALUE, table2.getInteger("int")); Assert.assertEquals((Long) Long.MAX_VALUE, table2.getLong("long")); Assert.assertEquals((Short) Short.MAX_VALUE, table2.getShort("short")); + Assert.assertEquals("hello", table2.getString("string")); + Assert.assertEquals(null, table2.getString("null-string")); + } catch (AMQFrameDecodingException e) { @@ -380,7 +756,7 @@ public class PropertyFieldTableTest extends TestCase public void testEncodingSize() { - FieldTable result = FieldTableFactory.newFieldTable(); + PropertyFieldTable result = new PropertyFieldTable(); int size = 0; result.setBoolean("boolean", true); @@ -468,63 +844,296 @@ public class PropertyFieldTableTest extends TestCase } - public void testEncodingSize1() +// public void testEncodingSize1() +// { +// PropertyFieldTable table = new PropertyFieldTable(); +// int size = 0; +// result.put("one", 1L); +// size = EncodingUtils.encodedShortStringLength("one"); +// size += 1 + EncodingUtils.encodedLongLength(); +// assertEquals(size, result.getEncodedSize()); +// +// result.put("two", 2L); +// size += EncodingUtils.encodedShortStringLength("two"); +// size += 1 + EncodingUtils.encodedLongLength(); +// assertEquals(size, result.getEncodedSize()); +// +// result.put("three", 3L); +// size += EncodingUtils.encodedShortStringLength("three"); +// size += 1 + EncodingUtils.encodedLongLength(); +// assertEquals(size, result.getEncodedSize()); +// +// result.put("four", 4L); +// size += EncodingUtils.encodedShortStringLength("four"); +// size += 1 + EncodingUtils.encodedLongLength(); +// assertEquals(size, result.getEncodedSize()); +// +// result.put("five", 5L); +// size += EncodingUtils.encodedShortStringLength("five"); +// size += 1 + EncodingUtils.encodedLongLength(); +// assertEquals(size, result.getEncodedSize()); +// +// //fixme should perhaps be expanded to incorporate all types. +// +// final ByteBuffer buffer = ByteBuffer.allocate((int) result.getEncodedSize()); // FIXME XXX: Is cast a problem? +// +// result.writeToBuffer(buffer); +// +// buffer.flip(); +// +// long length = buffer.getUnsignedInt(); +// +// try +// { +// PropertyFieldTable table2 = new PropertyFieldTable(buffer, length); +// +// Assert.assertEquals((Long) 1L, table2.getLong("one")); +// Assert.assertEquals((Long) 2L, table2.getLong("two")); +// Assert.assertEquals((Long) 3L, table2.getLong("three")); +// Assert.assertEquals((Long) 4L, table2.getLong("four")); +// Assert.assertEquals((Long) 5L, table2.getLong("five")); +// } +// catch (AMQFrameDecodingException e) +// { +// e.printStackTrace(); +// fail("PFT should be instantiated from bytes." + e.getCause()); +// } +// +// } + + + /** + * Additional test for setObject + */ + public void testSetObject() { - FieldTable result = FieldTableFactory.newFieldTable(); - int size = 0; - result.put("one", 1L); - size = EncodingUtils.encodedShortStringLength("one"); - size += 1 + EncodingUtils.encodedLongLength(); - assertEquals(size, result.getEncodedSize()); + PropertyFieldTable table = new PropertyFieldTable(); + + //Try setting a non primative object + + try + { + table.setObject("value", this); + fail("Only primative values allowed in setObject"); + } + catch (AMQPInvalidClassException iae) + { + //normal path + } + // so size should be zero + Assert.assertEquals(0, table.getEncodedSize()); + } - result.put("two", 2L); - size += EncodingUtils.encodedShortStringLength("two"); - size += 1 + EncodingUtils.encodedLongLength(); - assertEquals(size, result.getEncodedSize()); + /** + * Additional test checkPropertyName doesn't accept Null + */ + public void testCheckPropertyNameasNull() + { + PropertyFieldTable table = new PropertyFieldTable(); - result.put("three", 3L); - size += EncodingUtils.encodedShortStringLength("three"); - size += 1 + EncodingUtils.encodedLongLength(); - assertEquals(size, result.getEncodedSize()); + try + { + table.setObject(null, "String"); + fail("Null property name is not allowed"); + } + catch (IllegalArgumentException iae) + { + //normal path + } + // so size should be zero + Assert.assertEquals(0, table.getEncodedSize()); + } - result.put("four", 4L); - size += EncodingUtils.encodedShortStringLength("four"); - size += 1 + EncodingUtils.encodedLongLength(); - assertEquals(size, result.getEncodedSize()); - result.put("five", 5L); - size += EncodingUtils.encodedShortStringLength("five"); - size += 1 + EncodingUtils.encodedLongLength(); - assertEquals(size, result.getEncodedSize()); + /** + * Additional test checkPropertyName doesn't accept an empty String + */ + public void testCheckPropertyNameasEmptyString() + { + PropertyFieldTable table = new PropertyFieldTable(); - //fixme should perhaps be expanded to incorporate all types. + try + { + table.setObject("", "String"); + fail("empty property name is not allowed"); + } + catch (IllegalArgumentException iae) + { + //normal path + } + // so size should be zero + Assert.assertEquals(0, table.getEncodedSize()); + } - final ByteBuffer buffer = ByteBuffer.allocate((int) result.getEncodedSize()); // FIXME XXX: Is cast a problem? - result.writeToBuffer(buffer); + /** + * Additional test checkPropertyName doesn't accept an empty String + */ + public void testCheckPropertyNamehasMaxLength() + { + PropertyFieldTable table = new PropertyFieldTable(); - buffer.flip(); + StringBuffer longPropertyName = new StringBuffer(129); - long length = buffer.getUnsignedInt(); + for (int i = 0; i < 129; i++) + { + longPropertyName.append("x"); + } try { - PropertyFieldTable table2 = new PropertyFieldTable(buffer, length); + table.setObject(longPropertyName.toString(), "String"); + fail("property name must be < 128 characters"); + } + catch (IllegalArgumentException iae) + { + //normal path + } + // so size should be zero + Assert.assertEquals(0, table.getEncodedSize()); + } + - Assert.assertEquals((Long) 1L, table2.getLong("one")); - Assert.assertEquals((Long) 2L, table2.getLong("two")); - Assert.assertEquals((Long) 3L, table2.getLong("three")); - Assert.assertEquals((Long) 4L, table2.getLong("four")); - Assert.assertEquals((Long) 5L, table2.getLong("five")); + /** + * Additional test checkPropertyName starts with a letter + */ + public void testCheckPropertyNameStartCharacterIsLetter() + { + PropertyFieldTable table = new PropertyFieldTable(); + + //Try a name that starts with a number + try + { + table.setObject("1", "String"); + fail("property name must start with a letter"); } - catch (AMQFrameDecodingException e) + catch (IllegalArgumentException iae) { - e.printStackTrace(); - fail("PFT should be instantiated from bytes." + e.getCause()); + //normal path } + // so size should be zero + Assert.assertEquals(0, table.getEncodedSize()); + } + + + /** + * Additional test checkPropertyName starts with a hash or a dollar + */ + public void testCheckPropertyNameStartCharacterIsHashorDollar() + { + PropertyFieldTable table = new PropertyFieldTable(); + //Try a name that starts with a number + try + { + table.setObject("#", "String"); + table.setObject("$", "String"); + } + catch (IllegalArgumentException iae) + { + fail("property name are allowed to start with # and $s"); + } } + + /** + * Additional test to test the contents of the table + */ + public void testContents() + { + PropertyFieldTable table = new PropertyFieldTable(); + + table.put("StringProperty", "String"); + + Assert.assertTrue(table.containsValue("String")); + + Assert.assertEquals("String", table.get("StringProperty")); + + //Test Clear + + table.clear(); + + checkEmpty(table); + } + + /** + * Test the contents of the sets + */ + public void testSets() + { + + PropertyFieldTable table = new PropertyFieldTable(); + + table.put("n1", "1"); + table.put("n2", "2"); + table.put("n3", "3"); + + Iterator iterator = table.keySet().iterator(); + Assert.assertEquals("n1", iterator.next()); + Assert.assertEquals("n2", iterator.next()); + Assert.assertEquals("n3", iterator.next()); + Assert.assertFalse(iterator.hasNext()); + + + iterator = table.values().iterator(); + Assert.assertEquals("1", iterator.next()); + Assert.assertEquals("2", iterator.next()); + Assert.assertEquals("3", iterator.next()); + Assert.assertFalse(iterator.hasNext()); + + + iterator = table.entrySet().iterator(); + Map.Entry entry = (Map.Entry) iterator.next(); + Assert.assertEquals("n1", entry.getKey()); + Assert.assertEquals("1", entry.getValue()); + entry = (Map.Entry) iterator.next(); + Assert.assertEquals("n2", entry.getKey()); + Assert.assertEquals("2", entry.getValue()); + entry = (Map.Entry) iterator.next(); + Assert.assertEquals("n3", entry.getKey()); + Assert.assertEquals("3", entry.getValue()); + Assert.assertFalse(iterator.hasNext()); + + + } + + + /** + * Test that all the values are preserved after a putAll + */ + public void testPutAll() + { + Map map = new HashMap(); + + map.put("char", 'c'); + map.put("double", Double.MAX_VALUE); + map.put("float", Float.MAX_VALUE); + map.put("int", Integer.MAX_VALUE); + map.put("long", Long.MAX_VALUE); + map.put("short", Short.MAX_VALUE); + + PropertyFieldTable table = new PropertyFieldTable(); + + table.putAll(map); + + Assert.assertEquals(6, table.size()); + + Assert.assertTrue(table.containsKey("char")); + Assert.assertEquals('c', (char) table.getCharacter("char")); + Assert.assertTrue(table.containsKey("double")); + Assert.assertEquals(Double.MAX_VALUE, table.getDouble("double")); + Assert.assertTrue(table.containsKey("float")); + Assert.assertEquals(Float.MAX_VALUE, table.getFloat("float")); + Assert.assertTrue(table.containsKey("int")); + Assert.assertEquals(Integer.MAX_VALUE, (int) table.getInteger("int")); + Assert.assertTrue(table.containsKey("long")); + Assert.assertEquals(Long.MAX_VALUE, (long) table.getLong("long")); + Assert.assertTrue(table.containsKey("short")); + Assert.assertEquals(Short.MAX_VALUE, (short) table.getShort("short")); + Assert.assertEquals(Short.MAX_VALUE, (short) table.getShort("short")); + } + + private void assertBytesEqual(byte[] expected, byte[] actual) { Assert.assertEquals(expected.length, actual.length); |
