Path: blob/master/test/langtools/jdk/jshell/ImportTest.java
40931 views
/*1* Copyright (c) 2015, 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 814141526* @summary Test imports27* @modules jdk.compiler/com.sun.tools.javac.api28* jdk.compiler/com.sun.tools.javac.main29* jdk.jdeps/com.sun.tools.javap30* @library /tools/lib31* @build KullaTesting TestingInputStream toolbox.Task.ExpectedDiagnostic32* @run testng ImportTest33*/3435import java.nio.file.Path;36import java.nio.file.Paths;3738import javax.tools.Diagnostic;3940import jdk.jshell.Snippet;41import org.testng.annotations.Test;4243import static jdk.jshell.Snippet.Status.VALID;44import static jdk.jshell.Snippet.Status.OVERWRITTEN;45import static jdk.jshell.Snippet.SubKind.SINGLE_TYPE_IMPORT_SUBKIND;46import static jdk.jshell.Snippet.SubKind.SINGLE_STATIC_IMPORT_SUBKIND;47import static jdk.jshell.Snippet.SubKind.TYPE_IMPORT_ON_DEMAND_SUBKIND;48import static jdk.jshell.Snippet.SubKind.STATIC_IMPORT_ON_DEMAND_SUBKIND;4950@Test51public class ImportTest extends KullaTesting {5253public void testImport() {54assertImportKeyMatch("import java.util.List;", "List", SINGLE_TYPE_IMPORT_SUBKIND, added(VALID));55assertImportKeyMatch("import java.util.ArrayList;", "ArrayList", SINGLE_TYPE_IMPORT_SUBKIND, added(VALID));56assertEval("List<Integer> list = new ArrayList<>();");57assertEval("list.add(45);");58assertEval("list.size();", "1");59}6061public void testImportOnDemand() {62assertImportKeyMatch("import java.util.*;", "java.util.*", TYPE_IMPORT_ON_DEMAND_SUBKIND, added(VALID));63assertEval("List<Integer> list = new ArrayList<>();");64assertEval("list.add(45);");65assertEval("list.size();", "1");66}6768public void testImportStatic() {69assertImportKeyMatch("import static java.lang.Math.PI;", "PI", SINGLE_STATIC_IMPORT_SUBKIND, added(VALID));70assertEval("Double.valueOf(PI).toString().substring(0, 16).equals(\"3.14159265358979\");", "true");71}7273public void testImportStaticOnDemand() {74assertImportKeyMatch("import static java.lang.Math.*;", "java.lang.Math.*", STATIC_IMPORT_ON_DEMAND_SUBKIND, added(VALID));75assertEval("abs(cos(PI / 2)) < 0.00001;", "true");76}7778@Test(enabled = false) // TODO 812941879public void testUnknownPackage() {80assertDeclareFail("import unknown.qqq;",81new ExpectedDiagnostic("compiler.err.doesnt.exist", 7, 18, 14, -1, -1, Diagnostic.Kind.ERROR));82assertDeclareFail("import unknown.*;",83new ExpectedDiagnostic("compiler.err.doesnt.exist", 7, 15, 7, -1, -1, Diagnostic.Kind.ERROR));84}8586public void testBogusImportIgnoredInFuture() {87assertDeclareFail("import unknown.qqq;", "compiler.err.doesnt.exist");88assertDeclareFail("import unknown.*;", "compiler.err.doesnt.exist");89assertEval("2 + 2;");90}9192public void testBadImport() {93assertDeclareFail("import static java.lang.reflect.Modifier;",94new ExpectedDiagnostic("compiler.err.cant.resolve.location", 14, 31, 23, -1, -1, Diagnostic.Kind.ERROR));95}9697public void testBadSyntaxImport() {98assertDeclareFail("import not found.*;",99new ExpectedDiagnostic("compiler.err.expected", 10, 10, 10, -1, -1, Diagnostic.Kind.ERROR));100}101102public void testImportRedefinition() {103Compiler compiler = new Compiler();104Path path = Paths.get("testImport");105compiler.compile(path, "package util; public class ArrayList { public String toString() { return \"MyList\"; } }");106compiler.compile(path, "package util; public class A { public static class ArrayList {\n" +107"public String toString() { return \"MyInnerList\"; } } }");108addToClasspath(compiler.getPath(path));109110assertImportKeyMatch("import util.*;", "util.*", TYPE_IMPORT_ON_DEMAND_SUBKIND, added(VALID));111assertEval("new ArrayList();", "MyList", added(VALID));112113Snippet import0 = assertImportKeyMatch("import java.util.ArrayList;", "ArrayList", SINGLE_TYPE_IMPORT_SUBKIND, added(VALID));114assertEval("new ArrayList();", "[]");115116Snippet import1 = assertImportKeyMatch("import util.ArrayList;", "ArrayList", SINGLE_TYPE_IMPORT_SUBKIND,117ste(MAIN_SNIPPET, VALID, VALID, false, null),118ste(import0, VALID, OVERWRITTEN, false, MAIN_SNIPPET));119assertEval("new ArrayList();", "MyList");120121Snippet import2 = assertImportKeyMatch("import java.util.ArrayList;", "ArrayList", SINGLE_TYPE_IMPORT_SUBKIND,122ste(MAIN_SNIPPET, VALID, VALID, false, null),123ste(import1, VALID, OVERWRITTEN, false, MAIN_SNIPPET));124assertEval("new ArrayList();", "[]");125126Snippet import3 = assertImportKeyMatch("import util.A.ArrayList;", "ArrayList", SINGLE_TYPE_IMPORT_SUBKIND,127ste(MAIN_SNIPPET, VALID, VALID, false, null),128ste(import2, VALID, OVERWRITTEN, false, MAIN_SNIPPET));129assertEval("new ArrayList();", "MyInnerList");130131Snippet import4 = assertImportKeyMatch("import java.util.ArrayList;", "ArrayList", SINGLE_TYPE_IMPORT_SUBKIND,132ste(MAIN_SNIPPET, VALID, VALID, false, null),133ste(import3, VALID, OVERWRITTEN, false, MAIN_SNIPPET));134assertEval("new ArrayList();", "[]");135136Snippet import5 = assertImportKeyMatch("import static util.A.ArrayList;", "ArrayList", SINGLE_STATIC_IMPORT_SUBKIND,137ste(MAIN_SNIPPET, VALID, VALID, false, null),138ste(import4, VALID, OVERWRITTEN, false, MAIN_SNIPPET));139assertEval("new ArrayList();", "MyInnerList");140}141142public void testImportMemberRedefinition() {143Compiler compiler = new Compiler();144Path path = Paths.get("testImport");145compiler.compile(path, "package util; public class A {" +146"public static String field = \"A\";" +147"public static String method() { return \"A\"; } }");148compiler.compile(path, "package util; public class B {" +149"public static String field = \"B\";" +150"public static String method() { return \"B\"; } }");151addToClasspath(compiler.getPath(path));152153assertImportKeyMatch("import static util.B.*;", "util.B.*", STATIC_IMPORT_ON_DEMAND_SUBKIND, added(VALID));154assertEval("field;", "\"B\"");155assertEval("method();", "\"B\"");156157assertImportKeyMatch("import static util.A.method;", "method", SINGLE_STATIC_IMPORT_SUBKIND, added(VALID));158assertEval("field;", "\"B\"");159assertEval("method();", "\"A\"");160161assertImportKeyMatch("import static util.A.field;", "field", SINGLE_STATIC_IMPORT_SUBKIND, added(VALID));162assertEval("field;", "\"A\"");163assertEval("method();", "\"A\"");164}165166public void testImportWithComment() {167assertImportKeyMatch("import java.util.List;//comment", "List", SINGLE_TYPE_IMPORT_SUBKIND, added(VALID));168assertEval("List l = null;");169}170}171172173