StartOptionTest.java revision 3921:919a15cb34bb
1/*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test 8151754 8080883 8160089 8170162 8166581 8172102 8171343
26 * @summary Testing start-up options.
27 * @modules jdk.compiler/com.sun.tools.javac.api
28 *          jdk.compiler/com.sun.tools.javac.main
29 *          jdk.jdeps/com.sun.tools.javap
30 *          jdk.jshell/jdk.internal.jshell.tool
31 * @library /tools/lib
32 * @build Compiler toolbox.ToolBox
33 * @run testng StartOptionTest
34 */
35
36import java.io.ByteArrayInputStream;
37import java.io.ByteArrayOutputStream;
38import java.io.InputStream;
39import java.io.PrintStream;
40import java.nio.charset.StandardCharsets;
41import java.nio.file.Path;
42import java.util.HashMap;
43import java.util.Locale;
44import java.util.function.Consumer;
45
46import org.testng.annotations.AfterMethod;
47import org.testng.annotations.BeforeMethod;
48import org.testng.annotations.Test;
49import jdk.jshell.tool.JavaShellToolBuilder;
50import static org.testng.Assert.assertEquals;
51import static org.testng.Assert.assertFalse;
52import static org.testng.Assert.assertTrue;
53import static org.testng.Assert.fail;
54
55@Test
56public class StartOptionTest {
57
58    private ByteArrayOutputStream cmdout;
59    private ByteArrayOutputStream cmderr;
60    private ByteArrayOutputStream console;
61    private ByteArrayOutputStream userout;
62    private ByteArrayOutputStream usererr;
63    private InputStream cmdInStream;
64
65    private JavaShellToolBuilder builder() {
66        return JavaShellToolBuilder
67                    .builder()
68                    .out(new PrintStream(cmdout), new PrintStream(console), new PrintStream(userout))
69                    .err(new PrintStream(cmderr), new PrintStream(usererr))
70                    .in(cmdInStream, null)
71                    .persistence(new HashMap<>())
72                    .env(new HashMap<>())
73                    .locale(Locale.ROOT);
74    }
75
76    private void runShell(String... args) {
77        try {
78            builder()
79                    .run(args);
80        } catch (Exception ex) {
81            fail("Repl tool died with exception", ex);
82        }
83    }
84
85    protected void check(ByteArrayOutputStream str, Consumer<String> checkOut, String label) {
86        byte[] bytes = str.toByteArray();
87        str.reset();
88        String out =  new String(bytes, StandardCharsets.UTF_8);
89        if (checkOut != null) {
90            checkOut.accept(out);
91        } else {
92            assertEquals("", out, label + ": Expected empty -- ");
93        }
94    }
95
96    protected void start(Consumer<String> checkCmdOutput,
97            Consumer<String> checkUserOutput, Consumer<String> checkError,
98            String... args) throws Exception {
99        runShell(args);
100        check(cmdout, checkCmdOutput, "cmdout");
101        check(cmderr, checkError, "cmderr");
102        check(console, null, "console");
103        check(userout, checkUserOutput, "userout");
104        check(usererr, null, "usererr");
105    }
106
107    protected void start(String expectedCmdOutput, String expectedError, String... args) throws Exception {
108        startWithUserOutput(expectedCmdOutput, "",  expectedError, args);
109    }
110
111    private void startWithUserOutput(String expectedCmdOutput, String expectedUserOutput,
112            String expectedError, String... args) throws Exception {
113        start(
114                s -> assertEquals(s.trim(), expectedCmdOutput, "cmdout: "),
115                s -> assertEquals(s.trim(), expectedUserOutput, "userout: "),
116                s -> assertEquals(s.trim(), expectedError, "cmderr: "),
117                args);
118    }
119
120    @BeforeMethod
121    public void setUp() {
122        cmdout  = new ByteArrayOutputStream();
123        cmderr  = new ByteArrayOutputStream();
124        console = new ByteArrayOutputStream();
125        userout = new ByteArrayOutputStream();
126        usererr = new ByteArrayOutputStream();
127        cmdInStream = new ByteArrayInputStream("/exit\n".getBytes());
128    }
129
130    protected String writeToFile(String stuff) throws Exception {
131        Compiler compiler = new Compiler();
132        Path p = compiler.getPath("doit.repl");
133        compiler.writeToFile(p, stuff);
134        return p.toString();
135    }
136
137    public void testCommandFile() throws Exception {
138        String fn = writeToFile("String str = \"Hello \"\n/list\nSystem.out.println(str + str)\n/exit\n");
139        startWithUserOutput("1 : String str = \"Hello \";", "Hello Hello", "", "--no-startup", fn, "-s");
140    }
141
142    public void testUsage() throws Exception {
143        for (String opt : new String[]{"-h", "--help"}) {
144            start(s -> {
145                assertTrue(s.split("\n").length >= 7, "Not enough usage lines: " + s);
146                assertTrue(s.startsWith("Usage:   jshell <options>"), "Unexpect usage start: " + s);
147                assertTrue(s.contains("--show-version"), "Expected help: " + s);
148                assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
149            }, null, null, opt);
150        }
151    }
152
153    public void testHelpExtra() throws Exception {
154        for (String opt : new String[]{"-X", "--help-extra"}) {
155            start(s -> {
156                assertTrue(s.split("\n").length >= 5, "Not enough help-extra lines: " + s);
157                assertTrue(s.contains("--add-exports"), "Expected --add-exports: " + s);
158                assertTrue(s.contains("--execution"), "Expected --add-exports: " + s);
159                assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
160            }, null, null, opt);
161        }
162    }
163
164    public void testUnknown() throws Exception {
165        start(null, null,
166              s -> assertEquals(s.trim(), "Unknown option: u"), "-unknown");
167        start(null, null,
168              s -> assertEquals(s.trim(), "Unknown option: unknown"), "--unknown");
169    }
170
171    public void testStartup() throws Exception {
172        Compiler compiler = new Compiler();
173        Path p = compiler.getPath("file.txt");
174        compiler.writeToFile(p);
175        start("", "Argument to startup missing.", "--startup");
176        start("", "Conflicting options: both --startup and --no-startup were used.", "--no-startup", "--startup", p.toString());
177        start("", "Conflicting options: both --startup and --no-startup were used.", "--startup", p.toString(), "--no-startup");
178        start("", "Argument to startup missing.", "--no-startup", "--startup");
179    }
180
181    public void testStartupFailedOption() throws Exception {
182        try {
183            builder().run("-R-hoge-foo-bar");
184        } catch (IllegalStateException ex) {
185            String s = ex.getMessage();
186            assertTrue(s.startsWith("Launching JShell execution engine threw: Failed remote"), s);
187            return;
188        }
189        fail("Expected IllegalStateException");
190    }
191
192    public void testStartupUnknown() throws Exception {
193        start("", "File 'UNKNOWN' for '--startup' is not found.", "--startup", "UNKNOWN");
194        start("", "File 'UNKNOWN' for '--startup' is not found.", "--startup", "DEFAULT", "--startup", "UNKNOWN");
195    }
196
197    public void testClasspath() throws Exception {
198        for (String cp : new String[] {"--class-path"}) {
199            start("", "Only one --class-path option may be used.", cp, ".", "--class-path", ".");
200            start("", "Argument to class-path missing.", cp);
201        }
202    }
203
204    public void testFeedbackOptionConflict() throws Exception {
205        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.",
206                "--feedback", "concise", "--feedback", "verbose");
207        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-s");
208        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "verbose", "-q");
209        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-v");
210        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "--feedback", "concise");
211        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-v");
212        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-s", "-v");
213        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "-q");
214        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-s");
215    }
216
217    public void testNegFeedbackOption() throws Exception {
218        start("", "Argument to feedback missing.", "--feedback");
219        start("", "Does not match any current feedback mode: blorp -- --feedback blorp", "--feedback", "blorp");
220    }
221
222    public void testVersion() throws Exception {
223        start(
224                s -> {
225                    assertTrue(s.startsWith("jshell"), "unexpected version: " + s);
226                    assertFalse(s.contains("Welcome"), "Unexpected start: " + s);
227                },
228                null, null,
229                "--version");
230    }
231
232    public void testShowVersion() throws Exception {
233        runShell("--show-version");
234        check(cmdout,
235                s -> {
236                    assertTrue(s.startsWith("jshell"), "unexpected version: " + s);
237                    assertTrue(s.contains("Welcome"), "Expected start (but got no welcome): " + s);
238                },
239                "cmdout");
240        check(cmderr, null, "cmderr");
241        check(console,
242                s -> assertTrue(s.trim().startsWith("jshell>"), "Expected prompt, got: " + s),
243                "console");
244        check(userout, null, "userout");
245        check(usererr, null, "usererr");
246    }
247
248    @AfterMethod
249    public void tearDown() {
250        cmdout  = null;
251        cmderr  = null;
252        console = null;
253        userout = null;
254        usererr = null;
255        cmdInStream = null;
256    }
257}
258