Return null from getBytes if no value present.
This commit is contained in:
Timothy Bish 2015-03-03 16:17:40 -05:00
parent ad57cc6fcb
commit 6e038d5ffd
2 changed files with 101 additions and 134 deletions

View File

@ -494,6 +494,10 @@ public class ActiveMQMapMessage extends ActiveMQMessage implements MapMessage {
public byte[] getBytes(String name) throws JMSException { public byte[] getBytes(String name) throws JMSException {
initializeReading(); initializeReading();
Object value = map.get(name); Object value = map.get(name);
if (value == null) {
return null;
}
if (value instanceof byte[]) { if (value instanceof byte[]) {
return (byte[])value; return (byte[])value;
} else { } else {

View File

@ -16,8 +16,13 @@
*/ */
package org.apache.activemq.command; 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.io.IOException;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
@ -28,49 +33,24 @@ import javax.jms.MessageFormatException;
import javax.jms.MessageNotReadableException; import javax.jms.MessageNotReadableException;
import javax.jms.MessageNotWriteableException; import javax.jms.MessageNotWriteableException;
import junit.framework.TestCase; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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 static final Logger LOG = LoggerFactory.getLogger(ActiveMQMapMessageTest.class);
private String name = "testName"; private final 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();
}
@Test(timeout = 10000)
public void testBytesConversion() throws JMSException, IOException { public void testBytesConversion() throws JMSException, IOException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
msg.setBoolean("boolean", true); msg.setBoolean("boolean", true);
msg.setByte("byte", (byte)1); msg.setByte("byte", (byte) 1);
msg.setBytes("bytes", new byte[1]); msg.setBytes("bytes", new byte[1]);
msg.setChar("char", 'a'); msg.setChar("char", 'a');
msg.setDouble("double", 1.5); msg.setDouble("double", 1.5);
@ -78,22 +58,22 @@ public class ActiveMQMapMessageTest extends TestCase {
msg.setInt("int", 1); msg.setInt("int", 1);
msg.setLong("long", 1); msg.setLong("long", 1);
msg.setObject("object", "stringObj"); msg.setObject("object", "stringObj");
msg.setShort("short", (short)1); msg.setShort("short", (short) 1);
msg.setString("string", "string"); msg.setString("string", "string");
// Test with a 1Meg String // Test with a 1Meg String
StringBuffer bigSB = new StringBuffer(1024 * 1024); StringBuffer bigSB = new StringBuffer(1024 * 1024);
for (int i = 0; i < 1024 * 1024; i++) { for (int i = 0; i < 1024 * 1024; i++) {
bigSB.append((char)'a' + i % 26); bigSB.append('a' + i % 26);
} }
String bigString = bigSB.toString(); String bigString = bigSB.toString();
msg.setString("bigString", bigString); msg.setString("bigString", bigString);
msg = (ActiveMQMapMessage)msg.copy(); msg = (ActiveMQMapMessage) msg.copy();
assertEquals(msg.getBoolean("boolean"), true); 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.getBytes("bytes").length, 1);
assertEquals(msg.getChar("char"), 'a'); assertEquals(msg.getChar("char"), 'a');
assertEquals(msg.getDouble("double"), 1.5, 0); assertEquals(msg.getDouble("double"), 1.5, 0);
@ -101,11 +81,12 @@ public class ActiveMQMapMessageTest extends TestCase {
assertEquals(msg.getInt("int"), 1); assertEquals(msg.getInt("int"), 1);
assertEquals(msg.getLong("long"), 1); assertEquals(msg.getLong("long"), 1);
assertEquals(msg.getObject("object"), "stringObj"); 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("string"), "string");
assertEquals(msg.getString("bigString"), bigString); assertEquals(msg.getString("bigString"), bigString);
} }
@Test(timeout = 10000)
public void testGetBoolean() throws JMSException { public void testGetBoolean() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
msg.setBoolean(name, true); msg.setBoolean(name, true);
@ -114,120 +95,98 @@ public class ActiveMQMapMessageTest extends TestCase {
msg.clearBody(); msg.clearBody();
msg.setString(name, "true"); msg.setString(name, "true");
msg = (ActiveMQMapMessage)msg.copy(); msg = (ActiveMQMapMessage) msg.copy();
assertTrue(msg.getBoolean(name)); assertTrue(msg.getBoolean(name));
} }
@Test(timeout = 10000)
public void testGetByte() throws JMSException { public void testGetByte() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
msg.setByte(this.name, (byte)1); msg.setByte(this.name, (byte) 1);
msg = (ActiveMQMapMessage)msg.copy(); msg = (ActiveMQMapMessage) msg.copy();
assertTrue(msg.getByte(this.name) == (byte)1); assertTrue(msg.getByte(this.name) == (byte) 1);
} }
public void testGetShort() { @Test(timeout = 10000)
public void testGetShort() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { msg.setShort(this.name, (short) 1);
msg.setShort(this.name, (short)1); msg = (ActiveMQMapMessage) msg.copy();
msg = (ActiveMQMapMessage)msg.copy(); assertTrue(msg.getShort(this.name) == (short) 1);
assertTrue(msg.getShort(this.name) == (short)1);
} catch (JMSException jmsEx) {
jmsEx.printStackTrace();
assertTrue(false);
}
} }
public void testGetChar() { @Test(timeout = 10000)
public void testGetChar() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { msg.setChar(this.name, 'a');
msg.setChar(this.name, 'a'); msg = (ActiveMQMapMessage) msg.copy();
msg = (ActiveMQMapMessage)msg.copy(); assertTrue(msg.getChar(this.name) == 'a');
assertTrue(msg.getChar(this.name) == 'a');
} catch (JMSException jmsEx) {
jmsEx.printStackTrace();
assertTrue(false);
}
} }
public void testGetInt() { @Test(timeout = 10000)
public void testGetInt() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { msg.setInt(this.name, 1);
msg.setInt(this.name, 1); msg = (ActiveMQMapMessage) msg.copy();
msg = (ActiveMQMapMessage)msg.copy(); assertTrue(msg.getInt(this.name) == 1);
assertTrue(msg.getInt(this.name) == 1);
} catch (JMSException jmsEx) {
jmsEx.printStackTrace();
assertTrue(false);
}
} }
public void testGetLong() { @Test(timeout = 10000)
public void testGetLong() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { msg.setLong(this.name, 1);
msg.setLong(this.name, 1); msg = (ActiveMQMapMessage) msg.copy();
msg = (ActiveMQMapMessage)msg.copy(); assertTrue(msg.getLong(this.name) == 1);
assertTrue(msg.getLong(this.name) == 1);
} catch (JMSException jmsEx) {
jmsEx.printStackTrace();
assertTrue(false);
}
} }
public void testGetFloat() { @Test(timeout = 10000)
public void testGetFloat() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { msg.setFloat(this.name, 1.5f);
msg.setFloat(this.name, 1.5f); msg = (ActiveMQMapMessage) msg.copy();
msg = (ActiveMQMapMessage)msg.copy(); assertTrue(msg.getFloat(this.name) == 1.5f);
assertTrue(msg.getFloat(this.name) == 1.5f);
} catch (JMSException jmsEx) {
jmsEx.printStackTrace();
assertTrue(false);
}
} }
public void testGetDouble() { @Test(timeout = 10000)
public void testGetDouble() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { msg.setDouble(this.name, 1.5);
msg.setDouble(this.name, 1.5); msg = (ActiveMQMapMessage) msg.copy();
msg = (ActiveMQMapMessage)msg.copy(); assertTrue(msg.getDouble(this.name) == 1.5);
assertTrue(msg.getDouble(this.name) == 1.5);
} catch (JMSException jmsEx) {
jmsEx.printStackTrace();
assertTrue(false);
}
} }
public void testGetString() { @Test(timeout = 10000)
public void testGetString() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { String str = "test";
String str = "test"; msg.setString(this.name, str);
msg.setString(this.name, str); msg = (ActiveMQMapMessage) msg.copy();
msg = (ActiveMQMapMessage)msg.copy(); assertEquals(msg.getString(this.name), str);
assertEquals(msg.getString(this.name), str);
} catch (JMSException jmsEx) {
jmsEx.printStackTrace();
assertTrue(false);
}
} }
public void testGetBytes() { @Test(timeout = 10000)
public void testGetBytes() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { byte[] bytes1 = new byte[3];
byte[] bytes1 = new byte[3]; byte[] bytes2 = new byte[2];
byte[] bytes2 = new byte[2];
System.arraycopy(bytes1, 0, bytes2, 0, 2); System.arraycopy(bytes1, 0, bytes2, 0, 2);
msg.setBytes(this.name, bytes1); msg.setBytes(this.name, bytes1);
msg.setBytes(this.name + "2", bytes1, 0, 2); msg.setBytes(this.name + "2", bytes1, 0, 2);
msg = (ActiveMQMapMessage)msg.copy(); msg = (ActiveMQMapMessage) msg.copy();
assertTrue(Arrays.equals(msg.getBytes(this.name), bytes1));
assertEquals(msg.getBytes(this.name + "2").length, bytes2.length); assertTrue(Arrays.equals(msg.getBytes(this.name), bytes1));
} catch (JMSException jmsEx) { assertEquals(msg.getBytes(this.name + "2").length, bytes2.length);
jmsEx.printStackTrace();
assertTrue(false);
}
} }
@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 { public void testGetObject() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
Boolean booleanValue = Boolean.TRUE; Boolean booleanValue = Boolean.TRUE;
@ -258,7 +217,7 @@ public class ActiveMQMapMessageTest extends TestCase {
fail("object formats should be correct"); fail("object formats should be correct");
} }
msg = (ActiveMQMapMessage)msg.copy(); msg = (ActiveMQMapMessage) msg.copy();
assertTrue(msg.getObject("boolean") instanceof Boolean); assertTrue(msg.getObject("boolean") instanceof Boolean);
assertEquals(msg.getObject("boolean"), booleanValue); assertEquals(msg.getObject("boolean"), booleanValue);
@ -267,7 +226,7 @@ public class ActiveMQMapMessageTest extends TestCase {
assertEquals(msg.getObject("byte"), byteValue); assertEquals(msg.getObject("byte"), byteValue);
assertEquals(msg.getByte("byte"), byteValue.byteValue()); assertEquals(msg.getByte("byte"), byteValue.byteValue());
assertTrue(msg.getObject("bytes") instanceof byte[]); 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); assertEquals(msg.getBytes("bytes").length, bytesValue.length);
assertTrue(msg.getObject("char") instanceof Character); assertTrue(msg.getObject("char") instanceof Character);
assertEquals(msg.getObject("char"), charValue); assertEquals(msg.getObject("char"), charValue);
@ -300,10 +259,11 @@ public class ActiveMQMapMessageTest extends TestCase {
} }
@Test(timeout = 10000)
public void testGetMapNames() throws JMSException { public void testGetMapNames() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
msg.setBoolean("boolean", true); msg.setBoolean("boolean", true);
msg.setByte("byte", (byte)1); msg.setByte("byte", (byte) 1);
msg.setBytes("bytes1", new byte[1]); msg.setBytes("bytes1", new byte[1]);
msg.setBytes("bytes2", new byte[3], 0, 2); msg.setBytes("bytes2", new byte[3], 0, 2);
msg.setChar("char", 'a'); msg.setChar("char", 'a');
@ -312,10 +272,10 @@ public class ActiveMQMapMessageTest extends TestCase {
msg.setInt("int", 1); msg.setInt("int", 1);
msg.setLong("long", 1); msg.setLong("long", 1);
msg.setObject("object", "stringObj"); msg.setObject("object", "stringObj");
msg.setShort("short", (short)1); msg.setShort("short", (short) 1);
msg.setString("string", "string"); msg.setString("string", "string");
msg = (ActiveMQMapMessage)msg.copy(); msg = (ActiveMQMapMessage) msg.copy();
Enumeration<String> mapNamesEnum = msg.getMapNames(); Enumeration<String> mapNamesEnum = msg.getMapNames();
List<String> mapNamesList = Collections.list(mapNamesEnum); List<String> mapNamesList = Collections.list(mapNamesEnum);
@ -335,17 +295,19 @@ public class ActiveMQMapMessageTest extends TestCase {
assertTrue(mapNamesList.contains("string")); assertTrue(mapNamesList.contains("string"));
} }
@Test(timeout = 10000)
public void testItemExists() throws JMSException { public void testItemExists() throws JMSException {
ActiveMQMapMessage mapMessage = new ActiveMQMapMessage(); ActiveMQMapMessage mapMessage = new ActiveMQMapMessage();
mapMessage.setString("exists", "test"); mapMessage.setString("exists", "test");
mapMessage = (ActiveMQMapMessage)mapMessage.copy(); mapMessage = (ActiveMQMapMessage) mapMessage.copy();
assertTrue(mapMessage.itemExists("exists")); assertTrue(mapMessage.itemExists("exists"));
assertFalse(mapMessage.itemExists("doesntExist")); assertFalse(mapMessage.itemExists("doesntExist"));
} }
@Test(timeout = 10000)
public void testClearBody() throws JMSException { public void testClearBody() throws JMSException {
ActiveMQMapMessage mapMessage = new ActiveMQMapMessage(); ActiveMQMapMessage mapMessage = new ActiveMQMapMessage();
mapMessage.setString("String", "String"); mapMessage.setString("String", "String");
@ -358,15 +320,16 @@ public class ActiveMQMapMessageTest extends TestCase {
mapMessage.clearBody(); mapMessage.clearBody();
mapMessage.setString("String", "String"); mapMessage.setString("String", "String");
mapMessage = (ActiveMQMapMessage)mapMessage.copy(); mapMessage = (ActiveMQMapMessage) mapMessage.copy();
mapMessage.getString("String"); mapMessage.getString("String");
} }
@Test(timeout = 10000)
public void testReadOnlyBody() throws JMSException { public void testReadOnlyBody() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
msg.setBoolean("boolean", true); msg.setBoolean("boolean", true);
msg.setByte("byte", (byte)1); msg.setByte("byte", (byte) 1);
msg.setBytes("bytes", new byte[1]); msg.setBytes("bytes", new byte[1]);
msg.setBytes("bytes2", new byte[3], 0, 2); msg.setBytes("bytes2", new byte[3], 0, 2);
msg.setChar("char", 'a'); msg.setChar("char", 'a');
@ -375,7 +338,7 @@ public class ActiveMQMapMessageTest extends TestCase {
msg.setInt("int", 1); msg.setInt("int", 1);
msg.setLong("long", 1); msg.setLong("long", 1);
msg.setObject("object", "stringObj"); msg.setObject("object", "stringObj");
msg.setShort("short", (short)1); msg.setShort("short", (short) 1);
msg.setString("string", "string"); msg.setString("string", "string");
msg.setReadOnlyBody(true); msg.setReadOnlyBody(true);
@ -401,7 +364,7 @@ public class ActiveMQMapMessageTest extends TestCase {
} catch (MessageNotWriteableException mnwe) { } catch (MessageNotWriteableException mnwe) {
} }
try { try {
msg.setByte("byte", (byte)1); msg.setByte("byte", (byte) 1);
fail("should throw exception"); fail("should throw exception");
} catch (MessageNotWriteableException mnwe) { } catch (MessageNotWriteableException mnwe) {
} }
@ -446,7 +409,7 @@ public class ActiveMQMapMessageTest extends TestCase {
} catch (MessageNotWriteableException mnwe) { } catch (MessageNotWriteableException mnwe) {
} }
try { try {
msg.setShort("short", (short)1); msg.setShort("short", (short) 1);
fail("should throw exception"); fail("should throw exception");
} catch (MessageNotWriteableException mnwe) { } catch (MessageNotWriteableException mnwe) {
} }
@ -457,12 +420,13 @@ public class ActiveMQMapMessageTest extends TestCase {
} }
} }
@Test(timeout = 10000)
public void testWriteOnlyBody() throws JMSException { public void testWriteOnlyBody() throws JMSException {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
msg.setReadOnlyBody(false); msg.setReadOnlyBody(false);
msg.setBoolean("boolean", true); msg.setBoolean("boolean", true);
msg.setByte("byte", (byte)1); msg.setByte("byte", (byte) 1);
msg.setBytes("bytes", new byte[1]); msg.setBytes("bytes", new byte[1]);
msg.setBytes("bytes2", new byte[3], 0, 2); msg.setBytes("bytes2", new byte[3], 0, 2);
msg.setChar("char", 'a'); msg.setChar("char", 'a');
@ -471,7 +435,7 @@ public class ActiveMQMapMessageTest extends TestCase {
msg.setInt("int", 1); msg.setInt("int", 1);
msg.setLong("long", 1); msg.setLong("long", 1);
msg.setObject("object", "stringObj"); msg.setObject("object", "stringObj");
msg.setShort("short", (short)1); msg.setShort("short", (short) 1);
msg.setString("string", "string"); msg.setString("string", "string");
msg.setReadOnlyBody(true); msg.setReadOnlyBody(true);
@ -488,5 +452,4 @@ public class ActiveMQMapMessageTest extends TestCase {
msg.getShort("short"); msg.getShort("short");
msg.getString("string"); msg.getString("string");
} }
} }