author | shinyafox |
Thu, 29 Sep 2016 17:36:22 +0900 | |
changeset 41249 | 8c2aab77c2d2 |
parent 38521 | f2fe39ab9256 |
child 42407 | f3702cff2933 |
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 |
29 |
* jdk.jshell/jdk.internal.jshell.tool |
|
33362 | 30 |
* @build HistoryTest |
31 |
* @run testng HistoryTest |
|
32 |
*/ |
|
33 |
||
38521 | 34 |
import java.lang.reflect.Field; |
35 |
import jdk.internal.jline.extra.EditingHistory; |
|
33362 | 36 |
import org.testng.annotations.Test; |
37 |
import static org.testng.Assert.*; |
|
38 |
||
39 |
@Test |
|
38521 | 40 |
public class HistoryTest extends ReplToolTesting { |
33362 | 41 |
|
42 |
public void testHistory() { |
|
38521 | 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 |
}); |
|
33362 | 77 |
} |
78 |
||
41249
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
79 |
public void test8166744() { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
80 |
test( |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
81 |
a -> {if (!a) setCommandInput("class C {\n");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
82 |
a -> {if (!a) setCommandInput("void f() {\n");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
83 |
a -> {if (!a) setCommandInput("}\n");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
84 |
a -> {assertCommand(a, "}", "| created class C");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
85 |
a -> { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
86 |
if (!a) { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
87 |
try { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
88 |
previousAndAssert(getHistory(), "}"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
89 |
previousAndAssert(getHistory(), "}"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
90 |
previousAndAssert(getHistory(), "void f() {"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
91 |
previousAndAssert(getHistory(), "class C {"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
92 |
getHistory().add("class C{"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
93 |
} catch (Exception ex) { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
94 |
throw new IllegalStateException(ex); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
95 |
} |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
96 |
} |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
97 |
assertCommand(a, "int dummy;", "dummy ==> 0"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
98 |
}); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
99 |
test( |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
100 |
a -> {if (!a) setCommandInput("class C {\n");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
101 |
a -> {if (!a) setCommandInput("void f() {\n");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
102 |
a -> {if (!a) setCommandInput("}\n");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
103 |
a -> {assertCommand(a, "}", "| created class C");}, |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
104 |
a -> { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
105 |
if (!a) { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
106 |
try { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
107 |
previousSnippetAndAssert(getHistory(), "class C {"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
108 |
getHistory().add("class C{"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
109 |
} catch (Exception ex) { |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
110 |
throw new IllegalStateException(ex); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
111 |
} |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
112 |
} |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
113 |
assertCommand(a, "int dummy;", "dummy ==> 0"); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
114 |
}); |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
115 |
} |
8c2aab77c2d2
8166744: JShell: java.lang.IndexOutOfBoundsException for legal history access
shinyafox
parents:
38521
diff
changeset
|
116 |
|
38521 | 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); |
|
33362 | 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 |
} |