2015-10-19 17:15:16 +00:00
|
|
|
/*
|
2017-04-12 00:26:52 +00:00
|
|
|
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
|
2015-10-19 17:15:16 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-04-12 00:26:52 +00:00
|
|
|
* @test 8151754 8080883 8160089 8170162 8166581 8172102 8171343 8178023
|
2015-10-19 17:15:16 +00:00
|
|
|
* @summary Testing start-up options.
|
2016-01-25 19:01:32 +00:00
|
|
|
* @modules jdk.compiler/com.sun.tools.javac.api
|
|
|
|
* jdk.compiler/com.sun.tools.javac.main
|
8142968: Module System implementation
Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282
Co-authored-by: Alex Buckley <alex.buckley@oracle.com>
Co-authored-by: Jonathan Gibbons <jonathan.gibbons@oracle.com>
Co-authored-by: Karen Kinnear <karen.kinnear@oracle.com>
Co-authored-by: Mandy Chung <mandy.chung@oracle.com>
Co-authored-by: Mark Reinhold <mark.reinhold@oracle.com>
Co-authored-by: Jan Lahoda <jan.lahoda@oracle.com>
Co-authored-by: Vicente Romero <vicente.romero@oracle.com>
Co-authored-by: Andreas Lundblad <andreas.lundblad@oracle.com>
Co-authored-by: Andrey Nazarov <andrey.x.nazarov@oracle.com>
Co-authored-by: Chris Hegarty <chris.hegarty@oracle.com>
Co-authored-by: Erik Joelsson <erik.joelsson@oracle.com>
Co-authored-by: Kumar Srinivasan <kumar.x.srinivasan@oracle.com>
Co-authored-by: Sundararajan Athijegannathan <sundararajan.athijegannathan@oracle.com>
Reviewed-by: jjg, jlahoda, vromero, mcimadamore, bpatel, ksrini, darcy, anazarov, dfuchs
2016-03-17 19:04:28 +00:00
|
|
|
* jdk.jdeps/com.sun.tools.javap
|
2016-01-25 19:01:32 +00:00
|
|
|
* jdk.jshell/jdk.internal.jshell.tool
|
2015-10-19 17:15:16 +00:00
|
|
|
* @library /tools/lib
|
2016-03-31 22:20:50 +00:00
|
|
|
* @build Compiler toolbox.ToolBox
|
2015-10-19 17:15:16 +00:00
|
|
|
* @run testng StartOptionTest
|
|
|
|
*/
|
|
|
|
|
2017-01-25 18:43:41 +00:00
|
|
|
import java.io.ByteArrayInputStream;
|
2015-10-19 17:15:16 +00:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2017-01-25 18:43:41 +00:00
|
|
|
import java.io.InputStream;
|
2015-10-19 17:15:16 +00:00
|
|
|
import java.io.PrintStream;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.file.Path;
|
2016-10-25 00:06:10 +00:00
|
|
|
import java.util.HashMap;
|
2016-04-04 17:31:20 +00:00
|
|
|
import java.util.Locale;
|
2015-10-19 17:15:16 +00:00
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
2017-04-12 00:26:52 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2015-10-19 17:15:16 +00:00
|
|
|
import org.testng.annotations.AfterMethod;
|
|
|
|
import org.testng.annotations.BeforeMethod;
|
|
|
|
import org.testng.annotations.Test;
|
2016-12-20 21:42:13 +00:00
|
|
|
import jdk.jshell.tool.JavaShellToolBuilder;
|
2015-10-19 17:15:16 +00:00
|
|
|
import static org.testng.Assert.assertEquals;
|
2017-01-25 18:43:41 +00:00
|
|
|
import static org.testng.Assert.assertFalse;
|
2015-10-19 17:15:16 +00:00
|
|
|
import static org.testng.Assert.assertTrue;
|
2016-11-23 03:24:02 +00:00
|
|
|
import static org.testng.Assert.fail;
|
2015-10-19 17:15:16 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public class StartOptionTest {
|
|
|
|
|
2016-05-26 14:58:01 +00:00
|
|
|
private ByteArrayOutputStream cmdout;
|
|
|
|
private ByteArrayOutputStream cmderr;
|
|
|
|
private ByteArrayOutputStream console;
|
|
|
|
private ByteArrayOutputStream userout;
|
|
|
|
private ByteArrayOutputStream usererr;
|
2017-01-25 18:43:41 +00:00
|
|
|
private InputStream cmdInStream;
|
2015-10-19 17:15:16 +00:00
|
|
|
|
2016-12-20 21:42:13 +00:00
|
|
|
private JavaShellToolBuilder builder() {
|
2017-04-12 00:26:52 +00:00
|
|
|
// turn on logging of launch failures
|
|
|
|
Logger.getLogger("jdk.jshell.execution").setLevel(Level.ALL);
|
2016-12-20 21:42:13 +00:00
|
|
|
return JavaShellToolBuilder
|
|
|
|
.builder()
|
|
|
|
.out(new PrintStream(cmdout), new PrintStream(console), new PrintStream(userout))
|
|
|
|
.err(new PrintStream(cmderr), new PrintStream(usererr))
|
2017-01-25 18:43:41 +00:00
|
|
|
.in(cmdInStream, null)
|
2016-12-20 21:42:13 +00:00
|
|
|
.persistence(new HashMap<>())
|
|
|
|
.env(new HashMap<>())
|
|
|
|
.locale(Locale.ROOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void runShell(String... args) {
|
|
|
|
try {
|
|
|
|
builder()
|
|
|
|
.run(args);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
fail("Repl tool died with exception", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void check(ByteArrayOutputStream str, Consumer<String> checkOut, String label) {
|
2016-05-26 14:58:01 +00:00
|
|
|
byte[] bytes = str.toByteArray();
|
|
|
|
str.reset();
|
|
|
|
String out = new String(bytes, StandardCharsets.UTF_8);
|
|
|
|
if (checkOut != null) {
|
|
|
|
checkOut.accept(out);
|
|
|
|
} else {
|
|
|
|
assertEquals("", out, label + ": Expected empty -- ");
|
|
|
|
}
|
2015-10-19 17:15:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 21:42:13 +00:00
|
|
|
protected void start(Consumer<String> checkCmdOutput,
|
|
|
|
Consumer<String> checkUserOutput, Consumer<String> checkError,
|
|
|
|
String... args) throws Exception {
|
|
|
|
runShell(args);
|
|
|
|
check(cmdout, checkCmdOutput, "cmdout");
|
2016-05-26 14:58:01 +00:00
|
|
|
check(cmderr, checkError, "cmderr");
|
|
|
|
check(console, null, "console");
|
2016-12-20 21:42:13 +00:00
|
|
|
check(userout, checkUserOutput, "userout");
|
2016-05-26 14:58:01 +00:00
|
|
|
check(usererr, null, "usererr");
|
2015-10-19 17:15:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 21:42:13 +00:00
|
|
|
protected void start(String expectedCmdOutput, String expectedError, String... args) throws Exception {
|
|
|
|
startWithUserOutput(expectedCmdOutput, "", expectedError, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startWithUserOutput(String expectedCmdOutput, String expectedUserOutput,
|
|
|
|
String expectedError, String... args) throws Exception {
|
|
|
|
start(
|
|
|
|
s -> assertEquals(s.trim(), expectedCmdOutput, "cmdout: "),
|
|
|
|
s -> assertEquals(s.trim(), expectedUserOutput, "userout: "),
|
|
|
|
s -> assertEquals(s.trim(), expectedError, "cmderr: "),
|
|
|
|
args);
|
2015-10-19 17:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@BeforeMethod
|
|
|
|
public void setUp() {
|
2016-05-26 14:58:01 +00:00
|
|
|
cmdout = new ByteArrayOutputStream();
|
|
|
|
cmderr = new ByteArrayOutputStream();
|
|
|
|
console = new ByteArrayOutputStream();
|
|
|
|
userout = new ByteArrayOutputStream();
|
|
|
|
usererr = new ByteArrayOutputStream();
|
2017-01-25 18:43:41 +00:00
|
|
|
cmdInStream = new ByteArrayInputStream("/exit\n".getBytes());
|
2015-10-19 17:15:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 21:42:13 +00:00
|
|
|
protected String writeToFile(String stuff) throws Exception {
|
|
|
|
Compiler compiler = new Compiler();
|
|
|
|
Path p = compiler.getPath("doit.repl");
|
|
|
|
compiler.writeToFile(p, stuff);
|
|
|
|
return p.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testCommandFile() throws Exception {
|
|
|
|
String fn = writeToFile("String str = \"Hello \"\n/list\nSystem.out.println(str + str)\n/exit\n");
|
|
|
|
startWithUserOutput("1 : String str = \"Hello \";", "Hello Hello", "", "--no-startup", fn, "-s");
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:15:16 +00:00
|
|
|
public void testUsage() throws Exception {
|
2016-08-23 02:31:37 +00:00
|
|
|
for (String opt : new String[]{"-h", "--help"}) {
|
|
|
|
start(s -> {
|
|
|
|
assertTrue(s.split("\n").length >= 7, "Not enough usage lines: " + s);
|
|
|
|
assertTrue(s.startsWith("Usage: jshell <options>"), "Unexpect usage start: " + s);
|
2017-01-25 18:43:41 +00:00
|
|
|
assertTrue(s.contains("--show-version"), "Expected help: " + s);
|
|
|
|
assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
|
|
|
|
}, null, null, opt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testHelpExtra() throws Exception {
|
|
|
|
for (String opt : new String[]{"-X", "--help-extra"}) {
|
|
|
|
start(s -> {
|
|
|
|
assertTrue(s.split("\n").length >= 5, "Not enough help-extra lines: " + s);
|
|
|
|
assertTrue(s.contains("--add-exports"), "Expected --add-exports: " + s);
|
|
|
|
assertTrue(s.contains("--execution"), "Expected --add-exports: " + s);
|
|
|
|
assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
|
2016-12-20 21:42:13 +00:00
|
|
|
}, null, null, opt);
|
2016-08-23 02:31:37 +00:00
|
|
|
}
|
2015-10-19 17:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testUnknown() throws Exception {
|
2016-12-20 21:42:13 +00:00
|
|
|
start(null, null,
|
2016-08-23 02:31:37 +00:00
|
|
|
s -> assertEquals(s.trim(), "Unknown option: u"), "-unknown");
|
2016-12-20 21:42:13 +00:00
|
|
|
start(null, null,
|
2016-08-23 02:31:37 +00:00
|
|
|
s -> assertEquals(s.trim(), "Unknown option: unknown"), "--unknown");
|
2015-10-19 17:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testStartup() throws Exception {
|
|
|
|
Compiler compiler = new Compiler();
|
|
|
|
Path p = compiler.getPath("file.txt");
|
|
|
|
compiler.writeToFile(p);
|
2016-08-23 02:31:37 +00:00
|
|
|
start("", "Argument to startup missing.", "--startup");
|
2017-01-03 02:31:30 +00:00
|
|
|
start("", "Conflicting options: both --startup and --no-startup were used.", "--no-startup", "--startup", p.toString());
|
|
|
|
start("", "Conflicting options: both --startup and --no-startup were used.", "--startup", p.toString(), "--no-startup");
|
2016-08-23 02:31:37 +00:00
|
|
|
start("", "Argument to startup missing.", "--no-startup", "--startup");
|
2016-05-26 19:38:21 +00:00
|
|
|
}
|
|
|
|
|
2016-11-23 03:24:02 +00:00
|
|
|
public void testStartupFailedOption() throws Exception {
|
2017-04-12 00:26:52 +00:00
|
|
|
start(
|
|
|
|
s -> assertEquals(s.trim(), "", "cmdout: "),
|
|
|
|
s -> assertEquals(s.trim(), "", "userout: "),
|
|
|
|
s -> assertTrue(s.contains("Unrecognized option: -hoge-foo-bar"), "cmderr: " + s),
|
|
|
|
"-R-hoge-foo-bar");
|
2016-11-23 03:24:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 19:38:21 +00:00
|
|
|
public void testStartupUnknown() throws Exception {
|
2016-08-23 02:31:37 +00:00
|
|
|
start("", "File 'UNKNOWN' for '--startup' is not found.", "--startup", "UNKNOWN");
|
2017-01-03 02:31:30 +00:00
|
|
|
start("", "File 'UNKNOWN' for '--startup' is not found.", "--startup", "DEFAULT", "--startup", "UNKNOWN");
|
2015-10-19 17:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testClasspath() throws Exception {
|
2016-08-23 02:31:37 +00:00
|
|
|
for (String cp : new String[] {"--class-path"}) {
|
|
|
|
start("", "Only one --class-path option may be used.", cp, ".", "--class-path", ".");
|
|
|
|
start("", "Argument to class-path missing.", cp);
|
2015-10-19 17:15:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 00:26:52 +00:00
|
|
|
public void testUnknownModule() throws Exception {
|
|
|
|
start(
|
|
|
|
s -> assertEquals(s.trim(), "", "cmdout: "),
|
|
|
|
s -> assertEquals(s.trim(), "", "userout: "),
|
|
|
|
s -> assertTrue(s.contains("rror") && s.contains("unKnown"), "cmderr: " + s),
|
|
|
|
"--add-modules", "unKnown");
|
|
|
|
}
|
|
|
|
|
2016-08-23 02:31:37 +00:00
|
|
|
public void testFeedbackOptionConflict() throws Exception {
|
|
|
|
start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.",
|
|
|
|
"--feedback", "concise", "--feedback", "verbose");
|
|
|
|
start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-s");
|
|
|
|
start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "verbose", "-q");
|
|
|
|
start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-v");
|
|
|
|
start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "--feedback", "concise");
|
|
|
|
start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-v");
|
|
|
|
start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-s", "-v");
|
|
|
|
start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "-q");
|
|
|
|
start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-s");
|
|
|
|
}
|
|
|
|
|
2016-04-28 01:13:19 +00:00
|
|
|
public void testNegFeedbackOption() throws Exception {
|
2016-08-23 02:31:37 +00:00
|
|
|
start("", "Argument to feedback missing.", "--feedback");
|
|
|
|
start("", "Does not match any current feedback mode: blorp -- --feedback blorp", "--feedback", "blorp");
|
2016-04-28 01:13:19 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 17:15:16 +00:00
|
|
|
public void testVersion() throws Exception {
|
2017-01-25 18:43:41 +00:00
|
|
|
start(
|
|
|
|
s -> {
|
|
|
|
assertTrue(s.startsWith("jshell"), "unexpected version: " + s);
|
|
|
|
assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
|
|
|
|
},
|
|
|
|
null, null,
|
|
|
|
"--version");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testShowVersion() throws Exception {
|
|
|
|
runShell("--show-version");
|
|
|
|
check(cmdout,
|
|
|
|
s -> {
|
|
|
|
assertTrue(s.startsWith("jshell"), "unexpected version: " + s);
|
|
|
|
assertTrue(s.contains("Welcome"), "Expected start (but got no welcome): " + s);
|
|
|
|
},
|
|
|
|
"cmdout");
|
|
|
|
check(cmderr, null, "cmderr");
|
|
|
|
check(console,
|
|
|
|
s -> assertTrue(s.trim().startsWith("jshell>"), "Expected prompt, got: " + s),
|
|
|
|
"console");
|
|
|
|
check(userout, null, "userout");
|
|
|
|
check(usererr, null, "usererr");
|
2015-10-19 17:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@AfterMethod
|
|
|
|
public void tearDown() {
|
2016-05-26 14:58:01 +00:00
|
|
|
cmdout = null;
|
|
|
|
cmderr = null;
|
|
|
|
console = null;
|
|
|
|
userout = null;
|
|
|
|
usererr = null;
|
2017-01-25 18:43:41 +00:00
|
|
|
cmdInStream = null;
|
2015-10-19 17:15:16 +00:00
|
|
|
}
|
|
|
|
}
|