Path: blob/master/test/jdk/javax/swing/JComponent/8043610/bug8043610.java
41153 views
/*1* Copyright (c) 2014, 2017, 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* @key headful27* @bug 804361028* @summary Tests that JComponent invalidate, revalidate and repaint methods could29* be called from any thread30* @author Petr Pchelko31* @modules java.desktop/sun.awt32*/3334import sun.awt.SunToolkit;3536import javax.swing.*;37import java.awt.*;38import java.util.concurrent.CountDownLatch;39import java.util.concurrent.atomic.AtomicReference;4041public class bug8043610 {42private static volatile JFrame frame;43private static volatile JComponent component;4445public static void main(String[] args) throws Exception {46ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group");47ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");48try {49Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext);50stubThread.start();51stubThread.join();5253CountDownLatch startSwingLatch = new CountDownLatch(1);54new Thread(swingTG, () -> {55SunToolkit.createNewAppContext();56SwingUtilities.invokeLater(() -> {57frame = new JFrame();58component = new JLabel("Test Text");59frame.add(component);60frame.setBounds(100, 100, 100, 100);61frame.setVisible(true);62startSwingLatch.countDown();63});64}).start();65startSwingLatch.await();6667AtomicReference<Exception> caughtException = new AtomicReference<>();68Thread checkThread = new Thread(getRootThreadGroup(), () -> {69try {70component.invalidate();71component.revalidate();72component.repaint(new Rectangle(0, 0, 0, 0));73} catch (Exception e) {74caughtException.set(e);75}76});77checkThread.start();78checkThread.join();7980if (caughtException.get() != null) {81throw new RuntimeException("Failed. Caught exception!", caughtException.get());82}83} finally {84new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> {85if (frame != null) {86frame.dispose();87}88})).start();89}90}9192private static ThreadGroup getRootThreadGroup() {93ThreadGroup currentTG = Thread.currentThread().getThreadGroup();94ThreadGroup parentTG = currentTG.getParent();95while (parentTG != null) {96currentTG = parentTG;97parentTG = currentTG.getParent();98}99return currentTG;100}101}102103104