8199912: jshell tool: /open from URI

Reviewed-by: rfield
This commit is contained in:
Michal Vala 2018-05-04 23:19:10 -07:00 committed by Robert Field
parent 3ecce1fe48
commit 1e0c45b250
2 changed files with 65 additions and 7 deletions
src/jdk.jshell/share/classes/jdk/internal/jshell/tool
test/langtools/jdk/jshell

@ -40,6 +40,9 @@ import java.io.StringReader;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
@ -2999,13 +3002,22 @@ public class JShellTool implements MessageHandler {
} else {
Path path = toPathResolvingUserHome(filename);
String resource;
scanner = new Scanner(
(!Files.exists(path) && (resource = getResource(filename)) != null)
? new StringReader(resource) // Not found as file, but found as resource
: new FileReader(path.toString())
);
if (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);
}
}
}
try (var scannerIOContext = new ScannerIOContext(scanner)) {
run(scannerIOContext);
}
run(new ScannerIOContext(scanner));
return true;
} catch (FileNotFoundException e) {
errormsg("jshell.err.file.not.found", context, filename, e.getMessage());

@ -23,7 +23,7 @@
/*
* @test
* @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405 8174796 8174797 8175304 8167554 8180508 8166232 8196133
* @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405 8174796 8174797 8175304 8167554 8180508 8166232 8196133 8199912
* @summary Tests for Basic tests for REPL tool
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
@ -39,6 +39,8 @@ import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -52,6 +54,7 @@ import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.sun.net.httpserver.HttpServer;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
@ -491,6 +494,49 @@ public class ToolBasicTest extends ReplToolTesting {
}
}
public void testOpenLocalFileUrl() {
Compiler compiler = new Compiler();
Path path = compiler.getPath("testOpen.repl");
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, "a", "a ==> 10"),
(a) -> assertCommand(a, "b", "b ==> 20"),
(a) -> assertCommand(a, "c", "c ==> 30")
);
}
}
public void testOpenFileOverHttp() throws IOException {
var script = "int a = 10;int b = 20;int c = a + b;";
var localhostAddress = new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), 0);
var httpServer = HttpServer.create(localhostAddress, 0);
try {
httpServer.createContext("/script", exchange -> {
exchange.sendResponseHeaders(200, script.length());
try (var output = exchange.getResponseBody()) {
output.write(script.getBytes());
}
});
httpServer.setExecutor(null);
httpServer.start();
var urlAddress = "http:/" + httpServer.getAddress().toString() + "/script";
for (String s : new String[]{"/o", "/open"}) {
test(
(a) -> assertCommand(a, s + " " + urlAddress, ""),
(a) -> assertCommand(a, "a", "a ==> 10"),
(a) -> assertCommand(a, "b", "b ==> 20"),
(a) -> assertCommand(a, "c", "c ==> 30")
);
}
} finally {
httpServer.stop(0);
}
}
public void testOpenResource() {
test(
(a) -> assertCommand(a, "/open PRINTING", ""),