StartOptionTest.java revision 3781:f196b8e52c3a
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 8151754 8080883 8160089 8166581 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.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.HashMap; 41import java.util.Locale; 42import java.util.function.Consumer; 43 44import jdk.internal.jshell.tool.JShellTool; 45import org.testng.annotations.AfterMethod; 46import org.testng.annotations.BeforeMethod; 47import org.testng.annotations.Test; 48 49import static org.testng.Assert.assertEquals; 50import static org.testng.Assert.assertTrue; 51import static org.testng.Assert.fail; 52 53@Test 54public class StartOptionTest { 55 56 private ByteArrayOutputStream cmdout; 57 private ByteArrayOutputStream cmderr; 58 private ByteArrayOutputStream console; 59 private ByteArrayOutputStream userout; 60 private ByteArrayOutputStream usererr; 61 62 private JShellTool getShellTool() { 63 return new JShellTool( 64 new TestingInputStream(), 65 new PrintStream(cmdout), 66 new PrintStream(cmderr), 67 new PrintStream(console), 68 null, 69 new PrintStream(userout), 70 new PrintStream(usererr), 71 new ReplToolTesting.MemoryPreferences(), 72 new HashMap<>(), 73 Locale.ROOT); 74 } 75 76 private void check(ByteArrayOutputStream str, Consumer<String> checkOut, String label) { 77 byte[] bytes = str.toByteArray(); 78 str.reset(); 79 String out = new String(bytes, StandardCharsets.UTF_8); 80 if (checkOut != null) { 81 checkOut.accept(out); 82 } else { 83 assertEquals("", out, label + ": Expected empty -- "); 84 } 85 } 86 87 private void start(Consumer<String> checkOutput, Consumer<String> checkError, String... args) throws Exception { 88 JShellTool tool = getShellTool(); 89 tool.start(args); 90 check(cmdout, checkOutput, "cmdout"); 91 check(cmderr, checkError, "cmderr"); 92 check(console, null, "console"); 93 check(userout, null, "userout"); 94 check(usererr, null, "usererr"); 95 } 96 97 private void start(String expectedOutput, String expectedError, String... args) throws Exception { 98 start(s -> assertEquals(s.trim(), expectedOutput, "cmdout: "), s -> assertEquals(s.trim(), expectedError, "cmderr: "), args); 99 } 100 101 @BeforeMethod 102 public void setUp() { 103 cmdout = new ByteArrayOutputStream(); 104 cmderr = new ByteArrayOutputStream(); 105 console = new ByteArrayOutputStream(); 106 userout = new ByteArrayOutputStream(); 107 usererr = new ByteArrayOutputStream(); 108 } 109 110 @Test 111 public void testUsage() throws Exception { 112 for (String opt : new String[]{"-h", "--help"}) { 113 start(s -> { 114 assertTrue(s.split("\n").length >= 7, "Not enough usage lines: " + s); 115 assertTrue(s.startsWith("Usage: jshell <options>"), "Unexpect usage start: " + s); 116 }, null, opt); 117 } 118 } 119 120 @Test 121 public void testUnknown() throws Exception { 122 start(s -> { }, 123 s -> assertEquals(s.trim(), "Unknown option: u"), "-unknown"); 124 start(s -> { }, 125 s -> assertEquals(s.trim(), "Unknown option: unknown"), "--unknown"); 126 } 127 128 public void testStartup() throws Exception { 129 Compiler compiler = new Compiler(); 130 Path p = compiler.getPath("file.txt"); 131 compiler.writeToFile(p); 132 start("", "Argument to startup missing.", "--startup"); 133 start("", "Only one --startup or --no-startup option may be used.", "--startup", p.toString(), "--startup", p.toString()); 134 start("", "Only one --startup or --no-startup option may be used.", "--no-startup", "--startup", p.toString()); 135 start("", "Only one --startup or --no-startup option may be used.", "--startup", p.toString(), "--no-startup"); 136 start("", "Argument to startup missing.", "--no-startup", "--startup"); 137 } 138 139 public void testStartupFailedOption() throws Exception { 140 try { 141 start("", "", "-R-hoge-foo-bar"); 142 } catch (IllegalStateException ex) { 143 String s = ex.getMessage(); 144 assertTrue(s.startsWith("Launching JShell execution engine threw: Failed remote"), s); 145 return; 146 } 147 fail("Expected IllegalStateException"); 148 } 149 150 public void testStartupUnknown() throws Exception { 151 start("", "File 'UNKNOWN' for '--startup' is not found.", "--startup", "UNKNOWN"); 152 } 153 154 @Test 155 public void testClasspath() throws Exception { 156 for (String cp : new String[] {"--class-path"}) { 157 start("", "Only one --class-path option may be used.", cp, ".", "--class-path", "."); 158 start("", "Argument to class-path missing.", cp); 159 } 160 } 161 162 @Test 163 public void testFeedbackOptionConflict() throws Exception { 164 start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", 165 "--feedback", "concise", "--feedback", "verbose"); 166 start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-s"); 167 start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "verbose", "-q"); 168 start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-v"); 169 start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "--feedback", "concise"); 170 start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-v"); 171 start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-s", "-v"); 172 start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "-q"); 173 start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-s"); 174 } 175 176 @Test 177 public void testNegFeedbackOption() throws Exception { 178 start("", "Argument to feedback missing.", "--feedback"); 179 start("", "Does not match any current feedback mode: blorp -- --feedback blorp", "--feedback", "blorp"); 180 } 181 182 @Test 183 public void testVersion() throws Exception { 184 start(s -> assertTrue(s.startsWith("jshell"), "unexpected version: " + s), null, "--version"); 185 } 186 187 @AfterMethod 188 public void tearDown() { 189 cmdout = null; 190 cmderr = null; 191 console = null; 192 userout = null; 193 usererr = null; 194 } 195} 196