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