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