ARTEMIS-2025 Ensure correct calculation of message body size

This commit is contained in:
Martyn Taylor 2018-08-12 15:12:11 +01:00 committed by Clebert Suconic
parent 3ac7b9aef6
commit d6d73c7f23
3 changed files with 29 additions and 2 deletions

View File

@ -189,7 +189,8 @@ public class ClientMessageImpl extends CoreMessage implements ClientMessageInter
@Override
public int getBodySize() {
return getBodyBuffer().writerIndex() - getBodyBuffer().readerIndex();
checkEncode();
return endOfBodyPosition - BUFFER_HEADER_SPACE;
}
@Override

View File

@ -308,7 +308,7 @@ public class CoreMessage extends RefCountMessage implements ICoreMessage {
sendBuffer.readerIndex(0);
}
private synchronized void checkEncode() {
protected synchronized void checkEncode() {
if (!validBuffer) {
encode();
}

View File

@ -16,8 +16,13 @@
*/
package org.apache.activemq.artemis.tests.integration.ra;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.jms.JMSProducer;
import javax.jms.JMSRuntimeException;
import javax.jms.MessageFormatRuntimeException;
import javax.jms.Queue;
import javax.jms.TextMessage;
import javax.transaction.TransactionManager;
import java.util.HashSet;
import java.util.Set;
@ -143,4 +148,25 @@ public class JMSContextTest extends ActiveMQRATestBase {
assertEquals(context.getSessionMode(), JMSContext.AUTO_ACKNOWLEDGE);
}
@Test
public void testJMSContextConsumerThrowsMessageFormatExceptionOnMalformedBody() throws Exception {
Queue queue = createQueue(true, "ContextMalformedBodyTestQueue");
JMSContext context = qraConnectionFactory.createContext();
JMSProducer producer = context.createProducer();
TextMessage message = context.createTextMessage("TestMessage");
producer.send(queue, message);
JMSConsumer consumer = context.createConsumer(queue);
try {
consumer.receiveBody(Boolean.class);
fail("Should thrown MessageFormatException");
} catch (MessageFormatRuntimeException mfre) {
// Do nothing test passed
} catch (Exception e) {
fail("Threw wrong exception, should be MessageFormatRuntimeException, instead got: " + e.getClass().getCanonicalName());
}
}
}