author | coleenp |
Fri, 31 Aug 2018 07:03:46 -0400 | |
changeset 51608 | 625a5bdde0c5 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 |
/* |
|
40515
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
25 |
* @test 8164277 |
33362 | 26 |
* @summary Testing IllegalStateException. |
27 |
* @build KullaTesting TestingInputStream JShellStateClosedTest |
|
28 |
* @run testng JShellStateClosedTest |
|
29 |
*/ |
|
30 |
||
31 |
import java.util.function.Consumer; |
|
32 |
||
33 |
import jdk.jshell.DeclarationSnippet; |
|
40515
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
34 |
import jdk.jshell.ImportSnippet; |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
35 |
import jdk.jshell.MethodSnippet; |
33362 | 36 |
import jdk.jshell.Snippet; |
40515
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
37 |
import jdk.jshell.TypeDeclSnippet; |
33362 | 38 |
import jdk.jshell.VarSnippet; |
39 |
import org.testng.annotations.Test; |
|
40 |
||
41 |
import static org.testng.Assert.fail; |
|
42 |
||
43 |
@Test |
|
44 |
public class JShellStateClosedTest extends KullaTesting { |
|
45 |
||
46 |
private void testStateClosedException(Runnable action) { |
|
47 |
getState().close(); |
|
48 |
try { |
|
49 |
action.run(); |
|
50 |
fail("Exception expected"); |
|
51 |
} catch (IllegalStateException e) { |
|
52 |
// Expected |
|
53 |
} |
|
54 |
} |
|
55 |
||
56 |
public void testClasses() { |
|
40515
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
57 |
TypeDeclSnippet sc = classKey(assertEval("class C { }")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
58 |
TypeDeclSnippet si = classKey(assertEval("interface I { }")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
59 |
getState().close(); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
60 |
assertStreamMatch(getState().types(), sc, si); |
33362 | 61 |
} |
62 |
||
63 |
public void testVariables() { |
|
40515
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
64 |
VarSnippet sx = varKey(assertEval("int x = 5;")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
65 |
VarSnippet sfoo = varKey(assertEval("String foo;")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
66 |
getState().close(); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
67 |
assertStreamMatch(getState().variables(), sx, sfoo); |
33362 | 68 |
} |
69 |
||
70 |
public void testMethods() { |
|
40515
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
71 |
MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
72 |
MethodSnippet svv = methodKey(assertEval("void vv() { }")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
73 |
getState().close(); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
74 |
assertStreamMatch(getState().methods(), smm, svv); |
33362 | 75 |
} |
76 |
||
40515
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
77 |
public void testImports() { |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
78 |
ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
79 |
getState().close(); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
80 |
assertStreamMatch(getState().imports(), simp); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
81 |
} |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
82 |
|
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
83 |
public void testSnippets() { |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
84 |
VarSnippet sx = varKey(assertEval("int x = 5;")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
85 |
VarSnippet sfoo = varKey(assertEval("String foo;")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
86 |
MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
87 |
MethodSnippet svv = methodKey(assertEval("void vv() { }")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
88 |
TypeDeclSnippet sc = classKey(assertEval("class C { }")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
89 |
TypeDeclSnippet si = classKey(assertEval("interface I { }")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
90 |
ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;")); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
91 |
getState().close(); |
819fc588bd19
8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis
rfield
parents:
33362
diff
changeset
|
92 |
assertStreamMatch(getState().snippets(), sx, sfoo, smm, svv, sc, si, simp); |
33362 | 93 |
} |
94 |
||
95 |
public void testEval() { |
|
96 |
testStateClosedException(() -> getState().eval("int a;")); |
|
97 |
} |
|
98 |
||
99 |
private void testStateClosedException(Consumer<Snippet> action) { |
|
100 |
Snippet k = varKey(assertEval("int a;")); |
|
101 |
getState().close(); |
|
102 |
try { |
|
103 |
action.accept(k); |
|
104 |
fail("IllegalStateException expected since closed"); |
|
105 |
} catch (IllegalStateException e) { |
|
106 |
// Expected |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
private void testStateClosedWithoutException(Consumer<Snippet> action) { |
|
111 |
Snippet k = varKey(assertEval("int a;")); |
|
112 |
getState().close(); |
|
113 |
try { |
|
114 |
action.accept(k); |
|
115 |
} catch (IllegalStateException e) { |
|
116 |
fail("Expected no IllegalStateException even though closed"); |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
public void testStatus() { |
|
121 |
testStateClosedWithoutException((key) -> getState().status(key)); |
|
122 |
} |
|
123 |
||
124 |
public void testVarValue() { |
|
125 |
testStateClosedException((key) -> getState().varValue((VarSnippet) key)); |
|
126 |
} |
|
127 |
||
128 |
public void testDrop() { |
|
41514
a75c2b869d8d
8167128: JShell: /drop of statement gives confusing output
rfield
parents:
40515
diff
changeset
|
129 |
testStateClosedException((key) -> getState().drop(key)); |
33362 | 130 |
} |
131 |
||
132 |
public void testUnresolved() { |
|
133 |
testStateClosedWithoutException((key) -> getState().unresolvedDependencies((DeclarationSnippet) key)); |
|
134 |
} |
|
135 |
||
136 |
public void testDiagnostics() { |
|
137 |
testStateClosedWithoutException((key) -> getState().diagnostics(key)); |
|
138 |
} |
|
139 |
} |