@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(""); } }
void send(int count) throws JMSException { // create a publisher MessageProducer producer = _session.createProducer(_destination); for (int i = 0; i < count; i++) { BytesMessage msg = _session.createBytesMessage(); try { msg.readFloat(); Assert.fail("Message should not be readable"); } catch (MessageNotReadableException mnwe) { // normal execution } byte[] data = ("Message " + i).getBytes(); msg.writeBytes(data); messages.add(data); producer.send(msg); } }
/** * Tests that on creation a call to getBodyLength() throws an exception * if null was passed in during creation */ public void testNotReadableOnCreationWithNull() throws Exception { try { JMSBytesMessage bm = TestMessageHelper.newJMSBytesMessage(); bm.getBodyLength(); fail("expected exception did not occur"); } catch (MessageNotReadableException m) { // ok } catch (Exception e) { fail("expected MessageNotReadableException, got " + e); } }
/** * Tests that on creation a call to getBodyLength() throws an exception * if null was passed in during creation */ public void testNotReadableOnCreationWithNull() throws Exception { try { JMSStreamMessage bm = TestMessageHelper.newJMSStreamMessage(); bm.readByte(); fail("expected exception did not occur"); } catch (MessageNotReadableException m) { // ok } catch (Exception e) { fail("expected MessageNotReadableException, got " + e); } }
@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.peek(); assertEquals(9, envelope.getMessage().getJMSPriority()); queue.dequeueNoWait(); envelope = queue.peek(); try { envelope.getMessage().getJMSPriority(); fail("Unreadable priority message should sit at default level"); } catch (MessageNotReadableException mnre) {} queue.dequeueNoWait(); envelope = queue.peek(); assertEquals(1, envelope.getMessage().getJMSPriority()); queue.dequeueNoWait(); 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(""); } }
/** * Check the message is readable * * @throws javax.jms.JMSException when not readable */ private void checkRead() throws JMSException { if (bodyWriteOnly) { throw new MessageNotReadableException("readByte while the buffer is writeonly"); } // We have just received/reset() the message, and the client is trying to // read it if (istream == null || m == null) { istream = new ByteArrayInputStream(internalArray); m = new DataInputStream(istream); } }
public Object internalReadObject() throws JMSException { if (!bodyIsReadOnly) throw new MessageNotReadableException("Message body is write only"); if (readPos >= body.size()) throw new MessageEOFException("End of stream reached"); if (currentByteInputStream != null) throw new MessageFormatException("Cannot read another object before the end of the byte array"); return body.get(readPos++); }
private DataInputStream getInput() throws JMSException { if (!bodyIsReadOnly) throw new MessageNotReadableException("Message body is write-only"); if (input == null) { inputBuf = new ByteArrayInputStream(body != null ? body : new byte[0]); input = new DataInputStream(inputBuf); } return input; }
@Override public long getBodyLength() throws JMSException { if (!bodyIsReadOnly) throw new MessageNotReadableException("Message body is write-only"); return body != null ? body.length : 0; }
protected void checkReadable() throws MessageNotReadableException { if (!_readableMessage) { throw new MessageNotReadableException("You need to call reset() to make the message readable"); } }
protected byte readWireType() throws MessageFormatException, MessageEOFException, MessageNotReadableException { checkReadable(); checkAvailable(1); return _data.get(); }
@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; }
private void initializeReading() throws MessageNotReadableException { checkWriteOnlyBody(); if (stream == null) { stream = content; index = 0; bytes = null; remainingBytes = -1; } }
/** * 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 = 129014, value = "Message is write-only") MessageNotReadableException messageNotReadable();