use of javax.jms.MessageNotReadableException in project activemq-artemis by apache.
the class MessageBodyTest method testStreamMessage.
@Test
public void testStreamMessage() throws Exception {
StreamMessage m = queueProducerSession.createStreamMessage();
// Some arbitrary values
boolean myBool = true;
byte myByte = -111;
short myShort = 15321;
int myInt = 0x71ab6c80;
long myLong = 0x20bf1e3fb6fa31dfL;
float myFloat = Float.MAX_VALUE - 23465;
double myDouble = Double.MAX_VALUE - 72387633;
String myString = "abcdef&^*&!^ghijkl\uD5E2\uCAC7\uD2BB\uB7DD\uB7C7\uB3A3\uBCE4\uB5A5";
char myChar = 'q';
byte[] myBytes = new byte[] { -23, 114, -126, -12, 74, 87 };
m.writeBoolean(myBool);
m.writeByte(myByte);
m.writeShort(myShort);
m.writeChar(myChar);
m.writeInt(myInt);
m.writeLong(myLong);
m.writeFloat(myFloat);
m.writeDouble(myDouble);
m.writeString(myString);
m.writeBytes(myBytes);
m.writeBytes(myBytes, 2, 3);
m.writeObject(new Boolean(myBool));
m.writeObject(new Byte(myByte));
m.writeObject(new Short(myShort));
m.writeObject(new Integer(myInt));
m.writeObject(new Long(myLong));
m.writeObject(new Float(myFloat));
m.writeObject(new Double(myDouble));
m.writeObject(myString);
m.writeObject(myBytes);
try {
m.writeObject(new Object());
ProxyAssertSupport.fail();
} catch (MessageFormatException e) {
}
// Reading should not be possible when message is read-write
try {
m.readBoolean();
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotReadableException e) {
}
try {
m.readShort();
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotReadableException e) {
}
try {
m.readChar();
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotReadableException e) {
}
try {
m.readInt();
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotReadableException e) {
}
try {
m.readLong();
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotReadableException e) {
}
try {
m.readFloat();
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotReadableException e) {
}
try {
m.readDouble();
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotReadableException e) {
}
try {
byte[] bytes = new byte[333];
m.readBytes(bytes);
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotReadableException e) {
}
queueProducer.send(m);
StreamMessage m2 = (StreamMessage) queueConsumer.receive(2000);
ProxyAssertSupport.assertEquals(myBool, m2.readBoolean());
ProxyAssertSupport.assertEquals(myByte, m2.readByte());
ProxyAssertSupport.assertEquals(myShort, m2.readShort());
ProxyAssertSupport.assertEquals(myChar, m2.readChar());
ProxyAssertSupport.assertEquals(myInt, m2.readInt());
ProxyAssertSupport.assertEquals(myLong, m2.readLong());
ProxyAssertSupport.assertEquals(myFloat, m2.readFloat(), 0);
ProxyAssertSupport.assertEquals(myDouble, m2.readDouble(), 0);
ProxyAssertSupport.assertEquals(myString, m2.readString());
byte[] bytes = new byte[6];
int ret = m2.readBytes(bytes);
ProxyAssertSupport.assertEquals(6, ret);
Assert.assertArrayEquals(myBytes, bytes);
ret = m2.readBytes(bytes);
ProxyAssertSupport.assertEquals(-1, ret);
byte[] bytes2 = new byte[3];
ret = m2.readBytes(bytes2);
ProxyAssertSupport.assertEquals(3, ret);
ProxyAssertSupport.assertEquals(myBytes[2], bytes2[0]);
ProxyAssertSupport.assertEquals(myBytes[3], bytes2[1]);
ProxyAssertSupport.assertEquals(myBytes[4], bytes2[2]);
ret = m2.readBytes(bytes2);
ProxyAssertSupport.assertEquals(-1, ret);
ProxyAssertSupport.assertEquals(myBool, m2.readBoolean());
ProxyAssertSupport.assertEquals(myByte, m2.readByte());
ProxyAssertSupport.assertEquals(myShort, m2.readShort());
ProxyAssertSupport.assertEquals(myInt, m2.readInt());
ProxyAssertSupport.assertEquals(myLong, m2.readLong());
ProxyAssertSupport.assertEquals(myFloat, m2.readFloat(), 0);
ProxyAssertSupport.assertEquals(myDouble, m2.readDouble(), 0);
ProxyAssertSupport.assertEquals(myString, m2.readString());
bytes = new byte[6];
ret = m2.readBytes(bytes);
ProxyAssertSupport.assertEquals(6, ret);
Assert.assertArrayEquals(myBytes, bytes);
ret = m2.readBytes(bytes);
ProxyAssertSupport.assertEquals(-1, ret);
// Try and read past the end of the stream
try {
m2.readBoolean();
ProxyAssertSupport.fail();
} catch (MessageEOFException e) {
}
try {
m2.readByte();
ProxyAssertSupport.fail();
} catch (MessageEOFException e) {
}
try {
m2.readChar();
ProxyAssertSupport.fail();
} catch (MessageEOFException e) {
}
try {
m2.readDouble();
ProxyAssertSupport.fail();
} catch (MessageEOFException e) {
}
try {
m2.readFloat();
ProxyAssertSupport.fail();
} catch (MessageEOFException e) {
}
try {
m2.readInt();
ProxyAssertSupport.fail();
} catch (MessageEOFException e) {
}
try {
m2.readLong();
ProxyAssertSupport.fail();
} catch (MessageEOFException e) {
}
try {
m2.readShort();
ProxyAssertSupport.fail();
} catch (MessageEOFException e) {
}
// Message should not be writable in read-only mode
try {
m2.writeBoolean(myBool);
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotWriteableException e) {
}
try {
m2.writeByte(myByte);
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotWriteableException e) {
}
try {
m2.writeShort(myShort);
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotWriteableException e) {
}
try {
m2.writeChar(myChar);
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotWriteableException e) {
}
try {
m2.writeInt(myInt);
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotWriteableException e) {
}
try {
m2.writeLong(myLong);
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotWriteableException e) {
}
try {
m2.writeFloat(myFloat);
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotWriteableException e) {
}
try {
m2.writeDouble(myDouble);
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotWriteableException e) {
}
try {
m2.writeBytes(myBytes);
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotWriteableException e) {
}
try {
m2.writeObject(myString);
ProxyAssertSupport.fail();
} catch (javax.jms.MessageNotWriteableException e) {
}
m2.reset();
// check we go back to the beginning
ProxyAssertSupport.assertEquals(myBool, m2.readBoolean());
ProxyAssertSupport.assertEquals(myByte, m2.readByte());
ProxyAssertSupport.assertEquals(myShort, m2.readShort());
ProxyAssertSupport.assertEquals(myChar, m2.readChar());
ProxyAssertSupport.assertEquals(myInt, m2.readInt());
ProxyAssertSupport.assertEquals(myLong, m2.readLong());
ProxyAssertSupport.assertEquals(myFloat, m2.readFloat(), 0);
ProxyAssertSupport.assertEquals(myDouble, m2.readDouble(), 0);
ProxyAssertSupport.assertEquals(myString, m2.readString());
m2.clearBody();
try {
// Should now be write only
m2.readBoolean();
ProxyAssertSupport.fail();
} catch (MessageNotReadableException e) {
}
m2.writeBoolean(myBool);
m2.reset();
ProxyAssertSupport.assertEquals(myBool, m2.readBoolean());
try {
m2.readBoolean();
ProxyAssertSupport.fail();
} catch (MessageEOFException e) {
}
// Test that changing the received message doesn't affect the sent message
m.reset();
ProxyAssertSupport.assertEquals(myBool, m.readBoolean());
ProxyAssertSupport.assertEquals(myByte, m.readByte());
ProxyAssertSupport.assertEquals(myShort, m.readShort());
ProxyAssertSupport.assertEquals(myChar, m.readChar());
ProxyAssertSupport.assertEquals(myInt, m.readInt());
ProxyAssertSupport.assertEquals(myLong, m.readLong());
ProxyAssertSupport.assertEquals(myFloat, m.readFloat(), 0);
ProxyAssertSupport.assertEquals(myDouble, m.readDouble(), 0);
ProxyAssertSupport.assertEquals(myString, m.readString());
// Should be diffent object instances after sending *even* if in same JVM
ProxyAssertSupport.assertFalse(m == m2);
}
use of javax.jms.MessageNotReadableException in project activemq-artemis by apache.
the class SimpleJMSBytesMessage method checkRead.
// Public --------------------------------------------------------
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
// Private -------------------------------------------------------
/**
* 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");
}
// read it
if (istream == null || m == null) {
istream = new ByteArrayInputStream(internalArray);
m = new DataInputStream(istream);
}
}
use of javax.jms.MessageNotReadableException in project activemq-artemis by apache.
the class SimpleJMSStreamMessage method readLong.
@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("");
}
}
use of javax.jms.MessageNotReadableException in project activemq-artemis by apache.
the class SimpleJMSStreamMessage method readChar.
@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("");
}
}
use of javax.jms.MessageNotReadableException in project activemq-artemis by apache.
the class SimpleJMSStreamMessage method readDouble.
@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("");
}
}
Aggregations