ARTEMIS-1341 fixed getBody

This commit is contained in:
Stanislav Knot 2017-12-12 14:49:31 +01:00 committed by Clebert Suconic
parent d67c65b8a2
commit 58cd02090d
3 changed files with 21 additions and 10 deletions

View File

@ -742,6 +742,8 @@ public class ActiveMQMessage implements javax.jms.Message {
public <T> T getBody(Class<T> c) throws JMSException { public <T> T getBody(Class<T> c) throws JMSException {
if (isBodyAssignableTo(c)) { if (isBodyAssignableTo(c)) {
return getBodyInternal(c); return getBodyInternal(c);
} else if (hasNoBody()) {
return null;
} }
// XXX HORNETQ-1209 Do we need translations here? // XXX HORNETQ-1209 Do we need translations here?
throw new MessageFormatException("Body not assignable to " + c); throw new MessageFormatException("Body not assignable to " + c);

View File

@ -20,12 +20,12 @@ import javax.jms.BytesMessage;
import javax.jms.Connection; import javax.jms.Connection;
import javax.jms.Message; import javax.jms.Message;
import javax.jms.MessageConsumer; import javax.jms.MessageConsumer;
import javax.jms.MessageFormatException;
import javax.jms.MessageProducer; import javax.jms.MessageProducer;
import javax.jms.Session; import javax.jms.Session;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient; import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.tests.util.JMSTestBase; import org.apache.activemq.artemis.tests.util.JMSTestBase;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -48,7 +48,6 @@ public class BodyTest extends JMSTestBase {
try ( try (
Connection conn = cf.createConnection(); Connection conn = cf.createConnection();
) { ) {
Session sess = conn.createSession(); Session sess = conn.createSession();
MessageProducer producer = sess.createProducer(queue); MessageProducer producer = sess.createProducer(queue);
@ -56,17 +55,27 @@ public class BodyTest extends JMSTestBase {
conn.start(); conn.start();
BytesMessage bytesMessage = sess.createBytesMessage(); BytesMessage bytesMessage = sess.createBytesMessage();
BytesMessage bytesMessage2 = sess.createBytesMessage();
bytesMessage2.writeInt(42);
bytesMessage2.reset();
producer.send(bytesMessage); producer.send(bytesMessage);
Message msg = cons.receiveNoWait(); Message msg = cons.receiveNoWait();
producer.send(bytesMessage2);
Message msg2 = cons.receiveNoWait();
assertNotNull(msg); assertNotNull(msg);
assertNotNull(msg2);
try { // message body is empty. getBody parameter may be set to any type
msg.getBody(String.class); Assert.assertNull(msg.getBody(java.lang.Object.class));
fail("Exception expected"); Assert.assertNull(msg.getBody(byte[].class));
} catch (MessageFormatException e) { Assert.assertNull(msg.getBody(String.class));
}
// message body is not empty. getBody parameter must be set to byte[].class (or java.lang.Object.class)
Assert.assertNotNull(msg2.getBody(byte[].class));
Assert.assertNotNull(msg2.getBody(java.lang.Object.class));
} }
} }
} }

View File

@ -94,7 +94,7 @@ public class BodyIsAssignableFromTest extends MessageBodyTestCase {
} else { } else {
try { try {
Object foo = msg.getBody(c); Object foo = msg.getBody(c);
Assert.fail("expected a " + MessageFormatException.class); Assert.assertNull("body should be null", foo);
} catch (MessageFormatException e) { } catch (MessageFormatException e) {
// expected // expected
} }