Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JInternalFrame/InternalFrameIsNotCollectedTest.java
38838 views
/*1* Copyright (c) 2013, 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*/2223/* @test24@bug 801200425@summary JINTERNALFRAME NOT BEING FINALIZED AFTER CLOSING26@author mcherkas27@run main InternalFrameIsNotCollectedTest28*/29import javax.swing.*;30import java.awt.*;31import java.beans.PropertyVetoException;32import java.util.Date;3334public class InternalFrameIsNotCollectedTest {3536public static final int maxWaitTime = 100000;37public static final int waitTime = 5000;38private static Robot robot;39private static CustomInternalFrame iFrame;4041public static void main(String[] args) throws Exception {42initRobot();43SwingUtilities.invokeAndWait(new Runnable() {44@Override45public void run() {46initUI();47try {48closeInternalFrame();49} catch (PropertyVetoException e) {50throw new RuntimeException(e);51}52}53});54robot.waitForIdle();55invokeGC();56System.runFinalization();57Thread.sleep(1000); // it's better to wait 1 sec now then 10 sec later58Date startWaiting = new Date();59synchronized (CustomInternalFrame.waiter) {60// Sync with finalization thread.61Date now = new Date();62while (now.getTime() - startWaiting.getTime() < maxWaitTime && !CustomInternalFrame.finalized) {63CustomInternalFrame.waiter.wait(waitTime);64now = new Date();65}66}67if (!CustomInternalFrame.finalized) {68throw new RuntimeException("Closed internal frame wasn't collected");69}70}7172private static void initRobot() throws AWTException {73robot = new Robot();74robot.setAutoDelay(100);75}7677private static void closeInternalFrame() throws PropertyVetoException {78iFrame.setClosed(true);79iFrame = null;80}8182private static void initUI() {83JFrame frame = new JFrame("Internal Frame Test");84frame.getContentPane().setLayout(new BorderLayout());85JDesktopPane desktopPane = new JDesktopPane();86desktopPane.setDesktopManager(new DefaultDesktopManager());87frame.getContentPane().add(desktopPane, BorderLayout.CENTER);8889iFrame = new CustomInternalFrame("Dummy Frame");9091iFrame.setSize(200, 200);92iFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);93desktopPane.add(iFrame);9495frame.setSize(800, 600);96frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);97frame.setVisible(true);98iFrame.setVisible(true);99}100101private static void invokeGC() {102System.out.println("Firing garbage collection!");103try {104StringBuilder sb = new StringBuilder();105while (true) {106sb.append("any string. some test. a little bit more text." + sb.toString());107}108} catch (Throwable e) {109// do nothing110}111}112113114public static class CustomInternalFrame extends JInternalFrame {115public static volatile boolean finalized = false;116public static Object waiter = new Object();117118public CustomInternalFrame(String title) {119super(title, true, true, true, true);120}121122protected void finalize() {123System.out.println("Finalized!");124finalized = true;125waiter.notifyAll();126}127}128}129130131