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.midp.i3test.TestCase;
031: import java.io.IOException;
032: import java.io.InterruptedIOException;
033:
034: /**
035: * Tests simple exchange of different types of messages (data, data subrange,
036: * and String) within the same isolate. Covers simple cases only, with
037: * one receiver thread and one sender thread. Covers receive-then-send as well
038: * as send-then-receive, for each kind of message.
039: */
040: public class TestTransfer extends TestCase {
041:
042: /**
043: * Tests receive-then-send of a String.
044: */
045: void testReceiveString() throws IOException {
046: Isolate i = Isolate.currentIsolate();
047: Link link = Link.newLink(i, i);
048: Receiver receiver = new Receiver(link);
049: String sendstr = "bar";
050:
051: link.send(LinkMessage.newStringMessage(sendstr));
052: receiver.await();
053:
054: assertTrue("receiver should be done", receiver.done);
055: assertNull("receiver should have no exceptions",
056: receiver.exception);
057: assertNotNull("receiver should have received a message",
058: receiver.msg);
059:
060: String recvstr = receiver.msg.extractString();
061: assertTrue("strings shouldn't be identical", sendstr != recvstr);
062: assertEquals("strings should be equal", sendstr, recvstr);
063: }
064:
065: /**
066: * Tests send-then-receive of a String.
067: */
068: void testSendString() throws IOException {
069: Isolate i = Isolate.currentIsolate();
070: Link link = Link.newLink(i, i);
071: String sendstr = "foo";
072: Sender sender = new Sender(link, LinkMessage
073: .newStringMessage(sendstr));
074:
075: LinkMessage lm = link.receive();
076: sender.await();
077:
078: assertTrue("sender should be done", sender.done);
079: assertNull("sender should have no exceptions", sender.exception);
080:
081: String recvstr = lm.extractString();
082: assertTrue("strings shouldn't be identical", sendstr != recvstr);
083: assertEquals("strings should be equal", sendstr, recvstr);
084: }
085:
086: /**
087: * Tests receive-then-send of a byte array.
088: */
089: void testReceiveData() throws IOException {
090: Isolate i = Isolate.currentIsolate();
091: Link link = Link.newLink(i, i);
092: Receiver receiver = new Receiver(link);
093: byte[] sendarr = new byte[100];
094: Utils.fillRandom(sendarr);
095:
096: link.send(LinkMessage.newDataMessage(sendarr));
097: receiver.await();
098:
099: assertTrue("receiver should be done", receiver.done);
100: assertNull("receiver should have no exceptions",
101: receiver.exception);
102: assertNotNull("receiver should have received a message",
103: receiver.msg);
104:
105: byte[] recvarr = receiver.msg.extractData();
106: assertTrue("arrays shouldn't be identical", sendarr != recvarr);
107: assertTrue("arrays should be equal", Utils.bytesEqual(sendarr,
108: recvarr));
109: }
110:
111: /**
112: * Tests send-then-receive of a byte array.
113: */
114: void testSendData() throws IOException {
115: Isolate i = Isolate.currentIsolate();
116: Link link = Link.newLink(i, i);
117: byte[] sendarr = new byte[100];
118: Utils.fillRandom(sendarr);
119: Sender sender = new Sender(link, LinkMessage
120: .newDataMessage(sendarr));
121:
122: LinkMessage lm = link.receive();
123: sender.await();
124:
125: assertTrue("sender should be done", sender.done);
126: assertNull("sender should have no exceptions", sender.exception);
127:
128: byte[] recvarr = lm.extractData();
129: assertTrue("arrays shouldn't be identical", sendarr != recvarr);
130: assertTrue("arrays should be equal", Utils.bytesEqual(sendarr,
131: recvarr));
132: }
133:
134: /**
135: * Tests receive-then-send of a byte array subrange.
136: */
137: void testReceiveDataRange() throws IOException {
138: Isolate i = Isolate.currentIsolate();
139: Link link = Link.newLink(i, i);
140: Receiver receiver = new Receiver(link);
141: byte[] sendarr = new byte[100];
142: Utils.fillRandom(sendarr);
143:
144: link.send(LinkMessage.newDataMessage(sendarr, 17, 32));
145: receiver.await();
146:
147: assertTrue("receiver should be done", receiver.done);
148: assertNull("receiver should have no exceptions",
149: receiver.exception);
150: assertNotNull("receiver should have received a message",
151: receiver.msg);
152:
153: byte[] recvarr = receiver.msg.extractData();
154: assertTrue("arrays shouldn't be identical", sendarr != recvarr);
155: assertTrue("arrays should be equal", Utils.bytesEqual(sendarr,
156: 17, 32, recvarr));
157: }
158:
159: /**
160: * Tests send-then-receive of a byte array subrange.
161: */
162: void testSendDataRange() throws IOException {
163: Isolate i = Isolate.currentIsolate();
164: Link link = Link.newLink(i, i);
165: byte[] sendarr = new byte[100];
166: Utils.fillRandom(sendarr);
167: Sender sender = new Sender(link, LinkMessage.newDataMessage(
168: sendarr, 47, 22));
169:
170: LinkMessage lm = link.receive();
171: sender.await();
172:
173: assertTrue("sender should be done", sender.done);
174: assertNull("sender should have no exceptions", sender.exception);
175:
176: byte[] recvarr = lm.extractData();
177: assertTrue("arrays shouldn't be identical", sendarr != recvarr);
178: assertTrue("arrays should be equal", Utils.bytesEqual(sendarr,
179: 47, 22, recvarr));
180: }
181:
182: /**
183: * Runs all tests.
184: */
185: public void runTests() throws IOException {
186: declare("testReceiveString");
187: testReceiveString();
188:
189: declare("testSendString");
190: testSendString();
191:
192: declare("testReceiveData");
193: testReceiveData();
194:
195: declare("testSendData");
196: testSendData();
197:
198: declare("testReceiveDataRange");
199: testReceiveDataRange();
200:
201: declare("testSendDataRange");
202: testSendDataRange();
203: }
204:
205: }
|