1 /*
  2  * Copyright (c) 2003, 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 /*
 25  * @test
 26  * @library /java/text/testlib
 27  * @summary test Supplementary Character Collation
 28  * @run junit SurrogatesTest
 29  */
 30 
 31 import java.text.Collator;
 32 import java.text.RuleBasedCollator;
 33 
 34 import org.junit.jupiter.api.Test;
 35 
 36 import static org.junit.jupiter.api.Assertions.fail;
 37 
 38 // Quick dummy program for printing out test results
 39 public class SurrogatesTest {
 40 
 41     /*
 42      * Data for TestPrimary()
 43      */
 44     private static final String[] primarySourceData = {
 45         "A\ud800\udc04BCD"
 46     };
 47 
 48     private static final String[] primaryTargetData = {
 49         "A\ud800\udc05BCD"
 50     };
 51 
 52     private static final int[] primaryResults = {
 53          0
 54     };
 55 
 56     /*
 57      * Data for TestTertiary()
 58      */
 59     private static final String[] tertiarySourceData = {
 60         "ABCD",
 61         "ABCD",
 62         "A\ud800\udc00CD",
 63         "WXYZ",
 64         "WXYZ",
 65         "AFEM",
 66         "FGM",
 67         "BB",
 68         "BB"
 69     };
 70 
 71     private static final String[] tertiaryTargetData = {
 72         "A\ud800\udc00CD",
 73         "AB\ud800\udc00D",
 74         "A\ud800\udc01CD",
 75         "W\ud800\udc0aYZ",
 76         "W\ud800\udc0bYZ",
 77         "A\ud800\udc08M",
 78         "\ud800\udc08M",
 79         "\ud800\udc04\ud800\udc02",
 80         "\ud800\udc04\ud800\udc05"
 81     };
 82 
 83     private static final int[] tertiaryResults = {
 84         -1,  1,  1,  1, -1, -1, -1, -1,  1
 85     };
 86 
 87     @Test
 88     public void TestPrimary() {
 89         TestUtils.doCollatorTest(myCollation, Collator.PRIMARY,
 90                primarySourceData, primaryTargetData, primaryResults);
 91     }
 92 
 93     @Test
 94     public void TestTertiary() {
 95         TestUtils.doCollatorTest(myCollation, Collator.TERTIARY,
 96                tertiarySourceData, tertiaryTargetData, tertiaryResults);
 97     }
 98 
 99     private Collator getCollator() {
100         RuleBasedCollator base = (RuleBasedCollator)Collator.getInstance();
101         String rule = base.getRules();
102         try {
103             return new RuleBasedCollator(rule
104                                      + "&B < \ud800\udc01 < \ud800\udc00"
105                                      + ", \ud800\udc02, \ud800\udc03"
106                                      + "; \ud800\udc04, \ud800\udc05"
107                                      + "< \ud800\udc06 < \ud800\udc07"
108                                      + "&FE < \ud800\udc08"
109                                      + "&PE, \ud800\udc09"
110                                      + "&Z < \ud800\udc0a < \ud800\udc0b < \ud800\udc0c"
111                                      + "&\ud800\udc0a < x, X"
112                                      + "&A < \ud800\udc04\ud800\udc05");
113         } catch (Exception e) {
114             fail("Failed to create new RulebasedCollator object");
115             return null;
116         }
117     }
118 
119     private Collator myCollation = getCollator();
120 }