ClassPathTest.java revision 3222:94cfc50c1b8a
1/* 2 * Copyright (c) 2015, 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 * @library /tools/lib 30 * @build KullaTesting TestingInputStream ToolBox Compiler 31 * @run testng ClassPathTest 32 */ 33 34import java.nio.file.Path; 35import java.nio.file.Paths; 36 37import org.testng.annotations.Test; 38 39@Test 40public class ClassPathTest extends KullaTesting { 41 42 private final Compiler compiler = new Compiler(); 43 private final Path outDir = Paths.get("class_path_test"); 44 45 public void testDirectory() { 46 compiler.compile(outDir, "package pkg; public class TestDirectory { }"); 47 assertDeclareFail("import pkg.TestDirectory;", "compiler.err.doesnt.exist"); 48 assertDeclareFail("new pkg.TestDirectory();", "compiler.err.doesnt.exist"); 49 addToClasspath(compiler.getPath(outDir)); 50 assertEval("new pkg.TestDirectory();"); 51 } 52 53 public void testJar() { 54 compiler.compile(outDir, "package pkg; public class TestJar { }"); 55 String jarName = "test.jar"; 56 compiler.jar(outDir, jarName, "pkg/TestJar.class"); 57 assertDeclareFail("import pkg.TestJar;", "compiler.err.doesnt.exist"); 58 assertDeclareFail("new pkg.TestJar();", "compiler.err.doesnt.exist"); 59 addToClasspath(compiler.getPath(outDir).resolve(jarName)); 60 assertEval("new pkg.TestJar();"); 61 } 62 63 public void testAmbiguousDirectory() { 64 Path p1 = outDir.resolve("dir1"); 65 compiler.compile(p1, 66 "package p; public class TestAmbiguous {\n" + 67 " public String toString() {\n" + 68 " return \"first\";" + 69 " }\n" + 70 "}"); 71 addToClasspath(compiler.getPath(p1)); 72 Path p2 = outDir.resolve("dir2"); 73 compiler.compile(p2, 74 "package p; public class TestAmbiguous {\n" + 75 " public String toString() {\n" + 76 " return \"second\";" + 77 " }\n" + 78 "}"); 79 addToClasspath(compiler.getPath(p2)); 80 assertEval("new p.TestAmbiguous();", "first"); 81 } 82 83 public void testAmbiguousJar() { 84 Path p1 = outDir.resolve("dir1"); 85 compiler.compile(p1, 86 "package p; public class TestAmbiguous {\n" + 87 " public String toString() {\n" + 88 " return \"first\";" + 89 " }\n" + 90 "}"); 91 String jarName = "test.jar"; 92 compiler.jar(p1, jarName, "p/TestAmbiguous.class"); 93 addToClasspath(compiler.getPath(p1.resolve(jarName))); 94 Path p2 = outDir.resolve("dir2"); 95 compiler.compile(p2, 96 "package p; public class TestAmbiguous {\n" + 97 " public String toString() {\n" + 98 " return \"second\";" + 99 " }\n" + 100 "}"); 101 addToClasspath(compiler.getPath(p2)); 102 assertEval("new p.TestAmbiguous();", "first"); 103 } 104 105 public void testEmptyClassPath() { 106 addToClasspath(""); 107 assertEval("new java.util.ArrayList<String>();"); 108 } 109 110 public void testUnknown() { 111 addToClasspath(compiler.getPath(outDir.resolve("UNKNOWN"))); 112 assertDeclareFail("new Unknown();", "compiler.err.cant.resolve.location"); 113 addToClasspath(compiler.getPath(outDir.resolve("UNKNOWN.jar"))); 114 assertDeclareFail("new Unknown();", "compiler.err.cant.resolve.location"); 115 } 116} 117