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