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