langtools/test/tools/javac/T6668802.java
changeset 167 f4b81c733bea
child 5520 86e4b9a9da40
equal deleted inserted replaced
166:9f5e9d551da1 167:f4b81c733bea
       
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 /*
       
    27  * @test
       
    28  * @bug 6668802
       
    29  * @summary javac handles diagnostics for last line badly, if line not terminated by newline
       
    30  */
       
    31 
       
    32 import java.io.*;
       
    33 import java.util.*;
       
    34 
       
    35 public class T6668802
       
    36 {
       
    37     public static void main(String[] args) throws Exception {
       
    38         new T6668802().run();
       
    39     }
       
    40 
       
    41     void run() throws Exception {
       
    42         String test = "public class Test {";
       
    43         File f = writeTestFile("Test.java", test);
       
    44         String[] out = compileBadFile(f);
       
    45         for (String line: out)
       
    46             System.err.println(">>>" + line + "<<<");
       
    47         if (!out[1].equals(test)) {
       
    48             show("expected", test);
       
    49             show("  actual", out[1]);
       
    50             throw new Error("test failed");
       
    51         }
       
    52     }
       
    53 
       
    54     File writeTestFile(String path, String contents) throws IOException {
       
    55         File f = new File(path);
       
    56         FileWriter out = new FileWriter(f);
       
    57         out.write(contents);
       
    58         out.close();
       
    59         return f;
       
    60     }
       
    61 
       
    62     String[] compileBadFile(File file) throws IOException {
       
    63         List<String> options = new ArrayList<String>();
       
    64         options.add(file.getPath());
       
    65         System.err.println("compile: " + options);
       
    66         String[] opts = options.toArray(new String[options.size()]);
       
    67         StringWriter sw = new StringWriter();
       
    68         PrintWriter out = new PrintWriter(sw);
       
    69         int rc = com.sun.tools.javac.Main.compile(opts, out);
       
    70         if (rc == 0)
       
    71             throw new Error("compilation succeeded unexpectedly");
       
    72         out.close();
       
    73         return sw.toString().split("[\n\r]+");
       
    74     }
       
    75 
       
    76     void show(String prefix, String text) {
       
    77         System.err.println(prefix + ": (" + text.length() + ") " + text);
       
    78     }
       
    79 }