001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.links;
028:
029: import com.sun.cldc.isolate.Isolate;
030: import com.sun.cldc.isolate.IsolateStartupException;
031: import com.sun.midp.i3test.TestCase;
032: import java.io.IOException;
033:
034: /**
035: * Tests sending messages to and receiving messages from another isolate.
036: * Starts the Echo program in another isolate and tests echoing of string,
037: * data, data range, and link messages.
038: */
039: public class TestEcho extends TestCase {
040:
041: Isolate us;
042: Isolate them;
043: Link to;
044: Link from;
045:
046: void setUp() throws IOException, IsolateStartupException {
047: us = Isolate.currentIsolate();
048: them = new Isolate("com.sun.midp.links.Echo", null);
049: them.start();
050: to = Link.newLink(us, them);
051: from = Link.newLink(them, us);
052: LinkPortal.setLinks(them, new Link[] { to, from });
053: }
054:
055: void tearDown() {
056: LinkPortal.setLinks(them, null);
057: them.exit(0);
058: them.waitForExit();
059:
060: us = null;
061: them = null;
062: to = null;
063: from = null;
064: }
065:
066: /**
067: * Tests echoing of a string message.
068: */
069: void testString() throws IOException, IsolateStartupException {
070: String rs, ss;
071: LinkMessage lm;
072:
073: ss = "What hath God wrought?";
074: to.send(LinkMessage.newStringMessage(ss));
075: lm = from.receive();
076:
077: assertTrue("contains string", lm.containsString());
078: rs = lm.extractString();
079: assertNotSame("strings not same", ss, rs);
080: assertEquals("strings equal", ss, rs);
081: }
082:
083: /**
084: * Tests echoing of a data message.
085: */
086: void testData() throws IOException, IsolateStartupException {
087: byte[] rdata;
088: byte[] sdata;
089: LinkMessage lm;
090:
091: sdata = new byte[50];
092: Utils.fillRandom(sdata);
093: to.send(LinkMessage.newDataMessage(sdata));
094: lm = from.receive();
095:
096: assertTrue("contains data", lm.containsData());
097: rdata = lm.extractData();
098: assertNotSame("data not same", sdata, rdata);
099: assertTrue("data equal", Utils.bytesEqual(sdata, rdata));
100: }
101:
102: /**
103: * Tests echoing of a data subrange message.
104: */
105: void testDataRange() throws IOException, IsolateStartupException {
106: byte[] rdata;
107: byte[] sdata;
108: LinkMessage lm;
109:
110: sdata = new byte[60];
111: Utils.fillRandom(sdata);
112: to.send(LinkMessage.newDataMessage(sdata, 12, 23));
113: lm = from.receive();
114:
115: assertTrue("contains data", lm.containsData());
116: rdata = lm.extractData();
117: assertNotSame("data not same", sdata, rdata);
118: assertTrue("data equal", Utils.bytesEqual(sdata, 12, 23, rdata));
119: }
120:
121: /**
122: * Tests echoing of a link message.
123: */
124: void testLink() throws IOException, IsolateStartupException {
125: Link rlink, slink;
126: LinkMessage lm;
127:
128: slink = Link.newLink(us, us);
129: to.send(LinkMessage.newLinkMessage(slink));
130: lm = from.receive();
131:
132: assertTrue("contains link", lm.containsLink());
133: rlink = lm.extractLink();
134: assertNotSame("links not same", slink, rlink);
135: assertEquals("links equal", slink, rlink);
136: }
137:
138: /**
139: * Runs all tests.
140: */
141: public void runTests() throws IOException, IsolateStartupException {
142:
143: declare("testString");
144: setUp();
145: testString();
146: tearDown();
147:
148: declare("testData");
149: setUp();
150: testData();
151: tearDown();
152:
153: declare("testDataRange");
154: setUp();
155: testDataRange();
156: tearDown();
157:
158: declare("testLink");
159: setUp();
160: testLink();
161: tearDown();
162:
163: }
164: }
|