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 Test imports
|
35359
|
27 |
* @modules jdk.compiler/com.sun.tools.javac.api
|
|
28 |
* jdk.compiler/com.sun.tools.javac.main
|
33362
|
29 |
* @library /tools/lib
|
|
30 |
* @build KullaTesting TestingInputStream ToolBox ExpectedDiagnostic
|
|
31 |
* @run testng ImportTest
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.nio.file.Path;
|
|
35 |
import java.nio.file.Paths;
|
|
36 |
|
|
37 |
import javax.tools.Diagnostic;
|
|
38 |
|
|
39 |
import jdk.jshell.Snippet;
|
|
40 |
import org.testng.annotations.Test;
|
|
41 |
|
|
42 |
import static jdk.jshell.Snippet.Status.VALID;
|
|
43 |
import static jdk.jshell.Snippet.Status.OVERWRITTEN;
|
|
44 |
import static jdk.jshell.Snippet.SubKind.SINGLE_TYPE_IMPORT_SUBKIND;
|
|
45 |
import static jdk.jshell.Snippet.SubKind.SINGLE_STATIC_IMPORT_SUBKIND;
|
|
46 |
import static jdk.jshell.Snippet.SubKind.TYPE_IMPORT_ON_DEMAND_SUBKIND;
|
|
47 |
import static jdk.jshell.Snippet.SubKind.STATIC_IMPORT_ON_DEMAND_SUBKIND;
|
|
48 |
|
|
49 |
@Test
|
|
50 |
public class ImportTest extends KullaTesting {
|
|
51 |
|
|
52 |
public void testImport() {
|
|
53 |
assertImportKeyMatch("import java.util.List;", "List", SINGLE_TYPE_IMPORT_SUBKIND, added(VALID));
|
|
54 |
assertImportKeyMatch("import java.util.ArrayList;", "ArrayList", SINGLE_TYPE_IMPORT_SUBKIND, added(VALID));
|
|
55 |
assertEval("List<Integer> list = new ArrayList<>();");
|
|
56 |
assertEval("list.add(45);");
|
|
57 |
assertEval("list.size();", "1");
|
|
58 |
}
|
|
59 |
|
|
60 |
public void testImportOnDemand() {
|
|
61 |
assertImportKeyMatch("import java.util.*;", "java.util.*", TYPE_IMPORT_ON_DEMAND_SUBKIND, added(VALID));
|
|
62 |
assertEval("List<Integer> list = new ArrayList<>();");
|
|
63 |
assertEval("list.add(45);");
|
|
64 |
assertEval("list.size();", "1");
|
|
65 |
}
|
|
66 |
|
|
67 |
public void testImportStatic() {
|
|
68 |
assertImportKeyMatch("import static java.lang.Math.PI;", "PI", SINGLE_STATIC_IMPORT_SUBKIND, added(VALID));
|
|
69 |
assertEval("new Double(PI).toString().substring(0, 16).equals(\"3.14159265358979\");", "true");
|
|
70 |
}
|
|
71 |
|
|
72 |
public void testImportStaticOnDemand() {
|
|
73 |
assertImportKeyMatch("import static java.lang.Math.*;", "java.lang.Math.*", STATIC_IMPORT_ON_DEMAND_SUBKIND, added(VALID));
|
|
74 |
assertEval("abs(cos(PI / 2)) < 0.00001;", "true");
|
|
75 |
}
|
|
76 |
|
|
77 |
@Test(enabled = false) // TODO 8129418
|
|
78 |
public void testUnknownPackage() {
|
|
79 |
assertDeclareFail("import unknown.qqq;",
|
|
80 |
new ExpectedDiagnostic("compiler.err.doesnt.exist", 7, 18, 14, -1, -1, Diagnostic.Kind.ERROR));
|
|
81 |
assertDeclareFail("import unknown.*;",
|
|
82 |
new ExpectedDiagnostic("compiler.err.doesnt.exist", 7, 15, 7, -1, -1, Diagnostic.Kind.ERROR));
|
|
83 |
}
|
|
84 |
|
|
85 |
public void testBogusImportIgnoredInFuture() {
|
|
86 |
assertDeclareFail("import unknown.qqq;", "compiler.err.doesnt.exist");
|
|
87 |
assertDeclareFail("import unknown.*;", "compiler.err.doesnt.exist");
|
|
88 |
assertEval("2 + 2;");
|
|
89 |
}
|
|
90 |
|
|
91 |
public void testBadImport() {
|
|
92 |
assertDeclareFail("import static java.lang.reflect.Modifier;",
|
|
93 |
new ExpectedDiagnostic("compiler.err.cant.resolve.location", 14, 31, 23, -1, -1, Diagnostic.Kind.ERROR));
|
|
94 |
}
|
|
95 |
|
|
96 |
public void testBadSyntaxImport() {
|
|
97 |
assertDeclareFail("import not found.*;",
|
|
98 |
new ExpectedDiagnostic("compiler.err.expected", 10, 10, 10, -1, -1, Diagnostic.Kind.ERROR));
|
|
99 |
}
|
|
100 |
|
|
101 |
public void testImportRedefinition() {
|
|
102 |
Compiler compiler = new Compiler();
|
|
103 |
Path path = Paths.get("testImport");
|
|
104 |
compiler.compile(path, "package util; public class ArrayList { public String toString() { return \"MyList\"; } }");
|
|
105 |
compiler.compile(path, "package util; public class A { public static class ArrayList {\n" +
|
|
106 |
"public String toString() { return \"MyInnerList\"; } } }");
|
|
107 |
addToClasspath(compiler.getPath(path));
|
|
108 |
|
|
109 |
assertImportKeyMatch("import util.*;", "util.*", TYPE_IMPORT_ON_DEMAND_SUBKIND, added(VALID));
|
|
110 |
assertEval("new ArrayList();", "MyList", added(VALID));
|
|
111 |
|
|
112 |
Snippet import0 = assertImportKeyMatch("import java.util.ArrayList;", "ArrayList", SINGLE_TYPE_IMPORT_SUBKIND, added(VALID));
|
|
113 |
assertEval("new ArrayList();", "[]");
|
|
114 |
|
|
115 |
Snippet import1 = assertImportKeyMatch("import util.ArrayList;", "ArrayList", SINGLE_TYPE_IMPORT_SUBKIND,
|
|
116 |
ste(MAIN_SNIPPET, VALID, VALID, false, null),
|
|
117 |
ste(import0, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
|
|
118 |
assertEval("new ArrayList();", "MyList");
|
|
119 |
|
|
120 |
Snippet import2 = assertImportKeyMatch("import java.util.ArrayList;", "ArrayList", SINGLE_TYPE_IMPORT_SUBKIND,
|
|
121 |
ste(MAIN_SNIPPET, VALID, VALID, false, null),
|
|
122 |
ste(import1, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
|
|
123 |
assertEval("new ArrayList();", "[]");
|
|
124 |
|
|
125 |
Snippet import3 = assertImportKeyMatch("import util.A.ArrayList;", "ArrayList", SINGLE_TYPE_IMPORT_SUBKIND,
|
|
126 |
ste(MAIN_SNIPPET, VALID, VALID, false, null),
|
|
127 |
ste(import2, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
|
|
128 |
assertEval("new ArrayList();", "MyInnerList");
|
|
129 |
|
|
130 |
Snippet import4 = assertImportKeyMatch("import java.util.ArrayList;", "ArrayList", SINGLE_TYPE_IMPORT_SUBKIND,
|
|
131 |
ste(MAIN_SNIPPET, VALID, VALID, false, null),
|
|
132 |
ste(import3, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
|
|
133 |
assertEval("new ArrayList();", "[]");
|
|
134 |
|
|
135 |
Snippet import5 = assertImportKeyMatch("import static util.A.ArrayList;", "ArrayList", SINGLE_STATIC_IMPORT_SUBKIND,
|
|
136 |
ste(MAIN_SNIPPET, VALID, VALID, false, null),
|
|
137 |
ste(import4, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
|
|
138 |
assertEval("new ArrayList();", "MyInnerList");
|
|
139 |
}
|
|
140 |
|
|
141 |
public void testImportMemberRedefinition() {
|
|
142 |
Compiler compiler = new Compiler();
|
|
143 |
Path path = Paths.get("testImport");
|
|
144 |
compiler.compile(path, "package util; public class A {" +
|
|
145 |
"public static String field = \"A\";" +
|
|
146 |
"public static String method() { return \"A\"; } }");
|
|
147 |
compiler.compile(path, "package util; public class B {" +
|
|
148 |
"public static String field = \"B\";" +
|
|
149 |
"public static String method() { return \"B\"; } }");
|
|
150 |
addToClasspath(compiler.getPath(path));
|
|
151 |
|
|
152 |
assertImportKeyMatch("import static util.B.*;", "util.B.*", STATIC_IMPORT_ON_DEMAND_SUBKIND, added(VALID));
|
|
153 |
assertEval("field;", "\"B\"");
|
|
154 |
assertEval("method();", "\"B\"");
|
|
155 |
|
|
156 |
assertImportKeyMatch("import static util.A.method;", "method", SINGLE_STATIC_IMPORT_SUBKIND, added(VALID));
|
|
157 |
assertEval("field;", "\"B\"");
|
|
158 |
assertEval("method();", "\"A\"");
|
|
159 |
|
|
160 |
assertImportKeyMatch("import static util.A.field;", "field", SINGLE_STATIC_IMPORT_SUBKIND, added(VALID));
|
|
161 |
assertEval("field;", "\"A\"");
|
|
162 |
assertEval("method();", "\"A\"");
|
|
163 |
}
|
|
164 |
}
|