T8132857.java revision 3503:c06787799b4b
1/* 2 * Copyright (c) 2016, 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 8132857 27 * @summary Verify an up-to-date name for UTF-8 encoding is provided in error messages. 28 * @modules jdk.compiler 29 */ 30 31import java.io.*; 32import java.nio.charset.Charset; 33 34public class T8132857 { 35 public static void main(String... args) throws Exception{ 36 new T8132857().run(); 37 } 38 39 void run() throws IOException { 40 if (!Charset.defaultCharset().equals(Charset.forName("UTF-8"))) { 41 System.err.println("skipping test, default charset is not UTF-8"); 42 return; 43 } 44 45 File src = new File("src"); 46 src.mkdirs(); 47 try (OutputStream out = new FileOutputStream(new File(src, "Test.java"))) { 48 out.write('/'); 49 out.write('/'); 50 out.write(0b1100_0000); 51 out.write('a'); 52 } 53 54 try (StringWriter out = new StringWriter(); PrintWriter pw = new PrintWriter(out)) { 55 int rc = com.sun.tools.javac.Main.compile(new String[] {"-XDrawDiagnostics", "src/Test.java"}, pw); 56 57 pw.flush(); 58 59 String lineSeparator = System.getProperty("line.separator"); 60 String expected = 61 "Test.java:1:3: compiler.err.illegal.char.for.encoding: C0, UTF-8" + lineSeparator + 62 "1 error" + lineSeparator; 63 String actual = out.toString(); 64 65 System.err.println(actual); 66 67 if (rc == 0) { 68 throw new Error("compilation unexpectedly passed: " + rc); 69 } 70 71 if (!expected.equals(actual)) { 72 throw new Error("unexpected output: " + actual); 73 } 74 } 75 } 76 77} 78 79