StartOptionTest.java revision 3306:d77a6b663858
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
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
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.function.Consumer;
41
42import jdk.internal.jshell.tool.JShellTool;
43import org.testng.annotations.AfterMethod;
44import org.testng.annotations.BeforeMethod;
45import org.testng.annotations.Test;
46
47import static org.testng.Assert.assertEquals;
48import static org.testng.Assert.assertTrue;
49
50@Test
51public class StartOptionTest {
52
53    private ByteArrayOutputStream out;
54    private ByteArrayOutputStream err;
55
56    private JShellTool getShellTool() {
57        return new JShellTool(null, new PrintStream(out), new PrintStream(err), null, null, null,
58                              null, new ReplToolTesting.MemoryPreferences());
59    }
60
61    private String getOutput() {
62        byte[] bytes = out.toByteArray();
63        out.reset();
64        return new String(bytes, StandardCharsets.UTF_8);
65    }
66
67    private String getError() {
68        byte[] bytes = err.toByteArray();
69        err.reset();
70        return new String(bytes, StandardCharsets.UTF_8);
71    }
72
73    private void start(Consumer<String> checkOutput, Consumer<String> checkError, String... args) throws Exception {
74        JShellTool tool = getShellTool();
75        tool.start(args);
76        if (checkOutput != null) {
77            checkOutput.accept(getOutput());
78        } else {
79            assertEquals("", getOutput(), "Output: ");
80        }
81        if (checkError != null) {
82            checkError.accept(getError());
83        } else {
84            assertEquals("", getError(), "Error: ");
85        }
86    }
87
88    private void start(String expectedOutput, String expectedError, String... args) throws Exception {
89        start(s -> assertEquals(s, expectedOutput, "Output: "), s -> assertEquals(s, expectedError, "Error: "), args);
90    }
91
92    @BeforeMethod
93    public void setUp() {
94        out = new ByteArrayOutputStream();
95        err = new ByteArrayOutputStream();
96    }
97
98    @Test
99    public void testUsage() throws Exception {
100        start(s -> {
101            assertTrue(s.split("\n").length >= 7, s);
102            assertTrue(s.startsWith("Usage:   jshell <options>"), s);
103        },  null, "-help");
104    }
105
106    @Test
107    public void testUnknown() throws Exception {
108        start(s -> {
109            assertTrue(s.split("\n").length >= 7, s);
110            assertTrue(s.startsWith("Usage:   jshell <options>"), s);
111        }, s -> assertEquals(s, "Unknown option: -unknown\n"), "-unknown");
112    }
113
114    @Test(enabled = false) // TODO 8080883
115    public void testStartup() throws Exception {
116        Compiler compiler = new Compiler();
117        Path p = compiler.getPath("file.txt");
118        compiler.writeToFile(p);
119        start("", "Argument to -startup missing.\n", "-startup");
120        start("", "Conflicting -startup or -nostartup option.\n", "-startup", p.toString(), "-startup", p.toString());
121        start("", "Conflicting -startup or -nostartup option.\n", "-nostartup", "-startup", p.toString());
122        start("", "Conflicting -startup option.\n", "-startup", p.toString(), "-nostartup");
123    }
124
125    @Test
126    public void testClasspath() throws Exception {
127        for (String cp : new String[] {"-cp", "-classpath"}) {
128            start("", "Conflicting -classpath option.\n", cp, ".", "-classpath", ".");
129            start("", "Argument to -classpath missing.\n", cp);
130        }
131    }
132
133    @Test
134    public void testVersion() throws Exception {
135        start(s -> assertTrue(s.startsWith("jshell")), null, "-version");
136    }
137
138    @AfterMethod
139    public void tearDown() {
140        out = null;
141        err = null;
142    }
143}
144