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

Wed, 08 Oct 2014 14:16:40 -0700

author
asaha
date
Wed, 08 Oct 2014 14:16:40 -0700
changeset 2586
f5e5ca7505e2
parent 1733
e39669aea0bd
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

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

mercurial