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