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