File: TestUtils.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (184 lines) | stat: -rw-r--r-- 7,054 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.text.CollationElementIterator;
import java.text.CollationKey;
import java.text.Collator;
import java.text.DecimalFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * TestUtils provides utility methods used by i18n related tests.
 * This class was developed to help testing for internationalization &
 * localization and is not versatile. This class is split into the following sections,
 * 1) Methods to get a locale-dependent attribute.
 * For example,
 *   - whether a non-Gregorian calendar is used
 *   - whether non-ASCII digits are used
 * 2) Methods that help Collator related tests
 * For example,
 *   - compare CollationElementIterators
 *   - test the expected relation key result for a Collator
 */
public final class TestUtils {

    // Utility class should not be instantiated
    private TestUtils() {}

    /*
     * The below methods are utilities for getting locale-dependent attributes.
     */

    /**
     * Returns true if the give locale uses Gregorian calendar.
     */
    public static boolean usesGregorianCalendar(Locale locale) {
        return Calendar.getInstance(locale).getClass() == GregorianCalendar.class;
    }

    /**
     * Returns true if the given locale uses ASCII digits.
     */
    public static boolean usesAsciiDigits(Locale locale) {
        return DecimalFormatSymbols.getInstance(locale).getZeroDigit() == '0';
    }

    /**
     * Returns true if the given locale has a special variant which is treated
     * as ill-formed in BCP 47.
     * BCP 47 requires a variant subtag to be 5 to 8 alphanumerics or a
     * single numeric followed by 3 alphanumerics.
     * However, note that this methods doesn't check a variant so rigorously
     * because this is a utility method for testing. Intended special variants
     * are: JP, NY, and TH, which can be commonly provided by
     * Locale.getAvailableLocales().
     *
     */
    public static boolean hasSpecialVariant(Locale locale) {
        String variant = locale.getVariant();
        return !variant.isEmpty()
                   && "JP".equals(variant) || "NY".equals(variant) || "TH".equals(variant);
    }

    /*
     * The below methods are utilities specific to the Collation tests
     */

    /**
     * Compares two CollationElementIterators and throws an exception
     * with a message detailing which collation elements were not equal
     */
    public static void compareCollationElementIters(CollationElementIterator i1, CollationElementIterator i2) {
        int c1, c2, count = 0;
        do {
            c1 = i1.next();
            c2 = i2.next();
            if (c1 != c2) {
                throw new RuntimeException("    " + count + ": " + c1 + " != " + c2);
            }
            count++;
        } while (c1 != CollationElementIterator.NULLORDER);
    }

    // Replace non-printable characters with unicode escapes
    public static String prettify(String str) {
        StringBuilder result = new StringBuilder();

        String zero = "0000";

        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch < 0x09 || (ch > 0x0A && ch < 0x20)|| (ch > 0x7E && ch < 0xA0) || ch > 0x100) {
                String hex = Integer.toString((int)ch,16);

                result.append("\\u").append(zero.substring(0, 4 - hex.length())).append(hex);
            } else {
                result.append(ch);
            }
        }
        return result.toString();
    }

    // Produce a printable representation of a CollationKey
    public static String prettifyCKey(CollationKey key) {
        StringBuilder result = new StringBuilder();
        byte[] bytes = key.toByteArray();

        for (int i = 0; i < bytes.length; i += 2) {
            int val = (bytes[i] << 8) + bytes[i+1];
            result.append(Integer.toString(val, 16)).append(" ");
        }
        return result.toString();
    }

    /**
     * Utility to test a collator with an array of test values.
     * See the other doTest() method for specific comparison details.
     */
    public static void doCollatorTest(Collator col, int strength,
                                      String[] source, String[] target, int[] result) {
        if (source.length != target.length) {
            throw new RuntimeException("Data size mismatch: source = " +
                    source.length + ", target = " + target.length);
        }
        if (source.length != result.length) {
            throw new RuntimeException("Data size mismatch: source & target = " +
                    source.length + ", result = " + result.length);
        }

        col.setStrength(strength);
        for (int i = 0; i < source.length ; i++) {
            doCollatorTest(col, source[i], target[i], result[i]);
        }
    }

    /**
     * Test that a collator returns the correct relation result value when
     * comparing a source and target string. Also tests that the compare and collation
     * key results return the same value.
     */
    public static void doCollatorTest(Collator col,
                                      String source, String target, int result) {
        char relation = '=';
        if (result <= -1) {
            relation = '<';
        } else if (result >= 1) {
            relation = '>';
        }

        int compareResult = col.compare(source, target);
        CollationKey sortKey1 = col.getCollationKey(source);
        CollationKey sortKey2 = col.getCollationKey(target);
        int keyResult = sortKey1.compareTo(sortKey2);
        if (compareResult != keyResult) {
            throw new RuntimeException("Compare and Collation Key results are different! Source = " +
                    source + " Target = " + target);
        }
        if (keyResult != result) {
            throw new RuntimeException("Collation Test failed! Source = " + source + " Target = " +
                    target + " result should be " + relation);
        }
    }
}