author | jlahoda |
Thu, 05 May 2016 12:55:21 +0200 | |
changeset 37939 | 3eb8c2a89b77 |
parent 37745 | 4b6b59f8e327 |
child 38608 | 691b607bbcd6 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
36526 | 2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
33362 | 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 |
/* |
|
37745
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
25 |
* @test 8151754 |
33362 | 26 |
* @summary Testing start-up options. |
35359 | 27 |
* @modules jdk.compiler/com.sun.tools.javac.api |
28 |
* jdk.compiler/com.sun.tools.javac.main |
|
36526 | 29 |
* jdk.jdeps/com.sun.tools.javap |
35359 | 30 |
* jdk.jshell/jdk.internal.jshell.tool |
33362 | 31 |
* @library /tools/lib |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36715
diff
changeset
|
32 |
* @build Compiler toolbox.ToolBox |
33362 | 33 |
* @run testng StartOptionTest |
34 |
*/ |
|
35 |
||
36 |
import java.io.ByteArrayOutputStream; |
|
37745
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
37 |
import java.io.OutputStream; |
33362 | 38 |
import java.io.PrintStream; |
39 |
import java.nio.charset.StandardCharsets; |
|
40 |
import java.nio.file.Path; |
|
36990 | 41 |
import java.util.Locale; |
33362 | 42 |
import java.util.function.Consumer; |
43 |
||
44 |
import jdk.internal.jshell.tool.JShellTool; |
|
45 |
import org.testng.annotations.AfterMethod; |
|
46 |
import org.testng.annotations.BeforeMethod; |
|
47 |
import org.testng.annotations.Test; |
|
48 |
||
49 |
import static org.testng.Assert.assertEquals; |
|
50 |
import static org.testng.Assert.assertTrue; |
|
37745
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
51 |
import static org.testng.Assert.fail; |
33362 | 52 |
|
53 |
@Test |
|
54 |
public class StartOptionTest { |
|
55 |
||
56 |
private ByteArrayOutputStream out; |
|
57 |
private ByteArrayOutputStream err; |
|
58 |
||
59 |
private JShellTool getShellTool() { |
|
37745
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
60 |
class NoOutputAllowedStream extends OutputStream { |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
61 |
private final String label; |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
62 |
NoOutputAllowedStream(String label) { |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
63 |
this.label = label; |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
64 |
} |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
65 |
@Override |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
66 |
public void write(int b) { fail("Unexpected output to: " + label); } |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
67 |
} |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
68 |
return new JShellTool( |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
69 |
new TestingInputStream(), |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
70 |
new PrintStream(out), |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
71 |
new PrintStream(err), |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
72 |
new PrintStream(new NoOutputAllowedStream("console")), |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
73 |
new TestingInputStream(), |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
74 |
new PrintStream(new NoOutputAllowedStream("userout")), |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
75 |
new PrintStream(new NoOutputAllowedStream("usererr")), |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
76 |
new ReplToolTesting.MemoryPreferences(), |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
77 |
Locale.ROOT); |
33362 | 78 |
} |
79 |
||
80 |
private String getOutput() { |
|
81 |
byte[] bytes = out.toByteArray(); |
|
82 |
out.reset(); |
|
83 |
return new String(bytes, StandardCharsets.UTF_8); |
|
84 |
} |
|
85 |
||
86 |
private String getError() { |
|
87 |
byte[] bytes = err.toByteArray(); |
|
88 |
err.reset(); |
|
89 |
return new String(bytes, StandardCharsets.UTF_8); |
|
90 |
} |
|
91 |
||
92 |
private void start(Consumer<String> checkOutput, Consumer<String> checkError, String... args) throws Exception { |
|
93 |
JShellTool tool = getShellTool(); |
|
94 |
tool.start(args); |
|
95 |
if (checkOutput != null) { |
|
96 |
checkOutput.accept(getOutput()); |
|
97 |
} else { |
|
98 |
assertEquals("", getOutput(), "Output: "); |
|
99 |
} |
|
100 |
if (checkError != null) { |
|
101 |
checkError.accept(getError()); |
|
102 |
} else { |
|
103 |
assertEquals("", getError(), "Error: "); |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
private void start(String expectedOutput, String expectedError, String... args) throws Exception { |
|
36993
e00f4be3a47d
8153482: jdk/jshell/StartOptionTest.java fails on Windows after JDK-8147515
rfield
parents:
36990
diff
changeset
|
108 |
start(s -> assertEquals(s.trim(), expectedOutput, "Output: "), s -> assertEquals(s.trim(), expectedError, "Error: "), args); |
33362 | 109 |
} |
110 |
||
111 |
@BeforeMethod |
|
112 |
public void setUp() { |
|
113 |
out = new ByteArrayOutputStream(); |
|
114 |
err = new ByteArrayOutputStream(); |
|
115 |
} |
|
116 |
||
117 |
@Test |
|
118 |
public void testUsage() throws Exception { |
|
119 |
start(s -> { |
|
120 |
assertTrue(s.split("\n").length >= 7, s); |
|
121 |
assertTrue(s.startsWith("Usage: jshell <options>"), s); |
|
122 |
}, null, "-help"); |
|
123 |
} |
|
124 |
||
125 |
@Test |
|
126 |
public void testUnknown() throws Exception { |
|
127 |
start(s -> { |
|
128 |
assertTrue(s.split("\n").length >= 7, s); |
|
129 |
assertTrue(s.startsWith("Usage: jshell <options>"), s); |
|
36993
e00f4be3a47d
8153482: jdk/jshell/StartOptionTest.java fails on Windows after JDK-8147515
rfield
parents:
36990
diff
changeset
|
130 |
}, s -> assertEquals(s.trim(), "Unknown option: -unknown"), "-unknown"); |
33362 | 131 |
} |
132 |
||
133 |
@Test(enabled = false) // TODO 8080883 |
|
134 |
public void testStartup() throws Exception { |
|
135 |
Compiler compiler = new Compiler(); |
|
136 |
Path p = compiler.getPath("file.txt"); |
|
137 |
compiler.writeToFile(p); |
|
36993
e00f4be3a47d
8153482: jdk/jshell/StartOptionTest.java fails on Windows after JDK-8147515
rfield
parents:
36990
diff
changeset
|
138 |
start("", "'-startup' requires a filename argument.", "-startup"); |
e00f4be3a47d
8153482: jdk/jshell/StartOptionTest.java fails on Windows after JDK-8147515
rfield
parents:
36990
diff
changeset
|
139 |
start("", "Conflicting -startup or -nostartup option.", "-startup", p.toString(), "-startup", p.toString()); |
e00f4be3a47d
8153482: jdk/jshell/StartOptionTest.java fails on Windows after JDK-8147515
rfield
parents:
36990
diff
changeset
|
140 |
start("", "Conflicting -startup or -nostartup option.", "-nostartup", "-startup", p.toString()); |
e00f4be3a47d
8153482: jdk/jshell/StartOptionTest.java fails on Windows after JDK-8147515
rfield
parents:
36990
diff
changeset
|
141 |
start("", "Conflicting -startup or -nostartup option.", "-startup", p.toString(), "-nostartup"); |
33362 | 142 |
} |
143 |
||
144 |
@Test |
|
145 |
public void testClasspath() throws Exception { |
|
146 |
for (String cp : new String[] {"-cp", "-classpath"}) { |
|
36993
e00f4be3a47d
8153482: jdk/jshell/StartOptionTest.java fails on Windows after JDK-8147515
rfield
parents:
36990
diff
changeset
|
147 |
start("", "Conflicting -classpath option.", cp, ".", "-classpath", "."); |
e00f4be3a47d
8153482: jdk/jshell/StartOptionTest.java fails on Windows after JDK-8147515
rfield
parents:
36990
diff
changeset
|
148 |
start("", "Argument to -classpath missing.", cp); |
33362 | 149 |
} |
150 |
} |
|
151 |
||
152 |
@Test |
|
37745
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
153 |
public void testNegFeedbackOption() throws Exception { |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
154 |
start("", "Argument to -feedback missing. Mode required.", "-feedback"); |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
155 |
start("", "Does not match any current feedback mode: blorp -- -feedback blorp", "-feedback", "blorp"); |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
156 |
} |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
157 |
|
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
36993
diff
changeset
|
158 |
@Test |
33362 | 159 |
public void testVersion() throws Exception { |
160 |
start(s -> assertTrue(s.startsWith("jshell")), null, "-version"); |
|
161 |
} |
|
162 |
||
163 |
@AfterMethod |
|
164 |
public void tearDown() { |
|
165 |
out = null; |
|
166 |
err = null; |
|
167 |
} |
|
168 |
} |