Path: blob/master/test/jdk/javax/swing/JInternalFrame/InternalFrameIsNotCollectedTest.java
41149 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/*24* @test25* @key headful26* @bug 801200427* @summary JINTERNALFRAME NOT BEING FINALIZED AFTER CLOSING28* @author mcherkas29* @run main InternalFrameIsNotCollectedTest30*/31import javax.swing.*;32import java.awt.*;33import java.beans.PropertyVetoException;34import java.util.Date;3536public class InternalFrameIsNotCollectedTest {3738public static final int maxWaitTime = 100000;39public static final int waitTime = 5000;40private static Robot robot;41private static CustomInternalFrame iFrame;42private static JFrame frame;4344public static void main(String[] args) throws Exception {45try {46initRobot();47SwingUtilities.invokeAndWait(new Runnable() {48@Override49public void run() {50initUI();51try {52closeInternalFrame();53} catch (PropertyVetoException e) {54throw new RuntimeException(e);55}56}57});58robot.waitForIdle();59invokeGC();60System.runFinalization();61Thread.sleep(1000); // it's better to wait 1 sec now then 10 sec later62Date startWaiting = new Date();63synchronized (CustomInternalFrame.waiter) {64// Sync with finalization thread.65Date now = new Date();66while (now.getTime() - startWaiting.getTime() < maxWaitTime && !CustomInternalFrame.finalized) {67CustomInternalFrame.waiter.wait(waitTime);68now = new Date();69}70}71if (!CustomInternalFrame.finalized) {72throw new RuntimeException("Closed internal frame wasn't collected");73}74} finally {75if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());76}77}7879private static void initRobot() throws AWTException {80robot = new Robot();81robot.setAutoDelay(100);82}8384private static void closeInternalFrame() throws PropertyVetoException {85iFrame.setClosed(true);86iFrame = null;87}8889private static void initUI() {90frame = new JFrame("Internal Frame Test");91frame.getContentPane().setLayout(new BorderLayout());92JDesktopPane desktopPane = new JDesktopPane();93desktopPane.setDesktopManager(new DefaultDesktopManager());94frame.getContentPane().add(desktopPane, BorderLayout.CENTER);9596iFrame = new CustomInternalFrame("Dummy Frame");9798iFrame.setSize(200, 200);99iFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);100desktopPane.add(iFrame);101102frame.setSize(800, 600);103frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);104frame.setVisible(true);105iFrame.setVisible(true);106}107108private static void invokeGC() {109System.out.println("Firing garbage collection!");110try {111StringBuilder sb = new StringBuilder();112while (true) {113sb.append("any string. some test. a little bit more text." + sb.toString());114}115} catch (Throwable e) {116// do nothing117}118}119120121public static class CustomInternalFrame extends JInternalFrame {122public static volatile boolean finalized = false;123public static Object waiter = new Object();124125public CustomInternalFrame(String title) {126super(title, true, true, true, true);127}128129protected void finalize() {130System.out.println("Finalized!");131finalized = true;132waiter.notifyAll();133}134}135}136137138