jdk/test/javax/swing/JFileChooser/6741890/bug6741890.java
author rupashka
Wed, 21 Oct 2009 14:25:25 +0400
changeset 4269 0283fbab043c
child 4273 19adfe928d6c
permissions -rw-r--r--
6892340: Part of the CR 6741890 should be forwardported Reviewed-by: malenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4269
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
     1
/*
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
     4
 *
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
     8
 *
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    13
 * accompanied this code).
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    14
 *
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    18
 *
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    21
 * have any questions.
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    22
 */
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    23
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    24
/* @test
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    25
   @bug 6741890
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    26
   @summary Deadlock in Win32ShellFolderManager2
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    27
   @author Pavel Porvatov
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    28
   @run main bug6741890
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    29
*/
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    30
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    31
import sun.awt.shell.ShellFolder;
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    32
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    33
import java.io.File;
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    34
import java.lang.reflect.Field;
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    35
import java.util.concurrent.Callable;
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    36
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    37
public class bug6741890 {
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    38
    /**
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    39
     * This mux is used to prevent NPE in the isLink and isFileSystem methods
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    40
     */
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    41
    private static final Object mux = new Object();
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    42
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    43
    private static final int COUNT = 100000;
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    44
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    46
        String tmpDir = System.getProperty("java.io.tmpdir");
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    47
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    48
        if (tmpDir.length() == 0) { //'java.io.tmpdir' isn't guaranteed to be defined
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    49
            tmpDir = System.getProperty("user.home");
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    50
        }
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    51
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    52
        final ShellFolder tmpFile = ShellFolder.getShellFolder(new File(tmpDir));
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    53
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    54
        System.out.println("Temp directory: " + tmpDir);
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    55
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    56
        System.out.println("Stress test was run");
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    57
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    58
        Thread thread = new Thread() {
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    59
            public void run() {
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    60
                while (!isInterrupted()) {
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    61
                    ShellFolder.invoke(new Callable<Void>() {
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    62
                        public Void call() throws Exception {
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    63
                            synchronized (mux) {
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    64
                                tmpFile.isFileSystem();
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    65
                                tmpFile.isLink();
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    66
                            }
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    67
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    68
                            return null;
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    69
                        }
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    70
                    });
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    71
                }
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    72
            }
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    73
        };
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    74
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    75
        thread.start();
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    76
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    77
        for (int i = 0; i < COUNT; i++) {
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    78
            synchronized (mux) {
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    79
                clearField(tmpFile, "cachedIsLink");
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    80
                clearField(tmpFile, "cachedIsFileSystem");
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    81
            }
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    82
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    83
            tmpFile.isFileSystem();
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    84
            tmpFile.isLink();
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    85
        }
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    86
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    87
        thread.interrupt();
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    88
        thread.join();
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    89
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    90
        System.out.println("Test passed successfully");
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    91
    }
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    92
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    93
    private static void clearField(Object o, String fieldName) throws Exception {
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    94
        Field field = o.getClass().getDeclaredField(fieldName);
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    95
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    96
        field.setAccessible(true);
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    97
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    98
        field.set(o, null);
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
    99
    }
0283fbab043c 6892340: Part of the CR 6741890 should be forwardported
rupashka
parents:
diff changeset
   100
}