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 java.util.Hashtable;
030: import java.util.Enumeration;
031: import java.io.IOException;
032: import java.io.InterruptedIOException;
033:
034: import com.sun.cldc.isolate.Isolate;
035: import com.sun.cldc.isolate.IsolateStartupException;
036: import com.sun.midp.i3test.TestCase;
037:
038: /**
039: * Tests for the LinkPortal class.
040: */
041: public class TestNamedLinkPortal extends TestCase {
042:
043: class LinksReceiver extends Thread {
044: Link receiveLink = null;
045: boolean done = false;
046: Object result = null;
047:
048: LinksReceiver(Link receiveLink) {
049: this .receiveLink = receiveLink;
050: this .start();
051: }
052:
053: Object await() {
054: try {
055: synchronized (this ) {
056: while (!done) {
057: wait();
058: }
059: }
060: } catch (InterruptedException ignore) {
061: }
062:
063: return result;
064: }
065:
066: public void run() {
067: try {
068: NamedLinkPortal.receiveLinks(receiveLink);
069: } catch (Throwable t) {
070: result = t;
071: }
072:
073: synchronized (this ) {
074: done = true;
075: notifyAll();
076: }
077: }
078: }
079:
080: /**
081: * Tests passing illegal arguments.
082: */
083: void testIllegals1() throws InterruptedIOException, IOException {
084: boolean thrown;
085:
086: Isolate is = Isolate.currentIsolate();
087: Link sendLink = Link.newLink(is, is);
088: Link[] links = new Link[3];
089: links[0] = Link.newLink(is, is);
090: links[1] = Link.newLink(is, is);
091: links[2] = Link.newLink(is, is);
092:
093: thrown = false;
094: try {
095: NamedLinkPortal.putLink(null, links[0]);
096: } catch (IllegalArgumentException iae) {
097: thrown = true;
098: }
099: assertTrue("putting link with null name should throw IAE",
100: thrown);
101:
102: thrown = false;
103: try {
104: NamedLinkPortal.putLink("link1", null);
105: } catch (IllegalArgumentException iae) {
106: thrown = true;
107: }
108: assertTrue("putting null link should throw IAE", thrown);
109:
110: thrown = false;
111: try {
112: NamedLinkPortal.sendLinks(null);
113: } catch (IllegalArgumentException iae) {
114: thrown = true;
115: }
116: assertTrue("null send link should throw IAE", thrown);
117:
118: NamedLinkPortal.putLink("link0", links[0]);
119: NamedLinkPortal.putLink("link1", links[1]);
120: NamedLinkPortal.putLink("link2", links[2]);
121:
122: thrown = false;
123: try {
124: Link l = NamedLinkPortal.getLink("link1");
125: } catch (IllegalStateException ise) {
126: thrown = true;
127: }
128: assertTrue(
129: "getting link before links have been received should"
130: + " throw ISE", thrown);
131:
132: thrown = false;
133: links[1].close();
134: try {
135: NamedLinkPortal.sendLinks(sendLink);
136: } catch (IllegalStateException ise) {
137: thrown = true;
138: }
139: assertTrue("trying to send closed link should throw ISE",
140: thrown);
141:
142: }
143:
144: /**
145: * Tests setting and getting of actual data.
146: */
147: void testActual() throws InterruptedIOException, IOException {
148: Isolate is = Isolate.currentIsolate();
149:
150: Link sendLink = Link.newLink(is, is);
151: Link[] links = new Link[3];
152: links[0] = Link.newLink(is, is);
153: links[1] = Link.newLink(is, is);
154: links[2] = Link.newLink(is, is);
155:
156: LinksReceiver lr = new LinksReceiver(sendLink);
157: assertFalse("LinksReceiver should be blocked", lr.done);
158:
159: NamedLinkPortal.putLink("link0", links[0]);
160: NamedLinkPortal.putLink("link1", links[1]);
161: NamedLinkPortal.putLink("link2", links[2]);
162: NamedLinkPortal.sendLinks(sendLink);
163:
164: Object result = lr.await();
165: assertTrue("links received without error", result == null);
166:
167: for (int i = 0; i < 3; i++) {
168: Link l = NamedLinkPortal.getLink("link" + i);
169:
170: assertNotSame("link not same", l, links[i]);
171: assertTrue("links equal", l.equals(links[i]));
172: }
173: }
174:
175: /**
176: * Tests passing illegal arguments.
177: */
178: void testIllegals2() throws InterruptedIOException, IOException {
179: boolean thrown;
180: Isolate is = Isolate.currentIsolate();
181: Link sendLink = Link.newLink(is, is);
182: Link l = Link.newLink(is, is);
183:
184: thrown = false;
185: try {
186: NamedLinkPortal.putLink("link", l);
187: } catch (IllegalStateException iae) {
188: thrown = true;
189: }
190: assertTrue(
191: "trying to put link after links have been sent should"
192: + " throw ISE", thrown);
193:
194: thrown = false;
195: try {
196: NamedLinkPortal.sendLinks(sendLink);
197: } catch (IllegalStateException iae) {
198: thrown = true;
199: }
200: assertTrue("trying to send links again should throw ISE",
201: thrown);
202:
203: LinksReceiver lr = new LinksReceiver(sendLink);
204: Object result = lr.await();
205: assertTrue(
206: "trying to receive links again should produce error",
207: result != null);
208: }
209:
210: /**
211: * Runs all tests.
212: */
213: public void runTests() throws InterruptedIOException, IOException {
214: declare("testIllegals1");
215: testIllegals1();
216:
217: declare("testActual");
218: testActual();
219:
220: declare("testIllegals2");
221: testIllegals2();
222: }
223: }
|