T6306137.java revision 2687:56f8be952a5c
1/*
2 * Copyright (c) 2005, 2014, 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     6306137
27 * @summary JSR 199: encoding option doesn't affect standard file manager
28 * @compile -encoding utf-8 T6306137.java
29 * @run main T6306137
30 * @author  Peter von der Ah\u00e9
31 */
32
33import java.io.File;
34import java.io.IOException;
35import java.nio.charset.Charset;
36import java.nio.file.Files;
37import java.util.ArrayList;
38import java.util.Arrays;
39import java.util.List;
40import javax.tools.*;
41import static java.nio.file.StandardOpenOption.*;
42
43public class T6306137 {
44    boolean error;
45    final StandardJavaFileManager fm;
46    final JavaCompiler compiler;
47    Iterable<? extends JavaFileObject> files;
48    DiagnosticListener<JavaFileObject> dl;
49    final File testFile = new File("Utf8.java");
50
51    T6306137() throws IOException {
52        dl = new DiagnosticListener<JavaFileObject>() {
53                public void report(Diagnostic<? extends JavaFileObject> message) {
54                    if (message.getKind() == Diagnostic.Kind.ERROR)
55                        error = true;
56                    System.out.println(message.getSource()
57                                       +":"+message.getStartPosition()+":"
58                                       +message.getStartPosition()+":"+message.getPosition());
59                    System.out.println(message.toString());
60                    System.out.format("Found problem: %s%n", message.getCode());
61                    System.out.flush();
62                }
63        };
64        compiler = ToolProvider.getSystemJavaCompiler();
65        fm = compiler.getStandardFileManager(dl, null, null);
66        files =
67            fm.getJavaFileObjectsFromFiles(Arrays.asList(testFile));
68        createTestFile();
69    }
70    final void createTestFile() throws IOException {
71        List<String> scratch = new ArrayList<>();
72        scratch.add("// @author Peter von der Ah" + (char)0xe9);
73        scratch.add("class Utf8{}");
74        Files.write(testFile.toPath(), scratch, Charset.forName("UTF-8"),
75                CREATE, TRUNCATE_EXISTING);
76    }
77    void test(String encoding, boolean good) {
78        error = false;
79        Iterable<String> args = Arrays.asList("-encoding", encoding, "-d", ".");
80        compiler.getTask(null, fm, dl, args, null, files).call();
81        if (error == good) {
82            if (error) {
83                throw new AssertionError("Error reported");
84            } else {
85                throw new AssertionError("No error reported");
86            }
87        }
88    }
89
90    void close() throws IOException {
91        fm.close();
92    }
93
94    public static void main(String[] args) throws IOException {
95        T6306137 self = new T6306137();
96        try {
97            self.test("utf-8", true);
98            self.test("ascii", false);
99            self.test("utf-8", true);
100        } finally {
101            self.close();
102        }
103    }
104}
105