Path: blob/master/test/jdk/javax/swing/JFileChooser/6396844/TwentyThousandTest.java
41153 views
/*1* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 639684427* @summary Tests memory leak for 20000 files28* @author Sergey Malenkov29* @library ../../regtesthelpers30* @modules java.desktop/sun.java2d31* @build Util32* @run main/othervm/timeout=1000 -mx128m TwentyThousandTest33*/3435import sun.java2d.Disposer;36import sun.java2d.DisposerRecord;3738import javax.swing.*;39import java.awt.event.HierarchyEvent;40import java.awt.event.HierarchyListener;41import java.io.File;42import java.io.FileWriter;4344public class TwentyThousandTest {4546private static final int FILES = 20000;47private static final int ATTEMPTS = 20;48private static final int INTERVAL = 100;4950private static String tmpDir;5152private static volatile boolean disposerComplete;5354public static void main(String[] args) throws Exception {55tmpDir = System.getProperty("java.io.tmpdir");5657if (tmpDir.length() == 0) { //'java.io.tmpdir' isn't guaranteed to be defined58tmpDir = System.getProperty("user.home");59}6061System.out.println("Temp directory: " + tmpDir);6263System.out.println("Creating " + FILES + " files");6465for (int i = 0; i < FILES; i++) {66File file = getTempFile(i);6768FileWriter writer = new FileWriter(file);69writer.write("File " + i);70writer.close();71}7273for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {74if (laf.getClassName().contains("Motif")) {75continue;76}7778UIManager.setLookAndFeel(laf.getClassName());7980System.out.println("Do " + ATTEMPTS + " attempts for " + laf.getClassName());8182for (int i = 0; i < ATTEMPTS; i++) {83System.out.print(i + " ");8485doAttempt();86}8788System.out.println();89}9091System.out.println("Removing " + FILES + " files");9293for (int i = 0; i < FILES; i++) {94getTempFile(i).delete();95}9697System.out.println("Test passed successfully");98}99100private static File getTempFile(int i) {101return new File(tmpDir, "temp" + i + ".txt");102}103104private static void doAttempt() throws Exception {105SwingUtilities.invokeAndWait(new Runnable() {106public void run() {107final JFileChooser chooser = new JFileChooser(tmpDir);108109chooser.updateUI();110111// Postpone JFileChooser closing until it becomes visible112chooser.addHierarchyListener(new HierarchyListener() {113@Override114public void hierarchyChanged(HierarchyEvent e) {115if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {116if (chooser.isShowing()) {117Thread thread = new Thread(new Runnable() {118public void run() {119try {120Thread.sleep(INTERVAL);121122// Close JFileChooser123SwingUtilities.invokeLater(new Runnable() {124public void run() {125chooser.cancelSelection();126}127});128} catch (InterruptedException e) {129throw new RuntimeException(e);130}131}132});133134thread.start();135}136}137}138});139140chooser.showOpenDialog(null);141}142});143144DisposerRecord disposerRecord = new DisposerRecord() {145public void dispose() {146disposerComplete = true;147}148};149150disposerComplete = false;151152Disposer.addRecord(new Object(), disposerRecord);153154while (!disposerComplete) {155Util.generateOOME();156}157}158}159160161