1 /*
2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package test.java.text.testlib;
25 
26 import java.text.DecimalFormatSymbols;
27 import java.util.Calendar;
28 import java.util.GregorianCalendar;
29 import java.util.Locale;
30 
31 import java.util.Locale.Builder;
32 
33 /**
34  * TestUtils provides utility methods to get a locale-dependent attribute.
35  * For example,
36  *   - whether or not a non-Gregorian calendar is used
37  *   - whether or not non-ASCII digits are used
38  *
39  * This class was developed to help testing for internationalization &
40  * localization and is not versatile.
41  */
42 public class TestUtils {
43 
44     /**
45      * Returns true if the give locale uses Gregorian calendar.
46      */
usesGregorianCalendar(Locale locale)47     public static boolean usesGregorianCalendar(Locale locale) {
48         return Calendar.getInstance(locale).getClass() == GregorianCalendar.class;
49     }
50 
51     /**
52      * Returns true if the given locale uses ASCII digits.
53      */
usesAsciiDigits(Locale locale)54     public static boolean usesAsciiDigits(Locale locale) {
55         return DecimalFormatSymbols.getInstance(locale).getZeroDigit() == '0';
56     }
57 
58     /**
59      * Returns true if the given locale has a special variant which is treated
60      * as ill-formed in BCP 47.
61      *
62      * BCP 47 requires a variant subtag to be 5 to 8 alphanumerics or a
63      * single numeric followed by 3 alphanumerics.
64      * However, note that this methods doesn't check a variant so rigorously
65      * because this is a utility method for testing. Intended special variants
66      * are: JP, NY, and TH, which can be commonly provided by
67      * Locale.getAvailableLocales().
68      *
69      */
hasSpecialVariant(Locale locale)70     public static boolean hasSpecialVariant(Locale locale) {
71         String variant = locale.getVariant();
72         return !variant.isEmpty()
73                    && "JP".equals(variant) || "NY".equals(variant) || "TH".equals(variant);
74     }
75 
76 }
77