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