8198004: javax/swing/JFileChooser/6868611/bug6868611.java throws error
authorjdv
Fri, 23 Feb 2018 12:30:03 +0530
changeset 49100 bec86eb4a71a
parent 49099 8ff80b2e0fe0
child 49101 206a6f728ce5
8198004: javax/swing/JFileChooser/6868611/bug6868611.java throws error Reviewed-by: serb, ssadetsky, kaddepalli
test/jdk/javax/swing/JFileChooser/6868611/bug6868611.java
--- a/test/jdk/javax/swing/JFileChooser/6868611/bug6868611.java	Fri Feb 23 09:59:16 2018 +0530
+++ b/test/jdk/javax/swing/JFileChooser/6868611/bug6868611.java	Fri Feb 23 12:30:03 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -21,73 +21,74 @@
  * questions.
  */
 
-/* @test
-   @bug 6868611
-   @summary FileSystemView throws NullPointerException
-   @author Pavel Porvatov
-   @run main bug6868611
-*/
+/*
+ * @test
+ * @bug 6868611 8198004
+ * @summary FileSystemView throws NullPointerException
+ * @author Pavel Porvatov
+ * @run main bug6868611
+ */
 
 import javax.swing.*;
 import javax.swing.filechooser.FileSystemView;
 import java.io.File;
+import java.nio.file.Files;
 
 public class bug6868611 {
     private static final int COUNT = 1000;
+    private static File tempFolder;
+    private static File files[] = new File[COUNT];
 
     public static void main(String[] args) throws Exception {
-        String tempDirProp = System.getProperty("java.io.tmpdir");
-
-        final String tempDir = tempDirProp == null || !new File(tempDirProp).isDirectory() ?
-            System.getProperty("user.home") : tempDirProp;
-
-        System.out.println("Temp directory: " + tempDir);
+        int fileCount = 0;
+        try {
+            tempFolder = Files.createTempDirectory("temp_folder").toFile();
 
-        // Create 1000 files
-        for (int i = 0; i < 1000; i++) {
-            new File(tempDir, "temp" + i).createNewFile();
-        }
-
-        // Init default FileSystemView
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                FileSystemView.getFileSystemView().getFiles(new File(tempDir), false);
+            // Try creating 1000 files
+            for (fileCount = 0; fileCount < COUNT; fileCount++) {
+                files[fileCount] = new
+                        File(tempFolder, "temp" + fileCount + ".txt");
+                files[fileCount].createNewFile();
             }
-        });
 
-        for (int i = 0; i < COUNT; i++) {
-            Thread thread = new MyThread(tempDir);
+            // Init default FileSystemView
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    FileSystemView.getFileSystemView().
+                            getFiles(tempFolder, false);
+                }
+            });
 
-            thread.start();
-
-            Thread.sleep((long) (Math.random() * 100));
-
-            thread.interrupt();
+            for (int i = 0; i < COUNT; i++) {
+                Thread thread = new MyThread(tempFolder);
 
-            if (i % 100 == 0) {
-                System.out.print("*");
-            }
-        }
+                thread.start();
+
+                Thread.sleep((long) (Math.random() * 100));
 
-        System.out.println();
-
-        // Remove 1000 files
-        for (int i = 0; i < 1000; i++) {
-            new File(tempDir, "temp" + i).delete();
+                thread.interrupt();
+            }
+        } finally {
+            // Remove created files
+            for (int i = 0; i < fileCount; i++) {
+                Files.delete(files[i].toPath());
+            }
+            Files.delete(tempFolder.toPath());
         }
     }
 
     private static class MyThread extends Thread {
-        private final String dir;
+        private final File dir;
 
-        private MyThread(String dir) {
+        private MyThread(File dir) {
             this.dir = dir;
         }
 
         public void run() {
             FileSystemView fileSystemView = FileSystemView.getFileSystemView();
 
-            fileSystemView.getFiles(new File(dir), false);
+            fileSystemView.getFiles(dir, false);
         }
     }
 }
+