Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/text/Collator/APITest.java
47182 views
/*1* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @library /java/text/testlib26* @summary test Collation API27*/28/*29(C) Copyright Taligent, Inc. 1996 - All Rights Reserved30(C) Copyright IBM Corp. 1996 - All Rights Reserved3132The original version of this source code and documentation is copyrighted and33owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are34provided under terms of a License Agreement between Taligent and Sun. This35technology is protected by multiple US and International patents. This notice and36attribution to Taligent may not be removed.37Taligent is a registered trademark of Taligent, Inc.38*/3940import java.util.Locale;41import java.text.Collator;42import java.text.RuleBasedCollator;43import java.text.CollationKey;44import java.text.CollationElementIterator;4546public class APITest extends CollatorTest {4748public static void main(String[] args) throws Exception {49new APITest().run(args);50}5152final void doAssert(boolean condition, String message)53{54if (!condition) {55err("ERROR: ");56errln(message);57}58}5960public final void TestProperty( )61{62Collator col = null;63try {64col = Collator.getInstance(Locale.ROOT);65logln("The property tests begin : ");66logln("Test ctors : ");67doAssert(col.compare("ab", "abc") < 0, "ab < abc comparison failed");68doAssert(col.compare("ab", "AB") < 0, "ab < AB comparison failed");69doAssert(col.compare("black-bird", "blackbird") > 0, "black-bird > blackbird comparison failed");70doAssert(col.compare("black bird", "black-bird") < 0, "black bird < black-bird comparison failed");71doAssert(col.compare("Hello", "hello") > 0, "Hello > hello comparison failed");7273logln("Test ctors ends.");74logln("testing Collator.getStrength() method ...");75doAssert(col.getStrength() == Collator.TERTIARY, "collation object has the wrong strength");76doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");7778logln("testing Collator.setStrength() method ...");79col.setStrength(Collator.SECONDARY);80doAssert(col.getStrength() != Collator.TERTIARY, "collation object's strength is secondary difference");81doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");82doAssert(col.getStrength() == Collator.SECONDARY, "collation object has the wrong strength");8384logln("testing Collator.setDecomposition() method ...");85col.setDecomposition(Collator.NO_DECOMPOSITION);86doAssert(col.getDecomposition() != Collator.FULL_DECOMPOSITION, "collation object's strength is secondary difference");87doAssert(col.getDecomposition() != Collator.CANONICAL_DECOMPOSITION, "collation object's strength is primary difference");88doAssert(col.getDecomposition() == Collator.NO_DECOMPOSITION, "collation object has the wrong strength");89} catch (Exception foo) {90errln("Error : " + foo.getMessage());91errln("Default Collator creation failed.");92}93logln("Default collation property test ended.");94logln("Collator.getRules() testing ...");95doAssert(((RuleBasedCollator)col).getRules().length() != 0, "getRules() result incorrect" );96logln("getRules tests end.");97try {98col = Collator.getInstance(Locale.FRENCH);99col.setStrength(Collator.PRIMARY);100logln("testing Collator.getStrength() method again ...");101doAssert(col.getStrength() != Collator.TERTIARY, "collation object has the wrong strength");102doAssert(col.getStrength() == Collator.PRIMARY, "collation object's strength is not primary difference");103104logln("testing French Collator.setStrength() method ...");105col.setStrength(Collator.TERTIARY);106doAssert(col.getStrength() == Collator.TERTIARY, "collation object's strength is not tertiary difference");107doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");108doAssert(col.getStrength() != Collator.SECONDARY, "collation object's strength is secondary difference");109110} catch (Exception bar) {111errln("Error : " + bar.getMessage());112errln("Creating French collation failed.");113}114115logln("Create junk collation: ");116Locale abcd = new Locale("ab", "CD", "");117Collator junk = null;118try {119junk = Collator.getInstance(abcd);120} catch (Exception err) {121errln("Error : " + err.getMessage());122errln("Junk collation creation failed, should at least return the collator for the base bundle.");123}124try {125col = Collator.getInstance(Locale.ROOT);126doAssert(col.equals(junk), "The base bundle's collation should be returned.");127} catch (Exception exc) {128errln("Error : " + exc.getMessage());129errln("Default collation comparison, caching not working.");130}131132logln("Collator property test ended.");133}134135public final void TestHashCode( )136{137logln("hashCode tests begin.");138Collator col1 = null;139try {140col1 = Collator.getInstance(Locale.ROOT);141} catch (Exception foo) {142errln("Error : " + foo.getMessage());143errln("Default collation creation failed.");144}145Collator col2 = null;146Locale dk = new Locale("da", "DK", "");147try {148col2 = Collator.getInstance(dk);149} catch (Exception bar) {150errln("Error : " + bar.getMessage());151errln("Danish collation creation failed.");152return;153}154Collator col3 = null;155try {156col3 = Collator.getInstance(Locale.ROOT);157} catch (Exception err) {158errln("Error : " + err.getMessage());159errln("2nd default collation creation failed.");160}161logln("Collator.hashCode() testing ...");162163if (col1 != null) {164doAssert(col1.hashCode() != col2.hashCode(), "Hash test1 result incorrect");165if (col3 != null) {166doAssert(col1.hashCode() == col3.hashCode(), "Hash result not equal");167}168}169170logln("hashCode tests end.");171}172173//----------------------------------------------------------------------------174// ctor -- Tests the constructor methods175//176public final void TestCollationKey( )177{178logln("testing CollationKey begins...");179Collator col = null;180try {181col = Collator.getInstance(Locale.ROOT);182} catch (Exception foo) {183errln("Error : " + foo.getMessage());184errln("Default collation creation failed.");185}186if (col == null) {187return;188}189190String test1 = "Abcda", test2 = "abcda";191logln("Use tertiary comparison level testing ....");192CollationKey sortk1 = col.getCollationKey(test1);193CollationKey sortk2 = col.getCollationKey(test2);194doAssert(sortk1.compareTo(sortk2) > 0,195"Result should be \"Abcda\" >>> \"abcda\"");196CollationKey sortk3 = sortk2;197CollationKey sortkNew = sortk1;198doAssert(sortk1 != sortk2, "The sort keys should be different");199doAssert(sortk1.hashCode() != sortk2.hashCode(), "sort key hashCode() failed");200doAssert(sortk2.compareTo(sortk3) == 0, "The sort keys should be the same");201doAssert(sortk1 == sortkNew, "The sort keys assignment failed");202doAssert(sortk1.hashCode() == sortkNew.hashCode(), "sort key hashCode() failed");203doAssert(sortkNew != sortk3, "The sort keys should be different");204doAssert(sortk1.compareTo(sortk3) > 0, "Result should be \"Abcda\" >>> \"abcda\"");205doAssert(sortk2.compareTo(sortk3) == 0, "Result should be \"abcda\" == \"abcda\"");206long cnt1, cnt2;207byte byteArray1[] = sortk1.toByteArray();208byte byteArray2[] = sortk2.toByteArray();209doAssert(byteArray1 != null && byteArray2 != null, "CollationKey.toByteArray failed.");210logln("testing sortkey ends...");211}212//----------------------------------------------------------------------------213// ctor -- Tests the constructor methods214//215public final void TestElemIter( )216{217logln("testing sortkey begins...");218Collator col = null;219try {220col = Collator.getInstance();221} catch (Exception foo) {222errln("Error : " + foo.getMessage());223errln("Default collation creation failed.");224}225RuleBasedCollator rbCol;226if (col instanceof RuleBasedCollator) {227rbCol = (RuleBasedCollator) col;228} else {229return;230}231String testString1 = "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?";232String testString2 = "Xf ile What subset of all possible test cases has the lowest probability of detecting the least errors?";233logln("Constructors and comparison testing....");234CollationElementIterator iterator1 = rbCol.getCollationElementIterator(testString1);235CollationElementIterator iterator2 = rbCol.getCollationElementIterator(testString1);236CollationElementIterator iterator3 = rbCol.getCollationElementIterator(testString2);237int order1, order2, order3;238order1 = iterator1.next();239order2 = iterator2.next();240doAssert(order1 == order2, "The order result should be the same");241242order3 = iterator3.next();243doAssert(CollationElementIterator.primaryOrder(order1)244== CollationElementIterator.primaryOrder(order3),245"The primary orders should be the same");246doAssert(CollationElementIterator.secondaryOrder(order1)247== CollationElementIterator.secondaryOrder(order3),248"The secondary orders should be the same");249doAssert(CollationElementIterator.tertiaryOrder(order1)250== CollationElementIterator.tertiaryOrder(order3),251"The tertiary orders should be the same");252253order1 = iterator1.next();254order3 = iterator3.next();255doAssert(CollationElementIterator.primaryOrder(order1)256== CollationElementIterator.primaryOrder(order3),257"The primary orders should be identical");258doAssert(CollationElementIterator.tertiaryOrder(order1)259!= CollationElementIterator.tertiaryOrder(order3),260"The tertiary orders should be different");261262order1 = iterator1.next();263order3 = iterator3.next();264doAssert(CollationElementIterator.secondaryOrder(order1)265!= CollationElementIterator.secondaryOrder(order3),266"The secondary orders should be different");267doAssert(order1 != CollationElementIterator.NULLORDER,268"Unexpected end of iterator reached");269270iterator1.reset();271iterator2.reset();272iterator3.reset();273order1 = iterator1.next();274order2 = iterator2.next();275doAssert(order1 == order2, "The order result should be the same");276277order3 = iterator3.next();278doAssert(CollationElementIterator.primaryOrder(order1)279== CollationElementIterator.primaryOrder(order3),280"The orders should be the same");281doAssert(CollationElementIterator.secondaryOrder(order1)282== CollationElementIterator.secondaryOrder(order3),283"The orders should be the same");284doAssert(CollationElementIterator.tertiaryOrder(order1)285== CollationElementIterator.tertiaryOrder(order3),286"The orders should be the same");287288order1 = iterator1.next();289order2 = iterator2.next();290order3 = iterator3.next();291doAssert(CollationElementIterator.primaryOrder(order1)292== CollationElementIterator.primaryOrder(order3),293"The primary orders should be identical");294doAssert(CollationElementIterator.tertiaryOrder(order1)295!= CollationElementIterator.tertiaryOrder(order3),296"The tertiary orders should be different");297298order1 = iterator1.next();299order3 = iterator3.next();300doAssert(CollationElementIterator.secondaryOrder(order1)301!= CollationElementIterator.secondaryOrder(order3),302"The secondary orders should be different");303doAssert(order1 != CollationElementIterator.NULLORDER, "Unexpected end of iterator reached");304logln("testing CollationElementIterator ends...");305}306307public final void TestGetAll()308{309Locale[] list = Collator.getAvailableLocales();310for (int i = 0; i < list.length; ++i) {311log("Locale name: ");312log(list[i].toString());313log(" , the display name is : ");314logln(list[i].getDisplayName());315}316}317}318319320