langtools/test/jdk/jshell/HistoryTest.java
author rfield
Thu, 26 May 2016 07:58:01 -0700
changeset 38608 691b607bbcd6
parent 38521 f2fe39ab9256
child 41249 8c2aab77c2d2
permissions -rw-r--r--
8157917: JShell: shutdown could cause remote JDWP errors to be visible to users 8157918: JShell tests: StartOptionTest displays insufficient information to diagnose failures Reviewed-by: vromero
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
/*
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
     2
 * Copyright (c) 2015, 2016, 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
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    26
 * @summary Test Completion
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    27
 * @modules jdk.internal.le/jdk.internal.jline.extra
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    28
 *          jdk.jshell/jdk.internal.jshell.tool
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    29
 * @build HistoryTest
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    30
 * @run testng HistoryTest
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    31
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    32
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    33
import java.lang.reflect.Field;
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    34
import jdk.internal.jline.extra.EditingHistory;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    35
import org.testng.annotations.Test;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
import static org.testng.Assert.*;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    37
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    38
@Test
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    39
public class HistoryTest extends ReplToolTesting {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
    public void testHistory() {
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    42
        test(
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    43
             a -> {if (!a) setCommandInput("void test() {\n");},
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    44
             a -> {if (!a) setCommandInput("    System.err.println(1);\n");},
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    45
             a -> {if (!a) setCommandInput("    System.err.println(1);\n");},
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    46
             a -> {assertCommand(a, "} //test", "|  created method test()");},
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    47
             a -> {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    48
                 if (!a) {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    49
                     try {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    50
                         previousAndAssert(getHistory(), "} //test");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    51
                         previousSnippetAndAssert(getHistory(), "void test() {");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    52
                     } catch (Exception ex) {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    53
                         throw new IllegalStateException(ex);
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    54
                     }
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    55
                 }
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    56
                 assertCommand(a, "int dummy;", "dummy ==> 0");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    57
             });
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    58
        test(
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    59
             a -> {if (!a) setCommandInput("void test2() {\n");},
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    60
             a -> {assertCommand(a, "} //test2", "|  created method test2()");},
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    61
             a -> {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    62
                 if (!a) {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    63
                     try {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    64
                         previousAndAssert(getHistory(), "} //test2");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    65
                         previousSnippetAndAssert(getHistory(), "void test2() {");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    66
                         previousSnippetAndAssert(getHistory(), "/debug 0"); //added by test framework
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    67
                         previousSnippetAndAssert(getHistory(), "/exit");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    68
                         previousSnippetAndAssert(getHistory(), "int dummy;");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    69
                         previousSnippetAndAssert(getHistory(), "void test() {");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    70
                     } catch (Exception ex) {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    71
                         throw new IllegalStateException(ex);
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    72
                     }
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    73
                 }
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    74
                 assertCommand(a, "int dummy;", "dummy ==> 0");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    75
             });
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    76
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    77
38521
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    78
    private EditingHistory getHistory() throws Exception {
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    79
        Field input = repl.getClass().getDeclaredField("input");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    80
        input.setAccessible(true);
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    81
        Object console = input.get(repl);
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    82
        Field history = console.getClass().getDeclaredField("history");
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    83
        history.setAccessible(true);
f2fe39ab9256 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 35359
diff changeset
    84
        return (EditingHistory) history.get(console);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    85
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    87
    private void previousAndAssert(EditingHistory history, String expected) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    88
        assertTrue(history.previous());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    89
        assertEquals(history.current().toString(), expected);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    90
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
    private void previousSnippetAndAssert(EditingHistory history, String expected) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    93
        assertTrue(history.previousSnippet());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    94
        assertEquals(history.current().toString(), expected);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    95
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    96
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    97
}