1 /*
  2  * Copyright (c) 1997, 2024, 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 (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
 26 (C) Copyright IBM Corp. 1996 - All Rights Reserved
 27 
 28   The original version of this source code and documentation is copyrighted and
 29 owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
 30 provided under terms of a License Agreement between Taligent and Sun. This
 31 technology is protected by multiple US and International patents. This notice and
 32 attribution to Taligent may not be removed.
 33   Taligent is a registered trademark of Taligent, Inc.
 34 */
 35 
 36 /*
 37  * @test
 38  * @library /java/text/testlib
 39  * @summary test Collation API
 40  * @modules jdk.localedata
 41  * @run junit APITest
 42  */
 43 
 44 import java.util.Locale;
 45 import java.text.Collator;
 46 import java.text.RuleBasedCollator;
 47 import java.text.CollationKey;
 48 import java.text.CollationElementIterator;
 49 
 50 import org.junit.jupiter.api.Test;
 51 
 52 import static org.junit.jupiter.api.Assertions.fail;
 53 
 54 public class APITest {
 55 
 56     final void doAssert(boolean condition, String message)
 57     {
 58         if (!condition) {
 59             fail("ERROR: " + message);
 60         }
 61     }
 62 
 63     @Test
 64     public final void TestProperty( )
 65     {
 66         Collator col = null;
 67         try {
 68             col = Collator.getInstance(Locale.ROOT);
 69             System.out.println("The property tests begin : ");
 70             System.out.println("Test ctors : ");
 71             doAssert(col.compare("ab", "abc") < 0, "ab < abc comparison failed");
 72             doAssert(col.compare("ab", "AB") < 0, "ab < AB comparison failed");
 73             doAssert(col.compare("black-bird", "blackbird") > 0, "black-bird > blackbird comparison failed");
 74             doAssert(col.compare("black bird", "black-bird") < 0, "black bird < black-bird comparison failed");
 75             doAssert(col.compare("Hello", "hello") > 0, "Hello > hello comparison failed");
 76 
 77             System.out.println("Test ctors ends.");
 78             System.out.println("testing Collator.getStrength() method ...");
 79             doAssert(col.getStrength() == Collator.TERTIARY, "collation object has the wrong strength");
 80             doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
 81 
 82             System.out.println("testing Collator.setStrength() method ...");
 83             col.setStrength(Collator.SECONDARY);
 84             doAssert(col.getStrength() != Collator.TERTIARY, "collation object's strength is secondary difference");
 85             doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
 86             doAssert(col.getStrength() == Collator.SECONDARY, "collation object has the wrong strength");
 87 
 88             System.out.println("testing Collator.setDecomposition() method ...");
 89             col.setDecomposition(Collator.NO_DECOMPOSITION);
 90             doAssert(col.getDecomposition() != Collator.FULL_DECOMPOSITION, "collation object's strength is secondary difference");
 91             doAssert(col.getDecomposition() != Collator.CANONICAL_DECOMPOSITION, "collation object's strength is primary difference");
 92             doAssert(col.getDecomposition() == Collator.NO_DECOMPOSITION, "collation object has the wrong strength");
 93         } catch (Exception foo) {
 94             fail("Error : " + foo.getMessage()
 95             + "\n Default Collator creation failed.");
 96         }
 97         System.out.println("Default collation property test ended.");
 98         System.out.println("Collator.getRules() testing ...");
 99         doAssert(((RuleBasedCollator)col).getRules().length() != 0, "getRules() result incorrect" );
100         System.out.println("getRules tests end.");
101         try {
102             col = Collator.getInstance(Locale.FRENCH);
103             col.setStrength(Collator.PRIMARY);
104             System.out.println("testing Collator.getStrength() method again ...");
105             doAssert(col.getStrength() != Collator.TERTIARY, "collation object has the wrong strength");
106             doAssert(col.getStrength() == Collator.PRIMARY, "collation object's strength is not primary difference");
107 
108             System.out.println("testing French Collator.setStrength() method ...");
109             col.setStrength(Collator.TERTIARY);
110             doAssert(col.getStrength() == Collator.TERTIARY, "collation object's strength is not tertiary difference");
111             doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
112             doAssert(col.getStrength() != Collator.SECONDARY, "collation object's strength is secondary difference");
113 
114         } catch (Exception bar) {
115             fail("Error :  " + bar.getMessage()
116             + "\n Creating French collation failed.");
117         }
118 
119         System.out.println("Create junk collation: ");
120         Locale abcd = Locale.of("ab", "CD");
121         Collator junk = null;
122         try {
123             junk = Collator.getInstance(abcd);
124         } catch (Exception err) {
125             fail("Error : " + err.getMessage()
126             + "\n Junk collation creation failed, should at least return the collator for the base bundle.");
127         }
128         try {
129             col = Collator.getInstance(Locale.ROOT);
130             doAssert(col.equals(junk), "The base bundle's collation should be returned.");
131         } catch (Exception exc) {
132             fail("Error : " + exc.getMessage()
133             + "\n Default collation comparison, caching not working.");
134         }
135 
136         System.out.println("Collator property test ended.");
137     }
138 
139     @Test
140     public final void TestHashCode( )
141     {
142         System.out.println("hashCode tests begin.");
143         Collator col1 = null;
144         try {
145             col1 = Collator.getInstance(Locale.ROOT);
146         } catch (Exception foo) {
147             fail("Error : " + foo.getMessage()
148             + "\n Default collation creation failed.");
149         }
150         Collator col2 = null;
151         Locale dk = Locale.of("da", "DK");
152         try {
153             col2 = Collator.getInstance(dk);
154         } catch (Exception bar) {
155             fail("Error : " + bar.getMessage()
156             + "\n Danish collation creation failed.");
157             return;
158         }
159         Collator col3 = null;
160         try {
161             col3 = Collator.getInstance(Locale.ROOT);
162         } catch (Exception err) {
163             fail("Error : " + err.getMessage()
164             + "\n 2nd default collation creation failed.");
165         }
166         System.out.println("Collator.hashCode() testing ...");
167 
168         if (col1 != null) {
169             doAssert(col1.hashCode() != col2.hashCode(), "Hash test1 result incorrect");
170             if (col3 != null) {
171                 doAssert(col1.hashCode() == col3.hashCode(), "Hash result not equal");
172             }
173         }
174 
175         System.out.println("hashCode tests end.");
176     }
177 
178     //----------------------------------------------------------------------------
179     // ctor -- Tests the constructor methods
180     //
181     @Test
182     public final void TestCollationKey( )
183     {
184         System.out.println("testing CollationKey begins...");
185         Collator col = null;
186         try {
187             col = Collator.getInstance(Locale.ROOT);
188         } catch (Exception foo) {
189             fail("Error : " + foo.getMessage()
190             + "\n Default collation creation failed.");
191         }
192         if (col == null) {
193             return;
194         }
195 
196         String test1 = "Abcda", test2 = "abcda";
197         System.out.println("Use tertiary comparison level testing ....");
198         CollationKey sortk1 = col.getCollationKey(test1);
199         CollationKey sortk2 = col.getCollationKey(test2);
200         doAssert(sortk1.compareTo(sortk2) > 0,
201                     "Result should be \"Abcda\" >>> \"abcda\"");
202         CollationKey sortk3 = sortk2;
203         CollationKey sortkNew = sortk1;
204         doAssert(sortk1 != sortk2, "The sort keys should be different");
205         doAssert(sortk1.hashCode() != sortk2.hashCode(), "sort key hashCode() failed");
206         doAssert(sortk2.compareTo(sortk3) == 0, "The sort keys should be the same");
207         doAssert(sortk1 == sortkNew, "The sort keys assignment failed");
208         doAssert(sortk1.hashCode() == sortkNew.hashCode(), "sort key hashCode() failed");
209         doAssert(sortkNew != sortk3, "The sort keys should be different");
210         doAssert(sortk1.compareTo(sortk3) > 0, "Result should be \"Abcda\" >>> \"abcda\"");
211         doAssert(sortk2.compareTo(sortk3) == 0, "Result should be \"abcda\" == \"abcda\"");
212         long    cnt1, cnt2;
213         byte byteArray1[] = sortk1.toByteArray();
214         byte byteArray2[] = sortk2.toByteArray();
215         doAssert(byteArray1 != null && byteArray2 != null, "CollationKey.toByteArray failed.");
216         System.out.println("testing sortkey ends...");
217     }
218     //----------------------------------------------------------------------------
219     // ctor -- Tests the constructor methods
220     //
221     @Test
222     public final void TestElemIter( )
223     {
224         System.out.println("testing sortkey begins...");
225         Collator col = null;
226         try {
227             col = Collator.getInstance();
228         } catch (Exception foo) {
229             fail("Error : " + foo.getMessage()
230             + "\n Default collation creation failed.");
231         }
232         RuleBasedCollator rbCol;
233         if (col instanceof RuleBasedCollator) {
234             rbCol = (RuleBasedCollator) col;
235         } else {
236             return;
237         }
238         String testString1 = "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?";
239         String testString2 = "Xf ile What subset of all possible test cases has the lowest probability of detecting the least errors?";
240         System.out.println("Constructors and comparison testing....");
241         CollationElementIterator iterator1 = rbCol.getCollationElementIterator(testString1);
242         CollationElementIterator iterator2 = rbCol.getCollationElementIterator(testString1);
243         CollationElementIterator iterator3 = rbCol.getCollationElementIterator(testString2);
244         int order1, order2, order3;
245         order1 = iterator1.next();
246         order2 = iterator2.next();
247         doAssert(order1 == order2, "The order result should be the same");
248 
249         order3 = iterator3.next();
250         doAssert(CollationElementIterator.primaryOrder(order1)
251                      == CollationElementIterator.primaryOrder(order3),
252                  "The primary orders should be the same");
253         doAssert(CollationElementIterator.secondaryOrder(order1)
254                      == CollationElementIterator.secondaryOrder(order3),
255                  "The secondary orders should be the same");
256         doAssert(CollationElementIterator.tertiaryOrder(order1)
257                      == CollationElementIterator.tertiaryOrder(order3),
258                  "The tertiary orders should be the same");
259 
260         order1 = iterator1.next();
261         order3 = iterator3.next();
262         doAssert(CollationElementIterator.primaryOrder(order1)
263                      == CollationElementIterator.primaryOrder(order3),
264                  "The primary orders should be identical");
265         doAssert(CollationElementIterator.tertiaryOrder(order1)
266                      != CollationElementIterator.tertiaryOrder(order3),
267                  "The tertiary orders should be different");
268 
269         order1 = iterator1.next();
270         order3 = iterator3.next();
271         doAssert(CollationElementIterator.secondaryOrder(order1)
272                      != CollationElementIterator.secondaryOrder(order3),
273                  "The secondary orders should be different");
274         doAssert(order1 != CollationElementIterator.NULLORDER,
275                  "Unexpected end of iterator reached");
276 
277         iterator1.reset();
278         iterator2.reset();
279         iterator3.reset();
280         order1 = iterator1.next();
281         order2 = iterator2.next();
282         doAssert(order1 == order2, "The order result should be the same");
283 
284         order3 = iterator3.next();
285         doAssert(CollationElementIterator.primaryOrder(order1)
286                      == CollationElementIterator.primaryOrder(order3),
287                  "The orders should be the same");
288         doAssert(CollationElementIterator.secondaryOrder(order1)
289                      == CollationElementIterator.secondaryOrder(order3),
290                  "The orders should be the same");
291         doAssert(CollationElementIterator.tertiaryOrder(order1)
292                      == CollationElementIterator.tertiaryOrder(order3),
293                  "The orders should be the same");
294 
295         order1 = iterator1.next();
296         order2 = iterator2.next();
297         order3 = iterator3.next();
298         doAssert(CollationElementIterator.primaryOrder(order1)
299                      == CollationElementIterator.primaryOrder(order3),
300                  "The primary orders should be identical");
301         doAssert(CollationElementIterator.tertiaryOrder(order1)
302                      != CollationElementIterator.tertiaryOrder(order3),
303                  "The tertiary orders should be different");
304 
305         order1 = iterator1.next();
306         order3 = iterator3.next();
307         doAssert(CollationElementIterator.secondaryOrder(order1)
308                      != CollationElementIterator.secondaryOrder(order3),
309                  "The secondary orders should be different");
310         doAssert(order1 != CollationElementIterator.NULLORDER, "Unexpected end of iterator reached");
311         System.out.println("testing CollationElementIterator ends...");
312     }
313 
314     @Test
315     public final void TestGetAll()
316     {
317         Locale[] list = Collator.getAvailableLocales();
318         for (int i = 0; i < list.length; ++i) {
319             System.out.println("Locale name: ");
320             System.out.println(list[i].toString());
321             System.out.println(" , the display name is : ");
322             System.out.println(list[i].getDisplayName());
323         }
324     }
325 }