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