From 6e038d5ffd20678efac42c410fd7c2e0266b4416 Mon Sep 17 00:00:00 2001 From: Timothy Bish Date: Tue, 3 Mar 2015 16:17:40 -0500 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-5632 Return null from getBytes if no value present. --- .../activemq/command/ActiveMQMapMessage.java | 4 + .../command/ActiveMQMapMessageTest.java | 231 ++++++++---------- 2 files changed, 101 insertions(+), 134 deletions(-) diff --git a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQMapMessage.java b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQMapMessage.java index 9e544a9782..11bee21952 100755 --- a/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQMapMessage.java +++ b/activemq-client/src/main/java/org/apache/activemq/command/ActiveMQMapMessage.java @@ -494,6 +494,10 @@ public class ActiveMQMapMessage extends ActiveMQMessage implements MapMessage { public byte[] getBytes(String name) throws JMSException { initializeReading(); Object value = map.get(name); + if (value == null) { + return null; + } + if (value instanceof byte[]) { return (byte[])value; } else { diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQMapMessageTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQMapMessageTest.java index 5b82b2939b..a7c0dec5fc 100755 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQMapMessageTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQMapMessageTest.java @@ -16,8 +16,13 @@ */ package org.apache.activemq.command; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.IOException; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; @@ -28,49 +33,24 @@ import javax.jms.MessageFormatException; import javax.jms.MessageNotReadableException; import javax.jms.MessageNotWriteableException; -import junit.framework.TestCase; +import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * + * Test the MapMessage implementation for spec compliance. */ -public class ActiveMQMapMessageTest extends TestCase { +public class ActiveMQMapMessageTest { + private static final Logger LOG = LoggerFactory.getLogger(ActiveMQMapMessageTest.class); - private String name = "testName"; - - /** - * Constructor for ActiveMQMapMessageTest. - * - * @param name - */ - public ActiveMQMapMessageTest(String name) { - super(name); - } - - public static void main(String[] args) { - junit.textui.TestRunner.run(ActiveMQMapMessageTest.class); - } - - /* - * @see TestCase#setUp() - */ - protected void setUp() throws Exception { - super.setUp(); - } - - /* - * @see TestCase#tearDown() - */ - protected void tearDown() throws Exception { - super.tearDown(); - } + private final String name = "testName"; + @Test(timeout = 10000) public void testBytesConversion() throws JMSException, IOException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); msg.setBoolean("boolean", true); - msg.setByte("byte", (byte)1); + msg.setByte("byte", (byte) 1); msg.setBytes("bytes", new byte[1]); msg.setChar("char", 'a'); msg.setDouble("double", 1.5); @@ -78,22 +58,22 @@ public class ActiveMQMapMessageTest extends TestCase { msg.setInt("int", 1); msg.setLong("long", 1); msg.setObject("object", "stringObj"); - msg.setShort("short", (short)1); + msg.setShort("short", (short) 1); msg.setString("string", "string"); // Test with a 1Meg String StringBuffer bigSB = new StringBuffer(1024 * 1024); for (int i = 0; i < 1024 * 1024; i++) { - bigSB.append((char)'a' + i % 26); + bigSB.append('a' + i % 26); } String bigString = bigSB.toString(); msg.setString("bigString", bigString); - msg = (ActiveMQMapMessage)msg.copy(); + msg = (ActiveMQMapMessage) msg.copy(); assertEquals(msg.getBoolean("boolean"), true); - assertEquals(msg.getByte("byte"), (byte)1); + assertEquals(msg.getByte("byte"), (byte) 1); assertEquals(msg.getBytes("bytes").length, 1); assertEquals(msg.getChar("char"), 'a'); assertEquals(msg.getDouble("double"), 1.5, 0); @@ -101,11 +81,12 @@ public class ActiveMQMapMessageTest extends TestCase { assertEquals(msg.getInt("int"), 1); assertEquals(msg.getLong("long"), 1); assertEquals(msg.getObject("object"), "stringObj"); - assertEquals(msg.getShort("short"), (short)1); + assertEquals(msg.getShort("short"), (short) 1); assertEquals(msg.getString("string"), "string"); assertEquals(msg.getString("bigString"), bigString); } + @Test(timeout = 10000) public void testGetBoolean() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); msg.setBoolean(name, true); @@ -114,120 +95,98 @@ public class ActiveMQMapMessageTest extends TestCase { msg.clearBody(); msg.setString(name, "true"); - msg = (ActiveMQMapMessage)msg.copy(); + msg = (ActiveMQMapMessage) msg.copy(); assertTrue(msg.getBoolean(name)); } + @Test(timeout = 10000) public void testGetByte() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); - msg.setByte(this.name, (byte)1); - msg = (ActiveMQMapMessage)msg.copy(); - assertTrue(msg.getByte(this.name) == (byte)1); + msg.setByte(this.name, (byte) 1); + msg = (ActiveMQMapMessage) msg.copy(); + assertTrue(msg.getByte(this.name) == (byte) 1); } - public void testGetShort() { + @Test(timeout = 10000) + public void testGetShort() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); - try { - msg.setShort(this.name, (short)1); - msg = (ActiveMQMapMessage)msg.copy(); - assertTrue(msg.getShort(this.name) == (short)1); - } catch (JMSException jmsEx) { - jmsEx.printStackTrace(); - assertTrue(false); - } + msg.setShort(this.name, (short) 1); + msg = (ActiveMQMapMessage) msg.copy(); + assertTrue(msg.getShort(this.name) == (short) 1); } - public void testGetChar() { + @Test(timeout = 10000) + public void testGetChar() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); - try { - msg.setChar(this.name, 'a'); - msg = (ActiveMQMapMessage)msg.copy(); - assertTrue(msg.getChar(this.name) == 'a'); - } catch (JMSException jmsEx) { - jmsEx.printStackTrace(); - assertTrue(false); - } + msg.setChar(this.name, 'a'); + msg = (ActiveMQMapMessage) msg.copy(); + assertTrue(msg.getChar(this.name) == 'a'); } - public void testGetInt() { + @Test(timeout = 10000) + public void testGetInt() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); - try { - msg.setInt(this.name, 1); - msg = (ActiveMQMapMessage)msg.copy(); - assertTrue(msg.getInt(this.name) == 1); - } catch (JMSException jmsEx) { - jmsEx.printStackTrace(); - assertTrue(false); - } + msg.setInt(this.name, 1); + msg = (ActiveMQMapMessage) msg.copy(); + assertTrue(msg.getInt(this.name) == 1); } - public void testGetLong() { + @Test(timeout = 10000) + public void testGetLong() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); - try { - msg.setLong(this.name, 1); - msg = (ActiveMQMapMessage)msg.copy(); - assertTrue(msg.getLong(this.name) == 1); - } catch (JMSException jmsEx) { - jmsEx.printStackTrace(); - assertTrue(false); - } + msg.setLong(this.name, 1); + msg = (ActiveMQMapMessage) msg.copy(); + assertTrue(msg.getLong(this.name) == 1); } - public void testGetFloat() { + @Test(timeout = 10000) + public void testGetFloat() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); - try { - msg.setFloat(this.name, 1.5f); - msg = (ActiveMQMapMessage)msg.copy(); - assertTrue(msg.getFloat(this.name) == 1.5f); - } catch (JMSException jmsEx) { - jmsEx.printStackTrace(); - assertTrue(false); - } + msg.setFloat(this.name, 1.5f); + msg = (ActiveMQMapMessage) msg.copy(); + assertTrue(msg.getFloat(this.name) == 1.5f); } - public void testGetDouble() { + @Test(timeout = 10000) + public void testGetDouble() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); - try { - msg.setDouble(this.name, 1.5); - msg = (ActiveMQMapMessage)msg.copy(); - assertTrue(msg.getDouble(this.name) == 1.5); - } catch (JMSException jmsEx) { - jmsEx.printStackTrace(); - assertTrue(false); - } + msg.setDouble(this.name, 1.5); + msg = (ActiveMQMapMessage) msg.copy(); + assertTrue(msg.getDouble(this.name) == 1.5); } - public void testGetString() { + @Test(timeout = 10000) + public void testGetString() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); - try { - String str = "test"; - msg.setString(this.name, str); - msg = (ActiveMQMapMessage)msg.copy(); - assertEquals(msg.getString(this.name), str); - } catch (JMSException jmsEx) { - jmsEx.printStackTrace(); - assertTrue(false); - } + String str = "test"; + msg.setString(this.name, str); + msg = (ActiveMQMapMessage) msg.copy(); + assertEquals(msg.getString(this.name), str); } - public void testGetBytes() { + @Test(timeout = 10000) + public void testGetBytes() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); - try { - byte[] bytes1 = new byte[3]; - byte[] bytes2 = new byte[2]; - System.arraycopy(bytes1, 0, bytes2, 0, 2); - msg.setBytes(this.name, bytes1); - msg.setBytes(this.name + "2", bytes1, 0, 2); - msg = (ActiveMQMapMessage)msg.copy(); - assertTrue(Arrays.equals(msg.getBytes(this.name), bytes1)); - assertEquals(msg.getBytes(this.name + "2").length, bytes2.length); - } catch (JMSException jmsEx) { - jmsEx.printStackTrace(); - assertTrue(false); - } + byte[] bytes1 = new byte[3]; + byte[] bytes2 = new byte[2]; + + System.arraycopy(bytes1, 0, bytes2, 0, 2); + msg.setBytes(this.name, bytes1); + msg.setBytes(this.name + "2", bytes1, 0, 2); + msg = (ActiveMQMapMessage) msg.copy(); + + assertTrue(Arrays.equals(msg.getBytes(this.name), bytes1)); + assertEquals(msg.getBytes(this.name + "2").length, bytes2.length); } + @Test(timeout = 10000) + public void testGetBytesWithNullValue() throws JMSException { + ActiveMQMapMessage msg = new ActiveMQMapMessage(); + assertNull(msg.getBytes(this.name)); + } + + @Test(timeout = 10000) public void testGetObject() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); Boolean booleanValue = Boolean.TRUE; @@ -258,7 +217,7 @@ public class ActiveMQMapMessageTest extends TestCase { fail("object formats should be correct"); } - msg = (ActiveMQMapMessage)msg.copy(); + msg = (ActiveMQMapMessage) msg.copy(); assertTrue(msg.getObject("boolean") instanceof Boolean); assertEquals(msg.getObject("boolean"), booleanValue); @@ -267,7 +226,7 @@ public class ActiveMQMapMessageTest extends TestCase { assertEquals(msg.getObject("byte"), byteValue); assertEquals(msg.getByte("byte"), byteValue.byteValue()); assertTrue(msg.getObject("bytes") instanceof byte[]); - assertEquals(((byte[])msg.getObject("bytes")).length, bytesValue.length); + assertEquals(((byte[]) msg.getObject("bytes")).length, bytesValue.length); assertEquals(msg.getBytes("bytes").length, bytesValue.length); assertTrue(msg.getObject("char") instanceof Character); assertEquals(msg.getObject("char"), charValue); @@ -300,10 +259,11 @@ public class ActiveMQMapMessageTest extends TestCase { } + @Test(timeout = 10000) public void testGetMapNames() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); msg.setBoolean("boolean", true); - msg.setByte("byte", (byte)1); + msg.setByte("byte", (byte) 1); msg.setBytes("bytes1", new byte[1]); msg.setBytes("bytes2", new byte[3], 0, 2); msg.setChar("char", 'a'); @@ -312,10 +272,10 @@ public class ActiveMQMapMessageTest extends TestCase { msg.setInt("int", 1); msg.setLong("long", 1); msg.setObject("object", "stringObj"); - msg.setShort("short", (short)1); + msg.setShort("short", (short) 1); msg.setString("string", "string"); - msg = (ActiveMQMapMessage)msg.copy(); + msg = (ActiveMQMapMessage) msg.copy(); Enumeration mapNamesEnum = msg.getMapNames(); List mapNamesList = Collections.list(mapNamesEnum); @@ -335,17 +295,19 @@ public class ActiveMQMapMessageTest extends TestCase { assertTrue(mapNamesList.contains("string")); } + @Test(timeout = 10000) public void testItemExists() throws JMSException { ActiveMQMapMessage mapMessage = new ActiveMQMapMessage(); mapMessage.setString("exists", "test"); - mapMessage = (ActiveMQMapMessage)mapMessage.copy(); + mapMessage = (ActiveMQMapMessage) mapMessage.copy(); assertTrue(mapMessage.itemExists("exists")); assertFalse(mapMessage.itemExists("doesntExist")); } + @Test(timeout = 10000) public void testClearBody() throws JMSException { ActiveMQMapMessage mapMessage = new ActiveMQMapMessage(); mapMessage.setString("String", "String"); @@ -358,15 +320,16 @@ public class ActiveMQMapMessageTest extends TestCase { mapMessage.clearBody(); mapMessage.setString("String", "String"); - mapMessage = (ActiveMQMapMessage)mapMessage.copy(); + mapMessage = (ActiveMQMapMessage) mapMessage.copy(); mapMessage.getString("String"); } + @Test(timeout = 10000) public void testReadOnlyBody() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); msg.setBoolean("boolean", true); - msg.setByte("byte", (byte)1); + msg.setByte("byte", (byte) 1); msg.setBytes("bytes", new byte[1]); msg.setBytes("bytes2", new byte[3], 0, 2); msg.setChar("char", 'a'); @@ -375,7 +338,7 @@ public class ActiveMQMapMessageTest extends TestCase { msg.setInt("int", 1); msg.setLong("long", 1); msg.setObject("object", "stringObj"); - msg.setShort("short", (short)1); + msg.setShort("short", (short) 1); msg.setString("string", "string"); msg.setReadOnlyBody(true); @@ -401,7 +364,7 @@ public class ActiveMQMapMessageTest extends TestCase { } catch (MessageNotWriteableException mnwe) { } try { - msg.setByte("byte", (byte)1); + msg.setByte("byte", (byte) 1); fail("should throw exception"); } catch (MessageNotWriteableException mnwe) { } @@ -446,7 +409,7 @@ public class ActiveMQMapMessageTest extends TestCase { } catch (MessageNotWriteableException mnwe) { } try { - msg.setShort("short", (short)1); + msg.setShort("short", (short) 1); fail("should throw exception"); } catch (MessageNotWriteableException mnwe) { } @@ -457,12 +420,13 @@ public class ActiveMQMapMessageTest extends TestCase { } } + @Test(timeout = 10000) public void testWriteOnlyBody() throws JMSException { ActiveMQMapMessage msg = new ActiveMQMapMessage(); msg.setReadOnlyBody(false); msg.setBoolean("boolean", true); - msg.setByte("byte", (byte)1); + msg.setByte("byte", (byte) 1); msg.setBytes("bytes", new byte[1]); msg.setBytes("bytes2", new byte[3], 0, 2); msg.setChar("char", 'a'); @@ -471,7 +435,7 @@ public class ActiveMQMapMessageTest extends TestCase { msg.setInt("int", 1); msg.setLong("long", 1); msg.setObject("object", "stringObj"); - msg.setShort("short", (short)1); + msg.setShort("short", (short) 1); msg.setString("string", "string"); msg.setReadOnlyBody(true); @@ -488,5 +452,4 @@ public class ActiveMQMapMessageTest extends TestCase { msg.getShort("short"); msg.getString("string"); } - }