package com.ubermq.jms.common.datagram.impl;
import com.ubermq.jms.common.datagram.*;
import com.ubermq.kernel.*;
import junit.framework.*;
import java.nio.ByteBuffer;
import java.util.Arrays;
public class DatagramTestCase
extends TestCase
{
public static TestSuite suite() {
return new TestSuite(DatagramTestCase.class);
}
public DatagramTestCase(String sz) {
super(sz);
}
private ByteBuffer theBuffer;
private byte[] someBytes, moreBytes;
public void setUp()
{
theBuffer = ByteBuffer.allocateDirect(100000);
someBytes = new byte[512];
moreBytes = new byte[32768];
Arrays.fill(someBytes, (byte)0xcc);
Arrays.fill(moreBytes, (byte)0xff);
}
public void tearDown()
{
theBuffer = null;
}
private IDatagram inAndOut(IDatagram d,
ByteBuffer bb)
throws Exception
{
bb.clear();
d.outgoing(bb);
bb.flip();
IDatagram out = ((IDatagram)d.getClass().newInstance());
out.incoming(bb);
bb.rewind();
return out;
}
public void theBasics(IDatagram d)
throws Exception
{
d.setDatagramFlagBits(0xeaeaeaea);
Assert.assertEquals(d.getDatagramFlags(), 0xeaeaeaea);
d.unsetDatagramFlagBits(0xeaeaeaea);
Assert.assertEquals(d.getDatagramFlags(), 0);
}
public void testAck()
throws Exception
{
IAckDatagram ad = new AckDatagram(true);
theBasics(ad);
Assert.assertTrue(ad.isNegativeAck());
Assert.assertTrue(ad.getAckMessageId() == null);
ad = (IAckDatagram)inAndOut(ad, theBuffer);
Assert.assertTrue(ad.isNegativeAck());
Assert.assertTrue(ad.getAckMessageId() == null);
ad = new AckDatagram(new MessageId(5, 6), false);
Assert.assertTrue(!ad.isNegativeAck());
Assert.assertEquals(ad.getAckMessageId().getSenderId(), 5);
Assert.assertEquals(ad.getAckMessageId().getSequence(), 6);
ad = (IAckDatagram)inAndOut(ad, theBuffer);
Assert.assertTrue(!ad.isNegativeAck());
Assert.assertEquals(ad.getAckMessageId().getSenderId(), 5);
Assert.assertEquals(ad.getAckMessageId().getSequence(), 6);
}
public void testControl()
{
}
public void testMessage()
throws Exception
{
IMessageDatagram md = new MessageDatagram("a topic");
theBasics(md);
Assert.assertEquals(md.getTopicName(), "a topic");
md = new MessageDatagram();
md.setTopicName("the topic");
Assert.assertEquals(md.getTopicName(), "the topic");
// set some properties
md.setSenderId(900);
md.setSequence(1);
Assert.assertEquals(md.getSenderId(), 900);
Assert.assertEquals(md.getSequence(), 1);
// set some standard props
md.setStandardProperty(IMessageDatagram.STDPROP_CORRELATIONID, someBytes);
md.setStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE, new Integer(3));
md.setStandardProperty(IMessageDatagram.STDPROP_MSGTYPE, new Integer(5));
md.setStandardProperty(IMessageDatagram.STDPROP_PRIORITY, new Integer(8));
md.setStandardProperty(IMessageDatagram.STDPROP_REDELIVERY, Boolean.TRUE);
md.setStandardProperty(IMessageDatagram.STDPROP_REPLYTO, "ReplyTo");
md.setStandardProperty(IMessageDatagram.STDPROP_TIMESTAMP, new Long(42L));
md.setStandardProperty(IMessageDatagram.STDPROP_TTL, new Integer(1000));
md.setStandardProperty(IMessageDatagram.STDPROP_BODY, moreBytes);
// custom props
md.setCustomProperty("property1", someBytes);
md.setCustomProperty("property2", "Hello");
// get them & verify
Assert.assertTrue(Arrays.equals((byte[])md.getStandardProperty(IMessageDatagram.STDPROP_CORRELATIONID), someBytes));
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE), new Integer(3));
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_MSGTYPE), new Integer(5));
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_PRIORITY), new Integer(8));
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_REDELIVERY), Boolean.TRUE);
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_REPLYTO), "ReplyTo");
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_TIMESTAMP), new Long(42L));
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_TTL), new Integer(1000));
Assert.assertTrue(Arrays.equals((byte[])md.getStandardProperty(IMessageDatagram.STDPROP_BODY), moreBytes));
// set a few custom props
Assert.assertEquals(md.getCustomProperty("property1"), someBytes);
Assert.assertEquals(md.getCustomProperty("property2"), "Hello");
// send to disk, and bring back, and redo the tests.
md = (IMessageDatagram)inAndOut(md, theBuffer);
// get them & verify
Assert.assertTrue(Arrays.equals((byte[])md.getStandardProperty(IMessageDatagram.STDPROP_CORRELATIONID), someBytes));
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE), new Integer(3));
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_MSGTYPE), new Integer(5));
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_PRIORITY), new Integer(8));
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_REDELIVERY), Boolean.TRUE);
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_REPLYTO), "ReplyTo");
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_TIMESTAMP), new Long(42L));
Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_TTL), new Integer(1000));
Assert.assertTrue(Arrays.equals((byte[])md.getStandardProperty(IMessageDatagram.STDPROP_BODY), moreBytes));
// get the few custom props
Assert.assertTrue(Arrays.equals((byte[])md.getCustomProperty("property1"), someBytes));
Assert.assertEquals(md.getCustomProperty("property2"), "Hello");
// NOW - test "server," or readonly message grams.
ServerMessageDatagram smd = new ServerMessageDatagram();
smd.incoming(theBuffer);
// the server gram should represent the gram we just sent out.. we can do the same tests,
// except for the body.
Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE), new Integer(3));
Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_MSGTYPE), new Integer(5));
Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_PRIORITY), new Integer(8));
Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_REDELIVERY), Boolean.TRUE);
Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_TIMESTAMP), new Long(42L));
Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_TTL), new Integer(1000));
// make sure the body is not null and that the props have not been processed.
Assert.assertNotNull(smd.getStandardProperty(IMessageDatagram.STDPROP_BODY));
Assert.assertTrue(!smd.hasProcessedProperties());
// try to set topic name - this should fail.
try {
smd.setTopicName("BOGUS");
Assert.assertTrue(false);
} catch(UnsupportedOperationException e) {
Assert.assertTrue(true);
}
// get the few custom props. this should work, but after a delay
Assert.assertTrue(Arrays.equals((byte[])smd.getCustomProperty("property1"), someBytes));
Assert.assertEquals(smd.getCustomProperty("property2"), "Hello");
Assert.assertTrue(Arrays.equals((byte[])smd.getStandardProperty(IMessageDatagram.STDPROP_CORRELATIONID), someBytes));
Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_REPLYTO), "ReplyTo");
Assert.assertTrue(smd.hasProcessedProperties());
// the server gram should collect no state, so
// let's make sure that assumption holds
IMessageDatagram newmd = new MessageDatagram("blah blah topic");
newmd.prepareToSend(1, 1, 100);
theBuffer.clear();
newmd = (IMessageDatagram)inAndOut(newmd, theBuffer);
smd.incoming(theBuffer);
// ok verify
Assert.assertEquals(smd.getTopicName(), "blah blah topic");
Assert.assertEquals(((Number)smd.getStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE)).intValue(), 1);
Assert.assertEquals(((Number)smd.getStandardProperty(IMessageDatagram.STDPROP_PRIORITY)).intValue(), 1);
Assert.assertEquals(((Number)smd.getStandardProperty(IMessageDatagram.STDPROP_TTL)).intValue(), 100);
Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_REDELIVERY), Boolean.FALSE);
Assert.assertTrue(!smd.hasProcessedProperties());
}
public void testFactories()
{
doFactory(DatagramFactory.getInstance());
doFactory(ServerDatagramFactory.getInstance());
doFactory(new DelimitedDatagramFactory(
new byte[] { (byte)0xee, (byte)0xca, (byte)0xac, (byte)0xff },
new byte[] { (byte)0xff, (byte)0xac, (byte)0xca, (byte)0xee } ));
doFactory(new DelimitedDatagramFactory(
new byte[] { (byte)0xee, (byte)0xca },
new byte[] { (byte)0xff, (byte)0xac, (byte)0xca, (byte)0xee } ));
doFactory(new DelimitedDatagramFactory(
new byte[] { (byte)0xff, (byte)0xac, (byte)0xca }));
}
public void doFactory(IDatagramFactory f)
{
// make some grams.
IMessageDatagram md = new MessageDatagram("a topic");
IControlDatagram cd = new ControlDatagram(ControlDatagram.CONTROL_NOOP, new ControlDatagram.NoopDatagramImpl());
IAckDatagram ad = new AckDatagram();
// make a buffer
ByteBuffer bb = ByteBuffer.allocateDirect(1024*1024);
output(f, bb, md);
output(f, bb, cd);
output(f, bb, ad);
// ok flip the buffer
bb.flip();
com.ubermq.util.Utility.outputBuffer(bb);
System.out.println("\n\n");
// now read them in
try
{
Assert.assertEquals(md, read(f, bb));
Assert.assertEquals(cd, read(f, bb));
Assert.assertEquals(ad, read(f, bb));
}
catch (java.io.IOException e) {}
// goodbye.
}
private void output(IDatagramFactory f, ByteBuffer bb, IDatagram d)
{
ByteBuffer newb = bb.slice();
f.outgoing(newb, d);
bb.position(bb.position() + newb.position());
}
private IDatagram read(IDatagramFactory f, ByteBuffer bb)
throws java.io.IOException
{
int l;
IDatagram d;
l = f.frame(bb);
if (l > bb.limit()) {
return null;
}
d = f.incoming((ByteBuffer)bb.slice().limit(l));
bb.position(bb.position() + l);
return d;
}
}
|