8132497: (fs) FileSystems.newFileSystem(URI, ..) doesn't handle UOE thrown by provider
authorbpb
Mon, 10 Aug 2015 10:50:24 -0700
changeset 32097 5f00777700b0
parent 32096 486535cbcc97
child 32098 b39f6c2da1a0
8132497: (fs) FileSystems.newFileSystem(URI, ..) doesn't handle UOE thrown by provider Summary: Handle UOEs in newFileSystem(URI,...) similarly to as done in newFileSystem(Path path, ClassLoader loader). Reviewed-by: chegar
jdk/src/java.base/share/classes/java/nio/file/FileSystems.java
jdk/test/java/nio/file/FileSystem/Basic.java
--- a/jdk/src/java.base/share/classes/java/nio/file/FileSystems.java	Fri Aug 07 16:09:10 2015 -0700
+++ b/jdk/src/java.base/share/classes/java/nio/file/FileSystems.java	Mon Aug 10 10:50:24 2015 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2015, 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
@@ -321,9 +321,12 @@
         String scheme = uri.getScheme();
 
         // check installed providers
-        for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
+        for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
             if (scheme.equalsIgnoreCase(provider.getScheme())) {
-                return provider.newFileSystem(uri, env);
+                try {
+                    return provider.newFileSystem(uri, env);
+                } catch (UnsupportedOperationException uoe) {
+                }
             }
         }
 
@@ -331,9 +334,12 @@
         if (loader != null) {
             ServiceLoader<FileSystemProvider> sl = ServiceLoader
                 .load(FileSystemProvider.class, loader);
-            for (FileSystemProvider provider: sl) {
+            for (FileSystemProvider provider : sl) {
                 if (scheme.equalsIgnoreCase(provider.getScheme())) {
-                    return provider.newFileSystem(uri, env);
+                    try {
+                        return provider.newFileSystem(uri, env);
+                    } catch (UnsupportedOperationException uoe) {
+                    }
                 }
             }
         }
--- a/jdk/test/java/nio/file/FileSystem/Basic.java	Fri Aug 07 16:09:10 2015 -0700
+++ b/jdk/test/java/nio/file/FileSystem/Basic.java	Mon Aug 10 10:50:24 2015 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2015, 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
@@ -22,17 +22,23 @@
  */
 
 /* @test
- * @bug 4313887 6838333
+ * @bug 4313887 6838333 8132497
  * @summary Unit test for java.nio.file.FileSystem
- * @library ..
+ * @library .. /lib/testlibrary
+ * @build jdk.testlibrary.FileUtils
+ * @run main/othervm Basic
  */
 
+import java.io.File;
 import java.nio.file.*;
-import java.nio.file.attribute.*;
 import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import jdk.testlibrary.FileUtils;
 
 /**
- * Simple santity checks for java.nio.file.FileSystem
+ * Simple sanity checks for java.nio.file.FileSystem
  */
 public class Basic {
 
@@ -48,7 +54,25 @@
         }
     }
 
-    public static void main(String[] args) throws IOException {
+    static void checkNoUOE() throws IOException, URISyntaxException {
+        String dir = System.getProperty("test.dir", ".");
+        String fileName = dir + File.separator + "foo.bar";
+        Path path = Paths.get(fileName);
+        Path file = Files.createFile(path);
+        try {
+            URI uri = new URI("jar", file.toUri().toString(), null);
+            System.out.println(uri);
+            FileSystem fs = FileSystems.newFileSystem(uri, new HashMap());
+            fs.close();
+        } catch (ProviderNotFoundException pnfe) {
+            System.out.println("Expected ProviderNotFoundException caught: "
+                + "\"" + pnfe.getMessage() + "\"");
+        } finally {
+            FileUtils.deleteFileWithRetry(path);
+        }
+    }
+
+    public static void main(String[] args) throws IOException, URISyntaxException {
         FileSystem fs = FileSystems.getDefault();
 
         // close should throw UOE
@@ -80,5 +104,9 @@
             checkSupported(fs, "posix", "unix", "owner");
         if (os.equals("Windows"))
             checkSupported(fs, "owner", "dos", "acl", "user");
+
+        // sanity check non-throwing of UnsupportedOperationException by
+        // FileSystems.newFileSystem(URI, ..)
+        checkNoUOE();
     }
 }