StartOptionTest.java revision 3843:e5a42ddaf633
168651Skris/*
268651Skris * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
368651Skris * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
468651Skris *
568651Skris * This code is free software; you can redistribute it and/or modify it
668651Skris * under the terms of the GNU General Public License version 2 only, as
768651Skris * published by the Free Software Foundation.
868651Skris *
968651Skris * This code is distributed in the hope that it will be useful, but WITHOUT
1068651Skris * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11160814Ssimon * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1268651Skris * version 2 for more details (a copy is included in the LICENSE file that
1368651Skris * accompanied this code).
1468651Skris *
1568651Skris * You should have received a copy of the GNU General Public License version
1668651Skris * 2 along with this work; if not, write to the Free Software Foundation,
1768651Skris * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1868651Skris *
1968651Skris * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2079998Skris * or visit www.oracle.com if you need additional information or have any
2179998Skris * questions.
2289837Skris */
2379998Skris
2479998Skris/*
2579998Skris * @test 8151754 8080883 8160089 8170162 8166581
2668651Skris * @summary Testing start-up options.
2768651Skris * @modules jdk.compiler/com.sun.tools.javac.api
2868651Skris *          jdk.compiler/com.sun.tools.javac.main
2968651Skris *          jdk.jdeps/com.sun.tools.javap
3068651Skris *          jdk.jshell/jdk.internal.jshell.tool
3168651Skris * @library /tools/lib
3276866Skris * @build Compiler toolbox.ToolBox
3368651Skris * @run testng StartOptionTest
3468651Skris */
3568651Skris
3668651Skrisimport java.io.ByteArrayInputStream;
3768651Skrisimport java.io.ByteArrayOutputStream;
3868651Skrisimport java.io.PrintStream;
3968651Skrisimport java.nio.charset.StandardCharsets;
4068651Skrisimport java.nio.file.Path;
4168651Skrisimport java.util.HashMap;
4268651Skrisimport java.util.Locale;
4368651Skrisimport java.util.ServiceLoader;
4468651Skrisimport java.util.function.Consumer;
4568651Skris
4668651Skrisimport javax.tools.Tool;
4768651Skrisimport org.testng.annotations.AfterMethod;
4868651Skrisimport org.testng.annotations.BeforeMethod;
4968651Skrisimport org.testng.annotations.Test;
5068651Skrisimport jdk.jshell.tool.JavaShellToolBuilder;
5168651Skrisimport static org.testng.Assert.assertEquals;
5279998Skrisimport static org.testng.Assert.assertTrue;
5379998Skrisimport static org.testng.Assert.fail;
5468651Skris
5568651Skris@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
64    private JavaShellToolBuilder builder() {
65        return JavaShellToolBuilder
66                    .builder()
67                    .out(new PrintStream(cmdout), new PrintStream(console), new PrintStream(userout))
68                    .err(new PrintStream(cmderr), new PrintStream(usererr))
69                    .persistence(new HashMap<>())
70                    .env(new HashMap<>())
71                    .locale(Locale.ROOT);
72    }
73
74    private void runShell(String... args) {
75        try {
76            builder()
77                    .run(args);
78        } catch (Exception ex) {
79            fail("Repl tool died with exception", ex);
80        }
81    }
82
83    protected void check(ByteArrayOutputStream str, Consumer<String> checkOut, String label) {
84        byte[] bytes = str.toByteArray();
85        str.reset();
86        String out =  new String(bytes, StandardCharsets.UTF_8);
87        if (checkOut != null) {
88            checkOut.accept(out);
89        } else {
90            assertEquals("", out, label + ": Expected empty -- ");
91        }
92    }
93
94    protected void start(Consumer<String> checkCmdOutput,
95            Consumer<String> checkUserOutput, Consumer<String> checkError,
96            String... args) throws Exception {
97        runShell(args);
98        check(cmdout, checkCmdOutput, "cmdout");
99        check(cmderr, checkError, "cmderr");
100        check(console, null, "console");
101        check(userout, checkUserOutput, "userout");
102        check(usererr, null, "usererr");
103    }
104
105    protected void start(String expectedCmdOutput, String expectedError, String... args) throws Exception {
106        startWithUserOutput(expectedCmdOutput, "",  expectedError, args);
107    }
108
109    private void startWithUserOutput(String expectedCmdOutput, String expectedUserOutput,
110            String expectedError, String... args) throws Exception {
111        start(
112                s -> assertEquals(s.trim(), expectedCmdOutput, "cmdout: "),
113                s -> assertEquals(s.trim(), expectedUserOutput, "userout: "),
114                s -> assertEquals(s.trim(), expectedError, "cmderr: "),
115                args);
116    }
117
118    @BeforeMethod
119    public void setUp() {
120        cmdout  = new ByteArrayOutputStream();
121        cmderr  = new ByteArrayOutputStream();
122        console = new ByteArrayOutputStream();
123        userout = new ByteArrayOutputStream();
124        usererr = new ByteArrayOutputStream();
125    }
126
127    protected String writeToFile(String stuff) throws Exception {
128        Compiler compiler = new Compiler();
129        Path p = compiler.getPath("doit.repl");
130        compiler.writeToFile(p, stuff);
131        return p.toString();
132    }
133
134    public void testCommandFile() throws Exception {
135        String fn = writeToFile("String str = \"Hello \"\n/list\nSystem.out.println(str + str)\n/exit\n");
136        startWithUserOutput("1 : String str = \"Hello \";", "Hello Hello", "", "--no-startup", fn, "-s");
137    }
138
139    public void testUsage() throws Exception {
140        for (String opt : new String[]{"-h", "--help"}) {
141            start(s -> {
142                assertTrue(s.split("\n").length >= 7, "Not enough usage lines: " + s);
143                assertTrue(s.startsWith("Usage:   jshell <options>"), "Unexpect usage start: " + s);
144            }, null, null, opt);
145        }
146    }
147
148    public void testUnknown() throws Exception {
149        start(null, null,
150              s -> assertEquals(s.trim(), "Unknown option: u"), "-unknown");
151        start(null, null,
152              s -> assertEquals(s.trim(), "Unknown option: unknown"), "--unknown");
153    }
154
155    public void testStartup() throws Exception {
156        Compiler compiler = new Compiler();
157        Path p = compiler.getPath("file.txt");
158        compiler.writeToFile(p);
159        start("", "Argument to startup missing.", "--startup");
160        start("", "Only one --startup or --no-startup option may be used.", "--startup", p.toString(), "--startup", p.toString());
161        start("", "Only one --startup or --no-startup option may be used.", "--no-startup", "--startup", p.toString());
162        start("", "Only one --startup or --no-startup option may be used.", "--startup", p.toString(), "--no-startup");
163        start("", "Argument to startup missing.", "--no-startup", "--startup");
164    }
165
166    public void testStartupFailedOption() throws Exception {
167        try {
168            builder().run("-R-hoge-foo-bar");
169        } catch (IllegalStateException ex) {
170            String s = ex.getMessage();
171            assertTrue(s.startsWith("Launching JShell execution engine threw: Failed remote"), s);
172            return;
173        }
174        fail("Expected IllegalStateException");
175    }
176
177    public void testStartupUnknown() throws Exception {
178        start("", "File 'UNKNOWN' for '--startup' is not found.", "--startup", "UNKNOWN");
179    }
180
181    public void testClasspath() throws Exception {
182        for (String cp : new String[] {"--class-path"}) {
183            start("", "Only one --class-path option may be used.", cp, ".", "--class-path", ".");
184            start("", "Argument to class-path missing.", cp);
185        }
186    }
187
188    public void testFeedbackOptionConflict() throws Exception {
189        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.",
190                "--feedback", "concise", "--feedback", "verbose");
191        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-s");
192        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "verbose", "-q");
193        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-v");
194        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "--feedback", "concise");
195        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-v");
196        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-s", "-v");
197        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "-q");
198        start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-s");
199    }
200
201    public void testNegFeedbackOption() throws Exception {
202        start("", "Argument to feedback missing.", "--feedback");
203        start("", "Does not match any current feedback mode: blorp -- --feedback blorp", "--feedback", "blorp");
204    }
205
206    public void testVersion() throws Exception {
207        start(s -> assertTrue(s.startsWith("jshell"), "unexpected version: " + s), null, null, "--version");
208    }
209
210    @AfterMethod
211    public void tearDown() {
212        cmdout  = null;
213        cmderr  = null;
214        console = null;
215        userout = null;
216        usererr = null;
217    }
218}
219