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 import java.text.DecimalFormatSymbols;
25 import java.util.Calendar;
26 import java.util.GregorianCalendar;
27 import java.util.Locale;
28 
29 import java.util.Locale.Builder;
30 
31 /**
32  * TestUtils provides utility methods to get a locale-dependent attribute.
33  * For example,
34  *   - whether or not a non-Gregorian calendar is used
35  *   - whether or not non-ASCII digits are used
36  *
37  * This class was developed to help testing for internationalization &
38  * localization and is not versatile.
39  */
40 public class TestUtils {
41 
42     /**
43      * Returns true if the give locale uses Gregorian calendar.
44      */
45     public static boolean usesGregorianCalendar(Locale locale) {
46         return Calendar.getInstance(locale).getClass() == GregorianCalendar.class;
47     }
48 
49     /**
50      * Returns true if the given locale uses ASCII digits.
51      */
52     public static boolean usesAsciiDigits(Locale locale) {
53         return DecimalFormatSymbols.getInstance(locale).getZeroDigit() == '0';
54     }
55 
56     /**
57      * Returns true if the given locale has a special variant which is treated
58      * as ill-formed in BCP 47.
59      *
60      * BCP 47 requires a variant subtag to be 5 to 8 alphanumerics or a
61      * single numeric followed by 3 alphanumerics.
62      * However, note that this methods doesn't check a variant so rigorously
63      * because this is a utility method for testing. Intended special variants
64      * are: JP, NY, and TH, which can be commonly provided by
65      * Locale.getAvailableLocales().
66      *
67      */
68     public static boolean hasSpecialVariant(Locale locale) {
69         String variant = locale.getVariant();
70         return !variant.isEmpty()
71                    && "JP".equals(variant) || "NY".equals(variant) || "TH".equals(variant);
72     }
73 
74 }