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 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 36 import java.io.ByteArrayOutputStream; 37 import java.io.PrintStream; 38 import java.nio.charset.StandardCharsets; 39 import java.nio.file.Path; 40 import java.util.Locale; 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 cmdout; 55 private ByteArrayOutputStream cmderr; 56 private ByteArrayOutputStream console; 57 private ByteArrayOutputStream userout; 58 private ByteArrayOutputStream usererr; 59 60 private JShellTool getShellTool() { 61 return new JShellTool( 62 new TestingInputStream(), 63 new PrintStream(cmdout), 64 new PrintStream(cmderr), 65 new PrintStream(console), 66 new TestingInputStream(), 67 new PrintStream(userout), 68 new PrintStream(usererr), 69 new ReplToolTesting.MemoryPreferences(), 70 Locale.ROOT); 71 } 72 73 private void check(ByteArrayOutputStream str, Consumer<String> checkOut, String label) { 74 byte[] bytes = str.toByteArray(); 75 str.reset(); 76 String out = new String(bytes, StandardCharsets.UTF_8); 77 if (checkOut != null) { 78 checkOut.accept(out); 79 } else { 80 assertEquals("", out, label + ": Expected empty -- "); 81 } 82 } 83 84 private void start(Consumer<String> checkOutput, Consumer<String> checkError, String... args) throws Exception { 85 JShellTool tool = getShellTool(); 86 tool.start(args); 87 check(cmdout, checkOutput, "cmdout"); 88 check(cmderr, checkError, "cmderr"); 89 check(console, null, "console"); 90 check(userout, null, "userout"); 91 check(usererr, null, "usererr"); 92 } 93 94 private void start(String expectedOutput, String expectedError, String... args) throws Exception { 95 start(s -> assertEquals(s.trim(), expectedOutput, "cmdout: "), s -> assertEquals(s.trim(), expectedError, "cmderr: "), args); 96 } 97 98 @BeforeMethod 99 public void setUp() { 100 cmdout = new ByteArrayOutputStream(); 101 cmderr = new ByteArrayOutputStream(); 102 console = new ByteArrayOutputStream(); 103 userout = new ByteArrayOutputStream(); 104 usererr = new ByteArrayOutputStream(); 105 } 106 107 @Test 108 public void testUsage() throws Exception { 109 for (String opt : new String[]{"-h", "--help"}) { 110 start(s -> { 111 assertTrue(s.split("\n").length >= 7, "Not enough usage lines: " + s); 112 assertTrue(s.startsWith("Usage: jshell <options>"), "Unexpect usage start: " + s); 113 }, null, opt); 114 } 115 } 116 117 @Test 118 public void testUnknown() throws Exception { 119 start(s -> { }, 120 s -> assertEquals(s.trim(), "Unknown option: u"), "-unknown"); 121 start(s -> { }, 122 s -> assertEquals(s.trim(), "Unknown option: unknown"), "--unknown"); 123 } 124 125 public void testStartup() throws Exception { 126 Compiler compiler = new Compiler(); 127 Path p = compiler.getPath("file.txt"); 128 compiler.writeToFile(p); 129 start("", "Argument to startup missing.", "--startup"); 130 start("", "Only one --startup or --no-startup option may be used.", "--startup", p.toString(), "--startup", p.toString()); 131 start("", "Only one --startup or --no-startup option may be used.", "--no-startup", "--startup", p.toString()); 132 start("", "Only one --startup or --no-startup option may be used.", "--startup", p.toString(), "--no-startup"); 133 start("", "Argument to startup missing.", "--no-startup", "--startup"); 134 } 135 136 public void testStartupUnknown() throws Exception { 137 start("", "File 'UNKNOWN' for '--startup' is not found.", "--startup", "UNKNOWN"); 138 } 139 140 @Test 141 public void testClasspath() throws Exception { 142 for (String cp : new String[] {"--class-path"}) { 143 start("", "Only one --class-path option may be used.", cp, ".", "--class-path", "."); 144 start("", "Argument to class-path missing.", cp); 145 } 146 } 147 148 @Test 149 public void testNegFeedbackOption() throws Exception { 150 start("", "Argument to feedback missing.", "--feedback"); 151 start("", "Does not match any current feedback mode: blorp -- --feedback blorp", "--feedback", "blorp"); 152 } 153 154 @Test 155 public void testVersion() throws Exception { 156 start(s -> assertTrue(s.startsWith("jshell"), "unexpected version: " + s), null, "--version"); 157 } 158 159 @AfterMethod 160 public void tearDown() { 161 cmdout = null; 162 cmderr = null; 163 console = null; 164 userout = null; 165 usererr = null; 166 } 167 }