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: import java.io.InterruptedIOException;
034:
035: /**
036: * Tests basic operations on the Link class.
037: */
038: public class TestLink extends TestCase {
039:
040: /**
041: * Tests creation, isOpen(), and close().
042: */
043: void testCreate() {
044: Isolate i = Isolate.currentIsolate();
045: Link link = Link.newLink(i, i);
046:
047: assertTrue("link should be open", link.isOpen());
048:
049: link.close();
050: assertFalse("link should be closed", link.isOpen());
051: }
052:
053: /**
054: * Tests nulls.
055: */
056: void testNulls() {
057: Isolate i = Isolate.currentIsolate();
058: boolean thrown;
059:
060: thrown = false;
061: try {
062: Link link = Link.newLink(null, null);
063: } catch (NullPointerException npe) {
064: thrown = true;
065: }
066: assertTrue("null,null should throw NPE", thrown);
067:
068: thrown = false;
069: try {
070: Link link = Link.newLink(null, i);
071: } catch (NullPointerException npe) {
072: thrown = true;
073: }
074: assertTrue("null,i should throw NPE", thrown);
075:
076: thrown = false;
077: try {
078: Link link = Link.newLink(i, null);
079: } catch (NullPointerException npe) {
080: thrown = true;
081: }
082: assertTrue("i,null should throw NPE", thrown);
083:
084: thrown = false;
085: try {
086: Link link = Link.newLink(i, i);
087: } catch (NullPointerException npe) {
088: thrown = true;
089: }
090: assertFalse("i,i should not throw NPE", thrown);
091: }
092:
093: /**
094: * Tests for equals() and hashCode().
095: */
096: void testEquals() {
097: Isolate i = Isolate.currentIsolate();
098: Link link = Link.newLink(i, i);
099: Link link2 = Link.newLink(i, i);
100: assertFalse("must not equal null", link.equals(null));
101: assertTrue("must equal itself", link.equals(link));
102: assertFalse("must not equal other obj", link.equals("foobar"));
103: assertFalse("must not equal other link", link.equals(link2));
104:
105: link.close();
106: assertEquals("hashCode of closed should be zero", 0, link
107: .hashCode());
108: link2.close();
109: assertTrue("two closed links should be equal", link
110: .equals(link2));
111: }
112:
113: /**
114: * Tests receive() on a closed link.
115: */
116: void testReceiveClosed() throws IOException {
117: Isolate i = Isolate.currentIsolate();
118: Link link = Link.newLink(i, i);
119: link.close();
120:
121: boolean thrown = false;
122: try {
123: LinkMessage lm = link.receive();
124: } catch (ClosedLinkException cle) {
125: thrown = true;
126: }
127:
128: assertTrue("exception should be thrown", thrown);
129: }
130:
131: /**
132: * Tests send() on a closed link.
133: */
134: void testSendClosed() throws IOException {
135: Isolate i = Isolate.currentIsolate();
136: Link link = Link.newLink(i, i);
137: link.close();
138:
139: boolean thrown = false;
140: try {
141: link.send(LinkMessage.newStringMessage("foo"));
142: } catch (ClosedLinkException cle) {
143: thrown = true;
144: }
145:
146: assertTrue("exception should be thrown", thrown);
147: }
148:
149: /**
150: * Tests receive() followed by a send().
151: */
152: void testReceiveSend() throws IOException {
153: Isolate i = Isolate.currentIsolate();
154: Link link = Link.newLink(i, i);
155: Receiver receiver = new Receiver(link);
156:
157: assertFalse("receiver should be blocked", receiver.done);
158: assertNull("receiver should have no exceptions",
159: receiver.exception);
160:
161: String sendstr = "bar";
162: link.send(LinkMessage.newStringMessage(sendstr));
163:
164: receiver.await();
165: assertTrue("receiver should be done", receiver.done);
166: assertNull("receiver should have no exceptions",
167: receiver.exception);
168: assertNotNull("receiver should have received a message",
169: receiver.msg);
170:
171: String recvstr = receiver.msg.extractString();
172: assertTrue("strings shouldn't be identical", sendstr != recvstr);
173: assertEquals("strings should be equal", sendstr, recvstr);
174: }
175:
176: /**
177: * Tests send() followed by a receive().
178: */
179: void testSendReceive() throws IOException {
180: String sendstr = "foo";
181: Isolate i = Isolate.currentIsolate();
182: Link link = Link.newLink(i, i);
183: Sender sender = new Sender(link, LinkMessage
184: .newStringMessage(sendstr));
185:
186: assertFalse("sender should be blocked", sender.done);
187: assertNull("sender should have no exceptions", sender.exception);
188:
189: LinkMessage lm = link.receive();
190:
191: sender.await();
192: assertTrue("sender should be done", sender.done);
193: assertNull("sender should have no exceptions", sender.exception);
194:
195: String recvstr = lm.extractString();
196: assertTrue("strings shouldn't be identical", sendstr != recvstr);
197: assertEquals("strings should be equal", sendstr, recvstr);
198: }
199:
200: /**
201: * Tests that close() will unblock a thread blocked in receive().
202: */
203: void testReceiveClose() throws IOException {
204: Isolate i = Isolate.currentIsolate();
205: Link link = Link.newLink(i, i);
206: Receiver receiver = new Receiver(link);
207:
208: assertFalse("receiver should be blocked", receiver.done);
209: assertNull("receiver should have no exceptions",
210: receiver.exception);
211:
212: link.close();
213: receiver.await();
214: assertTrue("receiver should be done", receiver.done);
215:
216: boolean wasIIOE = receiver.exception instanceof InterruptedIOException;
217: assertTrue(
218: "receiver should have gotten InterruptedIOException",
219: wasIIOE);
220: if (!wasIIOE) {
221: System.out
222: .println("### receiver got " + receiver.exception);
223: }
224: }
225:
226: /**
227: * Tests that close() will unblock a thread blocked in send().
228: */
229: void testSendClose() throws IOException {
230: Isolate i = Isolate.currentIsolate();
231: Link link = Link.newLink(i, i);
232: Sender sender = new Sender(link, LinkMessage
233: .newStringMessage("foobar"));
234:
235: assertFalse("sender should be blocked", sender.done);
236: assertNull("sender should have no exceptions", sender.exception);
237:
238: link.close();
239: sender.await();
240: assertTrue("sender should be done", sender.done);
241:
242: boolean wasIIOE = sender.exception instanceof InterruptedIOException;
243: assertTrue("sender should have gotten InterruptedIOException",
244: wasIIOE);
245: if (!wasIIOE) {
246: System.out.println("### sender got " + sender.exception);
247: }
248: }
249:
250: /**
251: * Tests that the native rendezvous point structure is getting cleaned up
252: * properly by the finalizer.
253: */
254: void testCleanup() {
255: Isolate i = Isolate.currentIsolate();
256: Link link = Link.newLink(i, i);
257: int rp = link.hashCode();
258:
259: assertEquals("refcount must be 1", 1, Utils.getRefCount(link));
260:
261: Utils.forceGC();
262: /* ignored */Utils.getFreedRendezvousPoints();
263: link = null;
264: Utils.forceGC();
265: int[] freed = Utils.getFreedRendezvousPoints();
266: assertEquals("length must be one", 1, freed.length);
267: if (freed.length == 1) {
268: assertEquals("freed rp must match", rp, freed[0]);
269: }
270: }
271:
272: /**
273: * Tests link creation with isolates in different states.
274: */
275: void testIsolateStates() throws IOException,
276: IsolateStartupException {
277: boolean thrown;
278: Isolate us = Isolate.currentIsolate();
279: Isolate them = new Isolate("com.sun.midp.links.Empty", null);
280:
281: thrown = false;
282: try {
283: Link link = Link.newLink(us, them);
284: } catch (IllegalStateException ise) {
285: thrown = true;
286: }
287: assertTrue("started,new should throw ISE", thrown);
288:
289: thrown = false;
290: try {
291: Link link = Link.newLink(them, us);
292: } catch (IllegalStateException ise) {
293: thrown = true;
294: }
295: assertTrue("new,started should throw ISE", thrown);
296:
297: them.start();
298:
299: thrown = false;
300: try {
301: Link link = Link.newLink(us, them);
302: } catch (IllegalStateException ise) {
303: thrown = true;
304: }
305: assertFalse("started,started should not throw ISE", thrown);
306:
307: thrown = false;
308: try {
309: Link link = Link.newLink(them, us);
310: } catch (IllegalStateException ise) {
311: thrown = true;
312: }
313: assertFalse("started,started should not throw ISE", thrown);
314:
315: them.exit(0);
316: them.waitForExit();
317:
318: thrown = false;
319: try {
320: Link link = Link.newLink(us, them);
321: } catch (IllegalStateException ise) {
322: thrown = true;
323: }
324: assertTrue("started,terminated should throw ISE", thrown);
325:
326: thrown = false;
327: try {
328: Link link = Link.newLink(them, us);
329: } catch (IllegalStateException ise) {
330: thrown = true;
331: }
332: assertTrue("terminated,started should throw ISE", thrown);
333: }
334:
335: /**
336: * Tests receiver and sender isolate access for a link.
337: */
338: void testAccess() throws IOException, IsolateStartupException {
339: Isolate i1 = Isolate.currentIsolate();
340: Isolate i2 = new Isolate("com.sun.midp.links.Empty", null);
341: i2.start();
342: Link link1 = Link.newLink(i1, i2);
343: Link link2 = Link.newLink(i2, i1);
344: boolean thrown;
345:
346: thrown = false;
347: try {
348: LinkMessage lm = link1.receive();
349: } catch (IllegalArgumentException iae) {
350: thrown = true;
351: }
352: assertTrue("receive should catch IAE", thrown);
353:
354: thrown = false;
355: try {
356: link2.send(LinkMessage.newStringMessage("hello"));
357: } catch (IllegalArgumentException iae) {
358: thrown = true;
359: }
360: assertTrue("send should catch IAE", thrown);
361:
362: i2.exit(0);
363: }
364:
365: /**
366: * Runs all tests.
367: */
368: public void runTests() throws IOException, IsolateStartupException {
369: declare("testCreate");
370: testCreate();
371:
372: declare("testNulls");
373: testNulls();
374:
375: declare("testEquals");
376: testEquals();
377:
378: declare("testReceiveClosed");
379: testReceiveClosed();
380:
381: declare("testSendClosed");
382: testSendClosed();
383:
384: declare("testReceiveSend");
385: testReceiveSend();
386:
387: declare("testSendReceive");
388: testSendReceive();
389:
390: declare("testReceiveClose");
391: testReceiveClose();
392:
393: declare("testSendClose");
394: testSendClose();
395:
396: declare("testCleanup");
397: testCleanup();
398:
399: declare("testIsolateStates");
400: testIsolateStates();
401:
402: declare("testAccess");
403: testAccess();
404: }
405: }
|