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 org.openide.util;
043:
044: import java.util.concurrent.Executor;
045: import junit.framework.Test;
046: import org.netbeans.junit.NbTestCase;
047: import org.netbeans.junit.NbTestSuite;
048: import org.openide.util.Mutex.Action;
049: import org.openide.util.Mutex.ExceptionAction;
050:
051: public class MutexWrapTest extends NbTestCase implements Executor {
052: Mutex.Privileged p;
053: Mutex m;
054: ThreadLocal<Object> IN = new ThreadLocal<Object>();
055:
056: public MutexWrapTest(java.lang.String testName) {
057: super (testName);
058: }
059:
060: public static Test suite() {
061: // return new MutexWrapTest("testPostRead");
062: return new NbTestSuite(MutexWrapTest.class);
063: }
064:
065: /** Sets up the test.
066: */
067: @Override
068: protected void setUp() {
069: p = new Mutex.Privileged();
070: m = new Mutex(p, this );
071: }
072:
073: public void testRead() throws Exception {
074: A arg = new A();
075: Object ret = m.readAccess(arg);
076: assertEquals("Return type is ok", arg, ret);
077: }
078:
079: public void testWrite() throws Exception {
080: A arg = new A();
081: Object ret = m.writeAccess(arg);
082: assertEquals("Return type is ok", arg, ret);
083: }
084:
085: public void testExRead() throws Exception {
086: E arg = new E();
087: Object ret = m.readAccess(arg);
088: assertEquals("Return type is ok", arg, ret);
089: }
090:
091: public void testExWrite() throws Exception {
092: E arg = new E();
093: Object ret = m.writeAccess(arg);
094: assertEquals("Return type is ok", arg, ret);
095: }
096:
097: public void testRunRead() throws Exception {
098: R arg = new R();
099: m.readAccess(arg);
100: assertTrue("Executed", arg.exec);
101: }
102:
103: public void testRunWrite() throws Exception {
104: R arg = new R();
105: m.writeAccess(arg);
106: assertTrue("Executed", arg.exec);
107: }
108:
109: public void testPostRead() throws Exception {
110: R arg = new R();
111: m.postReadRequest(arg);
112: assertTrue("Executed", arg.exec);
113: }
114:
115: public void testPostReadFromWrite() throws Exception {
116: R arg = new R();
117: Del del = new Del();
118: del.read = arg;
119: m.writeAccess(del);
120: assertTrue("Executed", arg.exec);
121: }
122:
123: public void testPostReadFromRead() throws Exception {
124: R arg = new R();
125: Del del = new Del();
126: del.read = arg;
127: m.readAccess(del);
128: assertTrue("Executed", arg.exec);
129: }
130:
131: public void testPostWrite() throws Exception {
132: R arg = new R();
133: m.postWriteRequest(arg);
134: assertTrue("Executed", arg.exec);
135: }
136:
137: public void testPostWriteFromRead() throws Exception {
138: R arg = new R();
139: Del del = new Del();
140: del.write = arg;
141: m.readAccess(del);
142: assertTrue("Executed", arg.exec);
143: }
144:
145: public void testPostWriteFromWrite() throws Exception {
146: R arg = new R();
147: Del del = new Del();
148: del.write = arg;
149: m.writeAccess(del);
150: assertTrue("Executed", arg.exec);
151: }
152:
153: public void testReadAndRead() throws Exception {
154: R arg = new R();
155: Del del = new Del();
156: del.readNow = arg;
157: m.readAccess(del);
158: assertTrue("Executed", arg.exec);
159: }
160:
161: public void testWriteAndRead() throws Exception {
162: R arg = new R();
163: Del del = new Del();
164: del.readNow = arg;
165: m.writeAccess(del);
166: assertTrue("Executed", arg.exec);
167: }
168:
169: public void testWriteAndWrite() throws Exception {
170: R arg = new R();
171: Del del = new Del();
172: del.writeNow = arg;
173: m.writeAccess(del);
174: assertTrue("Executed", arg.exec);
175: }
176:
177: private class A implements Mutex.Action<Object> {
178: boolean exec;
179:
180: public Object run() {
181: exec = true;
182: assertEquals("I am wrapped", MutexWrapTest.this , IN.get());
183: return this ;
184: }
185: }
186:
187: private class E implements Mutex.ExceptionAction<Object> {
188: boolean exec;
189:
190: public Object run() {
191: exec = true;
192: assertEquals("I am wrapped", MutexWrapTest.this , IN.get());
193: return this ;
194: }
195: }
196:
197: private class R implements Runnable {
198: boolean exec;
199:
200: public void run() {
201: exec = true;
202: assertEquals("I am wrapped", MutexWrapTest.this , IN.get());
203: }
204: }
205:
206: private class Del implements Runnable {
207: Runnable write;
208: Runnable read;
209: Runnable writeNow;
210: Runnable readNow;
211:
212: public void run() {
213: if (write != null) {
214: m.postWriteRequest(write);
215: }
216: if (read != null) {
217: m.postReadRequest(read);
218: }
219: if (writeNow != null) {
220: m.writeAccess(writeNow);
221: }
222: if (readNow != null) {
223: m.readAccess(readNow);
224: }
225: }
226: }
227:
228: public void execute(final Runnable run) {
229: Object prev = IN.get();
230: assertEquals("No previous value set", null, prev);
231: IN.set(MutexWrapTest.this );
232: try {
233: run.run();
234: } finally {
235: IN.set(prev);
236: }
237: assertFalse("No read", m.isReadAccess());
238: assertFalse("No write", m.isWriteAccess());
239: }
240:
241: }
|