test/jdk/jshell/ToolSimpleTest.java

Print this page




   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  * @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024
  27  * @summary Simple jshell tool tests
  28  * @modules jdk.compiler/com.sun.tools.javac.api
  29  *          jdk.compiler/com.sun.tools.javac.main
  30  *          jdk.jdeps/com.sun.tools.javap
  31  *          jdk.jshell/jdk.internal.jshell.tool
  32  * @build KullaTesting TestingInputStream
  33  * @run testng ToolSimpleTest
  34  */
  35 import java.util.Arrays;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 import java.util.function.Consumer;
  39 import java.util.stream.Collectors;
  40 import java.util.stream.Stream;
  41 
  42 import org.testng.annotations.Test;
  43 
  44 import static org.testng.Assert.assertEquals;
  45 import static org.testng.Assert.assertTrue;
  46 


 192         test(
 193                 (a) -> assertCommand(a, "/save",
 194                         "|  '/save' requires a filename argument."),
 195                 (a) -> assertCommand(a, "/open",
 196                         "|  '/open' requires a filename argument."),
 197                 (a) -> assertCommand(a, "/set start",
 198                         "|  Specify either one option or a startup file name -- /set start")
 199         );
 200     }
 201 
 202     public void testDebug() {
 203         test(
 204                 (a) -> assertCommand(a, "/deb", "|  Debugging on"),
 205                 (a) -> assertCommand(a, "/debug", "|  Debugging off"),
 206                 (a) -> assertCommand(a, "/debug", "|  Debugging on"),
 207                 (a) -> assertCommand(a, "/deb", "|  Debugging off")
 208         );
 209     }
 210 
 211     public void testDrop() {
 212         test(false, new String[]{"-nostartup"},
 213                 a -> assertVariable(a, "int", "a"),
 214                 a -> dropVariable(a, "/drop 1", "int a = 0", "|  dropped variable a"),
 215                 a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
 216                 a -> dropMethod(a, "/drop 2", "b ()I", "|  dropped method b()"),
 217                 a -> assertClass(a, "class A {}", "class", "A"),
 218                 a -> dropClass(a, "/drop 3", "class A", "|  dropped class A"),
 219                 a -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
 220                 a -> dropImport(a, "/drop 4", "import java.util.stream.*", ""),
 221                 a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
 222                 a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
 223                 a -> assertCommandCheckOutput(a, "/types", assertClasses()),
 224                 a -> assertCommandCheckOutput(a, "/imports", assertImports())
 225         );
 226         test(false, new String[]{"-nostartup"},
 227                 a -> assertVariable(a, "int", "a"),
 228                 a -> dropVariable(a, "/drop a", "int a = 0", "|  dropped variable a"),
 229                 a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
 230                 a -> dropMethod(a, "/drop b", "b ()I", "|  dropped method b()"),
 231                 a -> assertClass(a, "class A {}", "class", "A"),
 232                 a -> dropClass(a, "/drop A", "class A", "|  dropped class A"),
 233                 a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
 234                 a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
 235                 a -> assertCommandCheckOutput(a, "/types", assertClasses()),
 236                 a -> assertCommandCheckOutput(a, "/imports", assertImports())
 237         );
 238     }
 239 
 240     public void testDropNegative() {
 241         test(false, new String[]{"-nostartup"},
 242                 a -> assertCommandOutputStartsWith(a, "/drop 0", "|  No such snippet: 0"),
 243                 a -> assertCommandOutputStartsWith(a, "/drop a", "|  No such snippet: a"),
 244                 a -> assertCommandCheckOutput(a, "/drop",
 245                         assertStartsWith("|  In the /drop argument, please specify an import, variable, method, or class to drop.")),
 246                 a -> assertVariable(a, "int", "a"),
 247                 a -> assertCommand(a, "a", "a ==> 0"),
 248                 a -> assertCommand(a, "/drop 2",
 249                         "|  This command does not accept the snippet '2' : a\n" +
 250                         "|  See /types, /methods, /vars, or /list")
 251         );
 252     }
 253 
 254     public void testAmbiguousDrop() {
 255         Consumer<String> check = s -> {
 256             assertTrue(s.startsWith("|  The argument references more than one import, variable, method, or class"), s);
 257             int lines = s.split("\n").length;
 258             assertEquals(lines, 5, "Expected 3 ambiguous keys, but found: " + (lines - 2) + "\n" + s);
 259         };
 260         test(
 261                 a -> assertVariable(a, "int", "a"),


 445                 (a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
 446                 (a) -> assertClass(a, "enum A { }", "enum", "A"),
 447                 (a) -> assertCommandCheckOutput(a, "/list", assertList()),
 448                 (a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
 449                 (a) -> assertClass(a, "@interface A { }", "@interface", "A"),
 450                 (a) -> assertCommandCheckOutput(a, "/list", assertList()),
 451                 (a) -> assertCommandCheckOutput(a, "/types", assertClasses())
 452         );
 453     }
 454     public void testCommandPrefix() {
 455         test(a -> assertCommandCheckOutput(a, "/s",
 456                       assertStartsWith("|  Command: '/s' is ambiguous: /save, /set")),
 457              a -> assertCommand(a, "int var", "var ==> 0"),
 458              a -> assertCommandCheckOutput(a, "/va",
 459                       assertStartsWith("|    int var = 0")),
 460              a -> assertCommandCheckOutput(a, "/save",
 461                       assertStartsWith("|  '/save' requires a filename argument.")));
 462     }
 463 
 464     public void testOptionQ() {
 465         test(new String[]{"-q", "-nostartup"},
 466                 (a) -> assertCommand(a, "1+1", "$1 ==> 2"),
 467                 (a) -> assertCommand(a, "int x = 5", "")
 468         );
 469     }
 470 
 471     public void testOptionQq() {
 472         test(new String[]{"-qq", "-nostartup"},
 473                 (a) -> assertCommand(a, "1+1", "")
 474         );
 475     }
 476 
 477     public void testOptionV() {
 478         test(new String[]{"-v", "-nostartup"},
 479                 (a) -> assertCommand(a, "1+1",
 480                         "$1 ==> 2\n" +
 481                         "|  created scratch variable $1 : int")
 482         );
 483     }
 484 
 485     public void testOptionFeedback() {
 486         test(new String[]{"-feedback", "concise", "-nostartup"},
 487                 (a) -> assertCommand(a, "1+1", "$1 ==> 2"),
 488                 (a) -> assertCommand(a, "int x = 5", "")
 489         );
 490     }
 491 
 492     public void testOptionR() {
 493         test(new String[]{"-R-Dthe.sound=blorp", "-nostartup"},
 494                 (a) -> assertCommand(a, "System.getProperty(\"the.sound\")",
 495                         "$1 ==> \"blorp\"")
 496         );
 497     }
 498 
 499     public void test8156910() {
 500         test(
 501                 (a) -> assertCommandOutputContains(a, "System.out.println(\"%5d\", 10);", "%5d"),
 502                 (a) -> assertCommandOutputContains(a, "1234", "==> 1234")
 503         );
 504     }
 505 }


   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  * @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024 8160089
  27  * @summary Simple jshell tool tests
  28  * @modules jdk.compiler/com.sun.tools.javac.api
  29  *          jdk.compiler/com.sun.tools.javac.main
  30  *          jdk.jdeps/com.sun.tools.javap
  31  *          jdk.jshell/jdk.internal.jshell.tool
  32  * @build KullaTesting TestingInputStream
  33  * @run testng ToolSimpleTest
  34  */
  35 import java.util.Arrays;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 import java.util.function.Consumer;
  39 import java.util.stream.Collectors;
  40 import java.util.stream.Stream;
  41 
  42 import org.testng.annotations.Test;
  43 
  44 import static org.testng.Assert.assertEquals;
  45 import static org.testng.Assert.assertTrue;
  46 


 192         test(
 193                 (a) -> assertCommand(a, "/save",
 194                         "|  '/save' requires a filename argument."),
 195                 (a) -> assertCommand(a, "/open",
 196                         "|  '/open' requires a filename argument."),
 197                 (a) -> assertCommand(a, "/set start",
 198                         "|  Specify either one option or a startup file name -- /set start")
 199         );
 200     }
 201 
 202     public void testDebug() {
 203         test(
 204                 (a) -> assertCommand(a, "/deb", "|  Debugging on"),
 205                 (a) -> assertCommand(a, "/debug", "|  Debugging off"),
 206                 (a) -> assertCommand(a, "/debug", "|  Debugging on"),
 207                 (a) -> assertCommand(a, "/deb", "|  Debugging off")
 208         );
 209     }
 210 
 211     public void testDrop() {
 212         test(false, new String[]{"--no-startup"},
 213                 a -> assertVariable(a, "int", "a"),
 214                 a -> dropVariable(a, "/drop 1", "int a = 0", "|  dropped variable a"),
 215                 a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
 216                 a -> dropMethod(a, "/drop 2", "b ()I", "|  dropped method b()"),
 217                 a -> assertClass(a, "class A {}", "class", "A"),
 218                 a -> dropClass(a, "/drop 3", "class A", "|  dropped class A"),
 219                 a -> assertImport(a, "import java.util.stream.*;", "", "java.util.stream.*"),
 220                 a -> dropImport(a, "/drop 4", "import java.util.stream.*", ""),
 221                 a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
 222                 a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
 223                 a -> assertCommandCheckOutput(a, "/types", assertClasses()),
 224                 a -> assertCommandCheckOutput(a, "/imports", assertImports())
 225         );
 226         test(false, new String[]{"--no-startup"},
 227                 a -> assertVariable(a, "int", "a"),
 228                 a -> dropVariable(a, "/drop a", "int a = 0", "|  dropped variable a"),
 229                 a -> assertMethod(a, "int b() { return 0; }", "()I", "b"),
 230                 a -> dropMethod(a, "/drop b", "b ()I", "|  dropped method b()"),
 231                 a -> assertClass(a, "class A {}", "class", "A"),
 232                 a -> dropClass(a, "/drop A", "class A", "|  dropped class A"),
 233                 a -> assertCommandCheckOutput(a, "/vars", assertVariables()),
 234                 a -> assertCommandCheckOutput(a, "/methods", assertMethods()),
 235                 a -> assertCommandCheckOutput(a, "/types", assertClasses()),
 236                 a -> assertCommandCheckOutput(a, "/imports", assertImports())
 237         );
 238     }
 239 
 240     public void testDropNegative() {
 241         test(false, new String[]{"--no-startup"},
 242                 a -> assertCommandOutputStartsWith(a, "/drop 0", "|  No such snippet: 0"),
 243                 a -> assertCommandOutputStartsWith(a, "/drop a", "|  No such snippet: a"),
 244                 a -> assertCommandCheckOutput(a, "/drop",
 245                         assertStartsWith("|  In the /drop argument, please specify an import, variable, method, or class to drop.")),
 246                 a -> assertVariable(a, "int", "a"),
 247                 a -> assertCommand(a, "a", "a ==> 0"),
 248                 a -> assertCommand(a, "/drop 2",
 249                         "|  This command does not accept the snippet '2' : a\n" +
 250                         "|  See /types, /methods, /vars, or /list")
 251         );
 252     }
 253 
 254     public void testAmbiguousDrop() {
 255         Consumer<String> check = s -> {
 256             assertTrue(s.startsWith("|  The argument references more than one import, variable, method, or class"), s);
 257             int lines = s.split("\n").length;
 258             assertEquals(lines, 5, "Expected 3 ambiguous keys, but found: " + (lines - 2) + "\n" + s);
 259         };
 260         test(
 261                 a -> assertVariable(a, "int", "a"),


 445                 (a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
 446                 (a) -> assertClass(a, "enum A { }", "enum", "A"),
 447                 (a) -> assertCommandCheckOutput(a, "/list", assertList()),
 448                 (a) -> assertCommandCheckOutput(a, "/types", assertClasses()),
 449                 (a) -> assertClass(a, "@interface A { }", "@interface", "A"),
 450                 (a) -> assertCommandCheckOutput(a, "/list", assertList()),
 451                 (a) -> assertCommandCheckOutput(a, "/types", assertClasses())
 452         );
 453     }
 454     public void testCommandPrefix() {
 455         test(a -> assertCommandCheckOutput(a, "/s",
 456                       assertStartsWith("|  Command: '/s' is ambiguous: /save, /set")),
 457              a -> assertCommand(a, "int var", "var ==> 0"),
 458              a -> assertCommandCheckOutput(a, "/va",
 459                       assertStartsWith("|    int var = 0")),
 460              a -> assertCommandCheckOutput(a, "/save",
 461                       assertStartsWith("|  '/save' requires a filename argument.")));
 462     }
 463 
 464     public void testOptionQ() {
 465         test(new String[]{"-q", "--no-startup"},
 466                 (a) -> assertCommand(a, "1+1", "$1 ==> 2"),
 467                 (a) -> assertCommand(a, "int x = 5", "")
 468         );
 469     }
 470 
 471     public void testOptionQq() {
 472         test(new String[]{"-s", "--no-startup"},
 473                 (a) -> assertCommand(a, "1+1", "")
 474         );
 475     }
 476 
 477     public void testOptionV() {
 478         test(new String[]{"-v", "--no-startup"},
 479                 (a) -> assertCommand(a, "1+1",
 480                         "$1 ==> 2\n" +
 481                         "|  created scratch variable $1 : int")
 482         );
 483     }
 484 
 485     public void testOptionFeedback() {
 486         test(new String[]{"--feedback", "concise", "--no-startup"},
 487                 (a) -> assertCommand(a, "1+1", "$1 ==> 2"),
 488                 (a) -> assertCommand(a, "int x = 5", "")
 489         );
 490     }
 491 
 492     public void testOptionR() {
 493         test(new String[]{"-R-Dthe.sound=blorp", "--no-startup"},
 494                 (a) -> assertCommand(a, "System.getProperty(\"the.sound\")",
 495                         "$1 ==> \"blorp\"")
 496         );
 497     }
 498 
 499     public void test8156910() {
 500         test(
 501                 (a) -> assertCommandOutputContains(a, "System.out.println(\"%5d\", 10);", "%5d"),
 502                 (a) -> assertCommandOutputContains(a, "1234", "==> 1234")
 503         );
 504     }
 505 }