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:
033: /**
034: * Tests for the LinkPortal class.
035: */
036: public class TestLinkPortal extends TestCase {
037:
038: class Getter extends Thread {
039: boolean done = false;
040: Object result = null;
041:
042: Getter() {
043: this .start();
044: Utils.sleep(50);
045: }
046:
047: Object await() {
048: try {
049: synchronized (this ) {
050: while (!done) {
051: wait();
052: }
053: }
054: } catch (InterruptedException ignore) {
055: }
056:
057: return result;
058: }
059:
060: public void run() {
061: try {
062: result = LinkPortal.getLinks();
063: } catch (Throwable t) {
064: result = t;
065: }
066:
067: synchronized (this ) {
068: done = true;
069: notifyAll();
070: }
071: }
072: }
073:
074: /**
075: * Tests passing null for arguments.
076: */
077: void testNulls() {
078: boolean thrown;
079: Isolate is = Isolate.currentIsolate();
080: Link[] la = new Link[3];
081: la[0] = Link.newLink(is, is);
082: la[1] = Link.newLink(is, is);
083: la[2] = Link.newLink(is, is);
084:
085: thrown = false;
086: try {
087: LinkPortal.setLinks(null, la);
088: } catch (NullPointerException npe) {
089: thrown = true;
090: }
091: assertTrue("null isolate should throw NPE", thrown);
092:
093: thrown = false;
094: try {
095: LinkPortal.setLinks(is, null);
096: } catch (NullPointerException npe) {
097: thrown = true;
098: }
099: assertFalse("null link array shouldn't throw NPE", thrown);
100:
101: la[1] = null;
102: thrown = false;
103: try {
104: LinkPortal.setLinks(is, la);
105: } catch (NullPointerException npe) {
106: thrown = true;
107: }
108: assertTrue("link array with nulls should throw NPE", thrown);
109: }
110:
111: /**
112: * Tests zero-length setting and getting operations.
113: */
114: void testZero() {
115: Link[] la;
116: Isolate is = Isolate.currentIsolate();
117:
118: LinkPortal.setLinks(is, new Link[0]);
119: la = LinkPortal.getLinks();
120: assertNotNull("shouldn't be null", la);
121: assertEquals("length zero", 0, la.length);
122: }
123:
124: /**
125: * Tests setting a closed link.
126: */
127: void testClosed() {
128: boolean thrown;
129: Link[] la;
130: Isolate is = Isolate.currentIsolate();
131:
132: la = new Link[3];
133: la[0] = Link.newLink(is, is);
134: la[1] = Link.newLink(is, is);
135: la[2] = Link.newLink(is, is);
136:
137: la[1].close();
138: thrown = false;
139: try {
140: LinkPortal.setLinks(is, la);
141: } catch (IllegalArgumentException iae) {
142: thrown = true;
143: }
144: assertTrue("closed link should throw IAE", thrown);
145: }
146:
147: /**
148: * Tests blocking getLinks(), followed by setLinks().
149: */
150: void testBlockedGet() {
151: Isolate is = Isolate.currentIsolate();
152:
153: LinkPortal.setLinks(is, new Link[] { Link.newLink(is, is) });
154: LinkPortal.setLinks(is, null);
155:
156: Getter get1 = new Getter();
157: assertFalse("get1 should be blocked", get1.done);
158:
159: LinkPortal.setLinks(is, new Link[] { Link.newLink(is, is) });
160: Object result = get1.await();
161:
162: assertTrue("get1 should return Link[]",
163: result instanceof Link[]);
164: Link[] gotten = (Link[]) result;
165: assertEquals("length 1", 1, gotten.length);
166:
167: Getter get2 = new Getter();
168: assertFalse("get2 should be blocked", get2.done);
169:
170: LinkPortal.setLinks(is, new Link[0]);
171: result = get2.await();
172: assertTrue("get2 should return Link[]",
173: result instanceof Link[]);
174: gotten = (Link[]) result;
175: assertEquals("length 0", 0, gotten.length);
176: }
177:
178: /**
179: * Tests setting and getting of actual data.
180: */
181: void testActual() {
182: Link[] la1;
183: Link[] la2;
184: Isolate is = Isolate.currentIsolate();
185:
186: la1 = new Link[3];
187: la1[0] = Link.newLink(is, is);
188: la1[1] = Link.newLink(is, is);
189: la1[2] = Link.newLink(is, is);
190:
191: LinkPortal.setLinks(is, la1);
192: la2 = LinkPortal.getLinks();
193:
194: assertEquals("lengths should be equal", la1.length, la2.length);
195: for (int i = 0; i < la1.length; i++) {
196: assertNotSame("link not same", la1[i], la2[i]);
197: assertTrue("links equal", la1[i].equals(la2[i]));
198: assertEquals("refcount 2", 2, Utils.getRefCount(la2[i]));
199: }
200:
201: for (int i = 0; i < la1.length; i++) {
202: Utils.forceGC();
203: Utils.getFreedRendezvousPoints();
204:
205: int hash = la1[i].hashCode();
206: la1[i] = la2[i] = null;
207: Utils.forceGC();
208: int[] ia = Utils.getFreedRendezvousPoints();
209: assertEquals("one freed", 1, ia.length);
210: assertEquals("freed one matches", hash, ia[0]);
211: }
212: }
213:
214: /**
215: * Tests replacement of a link array and proper cleanup.
216: */
217: void testReplace() {
218: Isolate is = Isolate.currentIsolate();
219: Link[] la = new Link[3];
220:
221: la[0] = Link.newLink(is, is);
222: la[1] = Link.newLink(is, is);
223: la[2] = Link.newLink(is, is);
224:
225: LinkPortal.setLinks(is, la);
226:
227: assertEquals("refcount 2", 2, Utils.getRefCount(la[0]));
228: assertEquals("refcount 2", 2, Utils.getRefCount(la[1]));
229: assertEquals("refcount 2", 2, Utils.getRefCount(la[2]));
230:
231: LinkPortal.setLinks(is, new Link[0]);
232:
233: assertEquals("refcount 1", 1, Utils.getRefCount(la[0]));
234: assertEquals("refcount 1", 1, Utils.getRefCount(la[1]));
235: assertEquals("refcount 1", 1, Utils.getRefCount(la[2]));
236:
237: for (int i = 0; i < la.length; i++) {
238: Utils.forceGC();
239: Utils.getFreedRendezvousPoints();
240:
241: int hash = la[i].hashCode();
242: la[i] = null;
243: Utils.forceGC();
244: int[] ia = Utils.getFreedRendezvousPoints();
245: assertEquals("one freed", 1, ia.length);
246: assertEquals("freed one matches", hash, ia[0]);
247: }
248:
249: // clean up
250: LinkPortal.setLinks(is, null);
251: }
252:
253: /**
254: * Tests whether setLinks does proper checking on the isolate's state.
255: */
256: void testIsolateState() throws IsolateStartupException {
257: Isolate us = Isolate.currentIsolate();
258: Isolate them = new Isolate("com.sun.midp.links.Empty", null);
259: Link[] la = new Link[1];
260: boolean thrown;
261:
262: la[0] = Link.newLink(us, us);
263:
264: thrown = false;
265: try {
266: LinkPortal.setLinks(them, la);
267: } catch (IllegalStateException ise) {
268: thrown = true;
269: }
270: assertTrue("not started: setLinks should throw ISE", thrown);
271:
272: them.start();
273: thrown = false;
274: try {
275: LinkPortal.setLinks(them, la);
276: } catch (IllegalStateException ise) {
277: thrown = true;
278: } finally {
279: LinkPortal.setLinks(them, null); // clean up
280: }
281: assertFalse("started: setLinks shouldn't throw ISE", thrown);
282:
283: them.exit(0);
284: them.waitForExit();
285: thrown = false;
286: try {
287: LinkPortal.setLinks(them, la);
288: } catch (IllegalStateException ise) {
289: thrown = true;
290: }
291: assertTrue("exited: setLinks should throw ISE", thrown);
292: }
293:
294: /**
295: * Runs all tests.
296: */
297: public void runTests() throws IsolateStartupException {
298: declare("testNulls");
299: testNulls();
300: declare("testZero");
301: testZero();
302: declare("testClosed");
303: testClosed();
304: declare("testBlockedGet");
305: testBlockedGet();
306: declare("testActual");
307: testActual();
308: declare("testReplace");
309: testReplace();
310: declare("testIsolateState");
311: testIsolateState();
312: }
313:
314: }
|