test/langtools/jdk/jshell/HistoryTest.java
author dtitov
Tue, 08 Oct 2019 11:18:00 -0700
changeset 58506 dfe3d11378cc
parent 52938 5ff7480c9e28
permissions -rw-r--r--
8199136: Dead code in src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java Reviewed-by: dholmes, dtitov Contributed-by: mandrikov@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     4
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     8
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    14
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    18
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    21
 * questions.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    22
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    23
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    24
/*
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    25
 * @test
41249
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
    26
 * @bug 8166744
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    27
 * @summary Test Completion
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    28
 * @modules jdk.internal.le/jdk.internal.org.jline.reader
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41249
diff changeset
    29
 *          jdk.jshell/jdk.internal.jshell.tool:+open
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    30
 * @build HistoryTest
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    31
 * @run testng HistoryTest
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    32
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    33
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    34
import java.lang.reflect.Field;
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    35
import java.lang.reflect.Method;
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    36
import java.util.Locale;
44683
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 42843
diff changeset
    37
import java.util.logging.Level;
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 42843
diff changeset
    38
import java.util.logging.Logger;
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    39
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import org.testng.annotations.Test;
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    41
import jdk.internal.jshell.tool.JShellTool;
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    42
import jdk.internal.jshell.tool.JShellToolBuilder;
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    43
import jdk.internal.org.jline.reader.History;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
import static org.testng.Assert.*;
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    45
import org.testng.annotations.BeforeMethod;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    47
public class HistoryTest extends ReplToolTesting {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    49
    private JShellTool repl;
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    50
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    51
    @Override
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    52
    protected void testRawRun(Locale locale, String[] args) {
44683
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 42843
diff changeset
    53
        // turn on logging of launch failures
610dc2b48954 8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents: 42843
diff changeset
    54
        Logger.getLogger("jdk.jshell.execution").setLevel(Level.ALL);
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    55
        repl = ((JShellToolBuilder) builder(locale))
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    56
                .rawTool();
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    57
        try {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    58
            repl.start(args);
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    59
        } catch (Exception ex) {
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    60
            fail("Repl tool died with exception", ex);
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    61
        }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    62
    }
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    63
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
    64
    @Test
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
    public void testHistory() {
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    66
        test(
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    67
             a -> {if (!a) setCommandInput("void test() {\n");},
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    68
             a -> {if (!a) setCommandInput("    System.err.println(1);\n");},
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    69
             a -> {if (!a) setCommandInput("    System.err.println(1);\n");},
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    70
             a -> {assertCommand(a, "} //test", "|  created method test()");},
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    71
             a -> {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    72
                 if (!a) {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    73
                     try {
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    74
                         previousAndAssert(getHistory(), "void test() {\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    75
                                                         "    System.err.println(1);\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    76
                                                         "    System.err.println(1);\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    77
                                                         "} //test");
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    78
                     } catch (Exception ex) {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    79
                         throw new IllegalStateException(ex);
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    80
                     }
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    81
                 }
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    82
                 assertCommand(a, "int dummy;", "dummy ==> 0");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    83
             });
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    84
        test(
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    85
             a -> {if (!a) setCommandInput("void test2() {\n");},
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    86
             a -> {assertCommand(a, "} //test2", "|  created method test2()");},
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    87
             a -> {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    88
                 if (!a) {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    89
                     try {
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    90
                         previousAndAssert(getHistory(), "void test2() {\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    91
                                                         "} //test2");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    92
                         previousAndAssert(getHistory(), "/debug 0"); //added by test framework
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    93
                         previousAndAssert(getHistory(), "/exit");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    94
                         previousAndAssert(getHistory(), "int dummy;");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    95
                         previousAndAssert(getHistory(), "void test() {\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    96
                                                         "    System.err.println(1);\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    97
                                                         "    System.err.println(1);\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
    98
                                                         "} //test");
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    99
                     } catch (Exception ex) {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
   100
                         throw new IllegalStateException(ex);
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
   101
                     }
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
   102
                 }
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
   103
                 assertCommand(a, "int dummy;", "dummy ==> 0");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
   104
             });
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   105
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   106
42843
a8d83044a192 8170162: jshell tool: no mechanism to programmatically launch
rfield
parents: 42407
diff changeset
   107
    @Test
41249
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   108
    public void test8166744() {
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   109
        test(
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   110
             a -> {if (!a) setCommandInput("class C {\n");},
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   111
             a -> {if (!a) setCommandInput("void f() {\n");},
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   112
             a -> {if (!a) setCommandInput("}\n");},
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   113
             a -> {assertCommand(a, "}", "|  created class C");},
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   114
             a -> {
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   115
                 if (!a) {
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   116
                     try {
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   117
                         previousAndAssert(getHistory(), "class C {\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   118
                                                         "void f() {\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   119
                                                         "}\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   120
                                                         "}");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   121
                         getHistory().add("class C {\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   122
                                          "void f() {\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   123
                                          "}\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   124
                                          "}");
41249
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   125
                     } catch (Exception ex) {
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   126
                         throw new IllegalStateException(ex);
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   127
                     }
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   128
                 }
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   129
                 assertCommand(a, "int dummy;", "dummy ==> 0");
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   130
             });
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   131
        test(
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   132
             a -> {if (!a) setCommandInput("class C {\n");},
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   133
             a -> {if (!a) setCommandInput("void f() {\n");},
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   134
             a -> {if (!a) setCommandInput("}\n");},
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   135
             a -> {assertCommand(a, "}", "|  created class C");},
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   136
             a -> {
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   137
                 if (!a) {
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   138
                     try {
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   139
                         previousAndAssert(getHistory(), "class C {\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   140
                                                         "void f() {\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   141
                                                         "}\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   142
                                                         "}");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   143
                         getHistory().add("class C {\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   144
                                          "void f() {\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   145
                                          "}\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   146
                                          "}");
41249
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   147
                     } catch (Exception ex) {
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   148
                         throw new IllegalStateException(ex);
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   149
                     }
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   150
                 }
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   151
                 assertCommand(a, "int dummy;", "dummy ==> 0");
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   152
             });
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   153
    }
8c2aab77c2d2 8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents: 38521
diff changeset
   154
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   155
    @Test
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   156
    public void testReadExistingHistory() {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   157
        prefsMap.put("HISTORY_LINE_0", "/debug 0");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   158
        prefsMap.put("HISTORY_LINE_1", "void test() {\\");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   159
        prefsMap.put("HISTORY_LINE_2", "    System.err.println(1);\\");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   160
        prefsMap.put("HISTORY_LINE_3", "    System.err.println(`\\\\\\\\\\");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   161
        prefsMap.put("HISTORY_LINE_4", "    \\\\\\");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   162
        prefsMap.put("HISTORY_LINE_5", "`);\\");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   163
        prefsMap.put("HISTORY_LINE_6", "} //test");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   164
        test(
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   165
             a -> {assertCommand(a, "int i", "i ==> 0");},
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   166
             a -> {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   167
                 if (!a) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   168
                     try {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   169
                         previousAndAssert(getHistory(), "int i");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   170
                         previousAndAssert(getHistory(), "/debug 0"); //added by test framework
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   171
                         previousAndAssert(getHistory(), "void test() {\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   172
                                                         "    System.err.println(1);\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   173
                                                         "    System.err.println(`\\\\\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   174
                                                         "    \\\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   175
                                                         "`);\n" +
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   176
                                                         "} //test");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   177
                     } catch (Exception ex) {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   178
                         throw new IllegalStateException(ex);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   179
                     }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   180
                 }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   181
                  assertCommand(a, "/exit", "");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   182
             });
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   183
        assertEquals(prefsMap.get("HISTORY_LINE_00"), "/debug 0");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   184
        assertEquals(prefsMap.get("HISTORY_LINE_01"), "void test() {\\");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   185
        assertEquals(prefsMap.get("HISTORY_LINE_02"), "    System.err.println(1);\\");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   186
        assertEquals(prefsMap.get("HISTORY_LINE_03"), "    System.err.println(`\\\\\\\\\\");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   187
        assertEquals(prefsMap.get("HISTORY_LINE_04"), "    \\\\\\");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   188
        assertEquals(prefsMap.get("HISTORY_LINE_05"), "`);\\");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   189
        assertEquals(prefsMap.get("HISTORY_LINE_06"), "} //test");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   190
        assertEquals(prefsMap.get("HISTORY_LINE_07"), "/debug 0");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   191
        assertEquals(prefsMap.get("HISTORY_LINE_08"), "int i");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   192
        assertEquals(prefsMap.get("HISTORY_LINE_09"), "/exit");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   193
        System.err.println("prefsMap: " + prefsMap);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   194
    }
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   195
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   196
    private History getHistory() throws Exception {
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
   197
        Field input = repl.getClass().getDeclaredField("input");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
   198
        input.setAccessible(true);
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
   199
        Object console = input.get(repl);
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   200
        Method getHistory = console.getClass().getDeclaredMethod("getHistory");
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   201
        getHistory.setAccessible(true);
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   202
        return (History) getHistory.invoke(console);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   203
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   204
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   205
    private void previousAndAssert(History history, String expected) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   206
        assertTrue(history.previous());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   207
        assertEquals(history.current().toString(), expected);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   208
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   209
52938
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   210
    @BeforeMethod
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   211
    public void setUp() {
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   212
        super.setUp();
5ff7480c9e28 8214491: Upgrade to JLine 3.9.0
jlahoda
parents: 47216
diff changeset
   213
        System.setProperty("jshell.test.allow.incomplete.inputs", "false");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   214
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   215
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   216
}