001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package threaddemo.locking;
043:
044: import java.awt.EventQueue;
045: import java.io.IOException;
046: import java.lang.reflect.InvocationTargetException;
047: import junit.framework.TestCase;
048: import junit.framework.TestSuite;
049: import junit.textui.TestRunner;
050:
051: /**
052: * Test Locks.event.
053: * @author Jesse Glick
054: */
055: public class EventLockTest extends TestCase {
056:
057: public EventLockTest(String name) {
058: super (name);
059: }
060:
061: public void testEventAccess() throws Exception {
062: assertTrue("Not starting in AWT", !EventQueue
063: .isDispatchThread());
064: // test canRead, canWrite, correct thread used by synch methods
065: assertTrue(!Locks.event().canRead());
066: assertTrue(!Locks.event().canWrite());
067: assertTrue(Locks.event().read(new LockAction<Boolean>() {
068: public Boolean run() {
069: return EventQueue.isDispatchThread()
070: && Locks.event().canRead()
071: && Locks.event().canWrite();
072: }
073: }));
074: assertTrue(Locks.event().read(
075: new LockExceptionAction<Boolean, Exception>() {
076: public Boolean run() throws Exception {
077: return EventQueue.isDispatchThread()
078: && Locks.event().canRead()
079: && Locks.event().canWrite();
080: }
081: }));
082: assertTrue(Locks.event().write(new LockAction<Boolean>() {
083: public Boolean run() {
084: return EventQueue.isDispatchThread()
085: && Locks.event().canRead()
086: && Locks.event().canWrite();
087: }
088: }));
089: assertTrue(Locks.event().write(
090: new LockExceptionAction<Boolean, Exception>() {
091: public Boolean run() throws Exception {
092: return EventQueue.isDispatchThread()
093: && Locks.event().canRead()
094: && Locks.event().canWrite();
095: }
096: }));
097: // test that r/wA(Runnable) runs in AWT eventually
098: final boolean[] b = new boolean[1];
099: // first, that r/wA will run (even asynch)
100: Locks.event().readLater(new Runnable() {
101: public void run() {
102: synchronized (b) {
103: b[0] = EventQueue.isDispatchThread();
104: b.notify();
105: }
106: }
107: });
108: synchronized (b) {
109: if (!b[0])
110: b.wait(9999);
111: }
112: assertTrue(b[0]);
113: Locks.event().writeLater(new Runnable() {
114: public void run() {
115: synchronized (b) {
116: b[0] = !EventQueue.isDispatchThread();
117: b.notify();
118: }
119: }
120: });
121: synchronized (b) {
122: if (b[0])
123: b.wait(9999);
124: }
125: assertTrue(!b[0]);
126: /*
127: // now that r/wA runs synch in event thread
128: EventQueue.invokeAndWait(new Runnable() {
129: public void run() {
130: Locks.event().readLater(new Runnable() {
131: public void run() {
132: b[0] = EventQueue.isDispatchThread();
133: }
134: });
135: }
136: });
137: assertTrue(b[0]);
138: EventQueue.invokeAndWait(new Runnable() {
139: public void run() {
140: Locks.event().writeLater(new Runnable() {
141: public void run() {
142: b[0] = !EventQueue.isDispatchThread();
143: }
144: });
145: }
146: });
147: assertTrue(!b[0]);
148: */
149: }
150:
151: public void testEventExceptions() throws Exception {
152: assertTrue("Not starting in AWT", !EventQueue
153: .isDispatchThread());
154: // test that checked excs from M.EA throw correct ME
155: try {
156: Locks.event().read(
157: new LockExceptionAction<Void, IOException>() {
158: public Void run() throws IOException {
159: throw new IOException();
160: }
161: });
162: fail();
163: } catch (IOException e) {
164: // OK
165: }
166: // but that unchecked excs are passed thru
167: try {
168: Locks.event().read(
169: new LockExceptionAction<Void, IOException>() {
170: public Void run() throws IOException {
171: throw new IllegalArgumentException();
172: }
173: });
174: fail();
175: } catch (IllegalArgumentException e) {
176: // OK
177: } catch (IOException e) {
178: fail(e.toString());
179: }
180: // similarly for unchecked excs from M.A
181: try {
182: Locks.event().read(new LockAction<Void>() {
183: public Void run() {
184: throw new IllegalArgumentException();
185: }
186: });
187: fail();
188: } catch (IllegalArgumentException e) {
189: // OK
190: } catch (RuntimeException e) {
191: fail(e.toString());
192: }
193: // and blocking runnables
194: try {
195: Locks.event().read(new Runnable() {
196: public void run() {
197: throw new IllegalArgumentException();
198: }
199: });
200: fail();
201: } catch (IllegalArgumentException e) {
202: // OK.
203: }
204: try {
205: Locks.event().write(new Runnable() {
206: public void run() {
207: throw new IllegalArgumentException();
208: }
209: });
210: fail();
211: } catch (IllegalArgumentException e) {
212: // OK.
213: }
214: }
215:
216: }
|