1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2008-2015, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.dev.test.localespi; 10 11 import java.util.HashSet; 12 import java.util.Locale; 13 import java.util.Set; 14 15 import com.ibm.icu.util.ULocale; 16 import com.ibm.icu.util.ULocale.Builder; 17 18 public class TestUtil { 19 20 static final String ICU_VARIANT = "ICU4J"; 21 private static final String ICU_VARIANT_SUFFIX = "_ICU4J"; 22 toICUExtendedLocale(Locale locale)23 public static Locale toICUExtendedLocale(Locale locale) { 24 if (isICUExtendedLocale(locale)) { 25 return locale; 26 } 27 28 String variant = locale.getVariant(); 29 variant = variant.length() == 0 ? ICU_VARIANT : variant + ICU_VARIANT_SUFFIX; 30 31 // We once convert Locale to ULocale, then update variant 32 // field. We could do this using Locale APIs, but have to 33 // use a lot of reflections, because the test code should 34 // also run on JRE 6. 35 ULocale uloc = ULocale.forLocale(locale); 36 if (uloc.getScript().length() == 0) { 37 return new Locale(locale.getLanguage(), locale.getCountry(), variant); 38 } 39 40 // For preserving JDK Locale's script, we cannot use 41 // the regular Locale constructor. 42 ULocale modUloc = null; 43 Builder locBld = new Builder(); 44 try { 45 locBld.setLocale(uloc); 46 locBld.setVariant(variant); 47 modUloc = locBld.build(); 48 return modUloc.toLocale(); 49 } catch (Exception e) { 50 // hmm, it should not happen 51 throw new RuntimeException(e); 52 } 53 } 54 isICUExtendedLocale(Locale locale)55 public static boolean isICUExtendedLocale(Locale locale) { 56 String variant = locale.getVariant(); 57 if (variant.equals(ICU_VARIANT) || variant.endsWith(ICU_VARIANT_SUFFIX)) { 58 return true; 59 } 60 return false; 61 } 62 equals(Object o1, Object o2)63 public static boolean equals(Object o1, Object o2) { 64 if (o1 == null && o2 == null) { 65 return true; 66 } 67 if (o1 == null || o2 == null) { 68 return false; 69 } 70 return o1.equals(o2); 71 } 72 73 private static final boolean SUNJRE; 74 private static final boolean IBMJRE; 75 76 static { 77 String javaVendor = System.getProperty("java.vendor"); 78 if (javaVendor != null) { 79 if (javaVendor.indexOf("Sun") >= 0) { 80 SUNJRE = true; 81 IBMJRE = false; 82 } else if (javaVendor.indexOf("IBM") >= 0) { 83 SUNJRE = false; 84 IBMJRE = true; 85 } else { 86 SUNJRE = false; 87 IBMJRE = false; 88 } 89 } else { 90 SUNJRE = false; 91 IBMJRE = false; 92 } 93 } 94 isSUNJRE()95 public static boolean isSUNJRE() { 96 return SUNJRE; 97 } isIBMJRE()98 public static boolean isIBMJRE() { 99 return IBMJRE; 100 } 101 102 private static final Set<Locale> EXCLUDED_LOCALES = new HashSet<Locale>(); 103 static { 104 EXCLUDED_LOCALES.add(Locale.ROOT); 105 // de-GR is supported by Java 8, but not supported by CLDR / ICU EXCLUDED_LOCALES.add(new Locale("de", "GR"))106 EXCLUDED_LOCALES.add(new Locale("de", "GR")); 107 } 108 109 /* 110 * Checks if the given locale is excluded from locale SPI test 111 */ isExcluded(Locale loc)112 public static boolean isExcluded(Locale loc) { 113 if (EXCLUDED_LOCALES.contains(loc)) { 114 return true; 115 } 116 return isProblematicIBMLocale(loc); 117 } 118 119 /* 120 * Ticket#6368 121 * 122 * The ICU4J locale spi test cases reports many errors on IBM Java 6. There are two kinds 123 * of problems observed and both of them look like implementation problems in IBM Java 6. 124 * 125 * - When a locale has variant field (for example, sr_RS_Cyrl, de_DE_PREEURO), adding ICU 126 * suffix in the variant field (for example, sr_RS_Cyrl_ICU, de_DE_PREEURO_ICU) has no effects. 127 * For these locales, IBM JRE 6 ignores installed Locale providers. 128 * 129 * - For "sh" sublocales with "ICU" variant (for example, sh__ICU, sh_CS_ICU), IBM JRE 6 also 130 * ignores installed ICU locale providers. Probably, "sh" is internally mapped to "sr_RS_Cyrl" 131 * internally before locale look up. 132 * 133 * For now, we exclude these problematic locales from locale spi test cases on IBM Java 6. 134 */ isProblematicIBMLocale(Locale loc)135 public static boolean isProblematicIBMLocale(Locale loc) { 136 if (!isIBMJRE()) { 137 return false; 138 } 139 if (loc.getLanguage().equals("sh")) { 140 return true; 141 } 142 String variant = loc.getVariant(); 143 if (variant.startsWith("EURO") || variant.startsWith("PREEURO") 144 || variant.startsWith("Cyrl") || variant.startsWith("Latn")) { 145 return true; 146 } 147 return false; 148 } 149 } 150