001: // $Id: MessageTest.java,v 1.13 2005/07/08 11:28:26 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.Message;
009: import org.jgroups.conf.ClassConfigurator;
010: import org.jgroups.protocols.UdpHeader;
011: import org.jgroups.protocols.TpHeader;
012: import org.jgroups.protocols.PingHeader;
013: import org.jgroups.protocols.pbcast.NakAckHeader;
014: import org.jgroups.stack.IpAddress;
015: import org.jgroups.util.Range;
016: import org.jgroups.util.Util;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022:
023: public class MessageTest extends TestCase {
024: Message m1, m2;
025:
026: public MessageTest(String name) {
027: super (name);
028: }
029:
030: public void testBufferSize() throws Exception {
031: m1 = new Message(null, null, "bela");
032: assertNotNull(m1.getRawBuffer());
033: assertNotNull(m1.getBuffer());
034: assertEquals(m1.getBuffer().length, m1.getLength());
035: byte[] new_buf = { 'm', 'i', 'c', 'h', 'e', 'l', 'l', 'e' };
036: m1.setBuffer(new_buf);
037: assertNotNull(m1.getRawBuffer());
038: assertNotNull(m1.getBuffer());
039: assertEquals(new_buf.length, m1.getLength());
040: assertEquals(m1.getBuffer().length, m1.getLength());
041: }
042:
043: public void testBufferOffset() throws Exception {
044: byte[] buf = { 'b', 'e', 'l', 'a', 'b', 'a', 'n' };
045: m1 = new Message(null, null, buf, 0, 4);
046: m2 = new Message(null, null, buf, 4, 3);
047:
048: byte[] b1, b2;
049:
050: b1 = new byte[m1.getLength()];
051: System.arraycopy(m1.getRawBuffer(), m1.getOffset(), b1, 0, m1
052: .getLength());
053:
054: b2 = new byte[m2.getLength()];
055: System.arraycopy(m2.getRawBuffer(), m2.getOffset(), b2, 0, m2
056: .getLength());
057:
058: assertEquals(4, b1.length);
059: assertEquals(3, b2.length);
060: }
061:
062: public void testInvalidOffset() {
063: byte[] buf = { 'b', 'e', 'l', 'a', 'b', 'a', 'n' };
064:
065: try {
066: m1 = new Message(null, null, buf, -1, 4);
067: fail("we should not get here (offset is -1)");
068: } catch (ArrayIndexOutOfBoundsException ex) {
069: assertTrue("correct: offset is invalid (caught correctly)",
070: true);
071: }
072: }
073:
074: public void testInvalidLength() {
075: byte[] buf = { 'b', 'e', 'l', 'a', 'b', 'a', 'n' };
076:
077: try {
078: m1 = new Message(null, null, buf, 3, 6);
079: fail("we should not get here (length is 9)");
080: } catch (ArrayIndexOutOfBoundsException ex) {
081: assertTrue("correct: length is invalid (caught correctly)",
082: true);
083: }
084: }
085:
086: public void testGetRawBuffer() {
087: byte[] buf = { 'b', 'e', 'l', 'a', 'b', 'a', 'n' };
088: m1 = new Message(null, null, buf, 0, 4);
089: m2 = new Message(null, null, buf, 4, 3);
090:
091: assertEquals(buf.length, m1.getRawBuffer().length);
092: assertEquals(4, m1.getBuffer().length);
093: assertEquals(4, m1.getLength());
094:
095: assertEquals(buf.length, m2.getRawBuffer().length);
096: assertEquals(3, m2.getBuffer().length);
097: assertEquals(3, m2.getLength());
098: }
099:
100: public void testSerialization() throws Exception {
101: byte[] buf = { 'b', 'e', 'l', 'a', 'b', 'a', 'n' };
102: byte[] tmp;
103: m1 = new Message(null, null, buf, 0, 4);
104: m2 = new Message(null, null, buf, 4, 3);
105:
106: ByteArrayOutputStream output = new ByteArrayOutputStream();
107: ObjectOutputStream out = new ObjectOutputStream(output);
108: out.writeObject(m1);
109: output.close();
110: tmp = output.toByteArray();
111:
112: ByteArrayInputStream input = new ByteArrayInputStream(tmp);
113: ObjectInputStream in = new ObjectInputStream(input);
114: Message m3, m4;
115:
116: m3 = (Message) in.readObject();
117: assertEquals(4, m3.getLength());
118: assertEquals(4, m3.getRawBuffer().length);
119: assertEquals(4, m3.getBuffer().length);
120: assertEquals(0, m3.getOffset());
121:
122: output = new ByteArrayOutputStream();
123: out = new ObjectOutputStream(output);
124: out.writeObject(m2);
125: output.close();
126: tmp = output.toByteArray();
127:
128: input = new ByteArrayInputStream(tmp);
129: in = new ObjectInputStream(input);
130: m4 = (Message) in.readObject();
131: assertEquals(3, m4.getLength());
132: assertEquals(3, m4.getBuffer().length);
133: assertEquals(3, m4.getRawBuffer().length);
134: assertEquals(0, m4.getOffset());
135: }
136:
137: public void testSetObject() {
138: String s1 = "Bela Ban";
139: m1 = new Message(null, null, s1);
140: assertEquals(0, m1.getOffset());
141: assertEquals(m1.getBuffer().length, m1.getLength());
142: String s2 = (String) m1.getObject();
143: assertEquals(s2, s1);
144: }
145:
146: public void testReset() {
147: m1 = new Message(null, null, "Bela Ban");
148: m1.reset();
149: assertEquals(0, m1.getOffset());
150: assertEquals(0, m1.getLength());
151: assertNull(m1.getBuffer());
152: assertNull(m1.getRawBuffer());
153: }
154:
155: public void testCopy() {
156: m1 = new Message(null, null, "Bela Ban");
157: m2 = m1.copy();
158: assertEquals(m1.getOffset(), m2.getOffset());
159: assertEquals(m1.getLength(), m2.getLength());
160: }
161:
162: public void testCopyWithOffset() {
163: byte[] buf = { 'b', 'e', 'l', 'a', 'b', 'a', 'n' };
164: m1 = new Message(null, null, buf, 0, 4);
165: m2 = new Message(null, null, buf, 4, 3);
166:
167: Message m3, m4;
168: m3 = m1.copy();
169: m4 = m2.copy();
170:
171: assertEquals(0, m3.getOffset());
172: assertEquals(4, m3.getLength());
173: assertEquals(4, m3.getBuffer().length);
174:
175: assertEquals(4, m4.getOffset());
176: assertEquals(3, m4.getLength());
177: assertEquals(3, m4.getBuffer().length);
178: }
179:
180: public void testComputeFragOffsets() {
181: Range r;
182: byte[] buf = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
183: java.util.List retval = Util.computeFragOffsets(buf, 4);
184: System.out.println("list is " + retval);
185: assertEquals(3, retval.size());
186: r = (Range) retval.get(0);
187: assertEquals(0, r.low);
188: assertEquals(4, r.high);
189:
190: r = (Range) retval.get(1);
191: assertEquals(4, r.low);
192: assertEquals(4, r.high);
193:
194: r = (Range) retval.get(2);
195: assertEquals(8, r.low);
196: assertEquals(2, r.high);
197: }
198:
199: public void testComputeFragOffsetsWithOffsets() {
200: Range r;
201: // byte[] buf={'p', 'a', 'd', 0,1,2,3,4,5,6,7,8,9, 'p', 'a', 'd', 'd', 'i', 'e'};
202: java.util.List retval = Util.computeFragOffsets(3, 10, 4);
203: System.out.println("list is " + retval);
204: assertEquals(3, retval.size());
205: r = (Range) retval.get(0);
206: assertEquals(3, r.low);
207: assertEquals(4, r.high);
208:
209: r = (Range) retval.get(1);
210: assertEquals(7, r.low);
211: assertEquals(4, r.high);
212:
213: r = (Range) retval.get(2);
214: assertEquals(11, r.low);
215: assertEquals(2, r.high);
216: }
217:
218: public void testComputeFragOffsets2() {
219: Range r;
220: byte[] buf = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
221: java.util.List retval = Util.computeFragOffsets(buf, 10);
222: System.out.println("list is " + retval);
223: assertEquals(1, retval.size());
224: r = (Range) retval.get(0);
225: assertEquals(0, r.low);
226: assertEquals(10, r.high);
227: }
228:
229: public void testComputeFragOffsets3() {
230: Range r;
231: byte[] buf = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
232: java.util.List retval = Util.computeFragOffsets(buf, 100);
233: System.out.println("list is " + retval);
234: assertEquals(1, retval.size());
235: r = (Range) retval.get(0);
236: assertEquals(0, r.low);
237: assertEquals(10, r.high);
238: }
239:
240: public void testComputeFragOffsets4() {
241: Range r;
242: byte[] buf = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
243: java.util.List retval = Util.computeFragOffsets(buf, 5);
244: System.out.println("list is " + retval);
245: assertEquals(2, retval.size());
246: r = (Range) retval.get(0);
247: assertEquals(0, r.low);
248: assertEquals(5, r.high);
249:
250: r = (Range) retval.get(1);
251: assertEquals(5, r.low);
252: assertEquals(5, r.high);
253: }
254:
255: public void testSizeNullMessage() throws Exception {
256: Message msg = new Message();
257: _testSize(msg);
258: }
259:
260: public void testSizeMessageWithDest() throws Exception {
261: Message msg = new Message(new IpAddress("127.0.0.1", 3333),
262: null, null);
263: _testSize(msg);
264: }
265:
266: public void testSizeMessageWithSrc() throws Exception {
267: Message msg = new Message(null,
268: new IpAddress("127.0.0.1", 4444), null);
269: _testSize(msg);
270: }
271:
272: public void testSizeMessageWithDestAndSrc() throws Exception {
273: Message msg = new Message(new IpAddress("127.0.0.1", 3333),
274: new IpAddress("127.0.0.1", 4444), null);
275: _testSize(msg);
276: }
277:
278: public void testSizeMessageWithBuffer() throws Exception {
279: Message msg = new Message(null, null, "bela".getBytes());
280: _testSize(msg);
281: }
282:
283: public void testSizeMessageWithBuffer2() throws Exception {
284: Message msg = new Message(null, null, new byte[] { 'b', 'e',
285: 'l', 'a' });
286: _testSize(msg);
287: }
288:
289: public void testSizeMessageWithBuffer3() throws Exception {
290: Message msg = new Message(null, null, "bela");
291: _testSize(msg);
292: }
293:
294: public void testSizeMessageWithAdditionalData() throws Exception {
295: IpAddress dest = new IpAddress("127.0.0.1", 5555);
296: dest.setAdditionalData("bela".getBytes());
297: Message msg = new Message(dest, null, null);
298: _testSize(msg);
299: }
300:
301: public void testSizeMessageWithDestAndSrcAndHeaders()
302: throws Exception {
303: ClassConfigurator.getInstance(true);
304: Message msg = new Message(new IpAddress("127.0.0.1", 3333),
305: new IpAddress("127.0.0.1", 4444), "bela".getBytes());
306: addHeaders(msg);
307: _testSize(msg);
308: }
309:
310: private void addHeaders(Message msg) {
311: UdpHeader udp_hdr = new UdpHeader("DemoChannel");
312: msg.putHeader("UDP", udp_hdr);
313: TpHeader tp_hdr = new TpHeader("DemoChannel2");
314: msg.putHeader("TP", tp_hdr);
315: PingHeader ping_hdr = new PingHeader(PingHeader.GET_MBRS_REQ,
316: null);
317: msg.putHeader("PING", ping_hdr);
318: NakAckHeader nak_hdr = new NakAckHeader(NakAckHeader.XMIT_REQ,
319: 100, 104);
320: msg.putHeader("NAKACK", nak_hdr);
321: }
322:
323: private void _testSize(Message msg) throws Exception {
324: long size = msg.size();
325: byte[] serialized_form = Util.streamableToByteBuffer(msg);
326: System.out.println("size=" + size + ", serialized size="
327: + serialized_form.length);
328: assertEquals(size, serialized_form.length);
329: }
330:
331: public static Test suite() {
332: TestSuite s = new TestSuite(MessageTest.class);
333: return s;
334: }
335:
336: public static void main(String[] args) {
337: junit.textui.TestRunner.run(suite());
338: }
339: }
|