001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package javax.swing;
018:
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021: import java.io.IOException;
022: import java.io.OutputStream;
023: import java.io.PrintStream;
024:
025: import junit.framework.TestCase;
026:
027: public class Timer_MultithreadedTest extends TestCase {
028: private Timer timer;
029:
030: static class ConcreteActionListener implements ActionListener {
031: class Delay {
032: private boolean wasStopped;
033:
034: public synchronized void stopWaiting() {
035: wasStopped = true;
036: notifyAll();
037: }
038:
039: public synchronized void waitForDelay(final int delay) {
040: wasStopped = false;
041: try {
042: wait(delay);
043: if (wasStopped) {
044: return;
045: }
046: SwingUtilities.invokeLater(new Runnable() {
047: public void run() {
048: stopWaiting();
049: }
050: });
051: wait();
052: } catch (Exception e) {
053: }
054: }
055: }
056:
057: private final Delay delay = new Delay();
058:
059: private final String name;
060:
061: private ActionEvent action;
062:
063: private boolean performed;
064:
065: private int counter;
066:
067: private boolean debug;
068:
069: ConcreteActionListener() {
070: name = "NoName";
071: }
072:
073: ConcreteActionListener(final String name) {
074: this .name = name;
075: }
076:
077: ConcreteActionListener(final String name, final boolean isDebug) {
078: this .name = name;
079: debug = isDebug;
080: }
081:
082: public void reset() {
083: action = null;
084: performed = false;
085: counter = 0;
086: }
087:
088: public void actionPerformed(final ActionEvent action) {
089: this .action = action;
090: counter++;
091: performed = true;
092: if (debug) {
093: System.out.println(name);
094: }
095: delay.stopWaiting();
096: }
097:
098: public int getCounter() {
099: return counter;
100: }
101:
102: public boolean waitNumActions(final int maxWaitTime,
103: final int numActions) {
104: int startNumActions = counter;
105: while (counter - startNumActions < numActions) {
106: delay.waitForDelay(maxWaitTime);
107: if (performed == false) {
108: break;
109: }
110: performed = false;
111: }
112: return (counter - startNumActions == numActions);
113: }
114:
115: public boolean waitAction(final int maxWaitTime) {
116: delay.waitForDelay(maxWaitTime);
117: return action != null;
118: }
119: };
120:
121: public Timer_MultithreadedTest() {
122: // As the first timer starts new thread, and how much time will it takes is
123: // system dependent and unpredictable, for tests stability we should run
124: // first timer ahead of running our testcases
125: runHare();
126: }
127:
128: @Override
129: protected void tearDown() throws Exception {
130: if (timer != null) {
131: timer.stop();
132: timer = null;
133: }
134: super .tearDown();
135: }
136:
137: public void testFireActionPerformed() {
138: ConcreteActionListener listener1 = new ConcreteActionListener(
139: "1");
140: ConcreteActionListener listener2 = new ConcreteActionListener(
141: "2");
142: ConcreteActionListener listener3 = new ConcreteActionListener(
143: "3");
144: ConcreteActionListener listener4 = new ConcreteActionListener(
145: "4");
146: timer = new Timer(10, listener1);
147: timer.addActionListener(listener2);
148: timer.addActionListener(listener3);
149: timer.addActionListener(listener4);
150: timer.start();
151: listener1.waitAction(500);
152: timer.stop();
153: assertNotNull("[1] ActionListener's ActionPerformed invoked ",
154: listener1.action);
155: assertSame("[2] ActionListener's ActionPerformed invoked ",
156: listener1.action, listener2.action);
157: assertSame("[3] ActionListener's ActionPerformed invoked ",
158: listener1.action, listener3.action);
159: assertSame("[4] ActionListener's ActionPerformed invoked ",
160: listener1.action, listener4.action);
161: }
162:
163: public void testSetRepeats() {
164: ConcreteActionListener listener1 = new ConcreteActionListener(
165: "1");
166: timer = new Timer(10, listener1);
167: timer.setRepeats(true);
168: assertTrue("repeats ", timer.isRepeats());
169: timer.setRepeats(false);
170: assertFalse("doesn't repeat ", timer.isRepeats());
171: timer.setRepeats(true);
172: assertTrue("repeats ", timer.isRepeats());
173: timer.start();
174: listener1.waitNumActions(500, 5);
175: assertTrue("ActionListener's ActionPerformed invoked ",
176: listener1.action != null);
177: timer.stop();
178:
179: // checking does setRepeats actually affects the work of timer
180: ConcreteActionListener listener2 = new ConcreteActionListener(
181: "2");
182: timer = new Timer(10, listener2);
183: timer.start();
184: listener2.waitAction(1500);
185: assertNotNull("ActionListener's ActionPerformed did invoke ",
186: listener2.action);
187: listener2.reset();
188: listener2.waitAction(1500);
189: assertNotNull("ActionListener's ActionPerformed did invoke ",
190: listener2.action);
191: timer.setRepeats(false);
192: listener2.reset();
193: listener2.waitAction(100);
194: listener2.reset();
195: listener2.waitAction(100);
196: assertNull("ActionListener's ActionPerformed didn't invoke ",
197: listener2.action);
198: timer.stop();
199:
200: ConcreteActionListener listener3 = new ConcreteActionListener(
201: "3");
202: timer = new Timer(100, listener3);
203: timer.setRepeats(false);
204: timer.start();
205: timer.setRepeats(true);
206: listener3.waitAction(1500);
207: assertNotNull("ActionListener's ActionPerformed did invoke ",
208: listener3.action);
209: listener3.reset();
210: listener3.waitAction(1500);
211: assertNotNull("ActionListener's ActionPerformed did invoke ",
212: listener3.action);
213: timer.stop();
214:
215: ConcreteActionListener listener4 = new ConcreteActionListener(
216: "4");
217: timer = new Timer(100, listener4);
218: timer.setRepeats(false);
219: timer.start();
220: listener4.waitAction(1500);
221: assertNotNull("ActionListener's ActionPerformed did invoke ",
222: listener4.action);
223: listener4.reset();
224: listener4.waitAction(10);
225: timer.setRepeats(true);
226: listener4.reset();
227: listener4.waitAction(500);
228: assertNull("ActionListener's ActionPerformed did invoke ",
229: listener4.action);
230: }
231:
232: public void testSetInitialDelay() {
233: ConcreteActionListener listener1 = new ConcreteActionListener(
234: "1");
235: timer = new Timer(10, listener1);
236: timer.setInitialDelay(100);
237: assertEquals("Initial delay is ", 100, timer.getInitialDelay());
238: timer.setInitialDelay(300);
239: assertEquals("Initial delay is ", 300, timer.getInitialDelay());
240:
241: // checking does SetInitialDelay actually affects the work of timer
242: ConcreteActionListener listener2 = new ConcreteActionListener(
243: "2");
244: timer = new Timer(10, listener2);
245: timer.setInitialDelay(700);
246: timer.start();
247: listener2.waitAction(500);
248: assertNull("ActionListener's ActionPerformed didn't invoke ",
249: listener2.action);
250: listener2.reset();
251: listener2.waitAction(1000);
252: assertTrue("ActionListener's ActionPerformed invoked ",
253: listener2.action != null);
254: timer.stop();
255:
256: timer = new Timer(10, listener2);
257: timer.setInitialDelay(200);
258: timer.start();
259: timer.setInitialDelay(500);
260: listener2.waitAction(500);
261: assertTrue("ActionListener's ActionPerformed invoked ",
262: listener2.action != null);
263: }
264:
265: public void testSetDelay() {
266: ConcreteActionListener listener = new ConcreteActionListener();
267: timer = new Timer(10, listener);
268: timer.setDelay(100);
269: assertEquals("delay is ", 100, timer.getDelay());
270: timer.setDelay(300);
271: assertEquals("delay is ", 300, timer.getDelay());
272:
273: // checking does SetDelay affects working timer
274: listener = new ConcreteActionListener();
275: timer = new Timer(4000, listener);
276: timer.start();
277: listener.waitAction(500);
278: timer.setDelay(100);
279: listener.waitAction(200);
280: assertNull("ActionListener's ActionPerformed didn't invoke ",
281: listener.action);
282: timer.stop();
283:
284: listener = new ConcreteActionListener();
285: timer = new Timer(150, listener);
286: timer.start();
287: listener.waitAction(5);
288: timer.setDelay(5000);
289: listener.waitAction(500);
290: assertNotNull("ActionListener's ActionPerformed invoked ",
291: listener.action);
292: }
293:
294: public void testIsRunning() {
295: ConcreteActionListener listener = new ConcreteActionListener();
296: timer = new Timer(10, listener);
297: listener.waitAction(200);
298: assertFalse("timer is not running ", timer.isRunning());
299: timer.start();
300: assertTrue("timer is running ", timer.isRunning());
301: listener.waitAction(1500);
302: assertTrue("timer is running ", timer.isRunning());
303: timer.stop();
304: assertFalse("timer is not running ", timer.isRunning());
305: listener.reset();
306: listener.waitAction(100);
307: assertFalse("timer is not running ", timer.isRunning());
308:
309: timer.setRepeats(false);
310: timer.setDelay(100);
311: listener.reset();
312: timer.start();
313: assertTrue("timer is running ", timer.isRunning());
314: listener.waitAction(1500);
315: assertTrue("timer hasn't rung", listener.performed);
316: listener.reset();
317: listener.waitAction(100);
318: assertFalse("timer must not ring", listener.performed);
319: assertFalse("timer is running ", timer.isRunning());
320: }
321:
322: public void testStop() {
323: ConcreteActionListener listener = new ConcreteActionListener();
324: timer = new Timer(10, listener);
325: listener.waitAction(200);
326: assertNull("ActionListener's ActionPerformed didn't invoke ",
327: listener.action);
328: timer.start();
329: listener.waitAction(1500);
330: assertTrue("ActionListener's ActionPerformed invoked ",
331: listener.action != null);
332: timer.stop();
333: listener.reset();
334: listener.waitAction(100);
335: assertNull("ActionListener's ActionPerformed didn't invoke ",
336: listener.action);
337: }
338:
339: public void testStart() {
340: ConcreteActionListener listener = new ConcreteActionListener(
341: "listener");
342: timer = new Timer(10, listener);
343: listener.waitAction(200);
344: assertNull("ActionListener's ActionPerformed didn't invoke ",
345: listener.action);
346: timer.start();
347: listener.waitAction(500);
348: assertTrue("ActionListener's ActionPerformed invoked ",
349: listener.action != null);
350: timer.stop();
351: listener.action = null;
352: timer.setInitialDelay(1000);
353: timer.start();
354: listener.waitAction(500);
355: assertNull("ActionListener's ActionPerformed didn't invoke ",
356: listener.action);
357:
358: // testing the right order of timers being triggered
359: ConcreteActionListener listener1 = new ConcreteActionListener(
360: "1");
361: ConcreteActionListener listener2 = new ConcreteActionListener(
362: "2");
363: ConcreteActionListener listener3 = new ConcreteActionListener(
364: "3");
365: Timer timer1 = new Timer(50, listener1);
366: Timer timer2 = new Timer(60, listener2);
367: Timer timer3 = new Timer(70, listener3);
368: Timer timer4 = new Timer(1, new ConcreteActionListener());
369: Timer timer5 = new Timer(2, new ConcreteActionListener());
370: Timer timer6 = new Timer(3, new ConcreteActionListener());
371: Timer timer7 = new Timer(4, new ConcreteActionListener());
372: timer4.start();
373: timer5.start();
374: timer6.start();
375: timer7.start();
376: try {
377: timer1.setRepeats(false);
378: timer2.setRepeats(false);
379: timer3.setRepeats(false);
380: timer1.start();
381: timer2.start();
382: timer3.start();
383: listener3.waitAction(500);
384: long when1 = getWhen(listener1);
385: long when2 = getWhen(listener2);
386: long when3 = getWhen(listener3);
387: assertTrue("The order of timers alerts is correct",
388: when2 >= when1);
389: assertTrue("The order of timers alerts is correct",
390: when3 >= when2);
391: } finally {
392: timer1.stop();
393: timer2.stop();
394: timer3.stop();
395: timer4.stop();
396: timer5.stop();
397: timer6.stop();
398: timer7.stop();
399: }
400: }
401:
402: public void testRestart() {
403: ConcreteActionListener listener = new ConcreteActionListener();
404: timer = new Timer(10, listener);
405: listener.waitAction(200);
406: assertNull("ActionListener's ActionPerformed didn't invoke ",
407: listener.action);
408: timer.start();
409: listener.waitAction(1500);
410: assertTrue("ActionListener's ActionPerformed invoked ",
411: listener.action != null);
412: timer.setInitialDelay(500);
413: timer.restart();
414: listener.reset();
415: listener.waitAction(250);
416: assertNull("ActionListener's ActionPerformed didn't invoke ",
417: listener.action);
418: listener.waitAction(500);
419: assertTrue("ActionListener's ActionPerformed invoked ",
420: listener.action != null);
421: }
422:
423: public void testSetLogTimers() {
424: class LogOutputStream extends OutputStream {
425: public boolean written = false;
426:
427: @Override
428: public void write(int b) throws IOException {
429: written = true;
430: }
431: }
432: ;
433: PrintStream oldOut = System.out;
434: LogOutputStream logOut = new LogOutputStream();
435: PrintStream newOut = new PrintStream(logOut);
436: System.setOut(newOut);
437: try {
438: Timer.setLogTimers(false);
439: ConcreteActionListener listener = new ConcreteActionListener();
440: timer = new Timer(10, listener);
441: timer.start();
442: assertFalse("[1] doesn't log timers", Timer.getLogTimers());
443: listener.waitAction(250);
444: assertTrue("[1] action performed ", listener.performed);
445: assertFalse("[1] log's not written", logOut.written);
446: timer.stop();
447: listener.reset();
448:
449: Timer.setLogTimers(true);
450: timer.start();
451: listener.waitAction(250);
452: assertTrue("[2] logs timers ", Timer.getLogTimers());
453: assertTrue("[2] action performed", listener.performed);
454: assertTrue("[2] log's written", logOut.written);
455: timer.stop();
456: listener.waitAction(200);
457: listener.reset();
458:
459: Timer.setLogTimers(false);
460: logOut.written = false;
461: timer.start();
462: listener.waitAction(50);
463: assertFalse("[3] doesn't log timers ", Timer.getLogTimers());
464: assertTrue("[3] action performed", listener.performed);
465: assertFalse("[3] log's not written", logOut.written);
466: } finally {
467: System.setOut(oldOut);
468: Timer.setLogTimers(false);
469: }
470: }
471:
472: private long getWhen(ConcreteActionListener listener) {
473: return (listener.action != null) ? listener.action.getWhen()
474: : 0;
475: }
476:
477: private void runHare() {
478: ConcreteActionListener listener = new ConcreteActionListener();
479: Timer hare = new Timer(1, listener);
480: hare.setRepeats(false);
481: hare.start();
482: listener.waitAction(1000);
483: }
484: }
|