Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JFileChooser/6798062/bug6798062.java
38918 views
/*1* Copyright (c) 2009, 2011, 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/* @test %W% %E%24@bug 679806225@summary Memory Leak on using getFiles of FileSystemView26@author Pavel Porvatov27@run applet/manual=done bug6798062.html28*/2930import sun.awt.OSInfo;31import sun.awt.shell.ShellFolder;3233import javax.swing.*;34import java.awt.event.ActionEvent;35import java.awt.event.ActionListener;36import java.awt.*;37import java.io.File;38import java.io.FileNotFoundException;3940public class bug6798062 extends JApplet {4142private final JSlider slider = new JSlider(0, 100);4344private final JTextField tfLink = new JTextField();4546private final JButton btnStart = new JButton("Start");4748private final JButton btnStop = new JButton("Stop");4950private final JButton btnGC = new JButton("Run System.gc()");5152private ShellFolder folder;5354private Thread thread;5556public static void main(String[] args) {57JFrame frame = new JFrame("bug6798062");5859frame.setSize(400, 300);60frame.setLocationRelativeTo(null);61frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);62frame.add(new bug6798062().initialize());6364frame.setVisible(true);65}6667public void init() {68add(initialize());69}7071private JComponent initialize() {72if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {73return new JLabel("The test is suitable only for Windows");74}7576String tempDir = System.getProperty("java.io.tmpdir");7778if (tempDir.length() == 0) { // 'java.io.tmpdir' isn't guaranteed to be defined79tempDir = System.getProperty("user.home");80}8182System.out.println("Temp directory: " + tempDir);8384try {85folder = ShellFolder.getShellFolder(new File(tempDir));86} catch (FileNotFoundException e) {87fail("Directory " + tempDir + " not found");88}8990slider.setMajorTickSpacing(10);91slider.setPaintTicks(true);92slider.setPaintLabels(true);93slider.setSnapToTicks(true);94slider.setValue(10);9596btnStart.addActionListener(new ActionListener() {97public void actionPerformed(ActionEvent e) {98setEnabledState(false);99100thread = new MyThread(slider.getValue(), tfLink.getText());101thread.start();102}103});104105btnStop.setEnabled(false);106107btnStop.addActionListener(new ActionListener() {108public void actionPerformed(ActionEvent e) {109thread.interrupt();110thread = null;111112setEnabledState(true);113}114});115116btnGC.addActionListener(new ActionListener() {117public void actionPerformed(ActionEvent e) {118System.gc();119}120});121122setEnabledState(true);123124JPanel pnButtons = new JPanel();125126pnButtons.setLayout(new BoxLayout(pnButtons, BoxLayout.X_AXIS));127128pnButtons.add(btnStart);129pnButtons.add(btnStop);130pnButtons.add(btnGC);131132tfLink.setMaximumSize(new Dimension(300, 20));133134JPanel pnContent = new JPanel();135136pnContent.setLayout(new BoxLayout(pnContent, BoxLayout.Y_AXIS));137pnContent.add(new JLabel("Delay between listFiles() invocation (ms):"));138pnContent.add(slider);139pnContent.add(new JLabel("Provide link here:"));140pnContent.add(tfLink);141pnContent.add(pnButtons);142143return pnContent;144}145146private void setEnabledState(boolean enabled) {147slider.setEnabled(enabled);148btnStart.setEnabled(enabled);149btnStop.setEnabled(!enabled);150}151152private static void fail(String msg) {153throw new RuntimeException(msg);154}155156private class MyThread extends Thread {157private final int delay;158159private final ShellFolder link;160161private MyThread(int delay, String link) {162this.delay = delay;163164ShellFolder linkFolder;165166try {167linkFolder = ShellFolder.getShellFolder(new File(link));168} catch (FileNotFoundException e) {169e.printStackTrace();170171linkFolder = null;172}173174this.link = linkFolder;175}176177public void run() {178while (!isInterrupted()) {179folder.listFiles();180if (link != null) {181try {182link.getLinkLocation();183} catch (FileNotFoundException e) {184e.printStackTrace();185}186}187188if (delay > 0) {189try {190Thread.sleep(delay);191} catch (InterruptedException e1) {192// The thread was interrupted193return;194}195}196}197}198}199}200201202