1 /* 2 * Copyright (c) 2014, 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 25 /* 26 @test 27 @bug 8043610 28 @summary Tests that JComponent invalidate, revalidate and repaint methods could 29 be called from any thread 30 @author Petr Pchelko 31 */ 32 33 import sun.awt.SunToolkit; 34 35 import javax.swing.*; 36 import java.awt.*; 37 import java.util.concurrent.CountDownLatch; 38 import java.util.concurrent.atomic.AtomicReference; 39 40 public class bug8043610 { 41 private static volatile JFrame frame; 42 private static volatile JComponent component; 43 44 public static void main(String[] args) throws Exception { 45 ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group"); 46 ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG"); 47 try { 48 Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext); 49 stubThread.start(); 50 stubThread.join(); 51 52 CountDownLatch startSwingLatch = new CountDownLatch(1); 53 new Thread(swingTG, () -> { 54 SunToolkit.createNewAppContext(); 55 SwingUtilities.invokeLater(() -> { 56 frame = new JFrame(); 57 component = new JLabel("Test Text"); 58 frame.add(component); 59 frame.setBounds(100, 100, 100, 100); 60 frame.setVisible(true); 61 startSwingLatch.countDown(); 62 }); 63 }).start(); 64 startSwingLatch.await(); 65 66 AtomicReference<Exception> caughtException = new AtomicReference<>(); 67 Thread checkThread = new Thread(getRootThreadGroup(), () -> { 68 try { 69 component.invalidate(); 70 component.revalidate(); 71 component.repaint(new Rectangle(0, 0, 0, 0)); 72 } catch (Exception e) { 73 caughtException.set(e); 74 } 75 }); 76 checkThread.start(); 77 checkThread.join(); 78 79 if (caughtException.get() != null) { 80 throw new RuntimeException("Failed. Caught exception!", caughtException.get()); 81 } 82 } finally { 83 new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> { 84 if (frame != null) { 85 frame.dispose(); 86 } 87 })).start(); 88 } 89 } 90 91 private static ThreadGroup getRootThreadGroup() { 92 ThreadGroup currentTG = Thread.currentThread().getThreadGroup(); 93 ThreadGroup parentTG = currentTG.getParent(); 94 while (parentTG != null) { 95 currentTG = parentTG; 96 parentTG = currentTG.getParent(); 97 } 98 return currentTG; 99 } 100 }