HistoryTest.java revision 3792:d516975e8110
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
26 * @bug 8166744
27 * @summary Test Completion
28 * @modules jdk.internal.le/jdk.internal.jline.extra
29 *          jdk.jshell/jdk.internal.jshell.tool:+open
30 * @build HistoryTest
31 * @run testng HistoryTest
32 */
33
34import java.lang.reflect.Field;
35import jdk.internal.jline.extra.EditingHistory;
36import org.testng.annotations.Test;
37import static org.testng.Assert.*;
38
39@Test
40public class HistoryTest extends ReplToolTesting {
41
42    public void testHistory() {
43        test(
44             a -> {if (!a) setCommandInput("void test() {\n");},
45             a -> {if (!a) setCommandInput("    System.err.println(1);\n");},
46             a -> {if (!a) setCommandInput("    System.err.println(1);\n");},
47             a -> {assertCommand(a, "} //test", "|  created method test()");},
48             a -> {
49                 if (!a) {
50                     try {
51                         previousAndAssert(getHistory(), "} //test");
52                         previousSnippetAndAssert(getHistory(), "void test() {");
53                     } catch (Exception ex) {
54                         throw new IllegalStateException(ex);
55                     }
56                 }
57                 assertCommand(a, "int dummy;", "dummy ==> 0");
58             });
59        test(
60             a -> {if (!a) setCommandInput("void test2() {\n");},
61             a -> {assertCommand(a, "} //test2", "|  created method test2()");},
62             a -> {
63                 if (!a) {
64                     try {
65                         previousAndAssert(getHistory(), "} //test2");
66                         previousSnippetAndAssert(getHistory(), "void test2() {");
67                         previousSnippetAndAssert(getHistory(), "/debug 0"); //added by test framework
68                         previousSnippetAndAssert(getHistory(), "/exit");
69                         previousSnippetAndAssert(getHistory(), "int dummy;");
70                         previousSnippetAndAssert(getHistory(), "void test() {");
71                     } catch (Exception ex) {
72                         throw new IllegalStateException(ex);
73                     }
74                 }
75                 assertCommand(a, "int dummy;", "dummy ==> 0");
76             });
77    }
78
79    public void test8166744() {
80        test(
81             a -> {if (!a) setCommandInput("class C {\n");},
82             a -> {if (!a) setCommandInput("void f() {\n");},
83             a -> {if (!a) setCommandInput("}\n");},
84             a -> {assertCommand(a, "}", "|  created class C");},
85             a -> {
86                 if (!a) {
87                     try {
88                         previousAndAssert(getHistory(), "}");
89                         previousAndAssert(getHistory(), "}");
90                         previousAndAssert(getHistory(), "void f() {");
91                         previousAndAssert(getHistory(), "class C {");
92                         getHistory().add("class C{");
93                     } catch (Exception ex) {
94                         throw new IllegalStateException(ex);
95                     }
96                 }
97                 assertCommand(a, "int dummy;", "dummy ==> 0");
98             });
99        test(
100             a -> {if (!a) setCommandInput("class C {\n");},
101             a -> {if (!a) setCommandInput("void f() {\n");},
102             a -> {if (!a) setCommandInput("}\n");},
103             a -> {assertCommand(a, "}", "|  created class C");},
104             a -> {
105                 if (!a) {
106                     try {
107                         previousSnippetAndAssert(getHistory(), "class C {");
108                         getHistory().add("class C{");
109                     } catch (Exception ex) {
110                         throw new IllegalStateException(ex);
111                     }
112                 }
113                 assertCommand(a, "int dummy;", "dummy ==> 0");
114             });
115    }
116
117    private EditingHistory getHistory() throws Exception {
118        Field input = repl.getClass().getDeclaredField("input");
119        input.setAccessible(true);
120        Object console = input.get(repl);
121        Field history = console.getClass().getDeclaredField("history");
122        history.setAccessible(true);
123        return (EditingHistory) history.get(console);
124    }
125
126    private void previousAndAssert(EditingHistory history, String expected) {
127        assertTrue(history.previous());
128        assertEquals(history.current().toString(), expected);
129    }
130
131    private void previousSnippetAndAssert(EditingHistory history, String expected) {
132        assertTrue(history.previousSnippet());
133        assertEquals(history.current().toString(), expected);
134    }
135
136}
137