1 /* 2 * Copyright (c) 2009, 2015, 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 6799345 26 @summary Tests that no exceptions are thrown from TimerQueue and 27 SwingWorker on AppContext shutdown 28 @author art 29 @modules java.desktop/sun.awt 30 @run main TestShutdown 31 @key randomness 32 */ 33 34 import java.awt.*; 35 import java.awt.event.*; 36 37 import java.util.*; 38 39 import javax.swing.*; 40 41 import sun.awt.*; 42 43 public class TestShutdown 44 { 45 private static AppContext targetAppContext; 46 47 private static JFrame f; 48 private static JTextField tf; 49 50 private static volatile boolean exceptionsOccurred = false; 51 private static volatile boolean appcontextInitDone = false; 52 53 private static int timerValue = 0; 54 55 public static void main(String[] args) 56 throws Exception 57 { 58 ThreadGroup tg = new TestThreadGroup("TTG"); 59 Thread t = new Thread(tg, new TestRunnable(), "InitThread"); 60 t.start(); 61 62 while (!appcontextInitDone) 63 { 64 Thread.sleep(1000); 65 } 66 67 targetAppContext.dispose(); 68 69 if (exceptionsOccurred) 70 { 71 throw new RuntimeException("Test FAILED: some exceptions occurred"); 72 } 73 } 74 75 static void initGUI() 76 { 77 f = new JFrame("F"); 78 f.setBounds(100, 100, 200, 100); 79 tf = new JTextField("Test"); 80 f.add(tf); 81 f.setVisible(true); 82 } 83 84 static void startGUI() 85 { 86 // caret blink Timer 87 tf.requestFocusInWindow(); 88 89 // misc Timer 90 ActionListener al = new ActionListener() 91 { 92 @Override 93 public void actionPerformed(ActionEvent ae) 94 { 95 System.out.println("Timer tick: " + timerValue++); 96 } 97 }; 98 new javax.swing.Timer(30, al).start(); 99 } 100 101 static class TestThreadGroup extends ThreadGroup 102 { 103 public TestThreadGroup(String name) 104 { 105 super(name); 106 } 107 108 @Override 109 public synchronized void uncaughtException(Thread thread, Throwable t) 110 { 111 if (t instanceof ThreadDeath) 112 { 113 // this one is expected, rethrow 114 throw (ThreadDeath)t; 115 } 116 System.err.println("Test FAILED: an exception is caught in the " + 117 "target thread group on thread " + thread.getName()); 118 t.printStackTrace(System.err); 119 exceptionsOccurred = true; 120 } 121 } 122 123 static class TestRunnable implements Runnable 124 { 125 @Override 126 public void run() 127 { 128 SunToolkit stk = (SunToolkit)Toolkit.getDefaultToolkit(); 129 targetAppContext = stk.createNewAppContext(); 130 131 // create and show frame and text field 132 SwingUtilities.invokeLater(new Runnable() 133 { 134 @Override 135 public void run() 136 { 137 initGUI(); 138 } 139 }); 140 stk.realSync(); 141 142 // start some Timers 143 SwingUtilities.invokeLater(new Runnable() 144 { 145 @Override 146 public void run() 147 { 148 startGUI(); 149 } 150 }); 151 152 // start multiple SwingWorkers 153 while (!Thread.interrupted()) 154 { 155 try 156 { 157 new TestSwingWorker().execute(); 158 Thread.sleep(40); 159 } 160 catch (Exception e) 161 { 162 // exception here is expected, skip 163 break; 164 } 165 } 166 } 167 } 168 169 static class TestSwingWorker extends SwingWorker<String, Integer> 170 { 171 @Override 172 public String doInBackground() 173 { 174 Random r = new Random(); 175 for (int i = 0; i < 10; i++) 176 { 177 try 178 { 179 int delay = r.nextInt() % 50; 180 Thread.sleep(delay); 181 publish(delay); 182 } 183 catch (Exception z) 184 { 185 break; 186 } 187 } 188 if (!appcontextInitDone) 189 { 190 appcontextInitDone = true; 191 } 192 return "Done"; 193 } 194 195 @Override 196 public void process(java.util.List<Integer> chunks) 197 { 198 for (Integer i : chunks) 199 { 200 System.err.println("Processed: " + i); 201 } 202 } 203 } 204 }