1 package com.ubermq.jms.common.datagram.impl; 2 3 import com.ubermq.jms.common.datagram.*; 4 import com.ubermq.kernel.*; 5 import junit.framework.*; 6 7 import java.nio.ByteBuffer ; 8 import java.util.Arrays ; 9 10 public class DatagramTestCase 11 extends TestCase 12 { 13 public static TestSuite suite() { 14 return new TestSuite(DatagramTestCase.class); 15 } 16 17 public DatagramTestCase(String sz) { 18 super(sz); 19 } 20 21 private ByteBuffer theBuffer; 22 private byte[] someBytes, moreBytes; 23 24 public void setUp() 25 { 26 theBuffer = ByteBuffer.allocateDirect(100000); 27 someBytes = new byte[512]; 28 moreBytes = new byte[32768]; 29 30 Arrays.fill(someBytes, (byte)0xcc); 31 Arrays.fill(moreBytes, (byte)0xff); 32 } 33 34 public void tearDown() 35 { 36 theBuffer = null; 37 } 38 39 private IDatagram inAndOut(IDatagram d, 40 ByteBuffer bb) 41 throws Exception 42 { 43 bb.clear(); 44 d.outgoing(bb); 45 bb.flip(); 46 47 IDatagram out = ((IDatagram)d.getClass().newInstance()); 48 out.incoming(bb); 49 bb.rewind(); 50 return out; 51 } 52 53 public void theBasics(IDatagram d) 54 throws Exception 55 { 56 d.setDatagramFlagBits(0xeaeaeaea); 57 Assert.assertEquals(d.getDatagramFlags(), 0xeaeaeaea); 58 d.unsetDatagramFlagBits(0xeaeaeaea); 59 Assert.assertEquals(d.getDatagramFlags(), 0); 60 } 61 62 public void testAck() 63 throws Exception 64 { 65 IAckDatagram ad = new AckDatagram(true); 66 theBasics(ad); 67 Assert.assertTrue(ad.isNegativeAck()); 68 Assert.assertTrue(ad.getAckMessageId() == null); 69 70 ad = (IAckDatagram)inAndOut(ad, theBuffer); 71 72 Assert.assertTrue(ad.isNegativeAck()); 73 Assert.assertTrue(ad.getAckMessageId() == null); 74 75 ad = new AckDatagram(new MessageId(5, 6), false); 76 Assert.assertTrue(!ad.isNegativeAck()); 77 Assert.assertEquals(ad.getAckMessageId().getSenderId(), 5); 78 Assert.assertEquals(ad.getAckMessageId().getSequence(), 6); 79 80 ad = (IAckDatagram)inAndOut(ad, theBuffer); 81 82 Assert.assertTrue(!ad.isNegativeAck()); 83 Assert.assertEquals(ad.getAckMessageId().getSenderId(), 5); 84 Assert.assertEquals(ad.getAckMessageId().getSequence(), 6); 85 } 86 87 public void testControl() 88 { 89 90 } 91 92 public void testMessage() 93 throws Exception 94 { 95 IMessageDatagram md = new MessageDatagram("a topic"); 96 theBasics(md); 97 Assert.assertEquals(md.getTopicName(), "a topic"); 98 99 md = new MessageDatagram(); 100 md.setTopicName("the topic"); 101 Assert.assertEquals(md.getTopicName(), "the topic"); 102 103 md.setSenderId(900); 105 md.setSequence(1); 106 Assert.assertEquals(md.getSenderId(), 900); 107 Assert.assertEquals(md.getSequence(), 1); 108 109 md.setStandardProperty(IMessageDatagram.STDPROP_CORRELATIONID, someBytes); 111 md.setStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE, new Integer (3)); 112 md.setStandardProperty(IMessageDatagram.STDPROP_MSGTYPE, new Integer (5)); 113 md.setStandardProperty(IMessageDatagram.STDPROP_PRIORITY, new Integer (8)); 114 md.setStandardProperty(IMessageDatagram.STDPROP_REDELIVERY, Boolean.TRUE); 115 md.setStandardProperty(IMessageDatagram.STDPROP_REPLYTO, "ReplyTo"); 116 md.setStandardProperty(IMessageDatagram.STDPROP_TIMESTAMP, new Long (42L)); 117 md.setStandardProperty(IMessageDatagram.STDPROP_TTL, new Integer (1000)); 118 md.setStandardProperty(IMessageDatagram.STDPROP_BODY, moreBytes); 119 120 md.setCustomProperty("property1", someBytes); 122 md.setCustomProperty("property2", "Hello"); 123 124 Assert.assertTrue(Arrays.equals((byte[])md.getStandardProperty(IMessageDatagram.STDPROP_CORRELATIONID), someBytes)); 126 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE), new Integer (3)); 127 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_MSGTYPE), new Integer (5)); 128 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_PRIORITY), new Integer (8)); 129 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_REDELIVERY), Boolean.TRUE); 130 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_REPLYTO), "ReplyTo"); 131 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_TIMESTAMP), new Long (42L)); 132 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_TTL), new Integer (1000)); 133 Assert.assertTrue(Arrays.equals((byte[])md.getStandardProperty(IMessageDatagram.STDPROP_BODY), moreBytes)); 134 135 Assert.assertEquals(md.getCustomProperty("property1"), someBytes); 137 Assert.assertEquals(md.getCustomProperty("property2"), "Hello"); 138 139 md = (IMessageDatagram)inAndOut(md, theBuffer); 141 142 Assert.assertTrue(Arrays.equals((byte[])md.getStandardProperty(IMessageDatagram.STDPROP_CORRELATIONID), someBytes)); 144 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE), new Integer (3)); 145 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_MSGTYPE), new Integer (5)); 146 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_PRIORITY), new Integer (8)); 147 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_REDELIVERY), Boolean.TRUE); 148 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_REPLYTO), "ReplyTo"); 149 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_TIMESTAMP), new Long (42L)); 150 Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_TTL), new Integer (1000)); 151 Assert.assertTrue(Arrays.equals((byte[])md.getStandardProperty(IMessageDatagram.STDPROP_BODY), moreBytes)); 152 153 Assert.assertTrue(Arrays.equals((byte[])md.getCustomProperty("property1"), someBytes)); 155 Assert.assertEquals(md.getCustomProperty("property2"), "Hello"); 156 157 ServerMessageDatagram smd = new ServerMessageDatagram(); 159 smd.incoming(theBuffer); 160 161 Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE), new Integer (3)); 164 Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_MSGTYPE), new Integer (5)); 165 Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_PRIORITY), new Integer (8)); 166 Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_REDELIVERY), Boolean.TRUE); 167 Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_TIMESTAMP), new Long (42L)); 168 Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_TTL), new Integer (1000)); 169 170 Assert.assertNotNull(smd.getStandardProperty(IMessageDatagram.STDPROP_BODY)); 172 Assert.assertTrue(!smd.hasProcessedProperties()); 173 174 try { 176 smd.setTopicName("BOGUS"); 177 Assert.assertTrue(false); 178 } catch(UnsupportedOperationException e) { 179 Assert.assertTrue(true); 180 } 181 182 Assert.assertTrue(Arrays.equals((byte[])smd.getCustomProperty("property1"), someBytes)); 184 Assert.assertEquals(smd.getCustomProperty("property2"), "Hello"); 185 Assert.assertTrue(Arrays.equals((byte[])smd.getStandardProperty(IMessageDatagram.STDPROP_CORRELATIONID), someBytes)); 186 Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_REPLYTO), "ReplyTo"); 187 Assert.assertTrue(smd.hasProcessedProperties()); 188 189 IMessageDatagram newmd = new MessageDatagram("blah blah topic"); 192 newmd.prepareToSend(1, 1, 100); 193 194 theBuffer.clear(); 195 newmd = (IMessageDatagram)inAndOut(newmd, theBuffer); 196 smd.incoming(theBuffer); 197 198 Assert.assertEquals(smd.getTopicName(), "blah blah topic"); 200 Assert.assertEquals(((Number )smd.getStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE)).intValue(), 1); 201 Assert.assertEquals(((Number )smd.getStandardProperty(IMessageDatagram.STDPROP_PRIORITY)).intValue(), 1); 202 Assert.assertEquals(((Number )smd.getStandardProperty(IMessageDatagram.STDPROP_TTL)).intValue(), 100); 203 Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_REDELIVERY), Boolean.FALSE); 204 Assert.assertTrue(!smd.hasProcessedProperties()); 205 } 206 207 public void testFactories() 208 { 209 doFactory(DatagramFactory.getInstance()); 210 doFactory(ServerDatagramFactory.getInstance()); 211 212 doFactory(new DelimitedDatagramFactory( 213 new byte[] { (byte)0xee, (byte)0xca, (byte)0xac, (byte)0xff }, 214 new byte[] { (byte)0xff, (byte)0xac, (byte)0xca, (byte)0xee } )); 215 216 doFactory(new DelimitedDatagramFactory( 217 new byte[] { (byte)0xee, (byte)0xca }, 218 new byte[] { (byte)0xff, (byte)0xac, (byte)0xca, (byte)0xee } )); 219 220 doFactory(new DelimitedDatagramFactory( 221 new byte[] { (byte)0xff, (byte)0xac, (byte)0xca })); 222 } 223 224 public void doFactory(IDatagramFactory f) 225 { 226 IMessageDatagram md = new MessageDatagram("a topic"); 228 IControlDatagram cd = new ControlDatagram(ControlDatagram.CONTROL_NOOP, new ControlDatagram.NoopDatagramImpl()); 229 IAckDatagram ad = new AckDatagram(); 230 231 ByteBuffer bb = ByteBuffer.allocateDirect(1024*1024); 233 output(f, bb, md); 234 output(f, bb, cd); 235 output(f, bb, ad); 236 237 bb.flip(); 239 com.ubermq.util.Utility.outputBuffer(bb); 240 System.out.println("\n\n"); 241 242 try 244 { 245 Assert.assertEquals(md, read(f, bb)); 246 Assert.assertEquals(cd, read(f, bb)); 247 Assert.assertEquals(ad, read(f, bb)); 248 } 249 catch (java.io.IOException e) {} 250 251 } 253 254 private void output(IDatagramFactory f, ByteBuffer bb, IDatagram d) 255 { 256 ByteBuffer newb = bb.slice(); 257 f.outgoing(newb, d); 258 bb.position(bb.position() + newb.position()); 259 } 260 261 private IDatagram read(IDatagramFactory f, ByteBuffer bb) 262 throws java.io.IOException 263 { 264 int l; 265 IDatagram d; 266 267 l = f.frame(bb); 268 if (l > bb.limit()) { 269 return null; 270 } 271 272 d = f.incoming((ByteBuffer )bb.slice().limit(l)); 273 bb.position(bb.position() + l); 274 275 return d; 276 } 277 } 278 279 280 | Popular Tags |