1 /*
2  * Copyright (c) 2008, 2015, 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 6668802
27  * @summary javac handles diagnostics for last line badly, if line not terminated by newline
28  * @modules jdk.compiler
29  */
30 
31 import java.io.*;
32 import java.util.*;
33 
34 public class T6668802
35 {
main(String[] args)36     public static void main(String[] args) throws Exception {
37         new T6668802().run();
38     }
39 
run()40     void run() throws Exception {
41         String test = "public class Test {";
42         File f = writeTestFile("Test.java", test);
43         String[] out = compileBadFile(f);
44         for (String line: out)
45             System.err.println(">>>" + line + "<<<");
46         if (!out[1].equals(test)) {
47             show("expected", test);
48             show("  actual", out[1]);
49             throw new Error("test failed");
50         }
51     }
52 
writeTestFile(String path, String contents)53     File writeTestFile(String path, String contents) throws IOException {
54         File f = new File(path);
55         FileWriter out = new FileWriter(f);
56         out.write(contents);
57         out.close();
58         return f;
59     }
60 
compileBadFile(File file)61     String[] compileBadFile(File file) throws IOException {
62         List<String> options = new ArrayList<String>();
63         options.add(file.getPath());
64         System.err.println("compile: " + options);
65         String[] opts = options.toArray(new String[options.size()]);
66         StringWriter sw = new StringWriter();
67         PrintWriter out = new PrintWriter(sw);
68         int rc = com.sun.tools.javac.Main.compile(opts, out);
69         if (rc == 0)
70             throw new Error("compilation succeeded unexpectedly");
71         out.close();
72         return sw.toString().split("[\n\r]+");
73     }
74 
show(String prefix, String text)75     void show(String prefix, String text) {
76         System.err.println(prefix + ": (" + text.length() + ") " + text);
77     }
78 }
79