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