T6406133.java revision 1732:e39669aea0bd
1/*
2 * Copyright (c) 2008, 2013, 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     6443132 6406133 6597678
27 * @summary Compiler API ignores locale settings
28 * @author  Maurizio Cimadamore
29 * @library ../lib
30 * @build ToolTester
31 * @run main T6406133
32 */
33
34import javax.tools.*;
35import javax.annotation.processing.*;
36import javax.lang.model.element.*;
37import java.util.*;
38import java.io.*;
39
40public class T6406133 extends ToolTester {
41
42    List<Locale> locales = Arrays.asList(Locale.US, Locale.JAPAN, Locale.CHINA);
43
44    class DiagnosticTester implements DiagnosticListener<JavaFileObject> {
45        Locale locale;
46        String result;
47
48        DiagnosticTester(Locale locale) {
49            this.locale = locale;
50        }
51        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
52            result = diagnostic.getMessage(locale); //6406133
53        }
54    }
55
56    class ProcessorTester extends AbstractProcessor {
57
58        Locale locale;
59
60        public Set<String> getSupportedAnnotationTypes() {
61            return new HashSet<String>(Arrays.asList("*"));
62        }
63
64        public void init(ProcessingEnvironment env) {
65            locale = env.getLocale();
66        }
67
68        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
69            return true;
70        }
71    }
72
73    void compare(Locale loc1, Locale loc2, boolean useListener) {
74        String res1 = exec(useListener, loc1);
75        String res2 = exec(useListener, loc2);
76        boolean success = (loc1.equals(loc2) && res1.equals(res2)) ||
77                          (!loc1.equals(loc2) && !res1.equals(res2));
78        if (!success)
79            throw new AssertionError("Error in diagnostic localization");
80    }
81
82    String exec(boolean useListener, Locale locale) {
83        final Iterable<? extends JavaFileObject> compilationUnits =
84            fm.getJavaFileObjects(new File(test_src, "Erroneous.java"));
85        StringWriter pw = new StringWriter();
86        DiagnosticTester listener = useListener ? new DiagnosticTester(locale) : null;
87        ProcessorTester processor = new ProcessorTester();
88        task = tool.getTask(pw, fm, listener, null, null, compilationUnits);
89        task.setProcessors(Arrays.asList(processor));
90        task.setLocale(locale); //6443132
91        task.call();
92        if (!processor.locale.equals(locale))
93            throw new AssertionError("Error in diagnostic localization during annotation processing");
94        String res = useListener ? listener.result : pw.toString();
95        System.err.println("[locale:"+ locale + ", listener:" + useListener + "] " +res);
96        return res;
97    }
98
99    void test() {
100        for (Locale l1 : locales) {
101            for (Locale l2 : locales) {
102                compare(l1, l2, true);
103                compare(l1, l2, false);
104            }
105        }
106    }
107
108    public static void main(String... args) throws Exception {
109        new T6406133().test();
110    }
111}
112