JShellStateClosedTest.java revision 3062:15bdc18525ff
1292915Sdim/*
2292915Sdim * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3353358Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4353358Sdim *
5353358Sdim * This code is free software; you can redistribute it and/or modify it
6292915Sdim * under the terms of the GNU General Public License version 2 only, as
7292915Sdim * published by the Free Software Foundation.
8292915Sdim *
9292915Sdim * This code is distributed in the hope that it will be useful, but WITHOUT
10292915Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11292915Sdim * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12292915Sdim * version 2 for more details (a copy is included in the LICENSE file that
13292915Sdim * accompanied this code).
14292915Sdim *
15292915Sdim * You should have received a copy of the GNU General Public License version
16292915Sdim * 2 along with this work; if not, write to the Free Software Foundation,
17292915Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18292915Sdim *
19292915Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20292915Sdim * or visit www.oracle.com if you need additional information or have any
21341825Sdim * questions.
22292915Sdim */
23327952Sdim
24327952Sdim/*
25327952Sdim * @test
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.PersistentSnippet;
35import jdk.jshell.Snippet;
36import jdk.jshell.VarSnippet;
37import org.testng.annotations.Test;
38
39import static org.testng.Assert.fail;
40
41@Test
42public class JShellStateClosedTest extends KullaTesting {
43
44    private void testStateClosedException(Runnable action) {
45        getState().close();
46        try {
47            action.run();
48            fail("Exception expected");
49        } catch (IllegalStateException e) {
50            // Expected
51        }
52    }
53
54    public void testClasses() {
55        testStateClosedException(() -> getState().types());
56    }
57
58    public void testVariables() {
59        testStateClosedException(() -> getState().variables());
60    }
61
62    public void testMethods() {
63        testStateClosedException(() -> getState().methods());
64    }
65
66    public void testKeys() {
67        testStateClosedException(() -> getState().snippets());
68    }
69
70    public void testEval() {
71        testStateClosedException(() -> getState().eval("int a;"));
72    }
73
74    private void testStateClosedException(Consumer<Snippet> action) {
75        Snippet k = varKey(assertEval("int a;"));
76        getState().close();
77        try {
78            action.accept(k);
79            fail("IllegalStateException expected since closed");
80        } catch (IllegalStateException e) {
81            // Expected
82        }
83    }
84
85    private void testStateClosedWithoutException(Consumer<Snippet> action) {
86        Snippet k = varKey(assertEval("int a;"));
87        getState().close();
88        try {
89            action.accept(k);
90        } catch (IllegalStateException e) {
91            fail("Expected no IllegalStateException even though closed");
92        }
93    }
94
95    public void testStatus() {
96        testStateClosedWithoutException((key) -> getState().status(key));
97    }
98
99    public void testVarValue() {
100        testStateClosedException((key) -> getState().varValue((VarSnippet) key));
101    }
102
103    public void testDrop() {
104        testStateClosedException((key) -> getState().drop((PersistentSnippet) key));
105    }
106
107    public void testUnresolved() {
108        testStateClosedWithoutException((key) -> getState().unresolvedDependencies((DeclarationSnippet) key));
109    }
110
111    public void testDiagnostics() {
112        testStateClosedWithoutException((key) -> getState().diagnostics(key));
113    }
114}
115