CompletionFailureDuringImport.java revision 3002:7eef740c1482
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 * @bug 8131915
27 * @summary Verify that CompletionFailure thrown during listing of import content is handled properly.
28 * @library /tools/lib
29 */
30
31import java.nio.file.Files;
32import java.nio.file.Paths;
33import java.util.Arrays;
34import java.util.List;
35
36public class CompletionFailureDuringImport {
37    public static void main(String... args) throws Exception {
38        new CompletionFailureDuringImport().run();
39    }
40
41    ToolBox tb = new ToolBox();
42
43    void run() throws Exception {
44        tb.new JavacTask()
45          .outdir(".")
46          .sources("package p; public class Super { public static final int I = 0; }",
47                   "package p; public class Sub extends Super { }")
48          .run()
49          .writeAll();
50
51        Files.delete(Paths.get(".", "p", "Super.class"));
52
53        doTest("import static p.Sub.*;",
54               "",
55               "Test.java:1:1: compiler.err.cant.access: p.Super, (compiler.misc.class.file.not.found: p.Super)",
56               "1 error");
57        doTest("import static p.Sub.I;",
58               "",
59               "Test.java:1:1: compiler.err.cant.access: p.Super, (compiler.misc.class.file.not.found: p.Super)",
60               "1 error");
61        doTest("import static p.Sub.*;",
62               "int i = I;",
63               "Test.java:1:1: compiler.err.cant.access: p.Super, (compiler.misc.class.file.not.found: p.Super)",
64               "Test.java:1:52: compiler.err.cant.resolve.location: kindname.variable, I, , , (compiler.misc.location: kindname.class, Test, null)",
65               "2 errors");
66        doTest("import static p.Sub.I;",
67               "int i = I;",
68               "Test.java:1:1: compiler.err.cant.access: p.Super, (compiler.misc.class.file.not.found: p.Super)",
69               "Test.java:1:52: compiler.err.cant.resolve.location: kindname.variable, I, , , (compiler.misc.location: kindname.class, Test, null)",
70               "2 errors");
71    }
72
73    void doTest(String importText, String useText, String... expectedOutput) {
74        List<String> log = tb.new JavacTask()
75                .classpath(".")
76                .sources(importText + " public class Test { " + useText + " }")
77                .options("-XDrawDiagnostics")
78                .run(ToolBox.Expect.FAIL)
79                .writeAll()
80                .getOutputLines(ToolBox.OutputKind.DIRECT);
81
82        if (!log.equals(Arrays.asList(expectedOutput))) {
83            throw new AssertionError("Unexpected output: " + log);
84        }
85    }
86}
87