KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: StreamableTest.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.*;
9 import org.jgroups.conf.ClassConfigurator;
10 import org.jgroups.protocols.PingHeader;
11 import org.jgroups.protocols.PingRsp;
12 import org.jgroups.protocols.UdpHeader;
13 import org.jgroups.stack.IpAddress;
14 import org.jgroups.util.Util;
15
16 import java.io.ByteArrayInputStream JavaDoc;
17 import java.io.ByteArrayOutputStream JavaDoc;
18 import java.io.DataInputStream JavaDoc;
19 import java.io.DataOutputStream JavaDoc;
20 import java.util.Vector JavaDoc;
21
22
23 public class StreamableTest extends TestCase {
24     Message m1, m2;
25
26
27     public StreamableTest(String JavaDoc name) {
28         super(name);
29     }
30
31
32
33     static {
34         try {
35             ClassConfigurator.getInstance(true);
36         }
37         catch(ChannelException e) {
38             e.printStackTrace();
39         }
40     }
41
42
43
44     public void testStreamable() throws Exception JavaDoc {
45         byte[] buf={'b', 'e', 'l', 'a', 'b', 'a', 'n'};
46         byte[] tmp;
47         m1=new Message(null, null, buf, 0, 4);
48         m2=new Message(null, null, buf, 4, 3);
49
50
51         ByteArrayOutputStream JavaDoc output=new ByteArrayOutputStream JavaDoc();
52         DataOutputStream JavaDoc out=new DataOutputStream JavaDoc(output);
53         m1.writeTo(out);
54         out.close();
55         tmp=output.toByteArray();
56         output.close();
57
58         ByteArrayInputStream JavaDoc input=new ByteArrayInputStream JavaDoc(tmp);
59         DataInputStream JavaDoc in=new DataInputStream JavaDoc(input);
60         Message m3, m4;
61
62         m3=new Message(false);
63         m3.readFrom(in);
64
65         assertEquals(4, m3.getLength());
66         assertEquals(4, m3.getRawBuffer().length);
67         assertEquals(4, m3.getBuffer().length);
68         assertEquals(0, m3.getOffset());
69
70         output=new ByteArrayOutputStream JavaDoc();
71         out=new DataOutputStream JavaDoc(output);
72         // out.writeObject(m2);
73
m2.writeTo(out);
74         out.close();
75         tmp=output.toByteArray();
76         output.close();
77
78         System.out.println("-- serialized buffer is " + tmp.length + " bytes");
79
80         input=new ByteArrayInputStream JavaDoc(tmp);
81         in=new DataInputStream JavaDoc(input);
82
83         // m4=(Message)in.readObject();
84
m4=new Message();
85         m4.readFrom(in);
86
87
88         assertEquals(3, m4.getLength());
89         assertEquals(3, m4.getBuffer().length);
90         assertEquals(3, m4.getRawBuffer().length);
91         assertEquals(0, m4.getOffset());
92     }
93
94
95
96     public void testStreamable2() throws Exception JavaDoc {
97         byte[] buf={'b', 'e', 'l', 'a', 'b', 'a', 'n'};
98         Message msg=new Message(null, null, buf, 0, 4);
99         stream(msg);
100     }
101
102     public void testStreamable3() throws Exception JavaDoc {
103         byte[] buf={'b', 'e', 'l', 'a', 'b', 'a', 'n'};
104         Message msg=new Message(null, null, buf, 4, 3);
105         stream(msg);
106     }
107
108     public void testNullBuffer() throws Exception JavaDoc {
109         Message msg=new Message();
110         stream(msg);
111     }
112
113
114     public void testNonNullBuffer() throws Exception JavaDoc {
115         Message msg=new Message(null, null, "Hello world".getBytes());
116         stream(msg);
117     }
118
119
120     public void testNonNullAddress() throws Exception JavaDoc {
121         Address dest, src;
122         dest=new IpAddress("228.1.2.3", 5555);
123         src=new IpAddress("127.0.0.1", 6666);
124         Message msg=new Message(dest, src, "Hello world".getBytes());
125         stream(msg);
126     }
127
128     public void testHeaders() throws Exception JavaDoc {
129         Address dest, src;
130         dest=new IpAddress("228.1.2.3", 5555);
131         src=new IpAddress("127.0.0.1", 6666);
132         Message msg=new Message(dest, src, "Hello world".getBytes());
133         PingHeader hdr=new PingHeader(PingHeader.GET_MBRS_REQ, new PingRsp(src, src, true));
134         msg.putHeader("ping-header", hdr);
135         UdpHeader udp_hdr=new UdpHeader("bla");
136         msg.putHeader("udp-header", udp_hdr);
137         stream(msg);
138     }
139
140
141     public void testAdditionalData() throws Exception JavaDoc {
142         IpAddress dest, src;
143         dest=new IpAddress("228.1.2.3", 5555);
144         dest.setAdditionalData("foo".getBytes());
145         src=new IpAddress("127.0.0.1", 6666);
146         src.setAdditionalData("foobar".getBytes());
147         Message msg=new Message(dest, src, "Hello world".getBytes());
148         PingHeader hdr=new PingHeader(PingHeader.GET_MBRS_REQ, new PingRsp(src, src, false));
149         msg.putHeader("ping-header", hdr);
150         UdpHeader udp_hdr=new UdpHeader("bla");
151         msg.putHeader("udp-header", udp_hdr);
152         stream(msg);
153     }
154
155
156
157     public void testMergeView() throws Exception JavaDoc {
158         Vector JavaDoc tmp_m1, tmp_m2 , m3, all, subgroups;
159         Address a,b,c,d,e,f;
160         View v1, v2, v3, v4, v5, view_all;
161
162         a=new IpAddress(1000);
163         b=new IpAddress(2000);
164         c=new IpAddress(3000);
165         d=new IpAddress(4000);
166         e=new IpAddress(5000);
167         f=new IpAddress(6000);
168
169         tmp_m1=new Vector JavaDoc(); tmp_m2=new Vector JavaDoc(); m3=new Vector JavaDoc(); all=new Vector JavaDoc(); subgroups=new Vector JavaDoc();
170         tmp_m1.add(a); tmp_m1.add(b); tmp_m1.add(c);
171         tmp_m2.add(d);
172         m3.add(e); m3.add(f);
173         all.add(a); all.add(b); all.add(c); all.add(d); all.add(e); all.add(f);
174
175         v1=new View(a, 1, tmp_m1);
176         v2=new MergeView(d, 2, tmp_m2, new Vector JavaDoc());
177         v3=new View(e, 3, m3);
178         v4=new MergeView(e, 4, m3, null);
179         v5=new View(e, 5, m3);
180         subgroups.add(v1);
181         subgroups.add(v2);
182         subgroups.add(v3);
183         subgroups.add(v4);
184         subgroups.add(v5);
185
186         view_all=new MergeView(a, 5, all, subgroups);
187         System.out.println("MergeView: " + view_all);
188         Vector JavaDoc sub=((MergeView)view_all).getSubgroups();
189         assertTrue(sub.get(0) instanceof View);
190         assertTrue(sub.get(1) instanceof MergeView);
191         assertTrue(sub.get(2) instanceof View);
192         assertTrue(sub.get(3) instanceof MergeView);
193         assertTrue(sub.get(4) instanceof View);
194
195         byte[] buf=Util.streamableToByteBuffer(view_all);
196         assertNotNull(buf);
197         assertTrue(buf.length > 0);
198
199         MergeView merge_view=(MergeView)Util.streamableFromByteBuffer(MergeView.class, buf);
200         assertNotNull(merge_view);
201         System.out.println("MergeView: " + merge_view);
202         sub=merge_view.getSubgroups();
203         assertTrue(sub.get(0) instanceof View);
204         assertTrue(sub.get(1) instanceof MergeView);
205         assertTrue(sub.get(2) instanceof View);
206         assertTrue(sub.get(3) instanceof MergeView);
207         assertTrue(sub.get(4) instanceof View);
208     }
209
210     private void stream(Message msg) throws Exception JavaDoc {
211         int length, bufLength;
212         byte[] tmp;
213         Message msg2;
214         Address src;
215         int num_headers=getNumHeaders(msg);
216
217         length=msg.getLength();
218         bufLength=getBufLength(msg);
219         src=msg.getSrc();
220
221         ByteArrayOutputStream JavaDoc output=new ByteArrayOutputStream JavaDoc();
222         DataOutputStream JavaDoc out=new DataOutputStream JavaDoc(output);
223         msg.writeTo(out);
224         out.close();
225         tmp=output.toByteArray();
226         output.close();
227
228         System.out.println("-- serialized buffer is " + tmp.length + " bytes");
229
230         ByteArrayInputStream JavaDoc input=new ByteArrayInputStream JavaDoc(tmp);
231         DataInputStream JavaDoc in=new DataInputStream JavaDoc(input);
232
233         msg2=new Message();
234         msg2.readFrom(in);
235
236         assertEquals(length, msg2.getLength());
237         assertEquals(bufLength, getBufLength(msg2));
238         // assertTrue(match(dest, msg2.getDest()));
239
assertNull(msg2.getDest()); // we don't marshal the destination address
240
assertTrue(match(src, msg2.getSrc()));
241         assertEquals(num_headers, getNumHeaders(msg2));
242     }
243
244     private int getNumHeaders(Message msg) {
245         return msg.getNumHeaders();
246     }
247
248
249     private boolean match(Address a1, Address a2) {
250         if(a1 == null && a2 == null)
251             return true;
252         if(a1 != null)
253             return a1.equals(a2);
254         else
255             return a2.equals(a1);
256     }
257
258 // private int getRawBufLength(Message msg) {
259
// return msg.getRawBuffer() != null? msg.getRawBuffer().length : 0;
260
// }
261

262     private int getBufLength(Message msg) {
263         return msg.getBuffer() != null? msg.getBuffer().length : 0;
264     }
265
266
267     public static Test suite() {
268         return new TestSuite(StreamableTest.class);
269     }
270
271     public static void main(String JavaDoc[] args) {
272         junit.textui.TestRunner.run(suite());
273     }
274 }
275
Popular Tags