Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JInternalFrame/InternalFrameIsNotCollectedTest.java
41149 views
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
*/
32
import javax.swing.*;
33
import java.awt.*;
34
import java.beans.PropertyVetoException;
35
import java.util.Date;
36
37
public 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
private static JFrame frame;
44
45
public static void main(String[] args) throws Exception {
46
try {
47
initRobot();
48
SwingUtilities.invokeAndWait(new Runnable() {
49
@Override
50
public void run() {
51
initUI();
52
try {
53
closeInternalFrame();
54
} catch (PropertyVetoException e) {
55
throw new RuntimeException(e);
56
}
57
}
58
});
59
robot.waitForIdle();
60
invokeGC();
61
System.runFinalization();
62
Thread.sleep(1000); // it's better to wait 1 sec now then 10 sec later
63
Date startWaiting = new Date();
64
synchronized (CustomInternalFrame.waiter) {
65
// Sync with finalization thread.
66
Date now = new Date();
67
while (now.getTime() - startWaiting.getTime() < maxWaitTime && !CustomInternalFrame.finalized) {
68
CustomInternalFrame.waiter.wait(waitTime);
69
now = new Date();
70
}
71
}
72
if (!CustomInternalFrame.finalized) {
73
throw new RuntimeException("Closed internal frame wasn't collected");
74
}
75
} finally {
76
if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
77
}
78
}
79
80
private static void initRobot() throws AWTException {
81
robot = new Robot();
82
robot.setAutoDelay(100);
83
}
84
85
private static void closeInternalFrame() throws PropertyVetoException {
86
iFrame.setClosed(true);
87
iFrame = null;
88
}
89
90
private static void initUI() {
91
frame = new JFrame("Internal Frame Test");
92
frame.getContentPane().setLayout(new BorderLayout());
93
JDesktopPane desktopPane = new JDesktopPane();
94
desktopPane.setDesktopManager(new DefaultDesktopManager());
95
frame.getContentPane().add(desktopPane, BorderLayout.CENTER);
96
97
iFrame = new CustomInternalFrame("Dummy Frame");
98
99
iFrame.setSize(200, 200);
100
iFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
101
desktopPane.add(iFrame);
102
103
frame.setSize(800, 600);
104
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
105
frame.setVisible(true);
106
iFrame.setVisible(true);
107
}
108
109
private static void invokeGC() {
110
System.out.println("Firing garbage collection!");
111
try {
112
StringBuilder sb = new StringBuilder();
113
while (true) {
114
sb.append("any string. some test. a little bit more text." + sb.toString());
115
}
116
} catch (Throwable e) {
117
// do nothing
118
}
119
}
120
121
122
public static class CustomInternalFrame extends JInternalFrame {
123
public static volatile boolean finalized = false;
124
public static Object waiter = new Object();
125
126
public CustomInternalFrame(String title) {
127
super(title, true, true, true, true);
128
}
129
130
protected void finalize() {
131
System.out.println("Finalized!");
132
finalized = true;
133
waiter.notifyAll();
134
}
135
}
136
}
137
138