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