InternalFrameIsNotCollectedTest.java revision 15235:fe58d505fffd
1/*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @key headful
27 * @bug 8012004
28 * @summary JINTERNALFRAME NOT BEING FINALIZED AFTER CLOSING
29 * @author mcherkas
30 * @run main InternalFrameIsNotCollectedTest
31 */
32import javax.swing.*;
33import java.awt.*;
34import java.beans.PropertyVetoException;
35import java.util.Date;
36
37public class InternalFrameIsNotCollectedTest {
38
39    public static final int maxWaitTime = 100000;
40    public static final int waitTime = 5000;
41    private static Robot robot;
42    private static CustomInternalFrame iFrame;
43
44    public static void main(String[] args) throws Exception {
45        initRobot();
46        SwingUtilities.invokeAndWait(new Runnable() {
47            @Override
48            public void run() {
49                initUI();
50                try {
51                    closeInternalFrame();
52                } catch (PropertyVetoException e) {
53                    throw new RuntimeException(e);
54                }
55            }
56        });
57        robot.waitForIdle();
58        invokeGC();
59        System.runFinalization();
60        Thread.sleep(1000); // it's better to wait 1 sec now then 10 sec later
61        Date startWaiting = new Date();
62        synchronized (CustomInternalFrame.waiter) {
63            // Sync with finalization thread.
64            Date now = new Date();
65            while (now.getTime() - startWaiting.getTime() < maxWaitTime && !CustomInternalFrame.finalized) {
66                CustomInternalFrame.waiter.wait(waitTime);
67                now = new Date();
68            }
69        }
70        if (!CustomInternalFrame.finalized) {
71            throw new RuntimeException("Closed internal frame wasn't collected");
72        }
73    }
74
75    private static void initRobot() throws AWTException {
76        robot = new Robot();
77        robot.setAutoDelay(100);
78    }
79
80    private static void closeInternalFrame() throws PropertyVetoException {
81        iFrame.setClosed(true);
82        iFrame = null;
83    }
84
85    private static void initUI() {
86        JFrame frame = new JFrame("Internal Frame Test");
87        frame.getContentPane().setLayout(new BorderLayout());
88        JDesktopPane desktopPane = new JDesktopPane();
89        desktopPane.setDesktopManager(new DefaultDesktopManager());
90        frame.getContentPane().add(desktopPane, BorderLayout.CENTER);
91
92        iFrame = new CustomInternalFrame("Dummy Frame");
93
94        iFrame.setSize(200, 200);
95        iFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
96        desktopPane.add(iFrame);
97
98        frame.setSize(800, 600);
99        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
100        frame.setVisible(true);
101        iFrame.setVisible(true);
102    }
103
104    private static void invokeGC() {
105        System.out.println("Firing garbage collection!");
106        try {
107            StringBuilder sb = new StringBuilder();
108            while (true) {
109                sb.append("any string. some test. a little bit more text." + sb.toString());
110            }
111        } catch (Throwable e) {
112            // do nothing
113        }
114    }
115
116
117    public static class CustomInternalFrame extends JInternalFrame {
118        public static volatile boolean finalized = false;
119        public static Object waiter = new Object();
120
121        public CustomInternalFrame(String title) {
122            super(title, true, true, true, true);
123        }
124
125        protected void finalize() {
126            System.out.println("Finalized!");
127            finalized = true;
128            waiter.notifyAll();
129        }
130    }
131}
132