36744
|
1 |
/*
|
|
2 |
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package org.netbeans.jemmy;
|
|
24 |
|
|
25 |
import java.util.Optional;
|
|
26 |
|
|
27 |
/**
|
|
28 |
*
|
|
29 |
* Runs actions with or without waiting.
|
|
30 |
*
|
|
31 |
* <BR><BR>Timeouts used: <BR>
|
|
32 |
* ActionProducer.MaxActionTime - time action should be finished in. <BR>
|
|
33 |
*
|
|
34 |
* @see Action
|
|
35 |
* @see Timeouts
|
|
36 |
*
|
|
37 |
* @author Alexandre Iline (alexandre.iline@oracle.com)
|
|
38 |
*/
|
|
39 |
public class ActionProducer<R, P> extends Thread
|
|
40 |
implements Action<R, P>, Waitable<Optional<R>, P>, Timeoutable {
|
|
41 |
|
|
42 |
private final static long ACTION_TIMEOUT = 10000;
|
|
43 |
|
|
44 |
private Action<R, P> action;
|
|
45 |
private boolean needWait = true;
|
|
46 |
private P parameter;
|
|
47 |
private boolean finished;
|
|
48 |
private R result = null;
|
|
49 |
private Timeouts timeouts;
|
|
50 |
private Waiter<Optional<R>, P> waiter;
|
|
51 |
private TestOut output;
|
|
52 |
private Throwable exception;
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Creates a producer for an action.
|
|
56 |
*
|
|
57 |
* @param a Action implementation.
|
|
58 |
*/
|
|
59 |
public ActionProducer(Action<R, P> a) {
|
|
60 |
super();
|
|
61 |
waiter = new Waiter<>(this);
|
|
62 |
action = a;
|
|
63 |
setTimeouts(JemmyProperties.getProperties().getTimeouts());
|
|
64 |
setOutput(JemmyProperties.getProperties().getOutput());
|
|
65 |
finished = false;
|
|
66 |
exception = null;
|
|
67 |
}
|
|
68 |
|
|
69 |
/**
|
|
70 |
* Creates a producer for an action.
|
|
71 |
*
|
|
72 |
* @param a Action implementation.
|
|
73 |
* @param nw Defines if {@code produceAction} method should wait for
|
|
74 |
* the end of action.
|
|
75 |
*/
|
|
76 |
public ActionProducer(Action<R, P> a, boolean nw) {
|
|
77 |
super();
|
|
78 |
waiter = new Waiter<>(this);
|
|
79 |
action = a;
|
|
80 |
needWait = nw;
|
|
81 |
setTimeouts(JemmyProperties.getProperties().getTimeouts());
|
|
82 |
setOutput(JemmyProperties.getProperties().getOutput());
|
|
83 |
finished = false;
|
|
84 |
exception = null;
|
|
85 |
}
|
|
86 |
|
|
87 |
/**
|
|
88 |
* Creates a producer. {@code produceAction} must be overridden.
|
|
89 |
*/
|
|
90 |
protected ActionProducer() {
|
|
91 |
super();
|
|
92 |
waiter = new Waiter<>(this);
|
|
93 |
setTimeouts(JemmyProperties.getProperties().getTimeouts());
|
|
94 |
setOutput(JemmyProperties.getProperties().getOutput());
|
|
95 |
finished = false;
|
|
96 |
exception = null;
|
|
97 |
}
|
|
98 |
|
|
99 |
/**
|
|
100 |
* Creates a producer. {@code produceAction} must be overridden.
|
|
101 |
*
|
|
102 |
* @param nw Defines if {@code produceAction} method should wait for
|
|
103 |
* the end of action.
|
|
104 |
*/
|
|
105 |
protected ActionProducer(boolean nw) {
|
|
106 |
super();
|
|
107 |
waiter = new Waiter<>(this);
|
|
108 |
needWait = nw;
|
|
109 |
setTimeouts(JemmyProperties.getProperties().getTimeouts());
|
|
110 |
setOutput(JemmyProperties.getProperties().getOutput());
|
|
111 |
finished = false;
|
|
112 |
exception = null;
|
|
113 |
}
|
|
114 |
|
|
115 |
static {
|
|
116 |
Timeouts.initDefault("ActionProducer.MaxActionTime", ACTION_TIMEOUT);
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* Set all the time outs used by sleeps or waits used by the launched
|
|
121 |
* action.
|
|
122 |
*
|
|
123 |
* @param ts An object containing timeout information.
|
|
124 |
* @see org.netbeans.jemmy.Timeouts
|
|
125 |
* @see org.netbeans.jemmy.Timeoutable
|
|
126 |
* @see #getTimeouts
|
|
127 |
*/
|
|
128 |
@Override
|
|
129 |
public void setTimeouts(Timeouts ts) {
|
|
130 |
timeouts = ts;
|
|
131 |
}
|
|
132 |
|
|
133 |
/**
|
|
134 |
* Get all the time outs used by sleeps or waits used by the launched
|
|
135 |
* action.
|
|
136 |
*
|
|
137 |
* @return an object containing information about timeouts.
|
|
138 |
* @see org.netbeans.jemmy.Timeouts
|
|
139 |
* @see org.netbeans.jemmy.Timeoutable
|
|
140 |
* @see #setTimeouts
|
|
141 |
*/
|
|
142 |
@Override
|
|
143 |
public Timeouts getTimeouts() {
|
|
144 |
return timeouts;
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Identity of the streams or writers used for print output.
|
|
149 |
*
|
|
150 |
* @param out An object containing print output assignments for output and
|
|
151 |
* error streams.
|
|
152 |
* @see org.netbeans.jemmy.TestOut
|
|
153 |
* @see org.netbeans.jemmy.Outputable
|
|
154 |
*/
|
|
155 |
public void setOutput(TestOut out) {
|
|
156 |
output = out;
|
|
157 |
waiter.setOutput(output);
|
|
158 |
}
|
|
159 |
|
|
160 |
/**
|
|
161 |
* Returns the exception value.
|
|
162 |
*
|
|
163 |
* @return a Throwable object representing the exception value
|
|
164 |
*/
|
|
165 |
public Throwable getException() {
|
|
166 |
return exception;
|
|
167 |
}
|
|
168 |
|
|
169 |
/**
|
|
170 |
* Defines action priority in terms of thread priority. Increase (decrease)
|
|
171 |
* parameter value to Thread.MIN_PRIORITY(MAX_PRIORITY) in case if it is
|
|
172 |
* less(more) then it.
|
|
173 |
*
|
|
174 |
* @param newPriority New thread priority.
|
|
175 |
*/
|
|
176 |
public void setActionPriority(int newPriority) {
|
|
177 |
int priority;
|
|
178 |
if (newPriority < Thread.MIN_PRIORITY) {
|
|
179 |
priority = MIN_PRIORITY;
|
|
180 |
} else if (newPriority > Thread.MAX_PRIORITY) {
|
|
181 |
priority = MAX_PRIORITY;
|
|
182 |
} else {
|
|
183 |
priority = newPriority;
|
|
184 |
}
|
|
185 |
try {
|
|
186 |
setPriority(priority);
|
|
187 |
} catch (IllegalArgumentException | SecurityException e) {
|
|
188 |
e.printStackTrace();
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
/**
|
|
193 |
* Get the result of a launched action.
|
|
194 |
*
|
|
195 |
* @return a launched action's result. without waiting in case if
|
|
196 |
* {@code getFinished()}
|
|
197 |
* @see #getFinished()
|
|
198 |
*/
|
|
199 |
public R getResult() {
|
|
200 |
return result;
|
|
201 |
}
|
|
202 |
|
|
203 |
/**
|
|
204 |
* Check if a launched action has finished.
|
|
205 |
*
|
|
206 |
* @return {@code true} if the launched action has completed, either
|
|
207 |
* normally or with an exception; {@code false} otherwise.
|
|
208 |
*/
|
|
209 |
public boolean getFinished() {
|
|
210 |
synchronized (this) {
|
|
211 |
return finished;
|
|
212 |
}
|
|
213 |
}
|
|
214 |
|
|
215 |
/**
|
|
216 |
* Does nothing; the method should be overridden by inheritors.
|
|
217 |
*
|
|
218 |
* @param obj An object used to modify execution. This might be a
|
|
219 |
* {@code java.lang.String[]} that lists a test's command line
|
|
220 |
* arguments.
|
|
221 |
* @return An object - result of the action.
|
|
222 |
* @see org.netbeans.jemmy.Action
|
|
223 |
*/
|
|
224 |
@Override
|
|
225 |
public R launch(P obj) {
|
|
226 |
return null;
|
|
227 |
}
|
|
228 |
|
|
229 |
/**
|
|
230 |
* @return this {@code ActionProducer}'s description.
|
|
231 |
* @see Action
|
|
232 |
*/
|
|
233 |
@Override
|
|
234 |
public String getDescription() {
|
|
235 |
if (action != null) {
|
|
236 |
return action.getDescription();
|
|
237 |
} else {
|
|
238 |
return "Unknown action";
|
|
239 |
}
|
|
240 |
}
|
|
241 |
|
|
242 |
/**
|
|
243 |
* Starts execution. Uses ActionProducer.MaxActionTime timeout.
|
|
244 |
*
|
|
245 |
* @param obj Parameter to be passed into action's
|
|
246 |
* {@code launch(Object)} method. This parameter might be a
|
|
247 |
* {@code java.lang.String[]} that lists a test's command line
|
|
248 |
* arguments.
|
|
249 |
* @param actionTimeOrigin is used for timeout reporting, if non-null.
|
|
250 |
* @return {@code launch(Object)} result.
|
|
251 |
* @throws TimeoutExpiredException
|
|
252 |
* @exception InterruptedException
|
|
253 |
*/
|
|
254 |
public R produceAction(P obj, String actionTimeOrigin) throws InterruptedException {
|
|
255 |
parameter = obj;
|
|
256 |
synchronized (this) {
|
|
257 |
finished = false;
|
|
258 |
}
|
|
259 |
start();
|
|
260 |
if (needWait) {
|
|
261 |
waiter.setTimeoutsToCloneOf(timeouts, "ActionProducer.MaxActionTime", actionTimeOrigin);
|
|
262 |
try {
|
|
263 |
waiter.waitAction(null);
|
|
264 |
} catch (TimeoutExpiredException e) {
|
|
265 |
output.printError("Timeout for \"" + getDescription()
|
|
266 |
+ "\" action has been expired. Thread has been interrupted.");
|
|
267 |
interrupt();
|
|
268 |
throw (e);
|
|
269 |
}
|
|
270 |
}
|
|
271 |
return result;
|
|
272 |
}
|
|
273 |
|
|
274 |
/**
|
|
275 |
* Launch an action in a separate thread of execution. When the action
|
|
276 |
* finishes, record that fact. If the action finishes normally, store it's
|
|
277 |
* result. Use {@code getFinished()} and {@code getResult} to
|
|
278 |
* answer questions about test completion and return value, respectively.
|
|
279 |
*
|
|
280 |
* @see #getFinished()
|
|
281 |
* @see #getResult()
|
|
282 |
* @see java.lang.Runnable
|
|
283 |
*/
|
|
284 |
@Override
|
|
285 |
public final void run() {
|
|
286 |
result = null;
|
|
287 |
try {
|
|
288 |
result = launchAction(parameter);
|
|
289 |
} catch (Throwable e) {
|
|
290 |
exception = e;
|
|
291 |
}
|
|
292 |
synchronized (this) {
|
|
293 |
finished = true;
|
|
294 |
}
|
|
295 |
}
|
|
296 |
|
|
297 |
/**
|
|
298 |
* Inquire for a reference to the object returned by a launched action.
|
|
299 |
*
|
|
300 |
* @param obj Not used.
|
|
301 |
* @return the result returned when a launched action finishes normally.
|
|
302 |
* @see org.netbeans.jemmy.Waitable
|
|
303 |
*/
|
|
304 |
@Override
|
|
305 |
public final Optional<R> actionProduced(P obj) {
|
|
306 |
synchronized (this) {
|
|
307 |
if (finished) {
|
|
308 |
return Optional.ofNullable(result);
|
|
309 |
} else {
|
|
310 |
return null;
|
|
311 |
}
|
|
312 |
}
|
|
313 |
}
|
|
314 |
|
|
315 |
/**
|
|
316 |
* Launch some action. Pass the action parameters and get it's return value,
|
|
317 |
* too.
|
|
318 |
*
|
|
319 |
* @param obj Parameter used to configure the execution of whatever this
|
|
320 |
* {@code ActionProducer} puts into execution.
|
|
321 |
* @return the return value of the action.
|
|
322 |
*/
|
|
323 |
private R launchAction(P obj) {
|
|
324 |
if (action != null) {
|
|
325 |
return action.launch(obj);
|
|
326 |
} else {
|
|
327 |
return launch(obj);
|
|
328 |
}
|
|
329 |
}
|
|
330 |
|
|
331 |
@Override
|
|
332 |
public String toString() {
|
|
333 |
return "ActionProducer{" + "action=" + action + ", needWait=" + needWait + ", parameter=" + parameter + ", finished=" + finished + ", result=" + result + ", exception=" + exception + '}';
|
|
334 |
}
|
|
335 |
}
|