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