Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComponent/8043610/bug8043610.java
38918 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25@test26@bug 804361027@summary Tests that JComponent invalidate, revalidate and repaint methods could28be called from any thread29@author Petr Pchelko30*/3132import sun.awt.SunToolkit;3334import javax.swing.*;35import java.awt.*;36import java.util.concurrent.CountDownLatch;37import java.util.concurrent.atomic.AtomicReference;3839public class bug8043610 {40private static volatile JFrame frame;41private static volatile JComponent component;4243public static void main(String[] args) throws Exception {44ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group");45ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");46try {47Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext);48stubThread.start();49stubThread.join();5051CountDownLatch startSwingLatch = new CountDownLatch(1);52new Thread(swingTG, () -> {53SunToolkit.createNewAppContext();54SwingUtilities.invokeLater(() -> {55frame = new JFrame();56component = new JLabel("Test Text");57frame.add(component);58frame.setBounds(100, 100, 100, 100);59frame.setVisible(true);60startSwingLatch.countDown();61});62}).start();63startSwingLatch.await();6465AtomicReference<Exception> caughtException = new AtomicReference<>();66Thread checkThread = new Thread(getRootThreadGroup(), () -> {67try {68component.invalidate();69component.revalidate();70component.repaint(new Rectangle(0, 0, 0, 0));71} catch (Exception e) {72caughtException.set(e);73}74});75checkThread.start();76checkThread.join();7778if (caughtException.get() != null) {79throw new RuntimeException("Failed. Caught exception!", caughtException.get());80}81} finally {82new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> {83if (frame != null) {84frame.dispose();85}86})).start();87}88}8990private static ThreadGroup getRootThreadGroup() {91ThreadGroup currentTG = Thread.currentThread().getThreadGroup();92ThreadGroup parentTG = currentTG.getParent();93while (parentTG != null) {94currentTG = parentTG;95parentTG = currentTG.getParent();96}97return currentTG;98}99}100101102