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