KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > MessageTest


1 // $Id: MessageTest.java,v 1.1 2007/07/04 07:29:33 belaban Exp $
2

3 package org.jgroups.tests;
4
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8 import org.jgroups.Message;
9 import org.jgroups.conf.ClassConfigurator;
10 import org.jgroups.protocols.UdpHeader;
11 import org.jgroups.protocols.TpHeader;
12 import org.jgroups.protocols.PingHeader;
13 import org.jgroups.protocols.pbcast.NakAckHeader;
14 import org.jgroups.stack.IpAddress;
15 import org.jgroups.util.Range;
16 import org.jgroups.util.Util;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22
23
24
25 public class MessageTest extends TestCase {
26     Message m1, m2;
27
28
29     public MessageTest(String JavaDoc name) {
30         super(name);
31     }
32
33
34     public void testFlags() {
35         m1=new Message();
36         assertFalse(m1.isFlagSet(Message.OOB));
37         try {
38             m1.setFlag((byte)1002);
39             fail("1002 is not a byte value");
40         }
41         catch(IllegalArgumentException JavaDoc ex) {
42         }
43         assertEquals(0, m1.getFlags());
44     }
45
46     public void testFlags2() {
47         m1=new Message();
48         m1.setFlag(Message.OOB);
49         assertTrue(m1.isFlagSet(Message.OOB));
50         assertEquals((m1.getFlags() & Message.OOB), Message.OOB);
51         assertFalse(m1.isFlagSet(Message.LOW_PRIO));
52         assertFalse((m1.getFlags() & Message.LOW_PRIO) == Message.LOW_PRIO);
53     }
54
55
56     public void testBufferSize() throws Exception JavaDoc {
57         m1=new Message(null, null, "bela");
58         assertNotNull(m1.getRawBuffer());
59         assertNotNull(m1.getBuffer());
60         assertEquals(m1.getBuffer().length, m1.getLength());
61         byte[] new_buf={'m', 'i', 'c', 'h', 'e', 'l', 'l', 'e'};
62         m1.setBuffer(new_buf);
63         assertNotNull(m1.getRawBuffer());
64         assertNotNull(m1.getBuffer());
65         assertEquals(new_buf.length, m1.getLength());
66         assertEquals(m1.getBuffer().length, m1.getLength());
67     }
68
69     public void testBufferOffset() throws Exception JavaDoc {
70         byte[] buf={'b', 'e', 'l', 'a', 'b', 'a', 'n'};
71         m1=new Message(null, null, buf, 0, 4);
72         m2=new Message(null, null, buf, 4, 3);
73
74         byte[] b1, b2;
75
76         b1=new byte[m1.getLength()];
77         System.arraycopy(m1.getRawBuffer(), m1.getOffset(), b1, 0, m1.getLength());
78
79         b2=new byte[m2.getLength()];
80         System.arraycopy(m2.getRawBuffer(), m2.getOffset(), b2, 0, m2.getLength());
81
82         assertEquals(4, b1.length);
83         assertEquals(3, b2.length);
84     }
85
86
87     public void testSetBufferWithNullBuffer() {
88         byte[] buf={'b', 'e', 'l', 'a'};
89         m1=new Message();
90         m1.setBuffer(buf, 1, 2); // dummy data with non 0 oiffset and length
91
assertEquals(1, m1.getOffset());
92         assertEquals(2, m1.getLength());
93
94         m1.setBuffer(null, 1, 2); // dummy offset and length, is ignored
95
assertEquals(0, m1.getOffset());
96         assertEquals(0, m1.getLength());
97     }
98
99
100     public void testInvalidOffset() {
101         byte[] buf={'b', 'e', 'l', 'a', 'b', 'a', 'n'};
102
103         try {
104             m1=new Message(null, null, buf, -1, 4);
105             fail("we should not get here (offset is -1)");
106         }
107         catch(ArrayIndexOutOfBoundsException JavaDoc ex) {
108             assertTrue("correct: offset is invalid (caught correctly)", true);
109         }
110     }
111
112     public void testInvalidLength() {
113         byte[] buf={'b', 'e', 'l', 'a', 'b', 'a', 'n'};
114
115         try {
116             m1=new Message(null, null, buf, 3, 6);
117             fail("we should not get here (length is 9)");
118         }
119         catch(ArrayIndexOutOfBoundsException JavaDoc ex) {
120             assertTrue("correct: length is invalid (caught correctly)", true);
121         }
122     }
123
124     public void testGetRawBuffer() {
125         byte[] buf={'b', 'e', 'l', 'a', 'b', 'a', 'n'};
126         m1=new Message(null, null, buf, 0, 4);
127         m2=new Message(null, null, buf, 4, 3);
128
129         assertEquals(buf.length, m1.getRawBuffer().length);
130         assertEquals(4, m1.getBuffer().length);
131         assertEquals(4, m1.getLength());
132
133         assertEquals(buf.length, m2.getRawBuffer().length);
134         assertEquals(3, m2.getBuffer().length);
135         assertEquals(3, m2.getLength());
136     }
137
138     public void testSerialization() throws Exception JavaDoc {
139         byte[] buf={'b', 'e', 'l', 'a', 'b', 'a', 'n'};
140         byte[] tmp;
141         m1=new Message(null, null, buf, 0, 4);
142         m2=new Message(null, null, buf, 4, 3);
143
144
145         ByteArrayOutputStream JavaDoc output=new ByteArrayOutputStream JavaDoc();
146         ObjectOutputStream JavaDoc out=new ObjectOutputStream JavaDoc(output);
147         out.writeObject(m1);
148         output.close();
149         tmp=output.toByteArray();
150
151         ByteArrayInputStream JavaDoc input=new ByteArrayInputStream JavaDoc(tmp);
152         ObjectInputStream JavaDoc in=new ObjectInputStream JavaDoc(input);
153         Message m3, m4;
154
155         m3=(Message)in.readObject();
156         assertEquals(4, m3.getLength());
157         assertEquals(4, m3.getRawBuffer().length);
158         assertEquals(4, m3.getBuffer().length);
159         assertEquals(0, m3.getOffset());
160
161         output=new ByteArrayOutputStream JavaDoc();
162         out=new ObjectOutputStream JavaDoc(output);
163         out.writeObject(m2);
164         output.close();
165         tmp=output.toByteArray();
166
167         input=new ByteArrayInputStream JavaDoc(tmp);
168         in=new ObjectInputStream JavaDoc(input);
169         m4=(Message)in.readObject();
170         assertEquals(3, m4.getLength());
171         assertEquals(3, m4.getBuffer().length);
172         assertEquals(3, m4.getRawBuffer().length);
173         assertEquals(0, m4.getOffset());
174     }
175
176
177     public void testSetObject() {
178         String JavaDoc s1="Bela Ban";
179         m1=new Message(null, null, s1);
180         assertEquals(0, m1.getOffset());
181         assertEquals(m1.getBuffer().length, m1.getLength());
182         String JavaDoc s2=(String JavaDoc)m1.getObject();
183         assertEquals(s2, s1);
184     }
185
186
187
188     public void testCopy() {
189         m1=new Message(null, null, "Bela Ban");
190         m2=m1.copy();
191         assertEquals(m1.getOffset(), m2.getOffset());
192         assertEquals(m1.getLength(), m2.getLength());
193     }
194
195
196     public void testCopyWithOffset() {
197         byte[] buf={'b', 'e', 'l', 'a', 'b', 'a', 'n'};
198         m1=new Message(null, null, buf, 0, 4);
199         m2=new Message(null, null, buf, 4, 3);
200
201         Message m3, m4;
202         m3=m1.copy();
203         m4=m2.copy();
204
205         assertEquals(0, m3.getOffset());
206         assertEquals(4, m3.getLength());
207         assertEquals(4, m3.getBuffer().length);
208
209         assertEquals(4, m4.getOffset());
210         assertEquals(3, m4.getLength());
211         assertEquals(3, m4.getBuffer().length);
212     }
213
214     public void testComputeFragOffsets() {
215         Range r;
216         byte[] buf={0,1,2,3,4,5,6,7,8,9};
217         java.util.List JavaDoc retval=Util.computeFragOffsets(buf, 4);
218         System.out.println("list is " + retval);
219         assertEquals(3, retval.size());
220         r=(Range)retval.get(0);
221         assertEquals(0, r.low);
222         assertEquals(4, r.high);
223
224         r=(Range)retval.get(1);
225         assertEquals(4, r.low);
226         assertEquals(4, r.high);
227
228         r=(Range)retval.get(2);
229         assertEquals(8, r.low);
230         assertEquals(2, r.high);
231     }
232
233
234     public void testComputeFragOffsetsWithOffsets() {
235         Range r;
236         // byte[] buf={'p', 'a', 'd', 0,1,2,3,4,5,6,7,8,9, 'p', 'a', 'd', 'd', 'i', 'e'};
237
java.util.List JavaDoc retval=Util.computeFragOffsets(3, 10, 4);
238         System.out.println("list is " + retval);
239         assertEquals(3, retval.size());
240         r=(Range)retval.get(0);
241         assertEquals(3, r.low);
242         assertEquals(4, r.high);
243
244         r=(Range)retval.get(1);
245         assertEquals(7, r.low);
246         assertEquals(4, r.high);
247
248         r=(Range)retval.get(2);
249         assertEquals(11, r.low);
250         assertEquals(2, r.high);
251     }
252
253     public void testComputeFragOffsets2() {
254         Range r;
255         byte[] buf={0,1,2,3,4,5,6,7,8,9};
256         java.util.List JavaDoc retval=Util.computeFragOffsets(buf, 10);
257         System.out.println("list is " + retval);
258         assertEquals(1, retval.size());
259         r=(Range)retval.get(0);
260         assertEquals(0, r.low);
261         assertEquals(10, r.high);
262     }
263
264     public void testComputeFragOffsets3() {
265         Range r;
266         byte[] buf={0,1,2,3,4,5,6,7,8,9};
267         java.util.List JavaDoc retval=Util.computeFragOffsets(buf, 100);
268         System.out.println("list is " + retval);
269         assertEquals(1, retval.size());
270         r=(Range)retval.get(0);
271         assertEquals(0, r.low);
272         assertEquals(10, r.high);
273     }
274
275     public void testComputeFragOffsets4() {
276         Range r;
277         byte[] buf={0,1,2,3,4,5,6,7,8,9};
278         java.util.List JavaDoc retval=Util.computeFragOffsets(buf, 5);
279         System.out.println("list is " + retval);
280         assertEquals(2, retval.size());
281         r=(Range)retval.get(0);
282         assertEquals(0, r.low);
283         assertEquals(5, r.high);
284
285         r=(Range)retval.get(1);
286         assertEquals(5, r.low);
287         assertEquals(5, r.high);
288     }
289
290
291     public void testSizeNullMessage() throws Exception JavaDoc {
292         Message msg=new Message();
293         _testSize(msg);
294     }
295
296     public void testSizeMessageWithDest() throws Exception JavaDoc {
297         Message msg=new Message(new IpAddress("127.0.0.1", 3333), null, null);
298         _testSize(msg);
299     }
300
301     public void testSizeMessageWithSrc() throws Exception JavaDoc {
302         Message msg=new Message(null, new IpAddress("127.0.0.1", 4444), null);
303         _testSize(msg);
304     }
305
306     public void testSizeMessageWithDestAndSrc() throws Exception JavaDoc {
307         Message msg=new Message(new IpAddress("127.0.0.1", 3333), new IpAddress("127.0.0.1", 4444), null);
308         _testSize(msg);
309     }
310
311
312     public void testSizeMessageWithDestAndSrcAndFlags() throws Exception JavaDoc {
313         Message msg=new Message(new IpAddress("127.0.0.1", 3333), new IpAddress("127.0.0.1", 4444), null);
314         msg.setFlag(Message.OOB);
315         msg.setFlag(Message.LOW_PRIO);
316         _testSize(msg);
317     }
318
319     public void testSizeMessageWithBuffer() throws Exception JavaDoc {
320         Message msg=new Message(null, null, "bela".getBytes());
321         _testSize(msg);
322     }
323
324     public void testSizeMessageWithBuffer2() throws Exception JavaDoc {
325         Message msg=new Message(null, null, new byte[]{'b', 'e', 'l', 'a'});
326         _testSize(msg);
327     }
328
329     public void testSizeMessageWithBuffer3() throws Exception JavaDoc {
330         Message msg=new Message(null, null, "bela");
331         _testSize(msg);
332     }
333
334     public void testSizeMessageWithAdditionalData() throws Exception JavaDoc {
335         IpAddress dest=new IpAddress("127.0.0.1", 5555);
336         dest.setAdditionalData("bela".getBytes());
337         Message msg=new Message(dest, null, null);
338         _testSize(msg);
339     }
340
341
342     public void testSizeMessageWithDestAndSrcAndHeaders() throws Exception JavaDoc {
343         ClassConfigurator.getInstance(true);
344         Message msg=new Message(new IpAddress("127.0.0.1", 3333), new IpAddress("127.0.0.1", 4444), "bela".getBytes());
345         addHeaders(msg);
346         _testSize(msg);
347     }
348
349     private void addHeaders(Message msg) {
350         UdpHeader udp_hdr=new UdpHeader("DemoChannel");
351         msg.putHeader("UDP", udp_hdr);
352         TpHeader tp_hdr=new TpHeader("DemoChannel2");
353         msg.putHeader("TP", tp_hdr);
354         PingHeader ping_hdr=new PingHeader(PingHeader.GET_MBRS_REQ, null);
355         msg.putHeader("PING", ping_hdr);
356         NakAckHeader nak_hdr=new NakAckHeader(NakAckHeader.XMIT_REQ, 100, 104);
357         msg.putHeader("NAKACK", nak_hdr);
358     }
359
360
361     private void _testSize(Message msg) throws Exception JavaDoc {
362         long size=msg.size();
363         byte[] serialized_form=Util.streamableToByteBuffer(msg);
364         System.out.println("size=" + size + ", serialized size=" + serialized_form.length);
365         assertEquals(size, serialized_form.length);
366     }
367
368
369
370     public static Test suite() {
371         return new TestSuite(MessageTest.class);
372     }
373
374     public static void main(String JavaDoc[] args) {
375         junit.textui.TestRunner.run(suite());
376     }
377 }
378
Popular Tags