test/tools/javac/api/6406133/T6406133.java

Thu, 26 Aug 2010 15:17:17 -0700

author
jjg
date
Thu, 26 Aug 2010 15:17:17 -0700
changeset 659
cfd047f3cf60
parent 554
9d9f26857129
child 1733
e39669aea0bd
permissions
-rw-r--r--

6604599: ToolProvider should be less compiler-specific
Reviewed-by: darcy

mcimadamore@136 1 /*
ohair@554 2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
mcimadamore@136 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@136 4 *
mcimadamore@136 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@136 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@136 7 * published by the Free Software Foundation.
mcimadamore@136 8 *
mcimadamore@136 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@136 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@136 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@136 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@136 13 * accompanied this code).
mcimadamore@136 14 *
mcimadamore@136 15 * You should have received a copy of the GNU General Public License version
mcimadamore@136 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@136 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@136 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
mcimadamore@136 22 */
mcimadamore@136 23
mcimadamore@136 24 /*
mcimadamore@136 25 * @test
mcimadamore@136 26 * @bug 6443132 6406133 6597678
mcimadamore@136 27 * @summary Compiler API ignores locale settings
mcimadamore@136 28 * @author Maurizio Cimadamore
mcimadamore@136 29 * @library ../lib
mcimadamore@136 30 */
mcimadamore@136 31
mcimadamore@136 32 import javax.tools.*;
mcimadamore@136 33 import javax.annotation.processing.*;
mcimadamore@136 34 import javax.lang.model.element.*;
mcimadamore@136 35 import java.util.*;
mcimadamore@136 36 import java.io.*;
mcimadamore@136 37
mcimadamore@136 38 public class T6406133 extends ToolTester {
mcimadamore@136 39
mcimadamore@136 40 List<Locale> locales = Arrays.asList(Locale.US, Locale.JAPAN, Locale.CHINA);
mcimadamore@136 41
mcimadamore@136 42 class DiagnosticTester implements DiagnosticListener<JavaFileObject> {
mcimadamore@136 43 Locale locale;
mcimadamore@136 44 String result;
mcimadamore@136 45
mcimadamore@136 46 DiagnosticTester(Locale locale) {
mcimadamore@136 47 this.locale = locale;
mcimadamore@136 48 }
mcimadamore@136 49 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
mcimadamore@136 50 result = diagnostic.getMessage(locale); //6406133
mcimadamore@136 51 }
mcimadamore@136 52 }
mcimadamore@136 53
mcimadamore@136 54 class ProcessorTester extends AbstractProcessor {
mcimadamore@136 55
mcimadamore@136 56 Locale locale;
mcimadamore@136 57
mcimadamore@136 58 public Set<String> getSupportedAnnotationTypes() {
mcimadamore@136 59 return new HashSet<String>(Arrays.asList("*"));
mcimadamore@136 60 }
mcimadamore@136 61
mcimadamore@136 62 public void init(ProcessingEnvironment env) {
mcimadamore@136 63 locale = env.getLocale();
mcimadamore@136 64 }
mcimadamore@136 65
mcimadamore@136 66 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
mcimadamore@136 67 return true;
mcimadamore@136 68 }
mcimadamore@136 69 }
mcimadamore@136 70
mcimadamore@136 71 void compare(Locale loc1, Locale loc2, boolean useListener) {
mcimadamore@136 72 String res1 = exec(useListener, loc1);
mcimadamore@136 73 String res2 = exec(useListener, loc2);
mcimadamore@136 74 boolean success = (loc1.equals(loc2) && res1.equals(res2)) ||
mcimadamore@136 75 (!loc1.equals(loc2) && !res1.equals(res2));
mcimadamore@136 76 if (!success)
mcimadamore@136 77 throw new AssertionError("Error in diagnostic localization");
mcimadamore@136 78 }
mcimadamore@136 79
mcimadamore@136 80 String exec(boolean useListener, Locale locale) {
mcimadamore@136 81 final Iterable<? extends JavaFileObject> compilationUnits =
mcimadamore@136 82 fm.getJavaFileObjects(new File(test_src, "Erroneous.java"));
mcimadamore@136 83 StringWriter pw = new StringWriter();
mcimadamore@136 84 DiagnosticTester listener = useListener ? new DiagnosticTester(locale) : null;
mcimadamore@136 85 ProcessorTester processor = new ProcessorTester();
mcimadamore@136 86 task = tool.getTask(pw, fm, listener, null, null, compilationUnits);
mcimadamore@136 87 task.setProcessors(Arrays.asList(processor));
mcimadamore@136 88 task.setLocale(locale); //6443132
mcimadamore@136 89 task.call();
mcimadamore@136 90 if (!processor.locale.equals(locale))
mcimadamore@136 91 throw new AssertionError("Error in diagnostic localization during annotation processing");
mcimadamore@136 92 String res = useListener ? listener.result : pw.toString();
mcimadamore@136 93 System.err.println("[locale:"+ locale + ", listener:" + useListener + "] " +res);
mcimadamore@136 94 return res;
mcimadamore@136 95 }
mcimadamore@136 96
mcimadamore@136 97 void test() {
mcimadamore@136 98 for (Locale l1 : locales) {
mcimadamore@136 99 for (Locale l2 : locales) {
mcimadamore@136 100 compare(l1, l2, true);
mcimadamore@136 101 compare(l1, l2, false);
mcimadamore@136 102 }
mcimadamore@136 103 }
mcimadamore@136 104 }
mcimadamore@136 105
mcimadamore@136 106 public static void main(String... args) throws Exception {
mcimadamore@136 107 new T6406133().test();
mcimadamore@136 108 }
mcimadamore@136 109 }

mercurial