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