8202690: jdk/jshell/ToolBasicTest.java failed in testOpenFileOverHttp() and testOpenLocalFileUrl()
authorxyin
Tue, 08 May 2018 09:51:42 +0800
changeset 50043 2d1952d4d067
parent 50042 9f2b32b87906
child 50044 2566ad726765
8202690: jdk/jshell/ToolBasicTest.java failed in testOpenFileOverHttp() and testOpenLocalFileUrl() Reviewed-by: rfield, jlahoda
src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java
test/langtools/jdk/jshell/ToolBasicTest.java
--- a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Mon May 07 21:48:28 2018 -0400
+++ b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Tue May 08 09:51:42 2018 +0800
@@ -42,6 +42,7 @@
 import java.lang.module.ModuleReference;
 import java.net.MalformedURLException;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.nio.charset.Charset;
 import java.nio.file.FileSystems;
@@ -3000,19 +3001,34 @@
                     regenerateOnDeath = false;
                     scanner = new Scanner(cmdin);
                 } else {
-                    Path path = toPathResolvingUserHome(filename);
+                    Path path = null;
+                    URL url = null;
                     String resource;
-                    if (Files.exists(path)) {
+                    try {
+                        path = toPathResolvingUserHome(filename);
+                    } catch (InvalidPathException ipe) {
+                        try {
+                            url = new URL(filename);
+                            if (url.getProtocol().equalsIgnoreCase("file")) {
+                                path = Paths.get(url.toURI());
+                            }
+                        } catch (MalformedURLException | URISyntaxException e) {
+                            throw new FileNotFoundException(filename);
+                        }
+                    }
+                    if (path != null && Files.exists(path)) {
                         scanner = new Scanner(new FileReader(path.toString()));
                     } else if ((resource = getResource(filename)) != null) {
                         scanner = new Scanner(new StringReader(resource));
                     } else {
-                        try {
-                            var url = new URL(filename);
-                            scanner = new Scanner(url.openStream());
-                        } catch (MalformedURLException mue) {
-                            throw new FileNotFoundException(filename);
+                        if (url == null) {
+                            try {
+                                url = new URL(filename);
+                            } catch (MalformedURLException mue) {
+                                throw new FileNotFoundException(filename);
+                            }
                         }
+                        scanner = new Scanner(url.openStream());
                     }
                 }
                 try (var scannerIOContext = new ScannerIOContext(scanner)) {
--- a/test/langtools/jdk/jshell/ToolBasicTest.java	Mon May 07 21:48:28 2018 -0400
+++ b/test/langtools/jdk/jshell/ToolBasicTest.java	Tue May 08 09:51:42 2018 +0800
@@ -501,7 +501,7 @@
         compiler.writeToFile(path, "int a = 10;int b = 20;int c = a + b;\n");
         for (String s : new String[]{"/o", "/open"}) {
             test(
-                    (a) -> assertCommand(a, s + " file://" + path.toString(), ""),
+                    (a) -> assertCommand(a, s + " " + path.toUri(), ""),
                     (a) -> assertCommand(a, "a", "a ==> 10"),
                     (a) -> assertCommand(a, "b", "b ==> 20"),
                     (a) -> assertCommand(a, "c", "c ==> 30")