1 /*
2  * Copyright (c) 2015, 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 /* @test
25  * @bug 8138824
26  * @summary Test expected equalsIgnoreCase behavior for some known asymmetric case mappings
27  */
28 
29 public class EqualsIgnoreCase {
30     private static final String SMALL_I = "i";
31     private static final String CAPITAL_I = "I";
32     // Characters that do not map symmetrically between upper/lower case
33     private static final String SMALL_DOTLESS_I = "\u0131";
34     private static final String CAPITAL_I_WITH_DOT = "\u0130";
35     private static final String LOWER_GREEK_THETA = "\u03D1";
36     private static final String CAPITAL_GREEK_THETA = "\u03F4";
37 
main(String[] args)38     public static void main(String[] args) {
39         compareFuncs(SMALL_I, CAPITAL_I, true, true);
40         compareFuncs(CAPITAL_I_WITH_DOT, SMALL_DOTLESS_I, true, false);
41         compareFuncs(LOWER_GREEK_THETA, CAPITAL_GREEK_THETA, true, false);
42     }
43 
44     /**
45      * Compare the actual results of equalsIgnoreCase():
46      *   toUpperCase(toLowerCase(eachChar))
47      * to the behavior described in the equalsIgnoreCase() spec prior to 8138824:
48      *   toUpperCase(eachChar)
49      *   toLowerCase(eachChar)
50      *
51      * @param s1 A string
52      * @param s2 Another string
53      * @param expectEquals Expected result of equalsIgnoreCase()
54      * @param expectTuTl Expected result of toUpperToLowerOriginals()
55      */
compareFuncs(String s1, String s2, boolean expectEquals, boolean expectTuTl)56     private static void compareFuncs(String s1, String s2, boolean expectEquals, boolean expectTuTl) {
57         System.out.println(s1 + ", " + s2);
58         boolean equalsResult = s1.equalsIgnoreCase(s2);
59         System.out.println("equalsIgnoreCase:" + equalsResult);
60 
61         boolean tuTlResult = toUpperToLowerOriginals(s1, s2);
62         System.out.println("tUtLO:" + tuTlResult);
63         boolean failed = false;
64 
65         if (equalsResult != expectEquals) {
66             System.out.println("Expected " + expectEquals + " from equalsIgnoreCase() but got " + equalsResult);
67             failed = true;
68         }
69         if (tuTlResult != expectTuTl) {
70             System.out.println("Expected " + expectTuTl + " from toUpperToLowerOriginals() but got " + tuTlResult);
71             failed = true;
72         }
73         if (failed) { throw new RuntimeException("Test Failed"); }
74     }
75 
76     /**
77      * Apply toUpperCase() and toLowerCase() to corresponding chars of both
78      * Strings.  Returns true if each pair of corresponding chars are either:
79      *   1. == after both are converted to upper case
80      * or
81      *   2. == after both are converted to lower case
82      * and the String lengths are equal.
83      */
toUpperToLowerOriginals(String str1, String str2)84     private static boolean toUpperToLowerOriginals(String str1, String str2) {
85         if (str1.length() != str2.length()) { return false; }
86         for (int i = 0; i < str1.length(); i++) {
87             char c1 = str1.charAt(i);
88             char c2 = str2.charAt(i);
89 
90             char uc1 = Character.toUpperCase(c1);
91             char uc2 = Character.toUpperCase(c2);
92             boolean upperMatch = uc1 == uc2;
93 
94             char lc1 = Character.toLowerCase(c1);
95             char lc2 = Character.toLowerCase(c2);
96             boolean lowerMatch = lc1 == lc2;
97 
98             if (!(upperMatch || lowerMatch)) {
99                 return false;
100             }
101         }
102         return true;
103     }
104 }
105