下面列出了javax.jms.MessageEOFException#javax.jms.MessageNotReadableException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public byte readByte() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Byte) {
position++;
return ((Byte) value).byteValue();
} else if (value instanceof String) {
byte result = Byte.parseByte((String) value);
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public char readChar() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Character) {
position++;
return ((Character) value).charValue();
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public float readFloat() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Float) {
position++;
return ((Float) value).floatValue();
} else if (value instanceof String) {
float result = Float.parseFloat((String) value);
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Test
public void testClearBody() throws JMSException, IOException {
JmsTextMessage textMessage = factory.createTextMessage();
textMessage.setText("string");
textMessage.clearBody();
assertFalse(textMessage.isReadOnlyBody());
assertNull(textMessage.getText());
try {
textMessage.setText("String");
textMessage.getText();
} catch (MessageNotWriteableException mnwe) {
fail("should be writeable");
} catch (MessageNotReadableException mnre) {
fail("should be readable");
}
}
@Test
public void testReadOnlyBody() throws JMSException {
JmsTextMessage textMessage = factory.createTextMessage();
textMessage.setText("test");
textMessage.setReadOnlyBody(true);
try {
textMessage.getText();
} catch (MessageNotReadableException e) {
fail("should be readable");
}
try {
textMessage.setText("test");
fail("should throw exception");
} catch (MessageNotWriteableException mnwe) {
}
}
/**
* Test that attempting to call {@link BytesMessage#getBodyLength()} on a received message after calling
* {@link BytesMessage#clearBody()} causes {@link MessageNotReadableException} to be thrown due to being write-only.
*
* @throws Exception if an error occurs during the test.
*/
@Test
public void testGetBodyLengthOnClearedReceivedMessageThrowsMessageNotReadableException() throws Exception {
byte[] content = "myBytesData".getBytes();
JmsTestBytesMessageFacade facade = new JmsTestBytesMessageFacade(content);
JmsBytesMessage bytesMessage = new JmsBytesMessage(facade);
bytesMessage.onDispatch();
assertEquals("Unexpected message length", content.length, bytesMessage.getBodyLength());
bytesMessage.clearBody();
try {
bytesMessage.getBodyLength();
fail("expected exception to be thrown");
} catch (MessageNotReadableException mnre) {
// expected
}
}
@Test
public void testUnreadablePrioirtyIsStillEnqueued() throws JMSException {
JmsInboundMessageDispatch message = createEnvelopeWithMessageThatCannotReadPriority();
queue.enqueue(createEnvelope(9));
queue.enqueue(message);
queue.enqueue(createEnvelope(1));
JmsInboundMessageDispatch envelope = queue.dequeueNoWait();
assertEquals(9, envelope.getMessage().getJMSPriority());
envelope = queue.dequeueNoWait();
try {
envelope.getMessage().getJMSPriority();
fail("Unreadable priority message should sit at default level");
} catch (MessageNotReadableException mnre) {}
envelope = queue.dequeueNoWait();
assertEquals(1, envelope.getMessage().getJMSPriority());
assertTrue(queue.isEmpty());
}
/**
* Test clear body
*/
@Test
public void testClearBody() throws JMSException, IOException {
SQSBytesMessage msg = new SQSBytesMessage();
byte[] byteArray = new byte[] { 1, 0, 'a', 65 };
msg.writeBytes(byteArray);
msg.clearBody();
byte[] readByteArray = new byte[4];
/*
* Verify message is in write-only mode
*/
try {
msg.readBytes(readByteArray);
} catch(MessageNotReadableException exception) {
assertEquals("Message is not readable", exception.getMessage());
}
msg.writeBytes(byteArray);
}
public void testClearBody() throws JMSException, IOException {
ActiveMQTextMessage textMessage = new ActiveMQTextMessage();
textMessage.setText("string");
textMessage.clearBody();
assertFalse(textMessage.isReadOnlyBody());
assertNull(textMessage.getText());
try {
textMessage.setText("String");
textMessage.getText();
} catch (MessageNotWriteableException mnwe) {
fail("should be writeable");
} catch (MessageNotReadableException mnre) {
fail("should be readable");
}
}
public void testReadOnlyBody() throws JMSException {
ActiveMQTextMessage textMessage = new ActiveMQTextMessage();
textMessage.setText("test");
textMessage.setReadOnlyBody(true);
try {
textMessage.getText();
} catch (MessageNotReadableException e) {
fail("should be readable");
}
try {
textMessage.setText("test");
fail("should throw exception");
} catch (MessageNotWriteableException mnwe) {
}
}
@Override
public boolean readBoolean() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Boolean) {
position++;
return ((Boolean) value).booleanValue();
} else if (value instanceof String) {
boolean result = Boolean.valueOf((String) value).booleanValue();
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public short readShort() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Byte) {
position++;
return ((Byte) value).shortValue();
} else if (value instanceof Short) {
position++;
return ((Short) value).shortValue();
} else if (value instanceof String) {
short result = Short.parseShort((String) value);
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public int readInt() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Byte) {
position++;
return ((Byte) value).intValue();
} else if (value instanceof Short) {
position++;
return ((Short) value).intValue();
} else if (value instanceof Integer) {
position++;
return ((Integer) value).intValue();
} else if (value instanceof String) {
int result = Integer.parseInt((String) value);
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public long readLong() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Byte) {
position++;
return ((Byte) value).longValue();
} else if (value instanceof Short) {
position++;
return ((Short) value).longValue();
} else if (value instanceof Integer) {
position++;
return ((Integer) value).longValue();
} else if (value instanceof Long) {
position++;
return ((Long) value).longValue();
} else if (value instanceof String) {
long result = Long.parseLong((String) value);
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public double readDouble() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
throw new NullPointerException("Value is null");
} else if (value instanceof Float) {
position++;
return ((Float) value).doubleValue();
} else if (value instanceof Double) {
position++;
return ((Double) value).doubleValue();
} else if (value instanceof String) {
double result = Double.parseDouble((String) value);
position++;
return result;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public Object readObject() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
position++;
offset = 0;
return value;
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Test
public void testNewMessageIsWriteOnlyThrowsMNRE() throws Exception {
JmsStreamMessage streamMessage = factory.createStreamMessage();
try {
streamMessage.readBoolean();
fail("Expected exception to be thrown as message is not readable");
} catch (MessageNotReadableException mnre) {
// expected
}
}
/**
* Test that attempting to read bytes from a new message (without calling {@link BytesMessage#reset()} first) causes a
* {@link MessageNotReadableException} to be thrown due to being write-only.
*
* @throws Exception if an error occurs during the test.
*/
@Test(expected = MessageNotReadableException.class)
public void testNewBytesMessageThrowsMessageNotReadableOnReadBytes() throws Exception {
JmsBytesMessage bytesMessage = factory.createBytesMessage();
byte[] receivedBytes = new byte[1];
bytesMessage.readBytes(receivedBytes);
}
private JmsInboundMessageDispatch createEnvelopeWithMessageThatCannotReadPriority() throws JMSException {
JmsInboundMessageDispatch envelope = new JmsInboundMessageDispatch(sequence++);
JmsMessage message = Mockito.mock(JmsMessage.class);
Mockito.when(message.getJMSPriority()).thenThrow(new MessageNotReadableException("Message is not readable"));
envelope.setMessage(message);
return envelope;
}
/**
* Test before reset the message is not readable
*/
@Test(expected = MessageNotReadableException.class)
public void testReadable() throws JMSException {
when(mockSQSSession.createBytesMessage()).thenReturn(new SQSBytesMessage());
SQSBytesMessage msg = (SQSBytesMessage) mockSQSSession.createBytesMessage();
byte[] byteArray = new byte[] { 'a', 0, 34, 65 };
msg.writeBytes(byteArray);
msg.readInt();
}
protected void checkWriteOnlyBody() throws MessageNotReadableException {
if (!readOnlyBody) {
throw new MessageNotReadableException("Message body is write-only");
}
}
@Override
public String readString() throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object value = content.get(position);
offset = 0;
if (value == null) {
position++;
return null;
} else if (value instanceof Boolean) {
position++;
return ((Boolean) value).toString();
} else if (value instanceof Byte) {
position++;
return ((Byte) value).toString();
} else if (value instanceof Short) {
position++;
return ((Short) value).toString();
} else if (value instanceof Character) {
position++;
return ((Character) value).toString();
} else if (value instanceof Integer) {
position++;
return ((Integer) value).toString();
} else if (value instanceof Long) {
position++;
return ((Long) value).toString();
} else if (value instanceof Float) {
position++;
return ((Float) value).toString();
} else if (value instanceof Double) {
position++;
return ((Double) value).toString();
} else if (value instanceof String) {
position++;
return (String) value;
} else {
throw new MessageFormatException("Invalid conversion");
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Override
public int readBytes(final byte[] value) throws JMSException {
if (bodyWriteOnly) {
throw new MessageNotReadableException("The message body is writeonly");
}
try {
Object myObj = content.get(position);
if (myObj == null) {
throw new NullPointerException("Value is null");
} else if (!(myObj instanceof byte[])) {
throw new MessageFormatException("Invalid conversion");
}
byte[] obj = (byte[]) myObj;
if (obj.length == 0) {
position++;
offset = 0;
return 0;
}
if (offset >= obj.length) {
position++;
offset = 0;
return -1;
}
if (obj.length - offset < value.length) {
System.arraycopy(obj, offset, value, 0, obj.length);
position++;
offset = 0;
return obj.length - offset;
} else {
System.arraycopy(obj, offset, value, 0, value.length);
offset += value.length;
return value.length;
}
} catch (IndexOutOfBoundsException e) {
throw new MessageEOFException("");
}
}
@Message(id = 139014, value = "Message is write-only")
MessageNotReadableException messageNotReadable();
protected void checkWriteOnlyBody() throws MessageNotReadableException {
if (!readOnlyBody) {
throw new MessageNotReadableException("Message body is write-only");
}
}
void checkCanRead() throws JMSException {
if (bytes == null) {
throw new MessageNotReadableException("Message is not readable");
}
}
/**
* Test that attempting to call {@link BytesMessage#getBodyLength()} on a new message causes
* a {@link MessageNotReadableException} to be thrown due to being write-only.
*
* @throws Exception if an error occurs during the test.
*/
@Test(expected = MessageNotReadableException.class)
public void testGetBodyLengthOnNewMessageThrowsMessageNotReadableException() throws Exception {
JmsBytesMessage bytesMessage = factory.createBytesMessage();
bytesMessage.getBodyLength();
}