StartOptionTest.java revision 3849:b2e915d476be
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
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.ByteArrayOutputStream;
37import java.io.PrintStream;
38import java.nio.charset.StandardCharsets;
39import java.nio.file.Path;
40import java.util.HashMap;
41import java.util.Locale;
42import java.util.function.Consumer;
43
44import org.testng.annotations.AfterMethod;
45import org.testng.annotations.BeforeMethod;
46import org.testng.annotations.Test;
47import jdk.jshell.tool.JavaShellToolBuilder;
48import static org.testng.Assert.assertEquals;
49import static org.testng.Assert.assertTrue;
50import static org.testng.Assert.fail;
51
52@Test
53public class StartOptionTest {
54
55    private ByteArrayOutputStream cmdout;
56    private ByteArrayOutputStream cmderr;
57    private ByteArrayOutputStream console;
58    private ByteArrayOutputStream userout;
59    private ByteArrayOutputStream usererr;
60
61    private JavaShellToolBuilder builder() {
62        return JavaShellToolBuilder
63                    .builder()
64                    .out(new PrintStream(cmdout), new PrintStream(console), new PrintStream(userout))
65                    .err(new PrintStream(cmderr), new PrintStream(usererr))
66                    .persistence(new HashMap<>())
67                    .env(new HashMap<>())
68                    .locale(Locale.ROOT);
69    }
70
71    private void runShell(String... args) {
72        try {
73            builder()
74                    .run(args);
75        } catch (Exception ex) {
76            fail("Repl tool died with exception", ex);
77        }
78    }
79
80    protected void check(ByteArrayOutputStream str, Consumer<String> checkOut, String label) {
81        byte[] bytes = str.toByteArray();
82        str.reset();
83        String out =  new String(bytes, StandardCharsets.UTF_8);
84        if (checkOut != null) {
85            checkOut.accept(out);
86        } else {
87            assertEquals("", out, label + ": Expected empty -- ");
88        }
89    }
90
91    protected void start(Consumer<String> checkCmdOutput,
92            Consumer<String> checkUserOutput, Consumer<String> checkError,
93            String... args) throws Exception {
94        runShell(args);
95        check(cmdout, checkCmdOutput, "cmdout");
96        check(cmderr, checkError, "cmderr");
97        check(console, null, "console");
98        check(userout, checkUserOutput, "userout");
99        check(usererr, null, "usererr");
100    }
101
102    protected void start(String expectedCmdOutput, String expectedError, String... args) throws Exception {
103        startWithUserOutput(expectedCmdOutput, "",  expectedError, args);
104    }
105
106    private void startWithUserOutput(String expectedCmdOutput, String expectedUserOutput,
107            String expectedError, String... args) throws Exception {
108        start(
109                s -> assertEquals(s.trim(), expectedCmdOutput, "cmdout: "),
110                s -> assertEquals(s.trim(), expectedUserOutput, "userout: "),
111                s -> assertEquals(s.trim(), expectedError, "cmderr: "),
112                args);
113    }
114
115    @BeforeMethod
116    public void setUp() {
117        cmdout  = new ByteArrayOutputStream();
118        cmderr  = new ByteArrayOutputStream();
119        console = new ByteArrayOutputStream();
120        userout = new ByteArrayOutputStream();
121        usererr = new ByteArrayOutputStream();
122    }
123
124    protected String writeToFile(String stuff) throws Exception {
125        Compiler compiler = new Compiler();
126        Path p = compiler.getPath("doit.repl");
127        compiler.writeToFile(p, stuff);
128        return p.toString();
129    }
130
131    public void testCommandFile() throws Exception {
132        String fn = writeToFile("String str = \"Hello \"\n/list\nSystem.out.println(str + str)\n/exit\n");
133        startWithUserOutput("1 : String str = \"Hello \";", "Hello Hello", "", "--no-startup", fn, "-s");
134    }
135
136    public void testUsage() throws Exception {
137        for (String opt : new String[]{"-h", "--help"}) {
138            start(s -> {
139                assertTrue(s.split("\n").length >= 7, "Not enough usage lines: " + s);
140                assertTrue(s.startsWith("Usage:   jshell <options>"), "Unexpect usage start: " + s);
141            }, null, null, opt);
142        }
143    }
144
145    public void testUnknown() throws Exception {
146        start(null, null,
147              s -> assertEquals(s.trim(), "Unknown option: u"), "-unknown");
148        start(null, null,
149              s -> assertEquals(s.trim(), "Unknown option: unknown"), "--unknown");
150    }
151
152    public void testStartup() throws Exception {
153        Compiler compiler = new Compiler();
154        Path p = compiler.getPath("file.txt");
155        compiler.writeToFile(p);
156        start("", "Argument to startup missing.", "--startup");
157        start("", "Conflicting options: both --startup and --no-startup were used.", "--no-startup", "--startup", p.toString());
158        start("", "Conflicting options: both --startup and --no-startup were used.", "--startup", p.toString(), "--no-startup");
159        start("", "Argument to startup missing.", "--no-startup", "--startup");
160    }
161
162    public void testStartupFailedOption() throws Exception {
163        try {
164            builder().run("-R-hoge-foo-bar");
165        } catch (IllegalStateException ex) {
166            String s = ex.getMessage();
167            assertTrue(s.startsWith("Launching JShell execution engine threw: Failed remote"), s);
168            return;
169        }
170        fail("Expected IllegalStateException");
171    }
172
173    public void testStartupUnknown() throws Exception {
174        start("", "File 'UNKNOWN' for '--startup' is not found.", "--startup", "UNKNOWN");
175        start("", "File 'UNKNOWN' for '--startup' is not found.", "--startup", "DEFAULT", "--startup", "UNKNOWN");
176    }
177
178    public void testClasspath() throws Exception {
179        for (String cp : new String[] {"--class-path"}) {
180            start("", "Only one --class-path option may be used.", cp, ".", "--class-path", ".");
181            start("", "Argument to class-path missing.", cp);
182        }
183    }
184
185    public void testFeedbackOptionConflict() throws Exception {
186        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.",
187                "--feedback", "concise", "--feedback", "verbose");
188        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-s");
189        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "verbose", "-q");
190        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-v");
191        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "--feedback", "concise");
192        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-v");
193        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-s", "-v");
194        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "-q");
195        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-s");
196    }
197
198    public void testNegFeedbackOption() throws Exception {
199        start("", "Argument to feedback missing.", "--feedback");
200        start("", "Does not match any current feedback mode: blorp -- --feedback blorp", "--feedback", "blorp");
201    }
202
203    public void testVersion() throws Exception {
204        start(s -> assertTrue(s.startsWith("jshell"), "unexpected version: " + s), null, null, "--version");
205    }
206
207    @AfterMethod
208    public void tearDown() {
209        cmdout  = null;
210        cmderr  = null;
211        console = null;
212        userout = null;
213        usererr = null;
214    }
215}
216