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