byte[] originalBytes = baos.toByteArray();
byte[] compressedBytes = compressor.compressByteArray(originalBytes);
assertTrue("Source compressedBytes should be compressed", compressor.isCompressed(compressedBytes));
// now create a BytesMessage from the compressed byte[]
AbstractJmsTransformer trans = new SessionEnabledObjectToJMSMessage(session);
trans.setReturnClass(BytesMessage.class);
Object result2 = trans.transform(compressedBytes);
assertTrue("Transformed object should be a Bytes message", result2 instanceof BytesMessage);
// check whether the BytesMessage contains the compressed bytes
BytesMessage intermediate = (BytesMessage)result2;
intermediate.reset();
byte[] intermediateBytes = new byte[(int)(intermediate.getBodyLength())];
int intermediateSize = intermediate.readBytes(intermediateBytes);
assertTrue("Intermediate bytes must be compressed", compressor.isCompressed(intermediateBytes));
assertTrue("Intermediate bytes must be equal to compressed source", Arrays.equals(compressedBytes,
intermediateBytes));
assertEquals("Intermediate bytes and compressed source must have same size", compressedBytes.length,
intermediateSize);
// now test the other way around: getting the byte[] from a manually created
// BytesMessage
AbstractJmsTransformer trans2 = new JMSMessageToObject();
trans2.setReturnClass(byte[].class);
BytesMessage bMsg = session.createBytesMessage();
bMsg.writeBytes(compressedBytes);
Object result = trans2.transform(bMsg);
assertTrue("Transformed object should be a byte[]", result instanceof byte[]);
assertTrue("Result should be compressed", compressor.isCompressed((byte[])result));
assertTrue("Source and result should be equal", Arrays.equals(compressedBytes, (byte[])result));
}