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