1/*
2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25   @bug 6688203
26   @summary Memory leak and performance problems in the method getFileSystemView of FileSystemView
27   @author Pavel Porvatov
28   @modules java.desktop/javax.swing.filechooser:open
29   @run main bug6688203
30*/
31
32import javax.swing.*;
33import javax.swing.filechooser.FileSystemView;
34import java.io.File;
35import java.lang.reflect.Field;
36
37public class bug6688203 {
38    public static void main(String[] args) {
39        // Create an instance of FileSystemView
40        FileSystemView.getFileSystemView();
41
42        int startCount = UIManager.getPropertyChangeListeners().length;
43
44        for (int i = 0; i < 100; i++) {
45            FileSystemView.getFileSystemView();
46        }
47
48        if (startCount != UIManager.getPropertyChangeListeners().length) {
49            throw new RuntimeException("New listeners were added into UIManager");
50        }
51
52        FileSystemView fileSystemView = FileSystemView.getFileSystemView();
53        File file = new File("Some file");
54
55        for (UIManager.LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels()) {
56            try {
57                UIManager.setLookAndFeel(lafInfo.getClassName());
58            } catch (Exception e) {
59                // Ignore such errors
60                System.out.println("Cannot set LAF " + lafInfo.getName());
61
62                continue;
63            }
64
65            fileSystemView.getSystemDisplayName(file);
66
67            try {
68                Field field = FileSystemView.class.getDeclaredField("useSystemExtensionHiding");
69
70                field.setAccessible(true);
71
72                Boolean value = field.getBoolean(fileSystemView);
73
74                if (value != UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding")) {
75                    throw new RuntimeException("Invalid cached value of the FileSystemView.useSystemExtensionHiding field");
76                }
77            } catch (Exception e) {
78                throw new RuntimeException("Cannot read the FileSystemView.useSystemExtensionHiding field", e);
79            }
80        }
81    }
82}
83