Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
|
42 |
package org.openide.util; |
43 |
|
44 |
import junit.framework.Test; |
45 |
import org.netbeans.junit.NbTestCase; |
46 |
import org.netbeans.junit.NbTestSuite; |
47 |
|
48 |
|
49 |
public class MutexWrapTest extends NbTestCase implements Mutex.Wrapper { |
50 |
Mutex.Privileged p; |
51 |
Mutex m; |
52 |
ThreadLocal<Object> IN = new ThreadLocal<Object>(); |
53 |
|
54 |
public MutexWrapTest(java.lang.String testName) { |
55 |
super(testName); |
56 |
} |
57 |
|
58 |
public static Test suite() { |
59 |
NbTestSuite suite = new NbTestSuite(MutexWrapTest.class); |
60 |
|
61 |
return suite; |
62 |
} |
63 |
|
64 |
/** Sets up the test. |
65 |
*/ |
66 |
@Override |
67 |
protected void setUp () { |
68 |
p = new Mutex.Privileged (); |
69 |
m = new Mutex (p, this); |
70 |
} |
71 |
|
72 |
public void testRead() throws Exception { |
73 |
A arg = new A(); |
74 |
Object ret = m.readAccess(arg); |
75 |
assertEquals("Return type is ok", arg, ret); |
76 |
} |
77 |
public void testWrite() throws Exception { |
78 |
A arg = new A(); |
79 |
Object ret = m.writeAccess(arg); |
80 |
assertEquals("Return type is ok", arg, ret); |
81 |
} |
82 |
public void testExRead() throws Exception { |
83 |
E arg = new E(); |
84 |
Object ret = m.readAccess(arg); |
85 |
assertEquals("Return type is ok", arg, ret); |
86 |
} |
87 |
public void testExWrite() throws Exception { |
88 |
E arg = new E(); |
89 |
Object ret = m.writeAccess(arg); |
90 |
assertEquals("Return type is ok", arg, ret); |
91 |
} |
92 |
public void testPostRead() throws Exception { |
93 |
R arg = new R(); |
94 |
m.postReadRequest(arg); |
95 |
assertTrue("Executed", arg.exec); |
96 |
} |
97 |
public void testPostReadFromWrite() throws Exception { |
98 |
R arg = new R(); |
99 |
Del del = new Del(); |
100 |
del.read = arg; |
101 |
m.writeAccess(del); |
102 |
assertTrue("Executed", arg.exec); |
103 |
} |
104 |
public void testPostWrite() throws Exception { |
105 |
R arg = new R(); |
106 |
m.postWriteRequest(arg); |
107 |
assertTrue("Executed", arg.exec); |
108 |
} |
109 |
public void testPostWriteFromRead() throws Exception { |
110 |
R arg = new R(); |
111 |
Del del = new Del(); |
112 |
del.write = arg; |
113 |
m.readAccess(del); |
114 |
assertTrue("Executed", arg.exec); |
115 |
} |
116 |
|
117 |
|
118 |
public Runnable wrap(final Runnable run) { |
119 |
return new Runnable() { |
120 |
public void run() { |
121 |
Object prev = IN.get(); |
122 |
IN.set(MutexWrapTest.this); |
123 |
try { |
124 |
run.run(); |
125 |
} finally { |
126 |
IN.set(prev); |
127 |
} |
128 |
assertFalse("No read", m.isReadAccess()); |
129 |
assertFalse("No write", m.isWriteAccess()); |
130 |
} |
131 |
}; |
132 |
} |
133 |
private class A implements Mutex.Action<Object> { |
134 |
boolean exec; |
135 |
|
136 |
public Object run() { |
137 |
exec = true; |
138 |
assertEquals("I am wrapped", MutexWrapTest.this, IN.get()); |
139 |
return this; |
140 |
} |
141 |
} |
142 |
private class E implements Mutex.ExceptionAction<Object> { |
143 |
boolean exec; |
144 |
|
145 |
public Object run() { |
146 |
exec = true; |
147 |
assertEquals("I am wrapped", MutexWrapTest.this, IN.get()); |
148 |
return this; |
149 |
} |
150 |
} |
151 |
private class R implements Runnable { |
152 |
boolean exec; |
153 |
|
154 |
public void run() { |
155 |
exec = true; |
156 |
assertEquals("I am wrapped", MutexWrapTest.this, IN.get()); |
157 |
} |
158 |
} |
159 |
private class Del implements Runnable { |
160 |
Runnable write; |
161 |
Runnable read; |
162 |
|
163 |
public void run() { |
164 |
if (write != null) { |
165 |
m.writeAccess(write); |
166 |
} |
167 |
if (read != null) { |
168 |
m.writeAccess(read); |
169 |
} |
170 |
} |
171 |
} |
172 |
} |