jdk/test/javax/swing/JFileChooser/6741890/bug6741890.java
changeset 4269 0283fbab043c
child 4273 19adfe928d6c
equal deleted inserted replaced
4002:4e7661eaa211 4269:0283fbab043c
       
     1 /*
       
     2  * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25    @bug 6741890
       
    26    @summary Deadlock in Win32ShellFolderManager2
       
    27    @author Pavel Porvatov
       
    28    @run main bug6741890
       
    29 */
       
    30 
       
    31 import sun.awt.shell.ShellFolder;
       
    32 
       
    33 import java.io.File;
       
    34 import java.lang.reflect.Field;
       
    35 import java.util.concurrent.Callable;
       
    36 
       
    37 public class bug6741890 {
       
    38     /**
       
    39      * This mux is used to prevent NPE in the isLink and isFileSystem methods
       
    40      */
       
    41     private static final Object mux = new Object();
       
    42 
       
    43     private static final int COUNT = 100000;
       
    44 
       
    45     public static void main(String[] args) throws Exception {
       
    46         String tmpDir = System.getProperty("java.io.tmpdir");
       
    47 
       
    48         if (tmpDir.length() == 0) { //'java.io.tmpdir' isn't guaranteed to be defined
       
    49             tmpDir = System.getProperty("user.home");
       
    50         }
       
    51 
       
    52         final ShellFolder tmpFile = ShellFolder.getShellFolder(new File(tmpDir));
       
    53 
       
    54         System.out.println("Temp directory: " + tmpDir);
       
    55 
       
    56         System.out.println("Stress test was run");
       
    57 
       
    58         Thread thread = new Thread() {
       
    59             public void run() {
       
    60                 while (!isInterrupted()) {
       
    61                     ShellFolder.invoke(new Callable<Void>() {
       
    62                         public Void call() throws Exception {
       
    63                             synchronized (mux) {
       
    64                                 tmpFile.isFileSystem();
       
    65                                 tmpFile.isLink();
       
    66                             }
       
    67 
       
    68                             return null;
       
    69                         }
       
    70                     });
       
    71                 }
       
    72             }
       
    73         };
       
    74 
       
    75         thread.start();
       
    76 
       
    77         for (int i = 0; i < COUNT; i++) {
       
    78             synchronized (mux) {
       
    79                 clearField(tmpFile, "cachedIsLink");
       
    80                 clearField(tmpFile, "cachedIsFileSystem");
       
    81             }
       
    82 
       
    83             tmpFile.isFileSystem();
       
    84             tmpFile.isLink();
       
    85         }
       
    86 
       
    87         thread.interrupt();
       
    88         thread.join();
       
    89 
       
    90         System.out.println("Test passed successfully");
       
    91     }
       
    92 
       
    93     private static void clearField(Object o, String fieldName) throws Exception {
       
    94         Field field = o.getClass().getDeclaredField(fieldName);
       
    95 
       
    96         field.setAccessible(true);
       
    97 
       
    98         field.set(o, null);
       
    99     }
       
   100 }