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