author | dtitov |
Tue, 08 Oct 2019 11:18:00 -0700 | |
changeset 58506 | dfe3d11378cc |
parent 52938 | 5ff7480c9e28 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
52938 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
33362 | 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 |
|
41249
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
26 |
* @bug 8166744 |
33362 | 27 |
* @summary Test Completion |
52938 | 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 | 30 |
* @build HistoryTest |
31 |
* @run testng HistoryTest |
|
32 |
*/ |
|
33 |
||
38521 | 34 |
import java.lang.reflect.Field; |
52938 | 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 | 39 |
|
33362 | 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 | 43 |
import jdk.internal.org.jline.reader.History; |
33362 | 44 |
import static org.testng.Assert.*; |
52938 | 45 |
import org.testng.annotations.BeforeMethod; |
33362 | 46 |
|
38521 | 47 |
public class HistoryTest extends ReplToolTesting { |
33362 | 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 | 65 |
public void testHistory() { |
38521 | 66 |
test( |
67 |
a -> {if (!a) setCommandInput("void test() {\n");}, |
|
68 |
a -> {if (!a) setCommandInput(" System.err.println(1);\n");}, |
|
69 |
a -> {if (!a) setCommandInput(" System.err.println(1);\n");}, |
|
70 |
a -> {assertCommand(a, "} //test", "| created method test()");}, |
|
71 |
a -> { |
|
72 |
if (!a) { |
|
73 |
try { |
|
52938 | 74 |
previousAndAssert(getHistory(), "void test() {\n" + |
75 |
" System.err.println(1);\n" + |
|
76 |
" System.err.println(1);\n" + |
|
77 |
"} //test"); |
|
38521 | 78 |
} catch (Exception ex) { |
79 |
throw new IllegalStateException(ex); |
|
80 |
} |
|
81 |
} |
|
82 |
assertCommand(a, "int dummy;", "dummy ==> 0"); |
|
83 |
}); |
|
84 |
test( |
|
85 |
a -> {if (!a) setCommandInput("void test2() {\n");}, |
|
86 |
a -> {assertCommand(a, "} //test2", "| created method test2()");}, |
|
87 |
a -> { |
|
88 |
if (!a) { |
|
89 |
try { |
|
52938 | 90 |
previousAndAssert(getHistory(), "void test2() {\n" + |
91 |
"} //test2"); |
|
92 |
previousAndAssert(getHistory(), "/debug 0"); //added by test framework |
|
93 |
previousAndAssert(getHistory(), "/exit"); |
|
94 |
previousAndAssert(getHistory(), "int dummy;"); |
|
95 |
previousAndAssert(getHistory(), "void test() {\n" + |
|
96 |
" System.err.println(1);\n" + |
|
97 |
" System.err.println(1);\n" + |
|
98 |
"} //test"); |
|
38521 | 99 |
} catch (Exception ex) { |
100 |
throw new IllegalStateException(ex); |
|
101 |
} |
|
102 |
} |
|
103 |
assertCommand(a, "int dummy;", "dummy ==> 0"); |
|
104 |
}); |
|
33362 | 105 |
} |
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 | 117 |
previousAndAssert(getHistory(), "class C {\n" + |
118 |
"void f() {\n" + |
|
119 |
"}\n" + |
|
120 |
"}"); |
|
121 |
getHistory().add("class C {\n" + |
|
122 |
"void f() {\n" + |
|
123 |
"}\n" + |
|
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 | 139 |
previousAndAssert(getHistory(), "class C {\n" + |
140 |
"void f() {\n" + |
|
141 |
"}\n" + |
|
142 |
"}"); |
|
143 |
getHistory().add("class C {\n" + |
|
144 |
"void f() {\n" + |
|
145 |
"}\n" + |
|
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 | 155 |
@Test |
156 |
public void testReadExistingHistory() { |
|
157 |
prefsMap.put("HISTORY_LINE_0", "/debug 0"); |
|
158 |
prefsMap.put("HISTORY_LINE_1", "void test() {\\"); |
|
159 |
prefsMap.put("HISTORY_LINE_2", " System.err.println(1);\\"); |
|
160 |
prefsMap.put("HISTORY_LINE_3", " System.err.println(`\\\\\\\\\\"); |
|
161 |
prefsMap.put("HISTORY_LINE_4", " \\\\\\"); |
|
162 |
prefsMap.put("HISTORY_LINE_5", "`);\\"); |
|
163 |
prefsMap.put("HISTORY_LINE_6", "} //test"); |
|
164 |
test( |
|
165 |
a -> {assertCommand(a, "int i", "i ==> 0");}, |
|
166 |
a -> { |
|
167 |
if (!a) { |
|
168 |
try { |
|
169 |
previousAndAssert(getHistory(), "int i"); |
|
170 |
previousAndAssert(getHistory(), "/debug 0"); //added by test framework |
|
171 |
previousAndAssert(getHistory(), "void test() {\n" + |
|
172 |
" System.err.println(1);\n" + |
|
173 |
" System.err.println(`\\\\\n" + |
|
174 |
" \\\n" + |
|
175 |
"`);\n" + |
|
176 |
"} //test"); |
|
177 |
} catch (Exception ex) { |
|
178 |
throw new IllegalStateException(ex); |
|
179 |
} |
|
180 |
} |
|
181 |
assertCommand(a, "/exit", ""); |
|
182 |
}); |
|
183 |
assertEquals(prefsMap.get("HISTORY_LINE_00"), "/debug 0"); |
|
184 |
assertEquals(prefsMap.get("HISTORY_LINE_01"), "void test() {\\"); |
|
185 |
assertEquals(prefsMap.get("HISTORY_LINE_02"), " System.err.println(1);\\"); |
|
186 |
assertEquals(prefsMap.get("HISTORY_LINE_03"), " System.err.println(`\\\\\\\\\\"); |
|
187 |
assertEquals(prefsMap.get("HISTORY_LINE_04"), " \\\\\\"); |
|
188 |
assertEquals(prefsMap.get("HISTORY_LINE_05"), "`);\\"); |
|
189 |
assertEquals(prefsMap.get("HISTORY_LINE_06"), "} //test"); |
|
190 |
assertEquals(prefsMap.get("HISTORY_LINE_07"), "/debug 0"); |
|
191 |
assertEquals(prefsMap.get("HISTORY_LINE_08"), "int i"); |
|
192 |
assertEquals(prefsMap.get("HISTORY_LINE_09"), "/exit"); |
|
193 |
System.err.println("prefsMap: " + prefsMap); |
|
194 |
} |
|
195 |
||
196 |
private History getHistory() throws Exception { |
|
38521 | 197 |
Field input = repl.getClass().getDeclaredField("input"); |
198 |
input.setAccessible(true); |
|
199 |
Object console = input.get(repl); |
|
52938 | 200 |
Method getHistory = console.getClass().getDeclaredMethod("getHistory"); |
201 |
getHistory.setAccessible(true); |
|
202 |
return (History) getHistory.invoke(console); |
|
33362 | 203 |
} |
204 |
||
52938 | 205 |
private void previousAndAssert(History history, String expected) { |
33362 | 206 |
assertTrue(history.previous()); |
207 |
assertEquals(history.current().toString(), expected); |
|
208 |
} |
|
209 |
||
52938 | 210 |
@BeforeMethod |
211 |
public void setUp() { |
|
212 |
super.setUp(); |
|
213 |
System.setProperty("jshell.test.allow.incomplete.inputs", "false"); |
|
33362 | 214 |
} |
215 |
||
216 |
} |