File: SpecialCasingTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (354 lines) | stat: -rw-r--r-- 14,898 bytes parent folder | download | duplicates (6)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Copyright (c) 2018, 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.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * 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.
 */

/*
 * @test
 * @bug 4397357 6565620 6959267 7070436 7198195 8041791 8032446 8072600
 * @summary Confirm special case mappings are handled correctly.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;

public class SpecialCasingTest {

    private static boolean err = false;

    // Locales which are used for testing
    private static List<Locale> locales = new ArrayList<>();
    static {
        locales.add(new Locale("az", ""));
        locales.addAll(java.util.Arrays.asList(Locale.getAvailableLocales()));
    }

    // Default locale
    private static String defaultLang;

    // True if the default language is az, lt, or tr which has locale-specific
    // mappings.
    private static boolean specificLocale;

    // Additional test cases
    //   Pseudo-locales which are used here:
    //     L1: locales other than lt
    //     L2: locales other than az and tr
    //     L3: locales other than az, lt and tr
    private static final String[] additionalTestData = {
        // Format:
        //   <code>; <lower>; <title>; <upper>; (<condition_list>)

        // Counterpart of Final_Sigma test case
        //   03A3; 03C2; 03A3; 03A3; Final_Sigma
            "03A3; 03C3; 03A3; 03A3; SunSpecific_Not_Final_Sigma1",
            "03A3; 03C3; 03A3; 03A3; SunSpecific_Not_Final_Sigma2",

        // Counterpart of After_Soft_Dotted test case
        //   0307; 0307;     ;     ; lt After_Soft_Dotted
            "0307; 0307; 0307; 0307; L1 After_Soft_Dotted",
            "0307; 0307; 0307; 0307; lt SunSpecific_Not_After_Soft_Dotted",
            "0307; 0307; 0307; 0307; L1 SunSpecific_Not_After_Soft_Dotted",

        // Counterpart of More_Above test cases
        //   0049; 0069 0307; 0049; 0049; lt More_Above
            "0049; 0131     ; 0049; 0049; az More_Above",
            "0049; 0131     ; 0049; 0049; tr More_Above",
            "0049; 0069     ; 0049; 0049; L3 More_Above",
            "0049; 0069     ; 0049; 0049; lt SunSpecific_Not_More_Above",
            "0049; 0131     ; 0049; 0049; az SunSpecific_Not_More_Above",
            "0049; 0131     ; 0049; 0049; tr SunSpecific_Not_More_Above",
            "0049; 0069     ; 0049; 0049; L3 SunSpecific_Not_More_Above",
        //   004A; 006A 0307; 004A; 004A; lt More_Above
            "004A; 006A     ; 004A; 004A; L1 More_Above",
            "004A; 006A     ; 004A; 004A; lt SunSpecific_Not_More_Above",
            "004A; 006A     ; 004A; 004A; L1 SunSpecific_Not_More_Above",
        //   012E; 012F 0307; 012E; 012E; lt More_Above
            "012E; 012F     ; 012E; 012E; L1 More_Above",
            "012E; 012F     ; 012E; 012E; lt SunSpecific_Not_More_Above",
            "012E; 012F     ; 012E; 012E; L1 SunSpecific_Not_More_Above",

        // Counterpart of After_I test cases
        //   0307;          ; 0307; 0307; tr After_I
        //   0307;          ; 0307; 0307; az After_I
            "0307; 0307 0307; 0307; 0307; lt After_I",
            "0307; 0307     ; 0307; 0307; L3 After_I",
            "0307; 0307     ; 0307; 0307; tr SunSpecific_Not_After_I",
            "0307; 0307     ; 0307; 0307; az SunSpecific_Not_After_I",
            "0307; 0307     ; 0307; 0307; L2 SunSpecific_Not_After_I",

        // Counterpart of Not_Before_Dot test cases
        //   0049; 0131          ; 0049; 0049; tr Not_Before_Dot
        //   0049; 0131          ; 0049; 0049; az Not_Before_Dot
            "0049; 0069          ; 0049; 0049; L2 Not_Before_Dot",
            "0049; 0069          ; 0049; 0049; tr SunSpecific_Before_Dot",
            "0049; 0069          ; 0049; 0049; az SunSpecific_Before_Dot",
            "0049; 0069 0307 0307; 0049; 0049; lt SunSpecific_Before_Dot",
            "0049; 0069 0307     ; 0049; 0049; L3 SunSpecific_Before_Dot",
    };

    public static void main (String[] args) {
        SpecialCasingTest specialCasingTest = new SpecialCasingTest();
        specialCasingTest.test();
    }

    private void test ()  {
        Locale defaultLocale = Locale.getDefault();
        BufferedReader in = null;

        try {
            int locale_num = locales.size();
            for (int l = 0; l < locale_num; l++) {
                Locale locale = locales.get(l);
                Locale.setDefault(locale);
                System.out.println("Testing on " + locale + " locale....");

                defaultLang = locale.getLanguage();
                if (defaultLang.equals("az") ||
                    defaultLang.equals("lt") ||
                    defaultLang.equals("tr")) {
                    specificLocale = true;
                } else {
                    specificLocale = false;
                }
                in = Files.newBufferedReader(Paths.get(System.getProperty("test.src.path"), "..", "/Character/SpecialCasing.txt")
                     .toRealPath());
                String line;
                while ((line = in.readLine()) != null) {
                    if (line.length() == 0 || line.charAt(0) == '#') {
                        continue;
                    }
                    test(line);
                }
                in.close();
                in = null;
                System.out.println("Testing with Sun original data....");
                for (String additionalTestData1 : additionalTestData) {
                    test(additionalTestData1);
                }
            }
        }
        catch (IOException e) {
            err = true;
            e.printStackTrace();
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                }
                catch (IOException e) {
                }
            }
            Locale.setDefault(defaultLocale);
            if (err) {
                throw new RuntimeException("SpecialCasingTest failed.");
            } else {
                System.out.println("*** SpecialCasingTest passed.");
            }
        }
    }

    private void test(String line) {
        int index = line.indexOf('#');
        if (index != -1) {
            line = line.substring(0, index);
        }

        String lang = null;
        String condition = null;
        String[] fields = line.split("; ");

        for (int i = 0; i < 4; i++) {
            if (fields[i].length() != 0) {
                fields[i] = convert(fields[i]);
            }
        }
        if (fields.length != 4) {
            StringTokenizer st = new StringTokenizer(fields[4]);

            while (st.hasMoreTokens()) {
                String token = st.nextToken();

                if (token.equals("Final_Sigma")) {
                    condition = "Final Sigma";
                    fields[0] = "Abc" + fields[0];
                    fields[1] = "abc" + fields[1];
                    fields[3] = "ABC" + fields[3];
                } else if (token.equals("SunSpecific_Not_Final_Sigma1")) {
                    condition = "*Sun Specific* Not Final Sigma 1";
                    fields[0] = "Abc" + fields[0] + "xyz";
                    fields[1] = "abc" + fields[1] + "xyz";
                    fields[3] = "ABC" + fields[3] + "XYZ";
                } else if (token.equals("SunSpecific_Not_Final_Sigma2")) {
                    condition = "*Sun Specific* Not Final Sigma 2";
                } else if (token.equals("After_Soft_Dotted")) {
                    condition = "After Soft-Dotted";
                    fields[0] = "\u1E2D" + fields[0];
                    fields[1] = "\u1E2D" + fields[1];
                    fields[3] = "\u1E2C" + fields[3];
                } else if (token.equals("SunSpecific_Not_After_Soft_Dotted")) {
                    condition = "*Sun Specific* Not After Soft-Dotted";
                    fields[0] = "Dot" + fields[0];
                    fields[1] = "dot" + fields[1];
                    fields[3] = "DOT" + fields[3];
                } else if (token.equals("More_Above")) {
                    condition = "More Above";
                    fields[0] = fields[0] + "\u0306";
                    fields[1] = fields[1] + "\u0306";
                    fields[3] = fields[3] + "\u0306";
                } else if (token.equals("SunSpecific_Not_More_Above")) {
                    condition = "*Sun Specific* Not More Above";
                    fields[0] = fields[0] + "breve";
                    fields[1] = fields[1] + "breve";
                    fields[3] = fields[3] + "BREVE";
                } else if (token.equals("After_I")) {
                    condition = "After I";
                    fields[0] = "I" + fields[0];
                    fields[1] = "i" + fields[1];
                    fields[3] = "I" + fields[3];
                } else if (token.equals("SunSpecific_Not_After_I")) {
                    condition = "*Sun Specific* Not After I";
                    fields[0] = "A" + fields[0];
                    fields[1] = "a" + fields[1];
                    fields[3] = "A" + fields[3];
                } else if (token.equals("Not_Before_Dot")) {
                    condition = "Not Before Dot";
                    fields[0] = fields[0] + "Z";
                    fields[1] = fields[1] + "z";
                    fields[3] = fields[3] + "Z";
                } else if (token.equals("SunSpecific_Before_Dot")) {
                    condition = "*Sun Specific* Before Dot";
                    fields[0] = fields[0] + "\u0307";
                    fields[3] = fields[3] + "\u0307";
                } else if (token.length() == 2) {
                    lang = token;

                    if (lang.equals("L1")) {
                        if (defaultLang.equals("lt")) {
                            lang = "en";
                        } else {
                            lang = defaultLang;
                        }
                    } else if (lang.equals("L2")) {
                        if (defaultLang.equals("az") ||
                            defaultLang.equals("tr")) {
                            lang = "en";
                        } else {
                            lang = defaultLang;
                        }
                    } else if (lang.equals("L3")) {
                        if (defaultLang.equals("az") ||
                            defaultLang.equals("lt") ||
                            defaultLang.equals("tr")) {
                            lang = "en";
                        } else {
                            lang = defaultLang;
                        }
                    // I want to have another test case here for double-check.
                    // Current implementation for Character and String considers
                    // only az, lt, and tr locales. I want to detect if other
                    // locales are specified.
                    } else if (!lang.equals("az") &&
                               !lang.equals("lt") &&
                               !lang.equals("tr")) {
                        throw new RuntimeException("Unsupported locale: " +
                            lang + ". It may need to be considered in ConditionalSpecialCasing.java. Please confirm.");
                    }
                } else {
                    throw new RuntimeException("Unknown condition: " + token);
                }
            }
        } else if (fields[0].equals("\u0130")) {
            // special case for \u0130
            if (defaultLang.equals("az") ||
                defaultLang.equals("tr")) {
                lang = "en";
            } else {
                lang = defaultLang;
            }
        }
        testLowerCase(fields[0], fields[1], lang, condition);
        testUpperCase(fields[0], fields[3], lang, condition);
    }

    private void testLowerCase(String orig, String expected,
                               String lang, String condition) {
        String got = (lang == null) ?
            orig.toLowerCase() : orig.toLowerCase(new Locale(lang, ""));

        if (!expected.equals(got)) {
            err = true;
            System.err.println("toLowerCase(lang=" + lang +
                ") failed.\n\tOriginal: " + toString(orig) +
                "\n\tGot:      " + toString(got) +
                "\n\tExpected: " + toString(expected) +
                ((condition == null) ? "" : ("\n    under condition(" +
                condition + ")")));
        }
    }

    private void testUpperCase(String orig, String expected,
                               String lang, String condition) {
        String got = (lang == null) ?
            orig.toUpperCase() : orig.toUpperCase(new Locale(lang, ""));

        if (!expected.equals(got)) {
            err = true;
            System.err.println("toUpperCase(lang=" + lang +
                ") failed.\n\tOriginal: " + toString(orig) +
                "\n\tGot:      " + toString(got) +
                "\n\tExpected: " + toString(expected) +
                ((condition == null) ? "" : ("\n    under condition(" +
                condition + ")")));
        }
    }
    StringBuilder sb = new StringBuilder();

    private String convert(String str) {
        sb.setLength(0);

        String[] tokens = str.split(" ");
        for (String token : tokens) {
            sb.append((char) Integer.parseInt(token, 16));
        }
        return sb.toString();
    }

    private String toString(String str) {
        sb.setLength(0);

        int len = str.length();
        for (int i = 0; i < len; i++) {
            sb.append("0x").append(Integer.toHexString(str.charAt(i)).toUpperCase()).append(" ");
        }
        return sb.toString();
    }

}