langtools/test/tools/javac/6668794/badClass/Test.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 5520 86e4b9a9da40
child 32337 c9d3ab9f601c
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 5520
diff changeset
     2
 * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     4
 *
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2212
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2212
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    10
 *
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    15
 * accompanied this code).
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    16
 *
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2212
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2212
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2212
diff changeset
    23
 * questions.
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    24
 */
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    25
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    26
/*
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    27
 * @test
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    28
 * @bug 6668794 6668796
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    29
 * @summary javac puts localized text in raw diagnostics
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    30
 *      bad diagnostic "bad class file" given for source files
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 5520
diff changeset
    31
 * @modules jdk.compiler
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    32
 */
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    33
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    34
import java.io.*;
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    35
import java.util.*;
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    36
import javax.tools.*;
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    37
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    38
public class Test {
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    39
    public static void main(String[] args) throws Exception {
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    40
        new Test().run();
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    41
    }
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    42
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    43
    void run() throws Exception {
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    44
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    45
        // compile q.A then move it to p.A
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    46
        compile("A.java");
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    47
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    48
        File p = new File("p");
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    49
        p.mkdirs();
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    50
        new File("q/A.class").renameTo(new File("p/A.class"));
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    51
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    52
        // compile B against p.A
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    53
        String[] out = compile("B.java");
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    54
        if (out.length == 0)
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    55
            throw new Error("no diagnostics generated");
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    56
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    57
        String expected = "B.java:6:6: compiler.err.cant.access: p.A, " +
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 169
diff changeset
    58
            "(compiler.misc.bad.class.file.header: A.class, " +
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 169
diff changeset
    59
            "(compiler.misc.class.file.wrong.class: q.A))";
169
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    60
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    61
        if (!out[0].equals(expected)) {
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    62
            System.err.println("expected: " + expected);
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    63
            System.err.println("   found: " + out[0]);
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    64
            throw new Error("test failed");
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    65
        }
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    66
    }
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    67
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    68
    String[] compile(String file) {
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    69
        String[] options = {
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    70
            "-XDrawDiagnostics",
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    71
            "-d", ".",
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    72
            "-classpath", ".",
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    73
            new File(testSrc, file).getPath()
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    74
        };
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    75
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    76
        System.err.println("compile: " + Arrays.asList(options));
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    77
        StringWriter sw = new StringWriter();
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    78
        PrintWriter out = new PrintWriter(sw);
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    79
        int rc = com.sun.tools.javac.Main.compile(options, out);
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    80
        out.close();
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    81
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    82
        String outText = sw.toString();
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    83
        System.err.println(outText);
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    84
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    85
        return sw.toString().split("[\\r\\n]+");
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    86
    }
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    87
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    88
    File testSrc = new File(System.getProperty("test.src", "."));
ff76730e430e 6668794: javac puts localized text in raw diagnostics
jjg
parents:
diff changeset
    89
}