001: // $Id: StreamableTest.java,v 1.5 2006/09/09 13:16:19 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import junit.framework.Test;
006: import junit.framework.TestCase;
007: import junit.framework.TestSuite;
008: import org.jgroups.*;
009: import org.jgroups.util.Util;
010: import org.jgroups.conf.ClassConfigurator;
011: import org.jgroups.protocols.PingHeader;
012: import org.jgroups.protocols.UdpHeader;
013: import org.jgroups.protocols.WanPipeAddress;
014: import org.jgroups.protocols.PingRsp;
015: import org.jgroups.stack.IpAddress;
016:
017: import java.io.ByteArrayInputStream;
018: import java.io.ByteArrayOutputStream;
019: import java.io.DataInputStream;
020: import java.io.DataOutputStream;
021: import java.util.Vector;
022:
023: public class StreamableTest extends TestCase {
024: Message m1, m2;
025:
026: public StreamableTest(String name) {
027: super (name);
028: }
029:
030: static {
031: try {
032: ClassConfigurator.getInstance(true);
033: } catch (ChannelException e) {
034: e.printStackTrace();
035: }
036: }
037:
038: public void testStreamable() throws Exception {
039: byte[] buf = { 'b', 'e', 'l', 'a', 'b', 'a', 'n' };
040: byte[] tmp;
041: m1 = new Message(null, null, buf, 0, 4);
042: m2 = new Message(null, null, buf, 4, 3);
043:
044: ByteArrayOutputStream output = new ByteArrayOutputStream();
045: DataOutputStream out = new DataOutputStream(output);
046: m1.writeTo(out);
047: out.close();
048: tmp = output.toByteArray();
049: output.close();
050:
051: ByteArrayInputStream input = new ByteArrayInputStream(tmp);
052: DataInputStream in = new DataInputStream(input);
053: Message m3, m4;
054:
055: m3 = new Message(false);
056: m3.readFrom(in);
057:
058: assertEquals(4, m3.getLength());
059: assertEquals(4, m3.getRawBuffer().length);
060: assertEquals(4, m3.getBuffer().length);
061: assertEquals(0, m3.getOffset());
062:
063: output = new ByteArrayOutputStream();
064: out = new DataOutputStream(output);
065: // out.writeObject(m2);
066: m2.writeTo(out);
067: out.close();
068: tmp = output.toByteArray();
069: output.close();
070:
071: System.out.println("-- serialized buffer is " + tmp.length
072: + " bytes");
073:
074: input = new ByteArrayInputStream(tmp);
075: in = new DataInputStream(input);
076:
077: // m4=(Message)in.readObject();
078: m4 = new Message();
079: m4.readFrom(in);
080:
081: assertEquals(3, m4.getLength());
082: assertEquals(3, m4.getBuffer().length);
083: assertEquals(3, m4.getRawBuffer().length);
084: assertEquals(0, m4.getOffset());
085: }
086:
087: public void testStreamable2() throws Exception {
088: byte[] buf = { 'b', 'e', 'l', 'a', 'b', 'a', 'n' };
089: Message msg = new Message(null, null, buf, 0, 4);
090: stream(msg);
091: }
092:
093: public void testStreamable3() throws Exception {
094: byte[] buf = { 'b', 'e', 'l', 'a', 'b', 'a', 'n' };
095: Message msg = new Message(null, null, buf, 4, 3);
096: stream(msg);
097: }
098:
099: public void testNullBuffer() throws Exception {
100: Message msg = new Message();
101: stream(msg);
102: }
103:
104: public void testNonNullBuffer() throws Exception {
105: Message msg = new Message(null, null, "Hello world".getBytes());
106: stream(msg);
107: }
108:
109: public void testNonNullAddress() throws Exception {
110: Address dest, src;
111: dest = new IpAddress("228.1.2.3", 5555);
112: src = new IpAddress("127.0.0.1", 6666);
113: Message msg = new Message(dest, src, "Hello world".getBytes());
114: stream(msg);
115: }
116:
117: public void testHeaders() throws Exception {
118: Address dest, src;
119: dest = new IpAddress("228.1.2.3", 5555);
120: src = new IpAddress("127.0.0.1", 6666);
121: Message msg = new Message(dest, src, "Hello world".getBytes());
122: PingHeader hdr = new PingHeader(PingHeader.GET_MBRS_REQ,
123: new PingRsp(src, src, true));
124: msg.putHeader("ping-header", hdr);
125: UdpHeader udp_hdr = new UdpHeader("bla");
126: msg.putHeader("udp-header", udp_hdr);
127: stream(msg);
128: }
129:
130: public void testAdditionalData() throws Exception {
131: IpAddress dest, src;
132: dest = new IpAddress("228.1.2.3", 5555);
133: dest.setAdditionalData("foo".getBytes());
134: src = new IpAddress("127.0.0.1", 6666);
135: src.setAdditionalData("foobar".getBytes());
136: Message msg = new Message(dest, src, "Hello world".getBytes());
137: PingHeader hdr = new PingHeader(PingHeader.GET_MBRS_REQ,
138: new PingRsp(src, src, false));
139: msg.putHeader("ping-header", hdr);
140: UdpHeader udp_hdr = new UdpHeader("bla");
141: msg.putHeader("udp-header", udp_hdr);
142: stream(msg);
143: }
144:
145: public void testDifferentAddress() throws Exception {
146: Address dest, src;
147: dest = new WanPipeAddress("foo");
148: src = new WanPipeAddress("foobar");
149: Message msg = new Message(dest, src, "Hello world".getBytes());
150: PingHeader hdr = new PingHeader(PingHeader.GET_MBRS_REQ,
151: new PingRsp(src, src, false));
152: msg.putHeader("ping-header", hdr);
153: UdpHeader udp_hdr = new UdpHeader("bla");
154: msg.putHeader("udp-header", udp_hdr);
155: stream(msg);
156: }
157:
158: public void testMergeView() throws Exception {
159: Vector tmp_m1, tmp_m2, m3, all, subgroups;
160: Address a, b, c, d, e, f;
161: View v1, v2, v3, v4, v5, view_all;
162:
163: a = new IpAddress(1000);
164: b = new IpAddress(2000);
165: c = new IpAddress(3000);
166: d = new IpAddress(4000);
167: e = new IpAddress(5000);
168: f = new IpAddress(6000);
169:
170: tmp_m1 = new Vector();
171: tmp_m2 = new Vector();
172: m3 = new Vector();
173: all = new Vector();
174: subgroups = new Vector();
175: tmp_m1.add(a);
176: tmp_m1.add(b);
177: tmp_m1.add(c);
178: tmp_m2.add(d);
179: m3.add(e);
180: m3.add(f);
181: all.add(a);
182: all.add(b);
183: all.add(c);
184: all.add(d);
185: all.add(e);
186: all.add(f);
187:
188: v1 = new View(a, 1, tmp_m1);
189: v2 = new MergeView(d, 2, tmp_m2, new Vector());
190: v3 = new View(e, 3, m3);
191: v4 = new MergeView(e, 4, m3, null);
192: v5 = new View(e, 5, m3);
193: subgroups.add(v1);
194: subgroups.add(v2);
195: subgroups.add(v3);
196: subgroups.add(v4);
197: subgroups.add(v5);
198:
199: view_all = new MergeView(a, 5, all, subgroups);
200: System.out.println("MergeView: " + view_all);
201: Vector sub = ((MergeView) view_all).getSubgroups();
202: assertTrue(sub.get(0) instanceof View);
203: assertTrue(sub.get(1) instanceof MergeView);
204: assertTrue(sub.get(2) instanceof View);
205: assertTrue(sub.get(3) instanceof MergeView);
206: assertTrue(sub.get(4) instanceof View);
207:
208: byte[] buf = Util.streamableToByteBuffer(view_all);
209: assertNotNull(buf);
210: assertTrue(buf.length > 0);
211:
212: MergeView merge_view = (MergeView) Util
213: .streamableFromByteBuffer(MergeView.class, buf);
214: assertNotNull(merge_view);
215: System.out.println("MergeView: " + merge_view);
216: sub = merge_view.getSubgroups();
217: assertTrue(sub.get(0) instanceof View);
218: assertTrue(sub.get(1) instanceof MergeView);
219: assertTrue(sub.get(2) instanceof View);
220: assertTrue(sub.get(3) instanceof MergeView);
221: assertTrue(sub.get(4) instanceof View);
222: }
223:
224: private void stream(Message msg) throws Exception {
225: int length, bufLength;
226: byte[] tmp;
227: Message msg2;
228: Address src;
229: int num_headers = getNumHeaders(msg);
230:
231: length = msg.getLength();
232: bufLength = getBufLength(msg);
233: src = msg.getSrc();
234:
235: ByteArrayOutputStream output = new ByteArrayOutputStream();
236: DataOutputStream out = new DataOutputStream(output);
237: msg.writeTo(out);
238: out.close();
239: tmp = output.toByteArray();
240: output.close();
241:
242: System.out.println("-- serialized buffer is " + tmp.length
243: + " bytes");
244:
245: ByteArrayInputStream input = new ByteArrayInputStream(tmp);
246: DataInputStream in = new DataInputStream(input);
247:
248: msg2 = new Message();
249: msg2.readFrom(in);
250:
251: assertEquals(length, msg2.getLength());
252: assertEquals(bufLength, getBufLength(msg2));
253: // assertTrue(match(dest, msg2.getDest()));
254: assertNull(msg2.getDest()); // we don't marshal the destination address
255: assertTrue(match(src, msg2.getSrc()));
256: assertEquals(num_headers, getNumHeaders(msg2));
257: }
258:
259: private int getNumHeaders(Message msg) {
260: return msg.getHeaders() != null ? msg.getHeaders().size() : 0;
261: }
262:
263: private boolean match(Address a1, Address a2) {
264: if (a1 == null && a2 == null)
265: return true;
266: if (a1 != null)
267: return a1.equals(a2);
268: else
269: return a2.equals(a1);
270: }
271:
272: // private int getRawBufLength(Message msg) {
273: // return msg.getRawBuffer() != null? msg.getRawBuffer().length : 0;
274: // }
275:
276: private int getBufLength(Message msg) {
277: return msg.getBuffer() != null ? msg.getBuffer().length : 0;
278: }
279:
280: public static Test suite() {
281: TestSuite s = new TestSuite(StreamableTest.class);
282: return s;
283: }
284:
285: public static void main(String[] args) {
286: junit.textui.TestRunner.run(suite());
287: }
288: }
|