1 /*
  2  * Copyright (c) 2016, 2023, 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.CollationElementIterator;
 25 import java.text.CollationKey;
 26 import java.text.Collator;
 27 import java.text.DecimalFormatSymbols;
 28 import java.util.Calendar;
 29 import java.util.GregorianCalendar;
 30 import java.util.Locale;
 31 
 32 /**
 33  * TestUtils provides utility methods used by i18n related tests.
 34  * This class was developed to help testing for internationalization &
 35  * localization and is not versatile. This class is split into the following sections,
 36  * 1) Methods to get a locale-dependent attribute.
 37  * For example,
 38  *   - whether a non-Gregorian calendar is used
 39  *   - whether non-ASCII digits are used
 40  * 2) Methods that help Collator related tests
 41  * For example,
 42  *   - compare CollationElementIterators
 43  *   - test the expected relation key result for a Collator
 44  */
 45 public final class TestUtils {
 46 
 47     // Utility class should not be instantiated
 48     private TestUtils() {}
 49 
 50     /*
 51      * The below methods are utilities for getting locale-dependent attributes.
 52      */
 53 
 54     /**
 55      * Returns true if the give locale uses Gregorian calendar.
 56      */
 57     public static boolean usesGregorianCalendar(Locale locale) {
 58         return Calendar.getInstance(locale).getClass() == GregorianCalendar.class;
 59     }
 60 
 61     /**
 62      * Returns true if the given locale uses ASCII digits.
 63      */
 64     public static boolean usesAsciiDigits(Locale locale) {
 65         return DecimalFormatSymbols.getInstance(locale).getZeroDigit() == '0';
 66     }
 67 
 68     /**
 69      * Returns true if the given locale has a special variant which is treated
 70      * as ill-formed in BCP 47.
 71      * BCP 47 requires a variant subtag to be 5 to 8 alphanumerics or a
 72      * single numeric followed by 3 alphanumerics.
 73      * However, note that this methods doesn't check a variant so rigorously
 74      * because this is a utility method for testing. Intended special variants
 75      * are: JP, NY, and TH, which can be commonly provided by
 76      * Locale.getAvailableLocales().
 77      *
 78      */
 79     public static boolean hasSpecialVariant(Locale locale) {
 80         String variant = locale.getVariant();
 81         return !variant.isEmpty()
 82                    && "JP".equals(variant) || "NY".equals(variant) || "TH".equals(variant);
 83     }
 84 
 85     /*
 86      * The below methods are utilities specific to the Collation tests
 87      */
 88 
 89     /**
 90      * Compares two CollationElementIterators and throws an exception
 91      * with a message detailing which collation elements were not equal
 92      */
 93     public static void compareCollationElementIters(CollationElementIterator i1, CollationElementIterator i2) {
 94         int c1, c2, count = 0;
 95         do {
 96             c1 = i1.next();
 97             c2 = i2.next();
 98             if (c1 != c2) {
 99                 throw new RuntimeException("    " + count + ": " + c1 + " != " + c2);
100             }
101             count++;
102         } while (c1 != CollationElementIterator.NULLORDER);
103     }
104 
105     // Replace non-printable characters with unicode escapes
106     public static String prettify(String str) {
107         StringBuilder result = new StringBuilder();
108 
109         String zero = "0000";
110 
111         for (int i = 0; i < str.length(); i++) {
112             char ch = str.charAt(i);
113             if (ch < 0x09 || (ch > 0x0A && ch < 0x20)|| (ch > 0x7E && ch < 0xA0) || ch > 0x100) {
114                 String hex = Integer.toString((int)ch,16);
115 
116                 result.append("\\u").append(zero.substring(0, 4 - hex.length())).append(hex);
117             } else {
118                 result.append(ch);
119             }
120         }
121         return result.toString();
122     }
123 
124     // Produce a printable representation of a CollationKey
125     public static String prettifyCKey(CollationKey key) {
126         StringBuilder result = new StringBuilder();
127         byte[] bytes = key.toByteArray();
128 
129         for (int i = 0; i < bytes.length; i += 2) {
130             int val = (bytes[i] << 8) + bytes[i+1];
131             result.append(Integer.toString(val, 16)).append(" ");
132         }
133         return result.toString();
134     }
135 
136     /**
137      * Utility to test a collator with an array of test values.
138      * See the other doTest() method for specific comparison details.
139      */
140     public static void doCollatorTest(Collator col, int strength,
141                                       String[] source, String[] target, int[] result) {
142         if (source.length != target.length) {
143             throw new RuntimeException("Data size mismatch: source = " +
144                     source.length + ", target = " + target.length);
145         }
146         if (source.length != result.length) {
147             throw new RuntimeException("Data size mismatch: source & target = " +
148                     source.length + ", result = " + result.length);
149         }
150 
151         col.setStrength(strength);
152         for (int i = 0; i < source.length ; i++) {
153             doCollatorTest(col, source[i], target[i], result[i]);
154         }
155     }
156 
157     /**
158      * Test that a collator returns the correct relation result value when
159      * comparing a source and target string. Also tests that the compare and collation
160      * key results return the same value.
161      */
162     public static void doCollatorTest(Collator col,
163                                       String source, String target, int result) {
164         char relation = '=';
165         if (result <= -1) {
166             relation = '<';
167         } else if (result >= 1) {
168             relation = '>';
169         }
170 
171         int compareResult = col.compare(source, target);
172         CollationKey sortKey1 = col.getCollationKey(source);
173         CollationKey sortKey2 = col.getCollationKey(target);
174         int keyResult = sortKey1.compareTo(sortKey2);
175         if (compareResult != keyResult) {
176             throw new RuntimeException("Compare and Collation Key results are different! Source = " +
177                     source + " Target = " + target);
178         }
179         if (keyResult != result) {
180             throw new RuntimeException("Collation Test failed! Source = " + source + " Target = " +
181                     target + " result should be " + relation);
182         }
183     }
184 }