author | tschatzl |
Fri, 04 Aug 2017 14:15:42 +0200 | |
changeset 46750 | bcab0128a16f |
parent 44683 | 610dc2b48954 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
38521 | 2 |
* Copyright (c) 2015, 2016, 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 |
38521 | 28 |
* @modules jdk.internal.le/jdk.internal.jline.extra |
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; |
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
35 |
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
|
36 |
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
|
37 |
import java.util.logging.Logger; |
38521 | 38 |
import jdk.internal.jline.extra.EditingHistory; |
33362 | 39 |
import org.testng.annotations.Test; |
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
40 |
import jdk.internal.jshell.tool.JShellTool; |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
41 |
import jdk.internal.jshell.tool.JShellToolBuilder; |
33362 | 42 |
import static org.testng.Assert.*; |
43 |
||
38521 | 44 |
public class HistoryTest extends ReplToolTesting { |
33362 | 45 |
|
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
46 |
private JShellTool repl; |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
47 |
|
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
48 |
@Override |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
49 |
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
|
50 |
// 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
|
51 |
Logger.getLogger("jdk.jshell.execution").setLevel(Level.ALL); |
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
52 |
repl = ((JShellToolBuilder) builder(locale)) |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
53 |
.rawTool(); |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
54 |
try { |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
55 |
repl.start(args); |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
56 |
} catch (Exception ex) { |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
57 |
fail("Repl tool died with exception", ex); |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
58 |
} |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
59 |
} |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
60 |
|
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
61 |
@Test |
33362 | 62 |
public void testHistory() { |
38521 | 63 |
test( |
64 |
a -> {if (!a) setCommandInput("void test() {\n");}, |
|
65 |
a -> {if (!a) setCommandInput(" System.err.println(1);\n");}, |
|
66 |
a -> {if (!a) setCommandInput(" System.err.println(1);\n");}, |
|
67 |
a -> {assertCommand(a, "} //test", "| created method test()");}, |
|
68 |
a -> { |
|
69 |
if (!a) { |
|
70 |
try { |
|
71 |
previousAndAssert(getHistory(), "} //test"); |
|
72 |
previousSnippetAndAssert(getHistory(), "void test() {"); |
|
73 |
} catch (Exception ex) { |
|
74 |
throw new IllegalStateException(ex); |
|
75 |
} |
|
76 |
} |
|
77 |
assertCommand(a, "int dummy;", "dummy ==> 0"); |
|
78 |
}); |
|
79 |
test( |
|
80 |
a -> {if (!a) setCommandInput("void test2() {\n");}, |
|
81 |
a -> {assertCommand(a, "} //test2", "| created method test2()");}, |
|
82 |
a -> { |
|
83 |
if (!a) { |
|
84 |
try { |
|
85 |
previousAndAssert(getHistory(), "} //test2"); |
|
86 |
previousSnippetAndAssert(getHistory(), "void test2() {"); |
|
87 |
previousSnippetAndAssert(getHistory(), "/debug 0"); //added by test framework |
|
88 |
previousSnippetAndAssert(getHistory(), "/exit"); |
|
89 |
previousSnippetAndAssert(getHistory(), "int dummy;"); |
|
90 |
previousSnippetAndAssert(getHistory(), "void test() {"); |
|
91 |
} catch (Exception ex) { |
|
92 |
throw new IllegalStateException(ex); |
|
93 |
} |
|
94 |
} |
|
95 |
assertCommand(a, "int dummy;", "dummy ==> 0"); |
|
96 |
}); |
|
33362 | 97 |
} |
98 |
||
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42407
diff
changeset
|
99 |
@Test |
41249
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
100 |
public void test8166744() { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
101 |
test( |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
102 |
a -> {if (!a) setCommandInput("class C {\n");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
103 |
a -> {if (!a) setCommandInput("void f() {\n");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
104 |
a -> {if (!a) setCommandInput("}\n");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
105 |
a -> {assertCommand(a, "}", "| created class C");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
106 |
a -> { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
107 |
if (!a) { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
108 |
try { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
109 |
previousAndAssert(getHistory(), "}"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
110 |
previousAndAssert(getHistory(), "}"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
111 |
previousAndAssert(getHistory(), "void f() {"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
112 |
previousAndAssert(getHistory(), "class C {"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
113 |
getHistory().add("class C{"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
114 |
} catch (Exception ex) { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
115 |
throw new IllegalStateException(ex); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
116 |
} |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
117 |
} |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
118 |
assertCommand(a, "int dummy;", "dummy ==> 0"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
119 |
}); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
120 |
test( |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
121 |
a -> {if (!a) setCommandInput("class C {\n");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
122 |
a -> {if (!a) setCommandInput("void f() {\n");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
123 |
a -> {if (!a) setCommandInput("}\n");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
124 |
a -> {assertCommand(a, "}", "| created class C");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
125 |
a -> { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
126 |
if (!a) { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
127 |
try { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
128 |
previousSnippetAndAssert(getHistory(), "class C {"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
129 |
getHistory().add("class C{"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
130 |
} catch (Exception ex) { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
131 |
throw new IllegalStateException(ex); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
132 |
} |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
133 |
} |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
134 |
assertCommand(a, "int dummy;", "dummy ==> 0"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
135 |
}); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
136 |
} |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
137 |
|
38521 | 138 |
private EditingHistory getHistory() throws Exception { |
139 |
Field input = repl.getClass().getDeclaredField("input"); |
|
140 |
input.setAccessible(true); |
|
141 |
Object console = input.get(repl); |
|
142 |
Field history = console.getClass().getDeclaredField("history"); |
|
143 |
history.setAccessible(true); |
|
144 |
return (EditingHistory) history.get(console); |
|
33362 | 145 |
} |
146 |
||
147 |
private void previousAndAssert(EditingHistory history, String expected) { |
|
148 |
assertTrue(history.previous()); |
|
149 |
assertEquals(history.current().toString(), expected); |
|
150 |
} |
|
151 |
||
152 |
private void previousSnippetAndAssert(EditingHistory history, String expected) { |
|
153 |
assertTrue(history.previousSnippet()); |
|
154 |
assertEquals(history.current().toString(), expected); |
|
155 |
} |
|
156 |
||
157 |
} |