Path: blob/master/test/jdk/javax/swing/JFileChooser/6798062/bug6798062.java
41153 views
/*1* Copyright (c) 2009, 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/* @test %W% %E%24@bug 679806225@summary Memory Leak on using getFiles of FileSystemView26@author Pavel Porvatov27@modules java.desktop/sun.awt28java.desktop/sun.awt.shell29@run applet/manual=done bug6798062.html30*/3132import sun.awt.OSInfo;33import sun.awt.shell.ShellFolder;3435import javax.swing.*;36import java.awt.event.ActionEvent;37import java.awt.event.ActionListener;38import java.awt.*;39import java.io.File;40import java.io.FileNotFoundException;4142public class bug6798062 extends JApplet {4344private final JSlider slider = new JSlider(0, 100);4546private final JTextField tfLink = new JTextField();4748private final JButton btnStart = new JButton("Start");4950private final JButton btnStop = new JButton("Stop");5152private final JButton btnGC = new JButton("Run System.gc()");5354private ShellFolder folder;5556private Thread thread;5758public static void main(String[] args) {59JFrame frame = new JFrame("bug6798062");6061frame.setSize(400, 300);62frame.setLocationRelativeTo(null);63frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);64frame.add(new bug6798062().initialize());6566frame.setVisible(true);67}6869public void init() {70add(initialize());71}7273private JComponent initialize() {74if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {75return new JLabel("The test is suitable only for Windows");76}7778String tempDir = System.getProperty("java.io.tmpdir");7980if (tempDir.length() == 0) { // 'java.io.tmpdir' isn't guaranteed to be defined81tempDir = System.getProperty("user.home");82}8384System.out.println("Temp directory: " + tempDir);8586try {87folder = ShellFolder.getShellFolder(new File(tempDir));88} catch (FileNotFoundException e) {89fail("Directory " + tempDir + " not found");90}9192slider.setMajorTickSpacing(10);93slider.setPaintTicks(true);94slider.setPaintLabels(true);95slider.setSnapToTicks(true);96slider.setValue(10);9798btnStart.addActionListener(new ActionListener() {99public void actionPerformed(ActionEvent e) {100setEnabledState(false);101102thread = new MyThread(slider.getValue(), tfLink.getText());103thread.start();104}105});106107btnStop.setEnabled(false);108109btnStop.addActionListener(new ActionListener() {110public void actionPerformed(ActionEvent e) {111thread.interrupt();112thread = null;113114setEnabledState(true);115}116});117118btnGC.addActionListener(new ActionListener() {119public void actionPerformed(ActionEvent e) {120System.gc();121}122});123124setEnabledState(true);125126JPanel pnButtons = new JPanel();127128pnButtons.setLayout(new BoxLayout(pnButtons, BoxLayout.X_AXIS));129130pnButtons.add(btnStart);131pnButtons.add(btnStop);132pnButtons.add(btnGC);133134tfLink.setMaximumSize(new Dimension(300, 20));135136JPanel pnContent = new JPanel();137138pnContent.setLayout(new BoxLayout(pnContent, BoxLayout.Y_AXIS));139pnContent.add(new JLabel("Delay between listFiles() invocation (ms):"));140pnContent.add(slider);141pnContent.add(new JLabel("Provide link here:"));142pnContent.add(tfLink);143pnContent.add(pnButtons);144145return pnContent;146}147148private void setEnabledState(boolean enabled) {149slider.setEnabled(enabled);150btnStart.setEnabled(enabled);151btnStop.setEnabled(!enabled);152}153154private static void fail(String msg) {155throw new RuntimeException(msg);156}157158private class MyThread extends Thread {159private final int delay;160161private final ShellFolder link;162163private MyThread(int delay, String link) {164this.delay = delay;165166ShellFolder linkFolder;167168try {169linkFolder = ShellFolder.getShellFolder(new File(link));170} catch (FileNotFoundException e) {171e.printStackTrace();172173linkFolder = null;174}175176this.link = linkFolder;177}178179public void run() {180while (!isInterrupted()) {181folder.listFiles();182if (link != null) {183try {184link.getLinkLocation();185} catch (FileNotFoundException e) {186e.printStackTrace();187}188}189190if (delay > 0) {191try {192Thread.sleep(delay);193} catch (InterruptedException e1) {194// The thread was interrupted195return;196}197}198}199}200}201}202203204