Path: blob/master/test/langtools/jdk/jshell/JShellQueryTest.java
40931 views
/*1* Copyright (c) 2016, 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* @test25* @bug 814396426* @summary test queries to the JShell that return Streams27* @build KullaTesting28* @run testng JShellQueryTest29*/30import jdk.jshell.Snippet;31import org.testng.annotations.Test;3233import jdk.jshell.ImportSnippet;34import jdk.jshell.MethodSnippet;35import jdk.jshell.TypeDeclSnippet;36import jdk.jshell.VarSnippet;37import static java.util.stream.Collectors.joining;38import static org.testng.Assert.assertEquals;3940@Test41public class JShellQueryTest extends KullaTesting {4243public void testSnippets() {44assertStreamMatch(getState().snippets());45VarSnippet sx = varKey(assertEval("int x = 5;"));46VarSnippet sfoo = varKey(assertEval("String foo;"));47MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }"));48MethodSnippet svv = methodKey(assertEval("void vv() { }"));49assertStreamMatch(getState().snippets(), sx, sfoo, smm, svv);50TypeDeclSnippet sc = classKey(assertEval("class C { }"));51TypeDeclSnippet si = classKey(assertEval("interface I { }"));52ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;"));53assertStreamMatch(getState().snippets(), sx, sfoo, smm, svv, sc, si, simp);54}5556public void testVars() {57assertStreamMatch(getState().variables());58VarSnippet sx = varKey(assertEval("int x = 5;"));59VarSnippet sfoo = varKey(assertEval("String foo;"));60MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }"));61MethodSnippet svv = methodKey(assertEval("void vv() { }"));62assertStreamMatch(getState().variables(), sx, sfoo);63TypeDeclSnippet sc = classKey(assertEval("class C { }"));64TypeDeclSnippet si = classKey(assertEval("interface I { }"));65ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;"));66assertStreamMatch(getState().variables(), sx, sfoo);67}6869public void testMethods() {70assertStreamMatch(getState().methods());71VarSnippet sx = varKey(assertEval("int x = 5;"));72VarSnippet sfoo = varKey(assertEval("String foo;"));73MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }"));74MethodSnippet svv = methodKey(assertEval("void vv() { }"));75TypeDeclSnippet sc = classKey(assertEval("class C { }"));76TypeDeclSnippet si = classKey(assertEval("interface I { }"));77ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;"));78assertStreamMatch(getState().methods(), smm, svv);79}8081public void testTypes() {82assertStreamMatch(getState().types());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.*;"));90assertStreamMatch(getState().types(), sc, si);91}9293public void testImports() {94assertStreamMatch(getState().imports());95VarSnippet sx = varKey(assertEval("int x = 5;"));96VarSnippet sfoo = varKey(assertEval("String foo;"));97MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }"));98MethodSnippet svv = methodKey(assertEval("void vv() { }"));99TypeDeclSnippet sc = classKey(assertEval("class C { }"));100TypeDeclSnippet si = classKey(assertEval("interface I { }"));101ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;"));102assertStreamMatch(getState().imports(), simp);103}104105public void testDiagnostics() {106Snippet sx = varKey(assertEval("int x = 5;"));107assertStreamMatch(getState().diagnostics(sx));108Snippet broken = methodKey(assertEvalFail("int m() { blah(); return \"hello\"; }"));109String res = getState().diagnostics(broken)110.map(d -> d.getCode())111.collect(joining("+"));112assertEquals(res, "compiler.err.cant.resolve.location.args+compiler.err.prob.found.req");113}114115public void testUnresolvedDependencies() {116VarSnippet sx = varKey(assertEval("int x = 5;"));117assertStreamMatch(getState().unresolvedDependencies(sx));118MethodSnippet unr = methodKey(getState().eval("void uu() { baz(); zips(); }"));119assertStreamMatch(getState().unresolvedDependencies(unr), "method zips()", "method baz()");120}121}122123124