author | rfield |
Sat, 09 Apr 2016 11:03:48 -0700 | |
changeset 37007 | 6023a9a9d58a |
parent 36993 | e00f4be3a47d |
child 37745 | 4b6b59f8e327 |
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 |
/* |
|
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 |
|
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; |
|
37 |
import java.io.PrintStream; |
|
38 |
import java.nio.charset.StandardCharsets; |
|
39 |
import java.nio.file.Path; |
|
36990 | 40 |
import java.util.Locale; |
33362 | 41 |
import java.util.function.Consumer; |
42 |
||
43 |
import jdk.internal.jshell.tool.JShellTool; |
|
44 |
import org.testng.annotations.AfterMethod; |
|
45 |
import org.testng.annotations.BeforeMethod; |
|
46 |
import org.testng.annotations.Test; |
|
47 |
||
48 |
import static org.testng.Assert.assertEquals; |
|
49 |
import static org.testng.Assert.assertTrue; |
|
50 |
||
51 |
@Test |
|
52 |
public class StartOptionTest { |
|
53 |
||
54 |
private ByteArrayOutputStream out; |
|
55 |
private ByteArrayOutputStream err; |
|
56 |
||
57 |
private JShellTool getShellTool() { |
|
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36526
diff
changeset
|
58 |
return new JShellTool(null, new PrintStream(out), new PrintStream(err), null, null, null, |
36990 | 59 |
null, new ReplToolTesting.MemoryPreferences(), |
60 |
Locale.ROOT); |
|
33362 | 61 |
} |
62 |
||
63 |
private String getOutput() { |
|
64 |
byte[] bytes = out.toByteArray(); |
|
65 |
out.reset(); |
|
66 |
return new String(bytes, StandardCharsets.UTF_8); |
|
67 |
} |
|
68 |
||
69 |
private String getError() { |
|
70 |
byte[] bytes = err.toByteArray(); |
|
71 |
err.reset(); |
|
72 |
return new String(bytes, StandardCharsets.UTF_8); |
|
73 |
} |
|
74 |
||
75 |
private void start(Consumer<String> checkOutput, Consumer<String> checkError, String... args) throws Exception { |
|
76 |
JShellTool tool = getShellTool(); |
|
77 |
tool.start(args); |
|
78 |
if (checkOutput != null) { |
|
79 |
checkOutput.accept(getOutput()); |
|
80 |
} else { |
|
81 |
assertEquals("", getOutput(), "Output: "); |
|
82 |
} |
|
83 |
if (checkError != null) { |
|
84 |
checkError.accept(getError()); |
|
85 |
} else { |
|
86 |
assertEquals("", getError(), "Error: "); |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
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
|
91 |
start(s -> assertEquals(s.trim(), expectedOutput, "Output: "), s -> assertEquals(s.trim(), expectedError, "Error: "), args); |
33362 | 92 |
} |
93 |
||
94 |
@BeforeMethod |
|
95 |
public void setUp() { |
|
96 |
out = new ByteArrayOutputStream(); |
|
97 |
err = new ByteArrayOutputStream(); |
|
98 |
} |
|
99 |
||
100 |
@Test |
|
101 |
public void testUsage() throws Exception { |
|
102 |
start(s -> { |
|
103 |
assertTrue(s.split("\n").length >= 7, s); |
|
104 |
assertTrue(s.startsWith("Usage: jshell <options>"), s); |
|
105 |
}, null, "-help"); |
|
106 |
} |
|
107 |
||
108 |
@Test |
|
109 |
public void testUnknown() throws Exception { |
|
110 |
start(s -> { |
|
111 |
assertTrue(s.split("\n").length >= 7, s); |
|
112 |
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
|
113 |
}, s -> assertEquals(s.trim(), "Unknown option: -unknown"), "-unknown"); |
33362 | 114 |
} |
115 |
||
116 |
@Test(enabled = false) // TODO 8080883 |
|
117 |
public void testStartup() throws Exception { |
|
118 |
Compiler compiler = new Compiler(); |
|
119 |
Path p = compiler.getPath("file.txt"); |
|
120 |
compiler.writeToFile(p); |
|
36993
e00f4be3a47d
8153482: jdk/jshell/StartOptionTest.java fails on Windows after JDK-8147515
rfield
parents:
36990
diff
changeset
|
121 |
start("", "'-startup' requires a filename argument.", "-startup"); |
e00f4be3a47d
8153482: jdk/jshell/StartOptionTest.java fails on Windows after JDK-8147515
rfield
parents:
36990
diff
changeset
|
122 |
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
|
123 |
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
|
124 |
start("", "Conflicting -startup or -nostartup option.", "-startup", p.toString(), "-nostartup"); |
33362 | 125 |
} |
126 |
||
127 |
@Test |
|
128 |
public void testClasspath() throws Exception { |
|
129 |
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
|
130 |
start("", "Conflicting -classpath option.", cp, ".", "-classpath", "."); |
e00f4be3a47d
8153482: jdk/jshell/StartOptionTest.java fails on Windows after JDK-8147515
rfield
parents:
36990
diff
changeset
|
131 |
start("", "Argument to -classpath missing.", cp); |
33362 | 132 |
} |
133 |
} |
|
134 |
||
135 |
@Test |
|
136 |
public void testVersion() throws Exception { |
|
137 |
start(s -> assertTrue(s.startsWith("jshell")), null, "-version"); |
|
138 |
} |
|
139 |
||
140 |
@AfterMethod |
|
141 |
public void tearDown() { |
|
142 |
out = null; |
|
143 |
err = null; |
|
144 |
} |
|
145 |
} |