KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > common > datagram > impl > DatagramTestCase


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 JavaDoc;
8 import java.util.Arrays JavaDoc;
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 JavaDoc sz) {
18         super(sz);
19     }
20
21     private ByteBuffer JavaDoc 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 JavaDoc bb)
41         throws Exception JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
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         // set some properties
104
md.setSenderId(900);
105         md.setSequence(1);
106         Assert.assertEquals(md.getSenderId(), 900);
107         Assert.assertEquals(md.getSequence(), 1);
108
109         // set some standard props
110
md.setStandardProperty(IMessageDatagram.STDPROP_CORRELATIONID, someBytes);
111         md.setStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE, new Integer JavaDoc(3));
112         md.setStandardProperty(IMessageDatagram.STDPROP_MSGTYPE, new Integer JavaDoc(5));
113         md.setStandardProperty(IMessageDatagram.STDPROP_PRIORITY, new Integer JavaDoc(8));
114         md.setStandardProperty(IMessageDatagram.STDPROP_REDELIVERY, Boolean.TRUE);
115         md.setStandardProperty(IMessageDatagram.STDPROP_REPLYTO, "ReplyTo");
116         md.setStandardProperty(IMessageDatagram.STDPROP_TIMESTAMP, new Long JavaDoc(42L));
117         md.setStandardProperty(IMessageDatagram.STDPROP_TTL, new Integer JavaDoc(1000));
118         md.setStandardProperty(IMessageDatagram.STDPROP_BODY, moreBytes);
119
120         // custom props
121
md.setCustomProperty("property1", someBytes);
122         md.setCustomProperty("property2", "Hello");
123
124         // get them & verify
125
Assert.assertTrue(Arrays.equals((byte[])md.getStandardProperty(IMessageDatagram.STDPROP_CORRELATIONID), someBytes));
126         Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE), new Integer JavaDoc(3));
127         Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_MSGTYPE), new Integer JavaDoc(5));
128         Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_PRIORITY), new Integer JavaDoc(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 JavaDoc(42L));
132         Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_TTL), new Integer JavaDoc(1000));
133         Assert.assertTrue(Arrays.equals((byte[])md.getStandardProperty(IMessageDatagram.STDPROP_BODY), moreBytes));
134
135         // set a few custom props
136
Assert.assertEquals(md.getCustomProperty("property1"), someBytes);
137         Assert.assertEquals(md.getCustomProperty("property2"), "Hello");
138
139         // send to disk, and bring back, and redo the tests.
140
md = (IMessageDatagram)inAndOut(md, theBuffer);
141
142         // get them & verify
143
Assert.assertTrue(Arrays.equals((byte[])md.getStandardProperty(IMessageDatagram.STDPROP_CORRELATIONID), someBytes));
144         Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE), new Integer JavaDoc(3));
145         Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_MSGTYPE), new Integer JavaDoc(5));
146         Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_PRIORITY), new Integer JavaDoc(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 JavaDoc(42L));
150         Assert.assertEquals(md.getStandardProperty(IMessageDatagram.STDPROP_TTL), new Integer JavaDoc(1000));
151         Assert.assertTrue(Arrays.equals((byte[])md.getStandardProperty(IMessageDatagram.STDPROP_BODY), moreBytes));
152
153         // get the few custom props
154
Assert.assertTrue(Arrays.equals((byte[])md.getCustomProperty("property1"), someBytes));
155         Assert.assertEquals(md.getCustomProperty("property2"), "Hello");
156
157         // NOW - test "server," or readonly message grams.
158
ServerMessageDatagram smd = new ServerMessageDatagram();
159         smd.incoming(theBuffer);
160
161         // the server gram should represent the gram we just sent out.. we can do the same tests,
162
// except for the body.
163
Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE), new Integer JavaDoc(3));
164         Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_MSGTYPE), new Integer JavaDoc(5));
165         Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_PRIORITY), new Integer JavaDoc(8));
166         Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_REDELIVERY), Boolean.TRUE);
167         Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_TIMESTAMP), new Long JavaDoc(42L));
168         Assert.assertEquals(smd.getStandardProperty(IMessageDatagram.STDPROP_TTL), new Integer JavaDoc(1000));
169
170         // make sure the body is not null and that the props have not been processed.
171
Assert.assertNotNull(smd.getStandardProperty(IMessageDatagram.STDPROP_BODY));
172         Assert.assertTrue(!smd.hasProcessedProperties());
173
174         // try to set topic name - this should fail.
175
try {
176             smd.setTopicName("BOGUS");
177             Assert.assertTrue(false);
178         } catch(UnsupportedOperationException JavaDoc e) {
179             Assert.assertTrue(true);
180         }
181
182         // get the few custom props. this should work, but after a delay
183
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         // the server gram should collect no state, so
190
// let's make sure that assumption holds
191
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         // ok verify
199
Assert.assertEquals(smd.getTopicName(), "blah blah topic");
200         Assert.assertEquals(((Number JavaDoc)smd.getStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE)).intValue(), 1);
201         Assert.assertEquals(((Number JavaDoc)smd.getStandardProperty(IMessageDatagram.STDPROP_PRIORITY)).intValue(), 1);
202         Assert.assertEquals(((Number JavaDoc)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         // make some grams.
227
IMessageDatagram md = new MessageDatagram("a topic");
228         IControlDatagram cd = new ControlDatagram(ControlDatagram.CONTROL_NOOP, new ControlDatagram.NoopDatagramImpl());
229         IAckDatagram ad = new AckDatagram();
230
231         // make a buffer
232
ByteBuffer JavaDoc bb = ByteBuffer.allocateDirect(1024*1024);
233         output(f, bb, md);
234         output(f, bb, cd);
235         output(f, bb, ad);
236
237         // ok flip the buffer
238
bb.flip();
239         com.ubermq.util.Utility.outputBuffer(bb);
240         System.out.println("\n\n");
241
242         // now read them in
243
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 JavaDoc e) {}
250
251         // goodbye.
252
}
253
254     private void output(IDatagramFactory f, ByteBuffer JavaDoc bb, IDatagram d)
255     {
256         ByteBuffer JavaDoc newb = bb.slice();
257         f.outgoing(newb, d);
258         bb.position(bb.position() + newb.position());
259     }
260
261     private IDatagram read(IDatagramFactory f, ByteBuffer JavaDoc bb)
262         throws java.io.IOException JavaDoc
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 JavaDoc)bb.slice().limit(l));
273         bb.position(bb.position() + l);
274
275         return d;
276     }
277 }
278
279
280
Popular Tags