langtools/test/tools/javac/T6668802.java
author alundblad
Wed, 26 Aug 2015 09:02:02 +0200
changeset 32337 c9d3ab9f601c
parent 30730 d3ce7619db2c
permissions -rw-r--r--
8133671: langtools tests have bad license Summary: Dropped classpath exception from copyright notice in tests. Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
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.
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
     4
 *
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
32337
c9d3ab9f601c 8133671: langtools tests have bad license
alundblad
parents: 30730
diff changeset
     7
 * published by the Free Software Foundation.
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
     8
 *
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    13
 * accompanied this code).
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    14
 *
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 167
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 167
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 167
diff changeset
    21
 * questions.
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    22
 */
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    23
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    24
/*
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    25
 * @test
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    26
 * @bug 6668802
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    27
 * @summary javac handles diagnostics for last line badly, if line not terminated by newline
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 5520
diff changeset
    28
 * @modules jdk.compiler
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    29
 */
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    30
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    31
import java.io.*;
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    32
import java.util.*;
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    33
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    34
public class T6668802
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    35
{
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    36
    public static void main(String[] args) throws Exception {
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    37
        new T6668802().run();
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    38
    }
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    39
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    40
    void run() throws Exception {
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    41
        String test = "public class Test {";
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    42
        File f = writeTestFile("Test.java", test);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    43
        String[] out = compileBadFile(f);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    44
        for (String line: out)
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    45
            System.err.println(">>>" + line + "<<<");
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    46
        if (!out[1].equals(test)) {
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    47
            show("expected", test);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    48
            show("  actual", out[1]);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    49
            throw new Error("test failed");
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    50
        }
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    51
    }
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    52
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    53
    File writeTestFile(String path, String contents) throws IOException {
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    54
        File f = new File(path);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    55
        FileWriter out = new FileWriter(f);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    56
        out.write(contents);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    57
        out.close();
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    58
        return f;
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    59
    }
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    60
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    61
    String[] compileBadFile(File file) throws IOException {
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    62
        List<String> options = new ArrayList<String>();
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    63
        options.add(file.getPath());
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    64
        System.err.println("compile: " + options);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    65
        String[] opts = options.toArray(new String[options.size()]);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    66
        StringWriter sw = new StringWriter();
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    67
        PrintWriter out = new PrintWriter(sw);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    68
        int rc = com.sun.tools.javac.Main.compile(opts, out);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    69
        if (rc == 0)
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    70
            throw new Error("compilation succeeded unexpectedly");
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    71
        out.close();
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    72
        return sw.toString().split("[\n\r]+");
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    73
    }
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    74
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    75
    void show(String prefix, String text) {
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    76
        System.err.println(prefix + ": (" + text.length() + ") " + text);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    77
    }
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    78
}