This commit is contained in:
Clebert Suconic 2017-12-18 22:39:20 -05:00
commit d1c9bc0f2d
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 {
if (isBodyAssignableTo(c)) {
return getBodyInternal(c);
} else if (hasNoBody()) {
return null;
}
// XXX HORNETQ-1209 Do we need translations here?
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.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageFormatException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient;
import org.apache.activemq.artemis.tests.util.JMSTestBase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -48,7 +48,6 @@ public class BodyTest extends JMSTestBase {
try (
Connection conn = cf.createConnection();
) {
Session sess = conn.createSession();
MessageProducer producer = sess.createProducer(queue);
@ -56,17 +55,27 @@ public class BodyTest extends JMSTestBase {
conn.start();
BytesMessage bytesMessage = sess.createBytesMessage();
BytesMessage bytesMessage2 = sess.createBytesMessage();
bytesMessage2.writeInt(42);
bytesMessage2.reset();
producer.send(bytesMessage);
Message msg = cons.receiveNoWait();
producer.send(bytesMessage2);
Message msg2 = cons.receiveNoWait();
assertNotNull(msg);
assertNotNull(msg2);
try {
msg.getBody(String.class);
fail("Exception expected");
} catch (MessageFormatException e) {
}
// message body is empty. getBody parameter may be set to any type
Assert.assertNull(msg.getBody(java.lang.Object.class));
Assert.assertNull(msg.getBody(byte[].class));
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 {
try {
Object foo = msg.getBody(c);
Assert.fail("expected a " + MessageFormatException.class);
Assert.assertNull("body should be null", foo);
} catch (MessageFormatException e) {
// expected
}