1 /*
   2  * Copyright (c) 2010, 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 @(#)bug6520101
  26  * @bug 6520101
  27  * @summary JFileChooser throws OOM in 1.4.2, 5.0u4 and 1.6.0
  28  * @author Praveen Gupta
  29  * @run main/othervm/timeout=600 -Xmx8m -verify bug6520101
  30 */
  31 
  32 import javax.swing.*;
  33 import java.awt.event.ActionEvent;
  34 import java.awt.event.ActionListener;
  35 
  36 public class bug6520101 implements Runnable {
  37 
  38     private static final int ATTEMPTS = 500;
  39     private static final int INTERVAL = 100;
  40 
  41     private static final boolean ALWAYS_NEW_INSTANCE = false;
  42     private static final boolean DO_GC_EACH_INTERVAL = false;
  43     private static final boolean UPDATE_UI_EACH_INTERVAL = true;
  44     private static final boolean AUTO_CLOSE_DIALOG = true;
  45 
  46     private static JFileChooser CHOOSER;
  47 
  48     public static void main(String[] args) throws Exception {
  49         UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
  50 
  51         for (int i = 0; i < ATTEMPTS; i++) {
  52             doAttempt();
  53         }
  54 
  55         System.out.println("Test passed successfully");
  56     }
  57 
  58     private static void doAttempt() throws InterruptedException {
  59         if (ALWAYS_NEW_INSTANCE || (CHOOSER == null))
  60             CHOOSER = new JFileChooser(".");
  61 
  62         if (UPDATE_UI_EACH_INTERVAL) {
  63             CHOOSER.updateUI();
  64         }
  65 
  66         if (AUTO_CLOSE_DIALOG) {
  67             Thread t = new Thread(new bug6520101(CHOOSER));
  68             t.start();
  69             CHOOSER.showOpenDialog(null);
  70             t.join();
  71         } else {
  72             CHOOSER.showOpenDialog(null);
  73         }
  74 
  75         if (DO_GC_EACH_INTERVAL) {
  76             System.gc();
  77         }
  78     }
  79 
  80     private final JFileChooser chooser;
  81 
  82     bug6520101(JFileChooser chooser) {
  83         this.chooser = chooser;
  84     }
  85 
  86     public void run() {
  87         while (!this.chooser.isShowing()) {
  88             try {
  89                 Thread.sleep(30);
  90             } catch (InterruptedException exception) {
  91                 exception.printStackTrace();
  92             }
  93         }
  94 
  95         Timer timer = new Timer(INTERVAL, new ActionListener() {
  96             public void actionPerformed(ActionEvent e) {
  97                 chooser.cancelSelection();
  98             }
  99         });
 100 
 101         timer.setRepeats(false);
 102         timer.start();
 103     }
 104 }