blob: 7a19ead272b071e242a12296a8af66125974a419 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 6493680
26 * @summary Tests if notifications are received in expected order
27 * @author Igor Kushnirskiy
28 */
29
30import javax.swing.*;
31import java.beans.*;
32import java.util.concurrent.atomic.*;
33import java.util.concurrent.*;
34
35
36public class bug6493680 {
37 /*
38 * because timing is involved in this bug we will run the test
39 * NUMBER_OF_TRIES times.
40 * the test passes if it does not fail once.
41 */
42 private final static int NUMBER_OF_TRIES = 50;
43
44 public static void main(String[] args) throws Exception {
45 for (int i = 0; i < NUMBER_OF_TRIES; i++) {
46 if (! (new Test()).test()) {
47 throw new RuntimeException("failed");
48 }
49 }
50 }
51
52 private static class Test {
53 private final AtomicInteger lastProgressValue = new AtomicInteger(-1);
54 private final Exchanger<Boolean> exchanger = new Exchanger<Boolean>();
55
56 boolean test() throws Exception {
57 TestSwingWorker swingWorker = new TestSwingWorker();
58 swingWorker.addPropertyChangeListener(
59 new PropertyChangeListener() {
60 public void propertyChange(PropertyChangeEvent evt) {
61 if ("progress" == evt.getPropertyName()) {
62 lastProgressValue.set((Integer) evt.getNewValue());
63 }
64 }
65 });
66
67 swingWorker.execute();
68 return exchanger.exchange(true);
69 }
70
71 private class TestSwingWorker extends SwingWorker<Void, Void> {
72 @Override
73 protected Void doInBackground() throws Exception {
74 for (int i = 0; i <= 100; i++) {
75 Thread.sleep(1);
76 setProgress(i);
77 }
78 return null;
79 }
80 @Override
81 protected void done() {
82 boolean isPassed = (lastProgressValue.get() == 100);
83 try {
84 exchanger.exchange(isPassed);
85 } catch (Exception ingore) {
86 }
87 }
88 }
89 }
90
91}