2 *******************************************************************************
3 * Copyright (C) 2008-2012, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.localespi;
9 import java.util.Locale;
11 import com.ibm.icu.util.ULocale;
12 import com.ibm.icu.util.ULocale.Builder;
14 public class TestUtil {
16 static final String ICU_VARIANT = "ICU4J";
17 private static final String ICU_VARIANT_SUFFIX = "_ICU4J";
19 public static Locale toICUExtendedLocale(Locale locale) {
20 if (isICUExtendedLocale(locale)) {
24 String variant = locale.getVariant();
25 variant = variant.length() == 0 ? ICU_VARIANT : variant + ICU_VARIANT_SUFFIX;
27 // We once convert Locale to ULocale, then update variant
28 // field. We could do this using Locale APIs, but have to
29 // use a lot of reflections, because the test code should
31 ULocale uloc = ULocale.forLocale(locale);
32 if (uloc.getScript().length() == 0) {
33 return new Locale(locale.getLanguage(), locale.getCountry(), variant);
36 // For preserving JDK Locale's script, we cannot use
37 // the regular Locale constructor.
38 ULocale modUloc = null;
39 Builder locBld = new Builder();
41 locBld.setLocale(uloc);
42 locBld.setVariant(variant);
43 modUloc = locBld.build();
44 return modUloc.toLocale();
45 } catch (Exception e) {
46 // hmm, it should not happen
47 throw new RuntimeException(e);
51 public static boolean isICUExtendedLocale(Locale locale) {
52 String variant = locale.getVariant();
53 if (variant.equals(ICU_VARIANT) || variant.endsWith(ICU_VARIANT_SUFFIX)) {
59 public static boolean equals(Object o1, Object o2) {
60 if (o1 == null && o2 == null) {
63 if (o1 == null || o2 == null) {
69 private static final boolean SUNJRE;
70 private static final boolean IBMJRE;
73 String javaVendor = System.getProperty("java.vendor");
74 if (javaVendor != null) {
75 if (javaVendor.indexOf("Sun") >= 0) {
78 } else if (javaVendor.indexOf("IBM") >= 0) {
91 public static boolean isSUNJRE() {
94 public static boolean isIBMJRE() {
101 * The ICU4J locale spi test cases reports many errors on IBM Java 6. There are two kinds
102 * of problems observed and both of them look like implementation problems in IBM Java 6.
104 * - When a locale has variant field (for example, sr_RS_Cyrl, de_DE_PREEURO), adding ICU
105 * suffix in the variant field (for example, sr_RS_Cyrl_ICU, de_DE_PREEURO_ICU) has no effects.
106 * For these locales, IBM JRE 6 ignores installed Locale providers.
108 * - For "sh" sublocales with "ICU" variant (for example, sh__ICU, sh_CS_ICU), IBM JRE 6 also
109 * ignores installed ICU locale providers. Probably, "sh" is internally mapped to "sr_RS_Cyrl"
110 * internally before locale look up.
112 * For now, we exclude these problematic locales from locale spi test cases on IBM Java 6.
114 public static boolean isProblematicIBMLocale(Locale loc) {
118 if (loc.getLanguage().equals("sh")) {
121 String variant = loc.getVariant();
122 if (variant.startsWith("EURO") || variant.startsWith("PREEURO")
123 || variant.startsWith("Cyrl") || variant.startsWith("Latn")) {