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 PrintStream(userout),
  67                 new PrintStream(usererr),
  68                 new ReplToolTesting.MemoryPreferences(),
  69                 Locale.ROOT);
  70     }
  71 
  72     private void check(ByteArrayOutputStream str, Consumer<String> checkOut, String label) {
  73         byte[] bytes = str.toByteArray();
  74         str.reset();
  75         String out =  new String(bytes, StandardCharsets.UTF_8);
  76         if (checkOut != null) {
  77             checkOut.accept(out);
  78         } else {
  79             assertEquals("", out, label + ": Expected empty -- ");
  80         }
  81     }
  82 
  83     private void start(Consumer<String> checkOutput, Consumer<String> checkError, String... args) throws Exception {
  84         JShellTool tool = getShellTool();
  85         tool.start(args);
  86         check(cmdout, checkOutput, "cmdout");
  87         check(cmderr, checkError, "cmderr");
  88         check(console, null, "console");
  89         check(userout, null, "userout");
  90         check(usererr, null, "usererr");
  91     }
  92 
  93     private void start(String expectedOutput, String expectedError, String... args) throws Exception {
  94         start(s -> assertEquals(s.trim(), expectedOutput, "cmdout: "), s -> assertEquals(s.trim(), expectedError, "cmderr: "), args);
  95     }
  96 
  97     @BeforeMethod
  98     public void setUp() {
  99         cmdout  = new ByteArrayOutputStream();
 100         cmderr  = new ByteArrayOutputStream();
 101         console = new ByteArrayOutputStream();
 102         userout = new ByteArrayOutputStream();
 103         usererr = new ByteArrayOutputStream();
 104     }
 105 
 106     @Test
 107     public void testUsage() throws Exception {
 108         for (String opt : new String[]{"-h", "--help"}) {
 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, opt);
 113         }
 114     }
 115 
 116     @Test
 117     public void testUnknown() throws Exception {
 118         start(s -> { },
 119               s -> assertEquals(s.trim(), "Unknown option: u"), "-unknown");
 120         start(s -> { },
 121               s -> assertEquals(s.trim(), "Unknown option: unknown"), "--unknown");
 122     }
 123 
 124     public void testStartup() throws Exception {
 125         Compiler compiler = new Compiler();
 126         Path p = compiler.getPath("file.txt");
 127         compiler.writeToFile(p);
 128         start("", "Argument to startup missing.", "--startup");
 129         start("", "Only one --startup or --no-startup option may be used.", "--startup", p.toString(), "--startup", p.toString());
 130         start("", "Only one --startup or --no-startup option may be used.", "--no-startup", "--startup", p.toString());
 131         start("", "Only one --startup or --no-startup option may be used.", "--startup", p.toString(), "--no-startup");
 132         start("", "Argument to startup missing.", "--no-startup", "--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[] {"--class-path"}) {
 142             start("", "Only one --class-path option may be used.", cp, ".", "--class-path", ".");
 143             start("", "Argument to class-path missing.", cp);
 144         }
 145     }
 146 
 147     @Test
 148     public void testFeedbackOptionConflict() throws Exception {
 149         start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.",
 150                 "--feedback", "concise", "--feedback", "verbose");
 151         start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-s");
 152         start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "verbose", "-q");
 153         start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "--feedback", "concise", "-v");
 154         start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "--feedback", "concise");
 155         start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-v");
 156         start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-s", "-v");
 157         start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-v", "-q");
 158         start("", "Only one feedback option (--feedback, -q, -s, or -v) may be used.", "-q", "-s");
 159     }
 160 
 161     @Test
 162     public void testNegFeedbackOption() throws Exception {
 163         start("", "Argument to feedback missing.", "--feedback");
 164         start("", "Does not match any current feedback mode: blorp -- --feedback blorp", "--feedback", "blorp");
 165     }
 166 
 167     @Test
 168     public void testVersion() throws Exception {
 169         start(s -> assertTrue(s.startsWith("jshell"), "unexpected version: " + s), null, "--version");
 170     }
 171 
 172     @AfterMethod
 173     public void tearDown() {
 174         cmdout  = null;
 175         cmderr  = null;
 176         console = null;
 177         userout = null;
 178         usererr = null;
 179     }
 180 }