langtools/test/tools/javac/api/6406133/T6406133.java
changeset 1471 57506cdfb7b4
child 5520 86e4b9a9da40
equal deleted inserted replaced
1470:6ff8524783fa 1471:57506cdfb7b4
       
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any 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  */
       
    31 
       
    32 import javax.tools.*;
       
    33 import javax.annotation.processing.*;
       
    34 import javax.lang.model.element.*;
       
    35 import java.util.*;
       
    36 import java.io.*;
       
    37 
       
    38 public class T6406133 extends ToolTester {
       
    39 
       
    40     List<Locale> locales = Arrays.asList(Locale.US, Locale.JAPAN, Locale.CHINA);
       
    41 
       
    42     class DiagnosticTester implements DiagnosticListener<JavaFileObject> {
       
    43         Locale locale;
       
    44         String result;
       
    45 
       
    46         DiagnosticTester(Locale locale) {
       
    47             this.locale = locale;
       
    48         }
       
    49         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
       
    50             result = diagnostic.getMessage(locale); //6406133
       
    51         }
       
    52     }
       
    53 
       
    54     class ProcessorTester extends AbstractProcessor {
       
    55 
       
    56         Locale locale;
       
    57 
       
    58         public Set<String> getSupportedAnnotationTypes() {
       
    59             return new HashSet<String>(Arrays.asList("*"));
       
    60         }
       
    61 
       
    62         public void init(ProcessingEnvironment env) {
       
    63             locale = env.getLocale();
       
    64         }
       
    65 
       
    66         public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
       
    67             return true;
       
    68         }
       
    69     }
       
    70 
       
    71     void compare(Locale loc1, Locale loc2, boolean useListener) {
       
    72         String res1 = exec(useListener, loc1);
       
    73         String res2 = exec(useListener, loc2);
       
    74         boolean success = (loc1.equals(loc2) && res1.equals(res2)) ||
       
    75                           (!loc1.equals(loc2) && !res1.equals(res2));
       
    76         if (!success)
       
    77             throw new AssertionError("Error in diagnostic localization");
       
    78     }
       
    79 
       
    80     String exec(boolean useListener, Locale locale) {
       
    81         final Iterable<? extends JavaFileObject> compilationUnits =
       
    82             fm.getJavaFileObjects(new File(test_src, "Erroneous.java"));
       
    83         StringWriter pw = new StringWriter();
       
    84         DiagnosticTester listener = useListener ? new DiagnosticTester(locale) : null;
       
    85         ProcessorTester processor = new ProcessorTester();
       
    86         task = tool.getTask(pw, fm, listener, null, null, compilationUnits);
       
    87         task.setProcessors(Arrays.asList(processor));
       
    88         task.setLocale(locale); //6443132
       
    89         task.call();
       
    90         if (!processor.locale.equals(locale))
       
    91             throw new AssertionError("Error in diagnostic localization during annotation processing");
       
    92         String res = useListener ? listener.result : pw.toString();
       
    93         System.err.println("[locale:"+ locale + ", listener:" + useListener + "] " +res);
       
    94         return res;
       
    95     }
       
    96 
       
    97     void test() {
       
    98         for (Locale l1 : locales) {
       
    99             for (Locale l2 : locales) {
       
   100                 compare(l1, l2, true);
       
   101                 compare(l1, l2, false);
       
   102             }
       
   103         }
       
   104     }
       
   105 
       
   106     public static void main(String... args) throws Exception {
       
   107         new T6406133().test();
       
   108     }
       
   109 }