1079N/A/*
3261N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
1079N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1079N/A *
1079N/A * This code is free software; you can redistribute it and/or modify it
1079N/A * under the terms of the GNU General Public License version 2 only, as
1079N/A * published by the Free Software Foundation.
1079N/A *
1079N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1079N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1079N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1079N/A * version 2 for more details (a copy is included in the LICENSE file that
1079N/A * accompanied this code).
1079N/A *
1079N/A * You should have received a copy of the GNU General Public License version
1079N/A * 2 along with this work; if not, write to the Free Software Foundation,
1079N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1079N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1079N/A */
1079N/A
1079N/A/* @test
1079N/A @bug 6799345
1079N/A @summary Tests that no exceptions are thrown from TimerQueue and
1079N/ASwingWorker on AppContext shutdown
1079N/A @author art
1079N/A @run main TestShutdown
1079N/A*/
1079N/A
1079N/Aimport java.awt.*;
1079N/Aimport java.awt.event.*;
1079N/A
1079N/Aimport java.util.*;
1079N/A
1079N/Aimport javax.swing.*;
1079N/A
1079N/Aimport sun.awt.*;
1079N/A
1079N/Apublic class TestShutdown
1079N/A{
1079N/A private static AppContext targetAppContext;
1079N/A
1079N/A private static JFrame f;
1079N/A private static JTextField tf;
1079N/A
1079N/A private static volatile boolean exceptionsOccurred = false;
1079N/A private static volatile boolean appcontextInitDone = false;
1079N/A
1079N/A private static int timerValue = 0;
1079N/A
1079N/A public static void main(String[] args)
1079N/A throws Exception
1079N/A {
1079N/A ThreadGroup tg = new TestThreadGroup("TTG");
1079N/A Thread t = new Thread(tg, new TestRunnable(), "InitThread");
1079N/A t.start();
1079N/A
1079N/A while (!appcontextInitDone)
1079N/A {
2854N/A Thread.sleep(1000);
1079N/A }
1079N/A
1079N/A targetAppContext.dispose();
1079N/A
1079N/A if (exceptionsOccurred)
1079N/A {
1079N/A throw new RuntimeException("Test FAILED: some exceptions occurred");
1079N/A }
1079N/A }
1079N/A
1079N/A static void initGUI()
1079N/A {
1079N/A f = new JFrame("F");
1079N/A f.setBounds(100, 100, 200, 100);
1079N/A tf = new JTextField("Test");
1079N/A f.add(tf);
1079N/A f.setVisible(true);
1079N/A }
1079N/A
1079N/A static void startGUI()
1079N/A {
1079N/A // caret blink Timer
1079N/A tf.requestFocusInWindow();
1079N/A
1079N/A // misc Timer
1079N/A ActionListener al = new ActionListener()
1079N/A {
1079N/A @Override
1079N/A public void actionPerformed(ActionEvent ae)
1079N/A {
1079N/A System.out.println("Timer tick: " + timerValue++);
1079N/A }
1079N/A };
1079N/A new javax.swing.Timer(30, al).start();
1079N/A }
1079N/A
1079N/A static class TestThreadGroup extends ThreadGroup
1079N/A {
1079N/A public TestThreadGroup(String name)
1079N/A {
1079N/A super(name);
1079N/A }
1079N/A
1079N/A @Override
1079N/A public synchronized void uncaughtException(Thread thread, Throwable t)
1079N/A {
1079N/A if (t instanceof ThreadDeath)
1079N/A {
1079N/A // this one is expected, rethrow
1079N/A throw (ThreadDeath)t;
1079N/A }
1079N/A System.err.println("Test FAILED: an exception is caught in the " +
1079N/A "target thread group on thread " + thread.getName());
1079N/A t.printStackTrace(System.err);
1079N/A exceptionsOccurred = true;
1079N/A }
1079N/A }
1079N/A
1079N/A static class TestRunnable implements Runnable
1079N/A {
1079N/A @Override
1079N/A public void run()
1079N/A {
1079N/A SunToolkit stk = (SunToolkit)Toolkit.getDefaultToolkit();
1079N/A targetAppContext = stk.createNewAppContext();
1079N/A
1079N/A // create and show frame and text field
1079N/A SwingUtilities.invokeLater(new Runnable()
1079N/A {
1079N/A @Override
1079N/A public void run()
1079N/A {
1079N/A initGUI();
1079N/A }
1079N/A });
1079N/A stk.realSync();
1079N/A
1079N/A // start some Timers
1079N/A SwingUtilities.invokeLater(new Runnable()
1079N/A {
1079N/A @Override
1079N/A public void run()
1079N/A {
1079N/A startGUI();
1079N/A }
1079N/A });
1079N/A
1079N/A // start multiple SwingWorkers
1079N/A while (!Thread.interrupted())
1079N/A {
1079N/A try
1079N/A {
1079N/A new TestSwingWorker().execute();
1079N/A Thread.sleep(40);
1079N/A }
1079N/A catch (Exception e)
1079N/A {
1079N/A // exception here is expected, skip
1079N/A break;
1079N/A }
1079N/A }
1079N/A }
1079N/A }
1079N/A
1079N/A static class TestSwingWorker extends SwingWorker<String, Integer>
1079N/A {
1079N/A @Override
1079N/A public String doInBackground()
1079N/A {
1079N/A Random r = new Random();
1079N/A for (int i = 0; i < 10; i++)
1079N/A {
1079N/A try
1079N/A {
1079N/A int delay = r.nextInt() % 50;
1079N/A Thread.sleep(delay);
1079N/A publish(delay);
1079N/A }
1079N/A catch (Exception z)
1079N/A {
1079N/A break;
1079N/A }
1079N/A }
1079N/A if (!appcontextInitDone)
1079N/A {
1079N/A appcontextInitDone = true;
1079N/A }
1079N/A return "Done";
1079N/A }
1079N/A
1079N/A @Override
1079N/A public void process(java.util.List<Integer> chunks)
1079N/A {
1079N/A for (Integer i : chunks)
1079N/A {
1079N/A System.err.println("Processed: " + i);
1079N/A }
1079N/A }
1079N/A }
1079N/A}