ClassPathTest.java revision 3294:9adfb22ff08f
1/* 2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @summary Tests for EvalState#addToClasspath 27 * @modules jdk.compiler/com.sun.tools.javac.api 28 * jdk.compiler/com.sun.tools.javac.main 29 * jdk.jdeps/com.sun.tools.javap 30 * @library /tools/lib 31 * @build KullaTesting TestingInputStream ToolBox Compiler 32 * @run testng ClassPathTest 33 */ 34 35import java.nio.file.Path; 36import java.nio.file.Paths; 37 38import org.testng.annotations.Test; 39 40@Test 41public class ClassPathTest extends KullaTesting { 42 43 private final Compiler compiler = new Compiler(); 44 private final Path outDir = Paths.get("class_path_test"); 45 46 public void testDirectory() { 47 compiler.compile(outDir, "package pkg; public class TestDirectory { }"); 48 assertDeclareFail("import pkg.TestDirectory;", "compiler.err.doesnt.exist"); 49 assertDeclareFail("new pkg.TestDirectory();", "compiler.err.doesnt.exist"); 50 addToClasspath(compiler.getPath(outDir)); 51 assertEval("new pkg.TestDirectory();"); 52 } 53 54 public void testJar() { 55 compiler.compile(outDir, "package pkg; public class TestJar { }"); 56 String jarName = "test.jar"; 57 compiler.jar(outDir, jarName, "pkg/TestJar.class"); 58 assertDeclareFail("import pkg.TestJar;", "compiler.err.doesnt.exist"); 59 assertDeclareFail("new pkg.TestJar();", "compiler.err.doesnt.exist"); 60 addToClasspath(compiler.getPath(outDir).resolve(jarName)); 61 assertEval("new pkg.TestJar();"); 62 } 63 64 public void testAmbiguousDirectory() { 65 Path p1 = outDir.resolve("dir1"); 66 compiler.compile(p1, 67 "package p; public class TestAmbiguous {\n" + 68 " public String toString() {\n" + 69 " return \"first\";" + 70 " }\n" + 71 "}"); 72 addToClasspath(compiler.getPath(p1)); 73 Path p2 = outDir.resolve("dir2"); 74 compiler.compile(p2, 75 "package p; public class TestAmbiguous {\n" + 76 " public String toString() {\n" + 77 " return \"second\";" + 78 " }\n" + 79 "}"); 80 addToClasspath(compiler.getPath(p2)); 81 assertEval("new p.TestAmbiguous();", "first"); 82 } 83 84 public void testAmbiguousJar() { 85 Path p1 = outDir.resolve("dir1"); 86 compiler.compile(p1, 87 "package p; public class TestAmbiguous {\n" + 88 " public String toString() {\n" + 89 " return \"first\";" + 90 " }\n" + 91 "}"); 92 String jarName = "test.jar"; 93 compiler.jar(p1, jarName, "p/TestAmbiguous.class"); 94 addToClasspath(compiler.getPath(p1.resolve(jarName))); 95 Path p2 = outDir.resolve("dir2"); 96 compiler.compile(p2, 97 "package p; public class TestAmbiguous {\n" + 98 " public String toString() {\n" + 99 " return \"second\";" + 100 " }\n" + 101 "}"); 102 addToClasspath(compiler.getPath(p2)); 103 assertEval("new p.TestAmbiguous();", "first"); 104 } 105 106 public void testEmptyClassPath() { 107 addToClasspath(""); 108 assertEval("new java.util.ArrayList<String>();"); 109 } 110 111 public void testUnknown() { 112 addToClasspath(compiler.getPath(outDir.resolve("UNKNOWN"))); 113 assertDeclareFail("new Unknown();", "compiler.err.cant.resolve.location"); 114 addToClasspath(compiler.getPath(outDir.resolve("UNKNOWN.jar"))); 115 assertDeclareFail("new Unknown();", "compiler.err.cant.resolve.location"); 116 } 117} 118