langtools/test/tools/javac/T6668802.java
author jjg
Thu, 06 Mar 2008 10:25:04 -0800
changeset 167 f4b81c733bea
child 5520 86e4b9a9da40
permissions -rw-r--r--
6668802: javac handles diagnostics for last line badly, if line not terminated by newline Summary: use CharBuffer.limit(), not the length of the backing array Reviewed-by: mcimadamore
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
/*
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
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
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    10
 *
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    11
 * 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
    12
 * 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
    13
 * 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
    14
 * 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
    15
 * accompanied this code).
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    16
 *
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    17
 * 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
    18
 * 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
    19
 * 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
    20
 *
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    23
 * have any questions.
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
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    26
/*
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    27
 * @test
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    28
 * @bug 6668802
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    29
 * @summary javac handles diagnostics for last line badly, if line not terminated by newline
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
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    32
import java.io.*;
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    33
import java.util.*;
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    34
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    35
public class T6668802
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    36
{
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    37
    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
    38
        new T6668802().run();
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
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    41
    void run() throws Exception {
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    42
        String test = "public class Test {";
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    43
        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
    44
        String[] out = compileBadFile(f);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    45
        for (String line: out)
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    46
            System.err.println(">>>" + line + "<<<");
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    47
        if (!out[1].equals(test)) {
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    48
            show("expected", test);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    49
            show("  actual", out[1]);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    50
            throw new Error("test failed");
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
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    54
    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
    55
        File f = new File(path);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    56
        FileWriter out = new FileWriter(f);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    57
        out.write(contents);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    58
        out.close();
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    59
        return f;
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
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    62
    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
    63
        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
    64
        options.add(file.getPath());
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    65
        System.err.println("compile: " + options);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    66
        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
    67
        StringWriter sw = new StringWriter();
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    68
        PrintWriter out = new PrintWriter(sw);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    69
        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
    70
        if (rc == 0)
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    71
            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
    72
        out.close();
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    73
        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
    74
    }
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    75
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    76
    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
    77
        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
    78
    }
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents:
diff changeset
    79
}