test/jdk/jshell/StartOptionTest.java

Print this page




   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
  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;


  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         start(s -> {
 110             assertTrue(s.split("\n").length >= 7, "Not enough usage lines: " + s);
 111             assertTrue(s.startsWith("Usage:   jshell <options>"), "Unexpect usage start: " + s);
 112         },  null, "-help");

 113     }
 114 
 115     @Test
 116     public void testUnknown() throws Exception {
 117         start(s -> {
 118             assertTrue(s.split("\n").length >= 7, "Not enough usage lines (unknown): " + s);
 119             assertTrue(s.startsWith("Usage:   jshell <options>"), "Unexpect usage start (unknown): " + s);
 120         }, s -> assertEquals(s.trim(), "Unknown option: -unknown"), "-unknown");
 121     }
 122 
 123     public void testStartup() throws Exception {
 124         Compiler compiler = new Compiler();
 125         Path p = compiler.getPath("file.txt");
 126         compiler.writeToFile(p);
 127         start("", "'-startup' requires a filename argument.", "-startup");
 128         start("", "Only one -startup or -nostartup option may be used.", "-startup", p.toString(), "-startup", p.toString());
 129         start("", "Only one -startup or -nostartup option may be used.", "-nostartup", "-startup", p.toString());
 130         start("", "Only one -startup or -nostartup option may be used.", "-startup", p.toString(), "-nostartup");
 131         start("", "Only one -startup or -nostartup option may be used.", "-nostartup", "-nostartup");
 132         start("", "Only one -startup or -nostartup option may be used.", "-nostartup", "-startup");
 133     }
 134 
 135     public void testStartupUnknown() throws Exception {
 136         start("", "File 'UNKNOWN' for '-startup' is not found.", "-startup", "UNKNOWN");
 137     }
 138 
 139     @Test
 140     public void testClasspath() throws Exception {
 141         for (String cp : new String[] {"-cp", "-classpath"}) {
 142             start("", "Conflicting -classpath option.", cp, ".", "-classpath", ".");
 143             start("", "Argument to -classpath missing.", cp);
 144         }
 145     }
 146 
 147     @Test
 148     public void testNegFeedbackOption() throws Exception {
 149         start("", "Argument to -feedback missing. Mode required.", "-feedback");
 150         start("", "Does not match any current feedback mode: blorp -- -feedback blorp", "-feedback", "blorp");
 151     }
 152 
 153     @Test
 154     public void testVersion() throws Exception {
 155         start(s -> assertTrue(s.startsWith("jshell"), "unexpected version: " + s), null, "-version");
 156     }
 157 
 158     @AfterMethod
 159     public void tearDown() {
 160         cmdout  = null;
 161         cmderr  = null;
 162         console = null;
 163         userout = null;
 164         usererr = null;
 165     }
 166 }


   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;


  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 }