1 /*
   2  * Copyright (c) 2009, 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 6868611
  26    @summary FileSystemView throws NullPointerException
  27    @author Pavel Porvatov
  28    @run main bug6868611
  29 */
  30 
  31 import javax.swing.*;
  32 import javax.swing.filechooser.FileSystemView;
  33 import java.io.File;
  34 
  35 public class bug6868611 {
  36     private static final int COUNT = 1000;
  37 
  38     public static void main(String[] args) throws Exception {
  39         String tempDirProp = System.getProperty("java.io.tmpdir");
  40 
  41         final String tempDir = tempDirProp == null || !new File(tempDirProp).isDirectory() ?
  42             System.getProperty("user.home") : tempDirProp;
  43 
  44         System.out.println("Temp directory: " + tempDir);
  45 
  46         // Create 1000 files
  47         for (int i = 0; i < 1000; i++) {
  48             new File(tempDir, "temp" + i).createNewFile();
  49         }
  50 
  51         // Init default FileSystemView
  52         SwingUtilities.invokeAndWait(new Runnable() {
  53             public void run() {
  54                 FileSystemView.getFileSystemView().getFiles(new File(tempDir), false);
  55             }
  56         });
  57 
  58         for (int i = 0; i < COUNT; i++) {
  59             Thread thread = new MyThread(tempDir);
  60 
  61             thread.start();
  62 
  63             Thread.sleep((long) (Math.random() * 100));
  64 
  65             thread.interrupt();
  66 
  67             if (i % 100 == 0) {
  68                 System.out.print("*");
  69             }
  70         }
  71 
  72         System.out.println();
  73 
  74         // Remove 1000 files
  75         for (int i = 0; i < 1000; i++) {
  76             new File(tempDir, "temp" + i).delete();
  77         }
  78     }
  79 
  80     private static class MyThread extends Thread {
  81         private final String dir;
  82 
  83         private MyThread(String dir) {
  84             this.dir = dir;
  85         }
  86 
  87         public void run() {
  88             FileSystemView fileSystemView = FileSystemView.getFileSystemView();
  89 
  90             fileSystemView.getFiles(new File(dir), false);
  91         }
  92     }
  93 }