1 /*
2  * Copyright (c) 2010, 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 
24 /* @test
25  * @bug 6880336
26  * @summary Test for nested SwingWorkers, i.e. when the second worker is
27 started from the first's doInBackground() method. A timeout when running
28 * this test is an indication of failure.
29  * @author Artem Ananiev
30  * @run main/timeout=32 NestedWorkers
31  */
32 
33 import javax.swing.*;
34 
35 public class NestedWorkers extends SwingWorker<String, Void> {
36 
37     private final static int MAX_LEVEL = 2;
38 
39     private int level;
40 
NestedWorkers(int level)41     public NestedWorkers(int level) {
42         super();
43         this.level = level;
44     }
45 
46     @Override
doInBackground()47     public String doInBackground() throws Exception {
48         if (level < MAX_LEVEL) {
49             SwingWorker<String, Void> nested = new NestedWorkers(level + 1);
50             nested.execute();
51             nested.get();
52         }
53         System.out.println("doInBackground " + level + " is complete");
54         return String.valueOf(level);
55     }
56 
main(String[] args)57     public static void main(String[] args) throws Exception {
58         SwingUtilities.invokeAndWait(new Runnable() {
59             @Override
60             public void run() {
61                 SwingWorker<String, Void> sw = new NestedWorkers(0);
62                 sw.execute();
63                 try {
64                     System.err.println(sw.get());
65                 } catch (Exception z) {
66                     throw new RuntimeException(z);
67                 }
68             }
69         });
70     }
71 
72 }
73