• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
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 Set<Locale> ICU_LOCALES = new HashSet<>();
74 
75     static {
76         ULocale[] icuULocales = ULocale.getAvailableLocales();
77         for (ULocale icuULoc : icuULocales) {
78             Locale jdkLoc = icuULoc.toLocale();
79             // Make sure nothing lost
80             ULocale uloc = ULocale.forLocale(jdkLoc);
81             if (icuULoc.equals(uloc)) {
82                 ICU_LOCALES.add(jdkLoc);
83             }
84         }
85     }
86 
87     /*
88      * Checks if the given locale is excluded from locale SPI test
89      */
isExcluded(Locale loc)90     public static boolean isExcluded(Locale loc) {
91         if (Locale.ROOT.equals(loc)) {
92             return true;
93         }
94         if (isICUExtendedLocale(loc)) {
95             return false;
96         }
97         return !ICU_LOCALES.contains(loc);
98     }
99 }
100