Path: blob/master/test/langtools/jdk/jshell/HistoryTest.java
40931 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 816674426* @summary Test Completion27* @modules jdk.internal.le/jdk.internal.org.jline.reader28* jdk.jshell/jdk.internal.jshell.tool:+open29* @build HistoryTest30* @run testng HistoryTest31*/3233import java.lang.reflect.Field;34import java.lang.reflect.Method;35import java.util.Locale;36import java.util.logging.Level;37import java.util.logging.Logger;3839import org.testng.annotations.Test;40import jdk.internal.jshell.tool.JShellTool;41import jdk.internal.jshell.tool.JShellToolBuilder;42import jdk.internal.org.jline.reader.History;43import static org.testng.Assert.*;44import org.testng.annotations.BeforeMethod;4546public class HistoryTest extends ReplToolTesting {4748private JShellTool repl;4950@Override51protected void testRawRun(Locale locale, String[] args) {52// turn on logging of launch failures53Logger.getLogger("jdk.jshell.execution").setLevel(Level.ALL);54repl = ((JShellToolBuilder) builder(locale))55.rawTool();56try {57repl.start(args);58} catch (Exception ex) {59fail("Repl tool died with exception", ex);60}61}6263@Test64public void testHistory() {65test(66a -> {if (!a) setCommandInput("void test() {\n");},67a -> {if (!a) setCommandInput(" System.err.println(1);\n");},68a -> {if (!a) setCommandInput(" System.err.println(1);\n");},69a -> {assertCommand(a, "} //test", "| created method test()");},70a -> {71if (!a) {72try {73previousAndAssert(getHistory(), "void test() {\n" +74" System.err.println(1);\n" +75" System.err.println(1);\n" +76"} //test");77} catch (Exception ex) {78throw new IllegalStateException(ex);79}80}81assertCommand(a, "int dummy;", "dummy ==> 0");82});83test(84a -> {if (!a) setCommandInput("void test2() {\n");},85a -> {assertCommand(a, "} //test2", "| created method test2()");},86a -> {87if (!a) {88try {89previousAndAssert(getHistory(), "void test2() {\n" +90"} //test2");91previousAndAssert(getHistory(), "/debug 0"); //added by test framework92previousAndAssert(getHistory(), "/exit");93previousAndAssert(getHistory(), "int dummy;");94previousAndAssert(getHistory(), "void test() {\n" +95" System.err.println(1);\n" +96" System.err.println(1);\n" +97"} //test");98} catch (Exception ex) {99throw new IllegalStateException(ex);100}101}102assertCommand(a, "int dummy;", "dummy ==> 0");103});104}105106@Test107public void test8166744() {108test(109a -> {if (!a) setCommandInput("class C {\n");},110a -> {if (!a) setCommandInput("void f() {\n");},111a -> {if (!a) setCommandInput("}\n");},112a -> {assertCommand(a, "}", "| created class C");},113a -> {114if (!a) {115try {116previousAndAssert(getHistory(), "class C {\n" +117"void f() {\n" +118"}\n" +119"}");120getHistory().add("class C {\n" +121"void f() {\n" +122"}\n" +123"}");124} catch (Exception ex) {125throw new IllegalStateException(ex);126}127}128assertCommand(a, "int dummy;", "dummy ==> 0");129});130test(131a -> {if (!a) setCommandInput("class C {\n");},132a -> {if (!a) setCommandInput("void f() {\n");},133a -> {if (!a) setCommandInput("}\n");},134a -> {assertCommand(a, "}", "| created class C");},135a -> {136if (!a) {137try {138previousAndAssert(getHistory(), "class C {\n" +139"void f() {\n" +140"}\n" +141"}");142getHistory().add("class C {\n" +143"void f() {\n" +144"}\n" +145"}");146} catch (Exception ex) {147throw new IllegalStateException(ex);148}149}150assertCommand(a, "int dummy;", "dummy ==> 0");151});152}153154@Test155public void testReadExistingHistory() {156prefsMap.put("HISTORY_LINE_0", "/debug 0");157prefsMap.put("HISTORY_LINE_1", "void test() {\\");158prefsMap.put("HISTORY_LINE_2", " System.err.println(1);\\");159prefsMap.put("HISTORY_LINE_3", " System.err.println(`\\\\\\\\\\");160prefsMap.put("HISTORY_LINE_4", " \\\\\\");161prefsMap.put("HISTORY_LINE_5", "`);\\");162prefsMap.put("HISTORY_LINE_6", "} //test");163test(164a -> {assertCommand(a, "int i", "i ==> 0");},165a -> {166if (!a) {167try {168previousAndAssert(getHistory(), "int i");169previousAndAssert(getHistory(), "/debug 0"); //added by test framework170previousAndAssert(getHistory(), "void test() {\n" +171" System.err.println(1);\n" +172" System.err.println(`\\\\\n" +173" \\\n" +174"`);\n" +175"} //test");176} catch (Exception ex) {177throw new IllegalStateException(ex);178}179}180assertCommand(a, "/exit", "");181});182assertEquals(prefsMap.get("HISTORY_LINE_00"), "/debug 0");183assertEquals(prefsMap.get("HISTORY_LINE_01"), "void test() {\\");184assertEquals(prefsMap.get("HISTORY_LINE_02"), " System.err.println(1);\\");185assertEquals(prefsMap.get("HISTORY_LINE_03"), " System.err.println(`\\\\\\\\\\");186assertEquals(prefsMap.get("HISTORY_LINE_04"), " \\\\\\");187assertEquals(prefsMap.get("HISTORY_LINE_05"), "`);\\");188assertEquals(prefsMap.get("HISTORY_LINE_06"), "} //test");189assertEquals(prefsMap.get("HISTORY_LINE_07"), "/debug 0");190assertEquals(prefsMap.get("HISTORY_LINE_08"), "int i");191assertEquals(prefsMap.get("HISTORY_LINE_09"), "/exit");192System.err.println("prefsMap: " + prefsMap);193}194195private History getHistory() throws Exception {196Field input = repl.getClass().getDeclaredField("input");197input.setAccessible(true);198Object console = input.get(repl);199Method getHistory = console.getClass().getDeclaredMethod("getHistory");200getHistory.setAccessible(true);201return (History) getHistory.invoke(console);202}203204private void previousAndAssert(History history, String expected) {205assertTrue(history.previous());206assertEquals(history.current().toString(), expected);207}208209@BeforeMethod210public void setUp() {211super.setUp();212System.setProperty("jshell.test.allow.incomplete.inputs", "false");213}214215}216217218