Path: blob/master/test/langtools/jdk/jshell/ClassPathTest.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* @summary Tests for EvalState#addToClasspath26* @modules jdk.compiler/com.sun.tools.javac.api27* jdk.compiler/com.sun.tools.javac.main28* jdk.jdeps/com.sun.tools.javap29* @library /tools/lib30* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask31* @build KullaTesting TestingInputStream Compiler32* @run testng ClassPathTest33*/3435import java.nio.file.Path;36import java.nio.file.Paths;3738import org.testng.annotations.Test;3940@Test41public class ClassPathTest extends KullaTesting {4243private final Compiler compiler = new Compiler();44private final Path outDir = Paths.get("class_path_test");4546public void testDirectory() {47compiler.compile(outDir, "package pkg; public class TestDirectory { }");48assertDeclareFail("import pkg.TestDirectory;", "compiler.err.doesnt.exist");49assertDeclareFail("new pkg.TestDirectory();", "compiler.err.doesnt.exist");50addToClasspath(compiler.getPath(outDir));51assertEval("new pkg.TestDirectory();");52}5354public void testJar() {55compiler.compile(outDir, "package pkg; public class TestJar { }");56String jarName = "test.jar";57compiler.jar(outDir, jarName, "pkg/TestJar.class");58assertDeclareFail("import pkg.TestJar;", "compiler.err.doesnt.exist");59assertDeclareFail("new pkg.TestJar();", "compiler.err.doesnt.exist");60addToClasspath(compiler.getPath(outDir).resolve(jarName));61assertEval("new pkg.TestJar();");62}6364public void testAmbiguousDirectory() {65Path p1 = outDir.resolve("dir1");66compiler.compile(p1,67"package p; public class TestAmbiguous {\n" +68" public String toString() {\n" +69" return \"first\";" +70" }\n" +71"}");72addToClasspath(compiler.getPath(p1));73Path p2 = outDir.resolve("dir2");74compiler.compile(p2,75"package p; public class TestAmbiguous {\n" +76" public String toString() {\n" +77" return \"second\";" +78" }\n" +79"}");80addToClasspath(compiler.getPath(p2));81assertEval("new p.TestAmbiguous();", "first");82}8384public void testAmbiguousJar() {85Path p1 = outDir.resolve("dir1");86compiler.compile(p1,87"package p; public class TestAmbiguous {\n" +88" public String toString() {\n" +89" return \"first\";" +90" }\n" +91"}");92String jarName = "test.jar";93compiler.jar(p1, jarName, "p/TestAmbiguous.class");94addToClasspath(compiler.getPath(p1.resolve(jarName)));95Path p2 = outDir.resolve("dir2");96compiler.compile(p2,97"package p; public class TestAmbiguous {\n" +98" public String toString() {\n" +99" return \"second\";" +100" }\n" +101"}");102addToClasspath(compiler.getPath(p2));103assertEval("new p.TestAmbiguous();", "first");104}105106public void testEmptyClassPath() {107addToClasspath("");108assertEval("new java.util.ArrayList<String>();");109}110111public void testUnknown() {112addToClasspath(compiler.getPath(outDir.resolve("UNKNOWN")));113assertDeclareFail("new Unknown();", "compiler.err.cant.resolve.location");114addToClasspath(compiler.getPath(outDir.resolve("UNKNOWN.jar")));115assertDeclareFail("new Unknown();", "compiler.err.cant.resolve.location");116}117}118119120