Hiram R. Chirino 2006-07-01 16:03:18 +00:00
parent 6f9cfad67b
commit 9befb11435
2 changed files with 44 additions and 26 deletions

View File

@ -51,6 +51,7 @@ public class MarshallingSupport {
public static final byte BYTE_ARRAY_TYPE = 10; public static final byte BYTE_ARRAY_TYPE = 10;
public static final byte MAP_TYPE = 11; public static final byte MAP_TYPE = 11;
public static final byte LIST_TYPE = 12; public static final byte LIST_TYPE = 12;
public static final byte BIG_STRING_TYPE = 13;
static public void marshalPrimitiveMap(Map map, DataOutputStream out) throws IOException { static public void marshalPrimitiveMap(Map map, DataOutputStream out) throws IOException {
if( map == null ) { if( map == null ) {
@ -143,8 +144,17 @@ public class MarshallingSupport {
out.writeInt(((byte[])value).length); out.writeInt(((byte[])value).length);
out.write(((byte[])value)); out.write(((byte[])value));
} else if( value.getClass() == String.class ) { } else if( value.getClass() == String.class ) {
out.writeByte(STRING_TYPE); String s = (String)value;
out.writeUTF((String)value);
// If it's too big, out.writeUTF may not able able to write it out.
if( s.length() < Short.MAX_VALUE/4 ) {
out.writeByte(STRING_TYPE);
out.writeUTF((String)value);
} else {
out.writeByte(BIG_STRING_TYPE);
writeUTF8(out, s);
}
} else if( value instanceof Map) { } else if( value instanceof Map) {
out.writeByte(MAP_TYPE); out.writeByte(MAP_TYPE);
marshalPrimitiveMap((Map) value, out); marshalPrimitiveMap((Map) value, out);
@ -191,6 +201,9 @@ public class MarshallingSupport {
case STRING_TYPE: case STRING_TYPE:
value = in.readUTF(); value = in.readUTF();
break; break;
case BIG_STRING_TYPE:
value = readUTF8(in);
break;
case MAP_TYPE: case MAP_TYPE:
value = unmarshalPrimitiveMap(in); value = unmarshalPrimitiveMap(in);
break; break;

View File

@ -18,6 +18,7 @@ package org.apache.activemq.command;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
@ -77,8 +78,16 @@ public class ActiveMQMapMessageTest extends TestCase {
msg.setShort("short", (short) 1); msg.setShort("short", (short) 1);
msg.setString("string", "string"); msg.setString("string", "string");
msg.onSend(); // Test with a 1Meg String
msg.setContent(msg.getContent()); StringBuffer bigSB = new StringBuffer(1024*1024);
for( int i=0; i < 1024*1024; i++ ) {
bigSB.append((char)'a'+i%26);
}
String bigString = bigSB.toString();
msg.setString("bigString", bigString);
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);
@ -91,6 +100,7 @@ public class ActiveMQMapMessageTest extends TestCase {
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);
} }
public void testGetBoolean() throws JMSException { public void testGetBoolean() throws JMSException {
@ -101,8 +111,7 @@ public class ActiveMQMapMessageTest extends TestCase {
msg.clearBody(); msg.clearBody();
msg.setString(name, "true"); msg.setString(name, "true");
msg.onSend(); msg = (ActiveMQMapMessage) msg.copy();
msg.setContent(msg.getContent());
assertTrue(msg.getBoolean(name)); assertTrue(msg.getBoolean(name));
} }
@ -110,7 +119,7 @@ public class ActiveMQMapMessageTest extends TestCase {
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.setReadOnlyBody(true); msg = (ActiveMQMapMessage) msg.copy();
assertTrue(msg.getByte(this.name) == (byte) 1); assertTrue(msg.getByte(this.name) == (byte) 1);
} }
@ -118,7 +127,7 @@ public class ActiveMQMapMessageTest extends TestCase {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { try {
msg.setShort(this.name, (short) 1); msg.setShort(this.name, (short) 1);
msg.setReadOnlyBody(true); msg = (ActiveMQMapMessage) msg.copy();
assertTrue(msg.getShort(this.name) == (short) 1); assertTrue(msg.getShort(this.name) == (short) 1);
} catch (JMSException jmsEx) { } catch (JMSException jmsEx) {
jmsEx.printStackTrace(); jmsEx.printStackTrace();
@ -130,7 +139,7 @@ public class ActiveMQMapMessageTest extends TestCase {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { try {
msg.setChar(this.name, 'a'); msg.setChar(this.name, 'a');
msg.setReadOnlyBody(true); msg = (ActiveMQMapMessage) msg.copy();
assertTrue(msg.getChar(this.name) == 'a'); assertTrue(msg.getChar(this.name) == 'a');
} catch (JMSException jmsEx) { } catch (JMSException jmsEx) {
jmsEx.printStackTrace(); jmsEx.printStackTrace();
@ -142,7 +151,7 @@ public class ActiveMQMapMessageTest extends TestCase {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { try {
msg.setInt(this.name, 1); msg.setInt(this.name, 1);
msg.setReadOnlyBody(true); msg = (ActiveMQMapMessage) msg.copy();
assertTrue(msg.getInt(this.name) == 1); assertTrue(msg.getInt(this.name) == 1);
} catch (JMSException jmsEx) { } catch (JMSException jmsEx) {
jmsEx.printStackTrace(); jmsEx.printStackTrace();
@ -154,7 +163,7 @@ public class ActiveMQMapMessageTest extends TestCase {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { try {
msg.setLong(this.name, 1); msg.setLong(this.name, 1);
msg.setReadOnlyBody(true); msg = (ActiveMQMapMessage) msg.copy();
assertTrue(msg.getLong(this.name) == 1); assertTrue(msg.getLong(this.name) == 1);
} catch (JMSException jmsEx) { } catch (JMSException jmsEx) {
jmsEx.printStackTrace(); jmsEx.printStackTrace();
@ -166,7 +175,7 @@ public class ActiveMQMapMessageTest extends TestCase {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { try {
msg.setFloat(this.name, 1.5f); msg.setFloat(this.name, 1.5f);
msg.setReadOnlyBody(true); msg = (ActiveMQMapMessage) msg.copy();
assertTrue(msg.getFloat(this.name) == 1.5f); assertTrue(msg.getFloat(this.name) == 1.5f);
} catch (JMSException jmsEx) { } catch (JMSException jmsEx) {
jmsEx.printStackTrace(); jmsEx.printStackTrace();
@ -178,7 +187,7 @@ public class ActiveMQMapMessageTest extends TestCase {
ActiveMQMapMessage msg = new ActiveMQMapMessage(); ActiveMQMapMessage msg = new ActiveMQMapMessage();
try { try {
msg.setDouble(this.name, 1.5); msg.setDouble(this.name, 1.5);
msg.setReadOnlyBody(true); msg = (ActiveMQMapMessage) msg.copy();
assertTrue(msg.getDouble(this.name) == 1.5); assertTrue(msg.getDouble(this.name) == 1.5);
} catch (JMSException jmsEx) { } catch (JMSException jmsEx) {
jmsEx.printStackTrace(); jmsEx.printStackTrace();
@ -191,8 +200,8 @@ public class ActiveMQMapMessageTest extends TestCase {
try { try {
String str = "test"; String str = "test";
msg.setString(this.name, str); msg.setString(this.name, str);
msg.setReadOnlyBody(true); msg = (ActiveMQMapMessage) msg.copy();
assertTrue(msg.getString(this.name) == str); assertEquals(msg.getString(this.name), str);
} catch (JMSException jmsEx) { } catch (JMSException jmsEx) {
jmsEx.printStackTrace(); jmsEx.printStackTrace();
assertTrue(false); assertTrue(false);
@ -207,8 +216,8 @@ public class ActiveMQMapMessageTest extends TestCase {
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.setReadOnlyBody(true); msg = (ActiveMQMapMessage) msg.copy();
assertTrue(msg.getBytes(this.name) == bytes1); assertTrue( Arrays.equals(msg.getBytes(this.name), bytes1));
assertEquals(msg.getBytes(this.name + "2").length, bytes2.length); assertEquals(msg.getBytes(this.name + "2").length, bytes2.length);
} catch (JMSException jmsEx) { } catch (JMSException jmsEx) {
jmsEx.printStackTrace(); jmsEx.printStackTrace();
@ -246,8 +255,7 @@ public class ActiveMQMapMessageTest extends TestCase {
fail("object formats should be correct"); fail("object formats should be correct");
} }
msg.onSend(); msg = (ActiveMQMapMessage) msg.copy();
msg.setContent(msg.getContent());
assertTrue(msg.getObject("boolean") instanceof Boolean); assertTrue(msg.getObject("boolean") instanceof Boolean);
assertEquals(msg.getObject("boolean"), booleanValue); assertEquals(msg.getObject("boolean"), booleanValue);
@ -304,8 +312,7 @@ public class ActiveMQMapMessageTest extends TestCase {
msg.setShort("short", (short) 1); msg.setShort("short", (short) 1);
msg.setString("string", "string"); msg.setString("string", "string");
msg.onSend(); msg = (ActiveMQMapMessage) msg.copy();
msg.setContent(msg.getContent());
Enumeration mapNamesEnum = msg.getMapNames(); Enumeration mapNamesEnum = msg.getMapNames();
ArrayList mapNamesList = Collections.list(mapNamesEnum); ArrayList mapNamesList = Collections.list(mapNamesEnum);
@ -330,8 +337,7 @@ public class ActiveMQMapMessageTest extends TestCase {
mapMessage.setString("exists", "test"); mapMessage.setString("exists", "test");
mapMessage.onSend(); mapMessage = (ActiveMQMapMessage) mapMessage.copy();
mapMessage.setContent(mapMessage.getContent());
assertTrue(mapMessage.itemExists("exists")); assertTrue(mapMessage.itemExists("exists"));
assertFalse(mapMessage.itemExists("doesntExist")); assertFalse(mapMessage.itemExists("doesntExist"));
@ -350,8 +356,7 @@ public class ActiveMQMapMessageTest extends TestCase {
mapMessage.clearBody(); mapMessage.clearBody();
mapMessage.setString("String", "String"); mapMessage.setString("String", "String");
mapMessage.onSend(); mapMessage = (ActiveMQMapMessage) mapMessage.copy();
mapMessage.setContent(mapMessage.getContent());
mapMessage.getString("String"); mapMessage.getString("String");
} }