1/* 2 ******************************************************************************* 3 * Copyright (C) 2008-2012, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 */ 7package com.ibm.icu.dev.test.localespi; 8 9import java.util.Locale; 10 11import com.ibm.icu.util.ULocale; 12import com.ibm.icu.util.ULocale.Builder; 13 14public class TestUtil { 15 16 static final String ICU_VARIANT = "ICU4J"; 17 private static final String ICU_VARIANT_SUFFIX = "_ICU4J"; 18 19 public static Locale toICUExtendedLocale(Locale locale) { 20 if (isICUExtendedLocale(locale)) { 21 return locale; 22 } 23 24 String variant = locale.getVariant(); 25 variant = variant.length() == 0 ? ICU_VARIANT : variant + ICU_VARIANT_SUFFIX; 26 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 30 // also run on JRE 6. 31 ULocale uloc = ULocale.forLocale(locale); 32 if (uloc.getScript().length() == 0) { 33 return new Locale(locale.getLanguage(), locale.getCountry(), variant); 34 } 35 36 // For preserving JDK Locale's script, we cannot use 37 // the regular Locale constructor. 38 ULocale modUloc = null; 39 Builder locBld = new Builder(); 40 try { 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); 48 } 49 } 50 51 public static boolean isICUExtendedLocale(Locale locale) { 52 String variant = locale.getVariant(); 53 if (variant.equals(ICU_VARIANT) || variant.endsWith(ICU_VARIANT_SUFFIX)) { 54 return true; 55 } 56 return false; 57 } 58 59 public static boolean equals(Object o1, Object o2) { 60 if (o1 == null && o2 == null) { 61 return true; 62 } 63 if (o1 == null || o2 == null) { 64 return false; 65 } 66 return o1.equals(o2); 67 } 68 69 private static final boolean SUNJRE; 70 private static final boolean IBMJRE; 71 72 static { 73 String javaVendor = System.getProperty("java.vendor"); 74 if (javaVendor != null) { 75 if (javaVendor.indexOf("Sun") >= 0) { 76 SUNJRE = true; 77 IBMJRE = false; 78 } else if (javaVendor.indexOf("IBM") >= 0) { 79 SUNJRE = false; 80 IBMJRE = true; 81 } else { 82 SUNJRE = false; 83 IBMJRE = false; 84 } 85 } else { 86 SUNJRE = false; 87 IBMJRE = false; 88 } 89 } 90 91 public static boolean isSUNJRE() { 92 return SUNJRE; 93 } 94 public static boolean isIBMJRE() { 95 return IBMJRE; 96 } 97 98 /* 99 * Ticket#6368 100 * 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. 103 * 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. 107 * 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. 111 * 112 * For now, we exclude these problematic locales from locale spi test cases on IBM Java 6. 113 */ 114 public static boolean isProblematicIBMLocale(Locale loc) { 115 if (!isIBMJRE()) { 116 return false; 117 } 118 if (loc.getLanguage().equals("sh")) { 119 return true; 120 } 121 String variant = loc.getVariant(); 122 if (variant.startsWith("EURO") || variant.startsWith("PREEURO") 123 || variant.startsWith("Cyrl") || variant.startsWith("Latn")) { 124 return true; 125 } 126 return false; 127 } 128} 129