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