1 /*
2  * Copyright (c) 2000, 2003, 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 /*
25     @test
26     @bug 4219630 4304573 4533872 4900935 8042589 8054307
27     @summary toUpperCase should upper-case German sharp s correctly even if
28              it's the only character in the string. should also uppercase
29              all of the 1:M char mappings correctly.  Also it should handle
30              Locale specific (lt, tr, and az) uppercasings and supplementary
31              characters correctly.
32 */
33 
34 import java.util.Locale;
35 
36 public class ToUpperCase {
37 
main(String[] args)38     public static void main(String[] args) {
39         Locale turkish = new Locale("tr", "TR");
40         Locale lt = new Locale("lt"); // Lithanian
41         Locale az = new Locale("az"); // Azeri
42 
43         test("\u00DF", turkish, "SS");
44         test("a\u00DF", turkish, "ASS");
45         test("i", turkish, "\u0130");
46         test("i", az, "\u0130");
47         test("\u0131", turkish, "I");
48         test("\u00DF", Locale.GERMANY, "SS");
49         test("a\u00DF", Locale.GERMANY, "ASS");
50         test("i", Locale.GERMANY, "I");
51 
52         // test some of the 1:M uppercase mappings
53         test("abc\u00DF", Locale.US, "ABC\u0053\u0053");
54         test("\u0149abc", Locale.US, "\u02BC\u004EABC");
55         test("\u0149abc", turkish, "\u02BC\u004EABC");
56         test("\u1F52", Locale.US, "\u03A5\u0313\u0300");
57         test("\u0149\u1F52", Locale.US, "\u02BC\u004E\u03A5\u0313\u0300");
58         test("\u1F54ZZZ", Locale.US, "\u03A5\u0313\u0301ZZZ");
59         test("\u1F54ZZZ", turkish, "\u03A5\u0313\u0301ZZZ");
60         test("a\u00DF\u1F56", Locale.US, "ASS\u03A5\u0313\u0342");
61         test("\u1FAD", turkish, "\u1F6D\u0399");
62         test("i\u1FC7", turkish, "\u0130\u0397\u0342\u0399");
63         test("i\u1FC7", az, "\u0130\u0397\u0342\u0399");
64         test("i\u1FC7", Locale.US, "I\u0397\u0342\u0399");
65         test("\uFB04", Locale.US, "\u0046\u0046\u004C");
66         test("\uFB17AbCdEfi", turkish, "\u0544\u053DABCDEF\u0130");
67         test("\uFB17AbCdEfi", az, "\u0544\u053DABCDEF\u0130");
68 
69         // Remove DOT ABOVE after "i" in Lithuanian
70         test("i\u0307", lt, "I");
71         test("\u0307", lt, "\u0307");
72         test("\u0307i", lt, "\u0307I");
73         test("j\u0307", lt, "J");
74         test("abci\u0307def", lt, "ABCIDEF");
75         test("a\u0307", lt, "A\u0307");
76         test("abc\u0307def", lt, "ABC\u0307DEF");
77         test("i\u0307", Locale.US, "I\u0307");
78         test("i\u0307", turkish, "\u0130\u0307");
79 
80         // Supplementary character tests
81         //
82         // U+10400 ("\uD801\uDC00"): DESERET CAPITAL LETTER LONG I
83         // U+10401 ("\uD801\uDC01"): DESERET CAPITAL LETTER LONG E
84         // U+10402 ("\uD801\uDC02"): DESERET CAPITAL LETTER LONG A
85         // U+10428 ("\uD801\uDC28"): DESERET SMALL LETTER LONG I
86         // U+10429 ("\uD801\uDC29"): DESERET SMALL LETTER LONG E
87         // U+1042A ("\uD801\uDC2A"): DESERET SMALL LETTER LONG A
88         //
89         // valid code point tests:
90         test("\uD801\uDC28\uD801\uDC29\uD801\uDC2A", Locale.US, "\uD801\uDC00\uD801\uDC01\uD801\uDC02");
91         test("\uD801\uDC28a\uD801\uDC29b\uD801\uDC2Ac", Locale.US, "\uD801\uDC00A\uD801\uDC01B\uD801\uDC02C");
92         // invalid code point tests:
93         test("\uD800\uD800\uD801a\uDC00\uDC00\uDC00b", Locale.US, "\uD800\uD800\uD801A\uDC00\uDC00\uDC00B");
94 
95         // lower/uppercase + surrogates
96         test("a\uD801\uDC44", Locale.ROOT, "A\uD801\uDC1c");
97         test("A\uD801\uDC44", Locale.ROOT, "A\uD801\uDC1c");
98         test("a\uD801\uDC28\uD801\uDC29\uD801\uDC2A", Locale.US, "A\uD801\uDC00\uD801\uDC01\uD801\uDC02");
99         test("A\uD801\uDC28a\uD801\uDC29b\uD801\uDC2Ac", Locale.US, "A\uD801\uDC00A\uD801\uDC01B\uD801\uDC02C");
100 
101         // test latin1 only case
102         StringBuilder src = new StringBuilder(0x100);
103         StringBuilder exp = new StringBuilder(0x100);
104         for (int cp = 0; cp < 0x100; cp++) {
105             int upperCase = Character.toUpperCase(cp);
106             if (upperCase == -1) {    //Character.ERROR
107                 continue;
108             }
109             src.appendCodePoint(cp);
110             if (cp == '\u00df') {
111                 exp.append("SS");     // need Character.toUpperCaseEx()
112             } else {
113                 exp.appendCodePoint(upperCase);
114             }
115         }
116         test(src.toString(), Locale.US, exp.toString());
117 
118         // test non-latin1 -> latin1
119         src = new StringBuilder(0x100).append("ABC");
120         exp = new StringBuilder(0x100).append("ABC");
121         for (int cp = 0x100; cp < 0x10000; cp++) {
122             int upperCase  = Character.toUpperCase(cp);
123             if (upperCase < 0x100) {
124                 src.appendCodePoint(cp);
125                 exp.appendCodePoint(upperCase);
126             }
127         }
128         test(src.toString(), Locale.US, exp.toString());
129 
130     }
131 
test(String in, Locale locale, String expected)132     static void test(String in, Locale locale, String expected) {
133         test0(in, locale,expected);
134         // trigger different code paths
135         for (String[] ss :  new String[][] {
136                                 new String[] {"abc",      "ABC"},
137                                 new String[] {"AbC",      "ABC"},
138                                 new String[] {"ABC",      "ABC"},
139                                 new String[] {"AB\u4e00", "AB\u4e00"},
140                                 new String[] {"ab\u4e00", "AB\u4e00"},
141                                 new String[] {"aB\u4e00", "AB\u4e00"},
142                                 new String[] {"AB\uD800\uDC00", "AB\uD800\uDC00"},
143                                 new String[] {"Ab\uD800\uDC00", "AB\uD800\uDC00"},
144                                 new String[] {"ab\uD800\uDC00", "AB\uD800\uDC00"},
145                                 new String[] {"AB\uD801\uDC44", "AB\uD801\uDC1C"},
146                                 new String[] {"Ab\uD801\uDC44", "AB\uD801\uDC1C"},
147                                 new String[] {"ab\uD801\uDC44", "AB\uD801\uDC1C"},
148                             }) {
149             test0(ss[0] + " " + in, locale, ss[1] + " " + expected);
150             test0(in + " " + ss[0], locale, expected + " " + ss[1]);
151         }
152     }
153 
test0(String in, Locale locale, String expected)154     static void test0(String in, Locale locale, String expected) {
155         String result = in.toUpperCase(locale);
156         if (!result.equals(expected)) {
157             System.err.println("input: " + in + ", locale: " + locale +
158                     ", expected: " + expected + ", actual: " + result);
159             throw new RuntimeException();
160         }
161     }
162 }
163