StartOptionTest.java revision 3436:10eaadcaba97
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
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.Locale;
41import java.util.function.Consumer;
42
43import jdk.internal.jshell.tool.JShellTool;
44import org.testng.annotations.AfterMethod;
45import org.testng.annotations.BeforeMethod;
46import org.testng.annotations.Test;
47
48import static org.testng.Assert.assertEquals;
49import static org.testng.Assert.assertTrue;
50
51@Test
52public class StartOptionTest {
53
54    private ByteArrayOutputStream cmdout;
55    private ByteArrayOutputStream cmderr;
56    private ByteArrayOutputStream console;
57    private ByteArrayOutputStream userout;
58    private ByteArrayOutputStream usererr;
59
60    private JShellTool getShellTool() {
61        return new JShellTool(
62                new TestingInputStream(),
63                new PrintStream(cmdout),
64                new PrintStream(cmderr),
65                new PrintStream(console),
66                new TestingInputStream(),
67                new PrintStream(userout),
68                new PrintStream(usererr),
69                new ReplToolTesting.MemoryPreferences(),
70                Locale.ROOT);
71    }
72
73    private void check(ByteArrayOutputStream str, Consumer<String> checkOut, String label) {
74        byte[] bytes = str.toByteArray();
75        str.reset();
76        String out =  new String(bytes, StandardCharsets.UTF_8);
77        if (checkOut != null) {
78            checkOut.accept(out);
79        } else {
80            assertEquals("", out, label + ": Expected empty -- ");
81        }
82    }
83
84    private void start(Consumer<String> checkOutput, Consumer<String> checkError, String... args) throws Exception {
85        JShellTool tool = getShellTool();
86        tool.start(args);
87        check(cmdout, checkOutput, "cmdout");
88        check(cmderr, checkError, "cmderr");
89        check(console, null, "console");
90        check(userout, null, "userout");
91        check(usererr, null, "usererr");
92    }
93
94    private void start(String expectedOutput, String expectedError, String... args) throws Exception {
95        start(s -> assertEquals(s.trim(), expectedOutput, "cmdout: "), s -> assertEquals(s.trim(), expectedError, "cmderr: "), args);
96    }
97
98    @BeforeMethod
99    public void setUp() {
100        cmdout  = new ByteArrayOutputStream();
101        cmderr  = new ByteArrayOutputStream();
102        console = new ByteArrayOutputStream();
103        userout = new ByteArrayOutputStream();
104        usererr = new ByteArrayOutputStream();
105    }
106
107    @Test
108    public void testUsage() throws Exception {
109        start(s -> {
110            assertTrue(s.split("\n").length >= 7, "Not enough usage lines: " + s);
111            assertTrue(s.startsWith("Usage:   jshell <options>"), "Unexpect usage start: " + s);
112        },  null, "-help");
113    }
114
115    @Test
116    public void testUnknown() throws Exception {
117        start(s -> {
118            assertTrue(s.split("\n").length >= 7, "Not enough usage lines (unknown): " + s);
119            assertTrue(s.startsWith("Usage:   jshell <options>"), "Unexpect usage start (unknown): " + s);
120        }, s -> assertEquals(s.trim(), "Unknown option: -unknown"), "-unknown");
121    }
122
123    @Test(enabled = false) // TODO 8080883
124    public void testStartup() throws Exception {
125        Compiler compiler = new Compiler();
126        Path p = compiler.getPath("file.txt");
127        compiler.writeToFile(p);
128        start("", "'-startup' requires a filename argument.", "-startup");
129        start("", "Conflicting -startup or -nostartup option.", "-startup", p.toString(), "-startup", p.toString());
130        start("", "Conflicting -startup or -nostartup option.", "-nostartup", "-startup", p.toString());
131        start("", "Conflicting -startup or -nostartup option.", "-startup", p.toString(), "-nostartup");
132    }
133
134    @Test
135    public void testClasspath() throws Exception {
136        for (String cp : new String[] {"-cp", "-classpath"}) {
137            start("", "Conflicting -classpath option.", cp, ".", "-classpath", ".");
138            start("", "Argument to -classpath missing.", cp);
139        }
140    }
141
142    @Test
143    public void testNegFeedbackOption() throws Exception {
144        start("", "Argument to -feedback missing. Mode required.", "-feedback");
145        start("", "Does not match any current feedback mode: blorp -- -feedback blorp", "-feedback", "blorp");
146    }
147
148    @Test
149    public void testVersion() throws Exception {
150        start(s -> assertTrue(s.startsWith("jshell"), "unexpected version: " + s), null, "-version");
151    }
152
153    @AfterMethod
154    public void tearDown() {
155        cmdout  = null;
156        cmderr  = null;
157        console = null;
158        userout = null;
159        usererr = null;
160    }
161}
162