0N/A/*
2362N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A /* @test
0N/A @bug 8012004
0N/A @summary JINTERNALFRAME NOT BEING FINALIZED AFTER CLOSING
0N/A @author mcherkas
0N/A @run main InternalFrameIsNotCollectedTest
0N/A */
0N/A
0N/Aimport sun.awt.SunToolkit;
0N/A
0N/Aimport javax.swing.*;
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.KeyEvent;
0N/Aimport java.beans.PropertyVetoException;
0N/Aimport java.util.Date;
0N/A
0N/Apublic class InternalFrameIsNotCollectedTest {
0N/A
0N/A public static final int waitTime = 10000;
0N/A private static Robot robot;
0N/A
0N/A public static void sync() {
0N/A
0N/A SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
0N/A toolkit.realSync();
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A initRobot();
0N/A SwingUtilities.invokeAndWait(new Runnable() {
0N/A @Override
0N/A public void run() {
0N/A initUI();
0N/A try {
closeInternalFrame();
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
});
sync();
invokeGC();
Thread.sleep(1000); // it's better to wait 1 sec now then 10 sec later
Date startWaiting = new Date();
synchronized (CustomInternalFrame.waiter) {
// Sync with finalization thread.
Date now = new Date();
while (now.getTime() - startWaiting.getTime() < waitTime && !CustomInternalFrame.finalized) {
CustomInternalFrame.waiter.wait(waitTime);
now = new Date();
}
}
if (!CustomInternalFrame.finalized) {
throw new RuntimeException("Closed internal frame wasn't collected");
}
}
private static void initRobot() throws AWTException {
robot = new Robot();
robot.setAutoDelay(100);
}
private static void closeInternalFrame() throws PropertyVetoException {
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_F4);
robot.keyRelease(KeyEvent.VK_F4);
robot.keyRelease(KeyEvent.VK_CONTROL);
}
private static void initUI() {
JFrame frame = new JFrame("Internal Frame Test");
frame.getContentPane().setLayout(new BorderLayout());
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setDesktopManager(new DefaultDesktopManager());
frame.getContentPane().add(desktopPane, BorderLayout.CENTER);
CustomInternalFrame iFrame = new CustomInternalFrame("Dummy Frame");
iFrame.setSize(200, 200);
iFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
desktopPane.add(iFrame);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setVisible(true);
iFrame.setVisible(true);
}
private static void invokeGC() {
System.out.println("Firing garbage collection!");
try {
StringBuilder sb = new StringBuilder();
while (true) {
sb.append("any string. some test. a little bit more text." + sb.toString());
}
} catch (Throwable e) {
// do nothing
}
}
public static class CustomInternalFrame extends JInternalFrame {
public static volatile boolean finalized = false;
public static Object waiter = new Object();
public CustomInternalFrame(String title) {
super(title, true, true, true, true);
}
protected void finalize() {
System.out.println("Finalized!");
finalized = true;
waiter.notifyAll();
}
}
}