Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JFileChooser/6396844/TwentyThousandTest.java
38853 views
/*1* Copyright (c) 2012, 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* @bug 639684426* @summary Tests memory leak for 20000 files27* @author Sergey Malenkov28* @library ../../regtesthelpers29* @build Util30* @run main/othervm/timeout=1000 -mx128m TwentyThousandTest31*/3233import sun.java2d.Disposer;34import sun.java2d.DisposerRecord;3536import javax.swing.*;37import java.awt.event.HierarchyEvent;38import java.awt.event.HierarchyListener;39import java.io.File;40import java.io.FileWriter;4142public class TwentyThousandTest {4344private static final int FILES = 20000;45private static final int ATTEMPTS = 20;46private static final int INTERVAL = 100;4748private static String tmpDir;4950private static volatile boolean disposerComplete;5152public static void main(String[] args) throws Exception {53tmpDir = System.getProperty("java.io.tmpdir");5455if (tmpDir.length() == 0) { //'java.io.tmpdir' isn't guaranteed to be defined56tmpDir = System.getProperty("user.home");57}5859System.out.println("Temp directory: " + tmpDir);6061System.out.println("Creating " + FILES + " files");6263for (int i = 0; i < FILES; i++) {64File file = getTempFile(i);6566FileWriter writer = new FileWriter(file);67writer.write("File " + i);68writer.close();69}7071for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {72if (laf.getClassName().contains("Motif")) {73continue;74}7576UIManager.setLookAndFeel(laf.getClassName());7778System.out.println("Do " + ATTEMPTS + " attempts for " + laf.getClassName());7980for (int i = 0; i < ATTEMPTS; i++) {81System.out.print(i + " ");8283doAttempt();84}8586System.out.println();87}8889System.out.println("Removing " + FILES + " files");9091for (int i = 0; i < FILES; i++) {92getTempFile(i).delete();93}9495System.out.println("Test passed successfully");96}9798private static File getTempFile(int i) {99return new File(tmpDir, "temp" + i + ".txt");100}101102private static void doAttempt() throws Exception {103SwingUtilities.invokeAndWait(new Runnable() {104public void run() {105final JFileChooser chooser = new JFileChooser(tmpDir);106107chooser.updateUI();108109// Postpone JFileChooser closing until it becomes visible110chooser.addHierarchyListener(new HierarchyListener() {111@Override112public void hierarchyChanged(HierarchyEvent e) {113if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {114if (chooser.isShowing()) {115Thread thread = new Thread(new Runnable() {116public void run() {117try {118Thread.sleep(INTERVAL);119120// Close JFileChooser121SwingUtilities.invokeLater(new Runnable() {122public void run() {123chooser.cancelSelection();124}125});126} catch (InterruptedException e) {127throw new RuntimeException(e);128}129}130});131132thread.start();133}134}135}136});137138chooser.showOpenDialog(null);139}140});141142DisposerRecord disposerRecord = new DisposerRecord() {143public void dispose() {144disposerComplete = true;145}146};147148disposerComplete = false;149150Disposer.addRecord(new Object(), disposerRecord);151152while (!disposerComplete) {153Util.generateOOME();154}155}156}157158159