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.i3test;
028:
029: import com.sun.cldc.util.Semaphore;
030:
031: public class TestIsolateSynch extends TestCase {
032:
033: boolean isBlocked;
034:
035: /**
036: * Tests semaphores are null prior to initialization.
037: */
038: void testNull() {
039: // no initialization here!
040: Semaphore s1 = IsolateSynch.getSemReady0();
041: Semaphore s2 = IsolateSynch.getSemContinue0();
042: assertNull(s1);
043: assertNull(s2);
044: }
045:
046: /**
047: * Checks semaphore identities after init, null after fini.
048: */
049: void testInitFini() {
050: IsolateSynch.init();
051:
052: Semaphore s1 = IsolateSynch.getSemReady0();
053: Semaphore s2 = IsolateSynch.getSemContinue0();
054: assertSame("semReady", IsolateSynch.semReady, s1);
055: assertSame("semContinue", IsolateSynch.semContinue, s2);
056:
057: IsolateSynch.fini();
058:
059: assertNull(IsolateSynch.semReady);
060: assertNull(IsolateSynch.semContinue);
061: assertNull(IsolateSynch.getSemReady0());
062: assertNull(IsolateSynch.getSemContinue0());
063: }
064:
065: /**
066: * Synchronizes with another thread within this isolate.
067: */
068: void testSynch() {
069: IsolateSynch.init();
070:
071: Thread t = new Thread() {
072: public void run() {
073: isBlocked = true;
074: IsolateSynch.pause();
075: isBlocked = false;
076: }
077: };
078:
079: isBlocked = false;
080: t.start();
081: IsolateSynch.awaitReady();
082: assertTrue(isBlocked);
083: IsolateSynch.signalContinue();
084: try {
085: t.join();
086: } catch (InterruptedException ignore) {
087: }
088: assertTrue(!isBlocked);
089:
090: IsolateSynch.fini();
091: }
092:
093: /**
094: * Tests what happens if initialization is done twice.
095: */
096: void testDoubleInit() {
097: boolean thrown = false;
098:
099: IsolateSynch.init();
100: try {
101: IsolateSynch.init();
102: } catch (IllegalStateException ise) {
103: thrown = true;
104: }
105: assertTrue("IllegalStateException not thrown", thrown);
106: IsolateSynch.fini();
107: }
108:
109: /**
110: * Runs all the tests.
111: */
112: public void runTests() {
113: declare("testNull");
114: testNull();
115: declare("testInitFini");
116: testInitFini();
117: declare("testSynch");
118: testSynch();
119: declare("testDoubleInit");
120: testDoubleInit();
121: }
122:
123: }
|