Path: blob/master/test/langtools/jdk/jshell/JShellStateClosedTest.java
40931 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test 816427725* @summary Testing IllegalStateException.26* @build KullaTesting TestingInputStream JShellStateClosedTest27* @run testng JShellStateClosedTest28*/2930import java.util.function.Consumer;3132import jdk.jshell.DeclarationSnippet;33import jdk.jshell.ImportSnippet;34import jdk.jshell.MethodSnippet;35import jdk.jshell.Snippet;36import jdk.jshell.TypeDeclSnippet;37import jdk.jshell.VarSnippet;38import org.testng.annotations.Test;3940import static org.testng.Assert.fail;4142@Test43public class JShellStateClosedTest extends KullaTesting {4445private void testStateClosedException(Runnable action) {46getState().close();47try {48action.run();49fail("Exception expected");50} catch (IllegalStateException e) {51// Expected52}53}5455public void testClasses() {56TypeDeclSnippet sc = classKey(assertEval("class C { }"));57TypeDeclSnippet si = classKey(assertEval("interface I { }"));58getState().close();59assertStreamMatch(getState().types(), sc, si);60}6162public void testVariables() {63VarSnippet sx = varKey(assertEval("int x = 5;"));64VarSnippet sfoo = varKey(assertEval("String foo;"));65getState().close();66assertStreamMatch(getState().variables(), sx, sfoo);67}6869public void testMethods() {70MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }"));71MethodSnippet svv = methodKey(assertEval("void vv() { }"));72getState().close();73assertStreamMatch(getState().methods(), smm, svv);74}7576public void testImports() {77ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;"));78getState().close();79assertStreamMatch(getState().imports(), simp);80}8182public void testSnippets() {83VarSnippet sx = varKey(assertEval("int x = 5;"));84VarSnippet sfoo = varKey(assertEval("String foo;"));85MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }"));86MethodSnippet svv = methodKey(assertEval("void vv() { }"));87TypeDeclSnippet sc = classKey(assertEval("class C { }"));88TypeDeclSnippet si = classKey(assertEval("interface I { }"));89ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;"));90getState().close();91assertStreamMatch(getState().snippets(), sx, sfoo, smm, svv, sc, si, simp);92}9394public void testEval() {95testStateClosedException(() -> getState().eval("int a;"));96}9798private void testStateClosedException(Consumer<Snippet> action) {99Snippet k = varKey(assertEval("int a;"));100getState().close();101try {102action.accept(k);103fail("IllegalStateException expected since closed");104} catch (IllegalStateException e) {105// Expected106}107}108109private void testStateClosedWithoutException(Consumer<Snippet> action) {110Snippet k = varKey(assertEval("int a;"));111getState().close();112try {113action.accept(k);114} catch (IllegalStateException e) {115fail("Expected no IllegalStateException even though closed");116}117}118119public void testStatus() {120testStateClosedWithoutException((key) -> getState().status(key));121}122123public void testVarValue() {124testStateClosedException((key) -> getState().varValue((VarSnippet) key));125}126127public void testDrop() {128testStateClosedException((key) -> getState().drop(key));129}130131public void testUnresolved() {132testStateClosedWithoutException((key) -> getState().unresolvedDependencies((DeclarationSnippet) key));133}134135public void testDiagnostics() {136testStateClosedWithoutException((key) -> getState().diagnostics(key));137}138}139140141