bug6798062.java revision 3909
0N/A/*
3261N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A */
2362N/A
0N/A/* @test %W% %E%
0N/A @bug 6798062
0N/A @summary Memory Leak on using getFiles of FileSystemView
0N/A @author Pavel Porvatov
0N/A @run applet/manual=done bug6798062.html
0N/A*/
0N/A
0N/Aimport sun.awt.OSInfo;
0N/Aimport sun.awt.shell.ShellFolder;
0N/A
0N/Aimport javax.swing.*;
0N/Aimport java.awt.event.ActionEvent;
0N/Aimport java.awt.event.ActionListener;
0N/Aimport java.awt.*;
0N/Aimport java.io.File;
0N/Aimport java.io.FileNotFoundException;
0N/A
0N/Apublic class bug6798062 extends JApplet {
0N/A
0N/A private final JSlider slider = new JSlider(0, 100);
0N/A
0N/A private final JTextField tfLink = new JTextField();
0N/A
0N/A private final JButton btnStart = new JButton("Start");
0N/A
0N/A private final JButton btnStop = new JButton("Stop");
0N/A
0N/A private final JButton btnGC = new JButton("Run System.gc()");
0N/A
7N/A private ShellFolder folder;
0N/A
0N/A private Thread thread;
0N/A
0N/A public static void main(String[] args) {
0N/A JFrame frame = new JFrame("bug6798062");
7N/A
0N/A frame.setSize(400, 300);
0N/A frame.setLocationRelativeTo(null);
0N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
0N/A frame.add(new bug6798062().initialize());
0N/A
0N/A frame.setVisible(true);
0N/A }
0N/A
0N/A public void init() {
0N/A add(initialize());
3429N/A }
3798N/A
3798N/A private JComponent initialize() {
3798N/A if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
3798N/A return new JLabel("The test is suitable only for Windows");
942N/A }
0N/A
0N/A String tempDir = System.getProperty("java.io.tmpdir");
0N/A
0N/A if (tempDir.length() == 0) { // 'java.io.tmpdir' isn't guaranteed to be defined
0N/A tempDir = System.getProperty("user.home");
0N/A }
0N/A
0N/A System.out.println("Temp directory: " + tempDir);
460N/A
460N/A try {
460N/A folder = ShellFolder.getShellFolder(new File(tempDir));
460N/A } catch (FileNotFoundException e) {
460N/A fail("Directory " + tempDir + " not found");
460N/A }
460N/A
460N/A slider.setMajorTickSpacing(10);
1006N/A slider.setPaintTicks(true);
1006N/A slider.setPaintLabels(true);
1006N/A slider.setSnapToTicks(true);
460N/A slider.setValue(10);
0N/A
3798N/A btnStart.addActionListener(new ActionListener() {
0N/A public void actionPerformed(ActionEvent e) {
0N/A setEnabledState(false);
0N/A
0N/A thread = new MyThread(slider.getValue(), tfLink.getText());
0N/A thread.start();
3798N/A }
3798N/A });
0N/A
0N/A btnStop.setEnabled(false);
0N/A
3798N/A btnStop.addActionListener(new ActionListener() {
0N/A public void actionPerformed(ActionEvent e) {
0N/A thread.interrupt();
0N/A thread = null;
3798N/A
3798N/A setEnabledState(true);
0N/A }
0N/A });
460N/A
460N/A btnGC.addActionListener(new ActionListener() {
460N/A public void actionPerformed(ActionEvent e) {
460N/A System.gc();
460N/A }
460N/A });
2324N/A
2324N/A setEnabledState(true);
2324N/A
2324N/A JPanel pnButtons = new JPanel();
2324N/A
795N/A pnButtons.setLayout(new BoxLayout(pnButtons, BoxLayout.X_AXIS));
795N/A
795N/A pnButtons.add(btnStart);
795N/A pnButtons.add(btnStop);
795N/A pnButtons.add(btnGC);
795N/A
795N/A tfLink.setMaximumSize(new Dimension(300, 20));
795N/A
795N/A JPanel pnContent = new JPanel();
795N/A
460N/A pnContent.setLayout(new BoxLayout(pnContent, BoxLayout.Y_AXIS));
0N/A pnContent.add(new JLabel("Delay between listFiles() invocation (ms):"));
0N/A pnContent.add(slider);
460N/A pnContent.add(new JLabel("Provide link here:"));
460N/A pnContent.add(tfLink);
460N/A pnContent.add(pnButtons);
460N/A
460N/A return pnContent;
0N/A }
0N/A
460N/A private void setEnabledState(boolean enabled) {
0N/A slider.setEnabled(enabled);
0N/A btnStart.setEnabled(enabled);
0N/A btnStop.setEnabled(!enabled);
3798N/A }
0N/A
0N/A private static void fail(String msg) {
0N/A throw new RuntimeException(msg);
0N/A }
0N/A
0N/A private class MyThread extends Thread {
795N/A private final int delay;
0N/A
3798N/A private final ShellFolder link;
3798N/A
3798N/A private MyThread(int delay, String link) {
0N/A this.delay = delay;
0N/A
0N/A ShellFolder linkFolder;
0N/A
0N/A try {
0N/A linkFolder = ShellFolder.getShellFolder(new File(link));
0N/A } catch (FileNotFoundException e) {
0N/A e.printStackTrace();
0N/A
0N/A linkFolder = null;
0N/A }
3429N/A
0N/A this.link = linkFolder;
0N/A }
0N/A
0N/A public void run() {
0N/A while (!isInterrupted()) {
0N/A folder.listFiles();
0N/A if (link != null) {
0N/A try {
0N/A link.getLinkLocation();
3262N/A } catch (FileNotFoundException e) {
3262N/A e.printStackTrace();
3262N/A }
3262N/A }
3262N/A
3262N/A if (delay > 0) {
3262N/A try {
0N/A Thread.sleep(delay);
0N/A } catch (InterruptedException e1) {
0N/A // The thread was interrupted
3798N/A return;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}
0N/A