Test.java revision 2689:f839b50088bc
1/*
2 * Copyright (c) 2006, 2014, 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     6427274 6347778 6469079
27 * @summary Various bugs fixed while writing Compiler API Guide
28 * @author  Peter von der Ah\u0081
29 * @library ../lib
30 * @build ToolTester
31 * @compile Test.java
32 * @run main Test
33 */
34
35import javax.tools.*;
36import java.io.File;
37import java.util.Collections;
38
39public class Test extends ToolTester {
40
41    public boolean success = false;
42
43    class DiagnosticTester implements DiagnosticListener<JavaFileObject> {
44        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
45            if (diagnostic.getKind() == Diagnostic.Kind.NOTE) {
46                try {
47                    // 6427274: FileObject.openReader throws exception
48                    // 6347778: getSource() returns null for notes
49                    diagnostic.getSource().openReader(true).getClass();
50                } catch (Exception ex) {
51                    throw new AssertionError(ex);
52                }
53                success = true;
54            }
55        }
56    }
57
58    void test(String... args) throws Exception {
59        final Iterable<? extends JavaFileObject> compilationUnits =
60            fm.getJavaFileObjects(new File(test_src, "TestMe.java"));
61        task = tool.getTask(null, fm, new DiagnosticTester(), null, null, compilationUnits);
62        if (!task.call())
63            throw new AssertionError("Compilation failed");
64        if (!success)
65            throw new AssertionError("Did not see a NOTE");
66        // 6427274: openReader throws exception
67        fm.getFileForInput(StandardLocation.PLATFORM_CLASS_PATH,
68                           "java.lang",
69                           "Object.class").openReader(true).getClass();
70        DiagnosticCollector<JavaFileObject> diags = new DiagnosticCollector<JavaFileObject>();
71        task = tool.getTask(null, fm, diags, Collections.singleton("-Xlint:all"),
72                            null, compilationUnits);
73        if (!task.call())
74            throw new AssertionError("Compilation failed");
75        String msg = diags.getDiagnostics().get(0).getMessage(null);
76        long lineno = diags.getDiagnostics().get(0).getLineNumber();
77        if (msg.contains(":"+lineno+":"))
78            // 6469079: Diagnostic.getMessage(Locale) includes line numbers
79            throw new AssertionError(msg);
80    }
81
82    public static void main(String... args) throws Exception {
83        try (Test t = new Test()) {
84            t.test(args);
85        }
86    }
87
88}
89