Added
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.awt; |
43 |
|
44 |
import java.awt.event.ActionEvent; |
45 |
import java.util.logging.Level; |
46 |
import javax.swing.AbstractAction; |
47 |
import javax.swing.Action; |
48 |
import org.netbeans.junit.Log; |
49 |
import org.netbeans.junit.NbTestCase; |
50 |
import org.openide.filesystems.FileUtil; |
51 |
import org.openide.util.ContextAwareAction; |
52 |
import org.openide.util.Lookup; |
53 |
|
54 |
/** Verifies asynchronous aspects of actions systems are close to the |
55 |
* original behaviour of SystemAction one. |
56 |
* Taken from org.openide.util.actions.AsynchronousTest |
57 |
* @author Jaroslav Tulach |
58 |
*/ |
59 |
public class AsynchronousTest extends NbTestCase { |
60 |
|
61 |
private CharSequence err; |
62 |
|
63 |
public AsynchronousTest(String name) { |
64 |
super(name); |
65 |
} |
66 |
|
67 |
@Override |
68 |
protected int timeOut() { |
69 |
return 5000; |
70 |
} |
71 |
|
72 |
@Override |
73 |
protected boolean runInEQ() { |
74 |
return true; |
75 |
} |
76 |
|
77 |
@Override |
78 |
protected void setUp() { |
79 |
err = Log.enable("", Level.WARNING); |
80 |
AC.finished = false; |
81 |
} |
82 |
|
83 |
public void testExecutionOfActionsThatDoesNotDefineAsynchronousIsSynchronousNoWarningIsPrinted() throws Exception { |
84 |
Action action = (Action)FileUtil.getConfigFile("actions/async/none.instance").getAttribute("instanceCreate"); |
85 |
|
86 |
synchronized (AsynchronousTest.class) { |
87 |
action.actionPerformed(new ActionEvent(this, 0, "")); |
88 |
assertTrue("The synchronous action is finished immediatelly", AC.finished); |
89 |
} |
90 |
|
91 |
if (err.length() > 0) { |
92 |
fail("There should be no warning about missing asynchronous: " + err); |
93 |
} |
94 |
} |
95 |
|
96 |
public void testExecutionCanBeAsynchronous() throws Exception { |
97 |
Action action = (Action)FileUtil.getConfigFile("actions/async/true.instance").getAttribute("instanceCreate"); |
98 |
|
99 |
synchronized (AsynchronousTest.class) { |
100 |
action.actionPerformed(new ActionEvent(this, 0, "")); |
101 |
Thread.sleep(500); |
102 |
assertFalse("Shall Not be finished yet", AC.finished); |
103 |
AsynchronousTest.class.wait(); |
104 |
assertTrue("The asynchronous action is finished", AC.finished); |
105 |
} |
106 |
|
107 |
if (err.length() > 0) { |
108 |
fail("No warning about the class: " + err); |
109 |
} |
110 |
} |
111 |
|
112 |
public void testExecutionCanBeAsynchronousForAlways() throws Exception { |
113 |
Action action = (Action)FileUtil.getConfigFile("actions/async/true-always.instance").getAttribute("instanceCreate"); |
114 |
|
115 |
synchronized (AsynchronousTest.class) { |
116 |
action.actionPerformed(new ActionEvent(this, 0, "")); |
117 |
Thread.sleep(500); |
118 |
assertFalse("Shall Not be finished yet", AC.finished); |
119 |
AsynchronousTest.class.wait(); |
120 |
assertTrue("The asynchronous action is finished", AC.finished); |
121 |
} |
122 |
|
123 |
if (err.length() > 0) { |
124 |
fail("No warning about the class: " + err); |
125 |
} |
126 |
} |
127 |
public void testExecutionCanBeSynchronous() throws Exception { |
128 |
Action action = (Action)FileUtil.getConfigFile("actions/async/false.instance").getAttribute("instanceCreate"); |
129 |
|
130 |
synchronized (AsynchronousTest.class) { |
131 |
action.actionPerformed(new ActionEvent(this, 0, "")); |
132 |
assertTrue("The synchronous action is finished immediatelly", AC.finished); |
133 |
} |
134 |
|
135 |
if (err.length() > 0) { |
136 |
fail("No warning about the class: " + err); |
137 |
} |
138 |
} |
139 |
|
140 |
public void testExecutionCanBeForcedToBeSynchronous() throws Exception { |
141 |
Action action = (Action)FileUtil.getConfigFile("actions/async/true.instance").getAttribute("instanceCreate"); |
142 |
|
143 |
synchronized (AsynchronousTest.class) { |
144 |
action.actionPerformed(new ActionEvent(this, 0, "waitFinished")); |
145 |
assertTrue("When asked for synchronous the action is finished immediatelly", AC.finished); |
146 |
} |
147 |
|
148 |
if (err.length() > 0) { |
149 |
fail("No warning about the class: " + err); |
150 |
} |
151 |
} |
152 |
|
153 |
public void testExecutionCanBeAsynchronousForContext() throws Exception { |
154 |
Action action = (Action)FileUtil.getConfigFile("actions/async/true-context.instance").getAttribute("instanceCreate"); |
155 |
|
156 |
synchronized (AsynchronousTest.class) { |
157 |
action.actionPerformed(new ActionEvent(this, 0, "")); |
158 |
Thread.sleep(500); |
159 |
assertFalse("Shall Not be finished yet", AC.finished); |
160 |
AsynchronousTest.class.wait(); |
161 |
assertTrue("The asynchronous action is finished", AC.finished); |
162 |
} |
163 |
|
164 |
if (err.length() > 0) { |
165 |
fail("No warning about the class: " + err); |
166 |
} |
167 |
} |
168 |
|
169 |
public static class AC extends AbstractAction { |
170 |
static boolean finished; |
171 |
|
172 |
public void actionPerformed(ActionEvent ev) { |
173 |
synchronized (AsynchronousTest.class) { |
174 |
AsynchronousTest.class.notifyAll(); |
175 |
finished = true; |
176 |
} |
177 |
} |
178 |
} |
179 |
|
180 |
public static class CAC extends AC implements ContextAwareAction { |
181 |
public Action createContextAwareInstance(Lookup actionContext) { |
182 |
return this; |
183 |
} |
184 |
} |
185 |
} |