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