Path: blob/master/test/jdk/java/text/Collator/RuleBasedCollatorTest.java
41149 views
/*1* Copyright (c) 2019, 2021, 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* @bug 4406815 8222969 826678426* @summary RuleBasedCollatorTest uses very limited but selected test data27* to test basic functionalities provided by RuleBasedCollator.28* @run testng/othervm RuleBasedCollatorTest29*/3031import java.text.CollationElementIterator;32import java.text.CollationKey;33import java.text.RuleBasedCollator;34import java.text.Collator;35import java.text.ParseException;36import java.util.Arrays;37import java.util.Locale;3839import org.testng.annotations.BeforeClass;40import org.testng.annotations.DataProvider;41import org.testng.annotations.Test;42import org.testng.SkipException;43import static org.testng.Assert.*;4445public class RuleBasedCollatorTest {4647static RuleBasedCollator USC;48static String US_RULES;4950@BeforeClass51public void setup() {52Collator c = Collator.getInstance(Locale.US);53if (!(c instanceof RuleBasedCollator)) {54throw new SkipException("skip tests.");55}56USC = (RuleBasedCollator) c;57US_RULES = USC.getRules();58}596061@DataProvider(name = "rulesData")62Object[][] rulesData() {63//Basic Tailor64String BASIC_TAILOR_RULES = "< b=c<\u00e6;A,a";65String[] BASIC_TAILOR_DATA = {"\u00e6", "b", "a", "c", "A"};66String[] BASIC_TAILOR_EXPECTED = {"b", "c", "\u00e6", "A", "a"};6768//Contraction69String CONTRACTION_RULES = US_RULES + "& b < ch ,cH, Ch, CH < c ";70String[] CONTRACTION_DATA = {"b", "c", "ch", "CH", "Ch", "cH"};71String[] CONTRACTION_EXPECTED = {"b", "ch", "cH", "Ch", "CH", "c"};7273//Expansion74String EXPANSION_RULES = US_RULES + "& ae = \u00e4 < b";75String[] EXPANSION_DATA = {"ad", "af", "\u00e4"};76String[] EXPANSION_EXPECTED = {"ad", "\u00e4", "af"};7778//Punctuation79String PUNCTUATION_RULES = US_RULES + "< ' ' < '-'";80String[] PUNCTUATION_DATA = {"b-w", "b-W", "B-w", "B-W", "bW", "bw",81"Bw", "BW", "b w", "b W", "B w", "B W"};82String[] PUNCTUATION_EXPECTED = {"bw", "bW", "Bw", "BW", "b w", "b W",83"B w", "B W", "b-w", "b-W", "B-w", "B-W"};8485return new Object[][] {86{BASIC_TAILOR_RULES, BASIC_TAILOR_DATA, BASIC_TAILOR_EXPECTED},87{CONTRACTION_RULES, CONTRACTION_DATA, CONTRACTION_EXPECTED},88{EXPANSION_RULES, EXPANSION_DATA, EXPANSION_EXPECTED},89{PUNCTUATION_RULES, PUNCTUATION_DATA, PUNCTUATION_EXPECTED}90};91}9293@Test(dataProvider = "rulesData")94public void testRules(String rules, String[] testData, String[] expected)95throws ParseException {96Arrays.sort(testData, new RuleBasedCollator(rules));97assertEquals(testData, expected);9899}100101@DataProvider(name = "FrenchSecondarySort")102Object[][] FrenchSecondarySort() {103return new Object[][] {104{ "\u0061\u00e1\u0061", "\u00e1\u0061\u0061", 1 },105//{"\u0061\u00e1", "\u00e1\u0041", 1}, //JDK-4406815106//{"\u00e1\u0041", "\u0061\u00e1", -1}, //JDK-4406815107{"\u1ea0a", "\u1ea2A", -1}, //case ignore108{ "\u1ea0b", "\u1ea2A", 1 }, //primary overwrite109{ "\u1e15", "\u1e1d", -1 }, //ignore sec diacritic110{ "a", "\u1ea1", -1 } };111}112113@Test(dataProvider = "FrenchSecondarySort")114public void testFrenchSecondarySort(String sData, String tData,115int expected) throws ParseException {116String french_rule = "@";117String rules = US_RULES + french_rule;118RuleBasedCollator rc = new RuleBasedCollator(rules);119int result = rc.compare(sData, tData);120assertEquals(expected, result);121}122123@DataProvider(name = "ThaiLaoVowelConsonantSwapping")124Object[][] ThaiLaoVowelConsonantSwapping() {125return new Object[][] {{"\u0e44\u0e01", "\u0e40\u0e2e", -1},//swap126{"\u0e2e\u0e40", "\u0e01\u0e44", 1},//no swap127{"\u0e44\u0061", "\u0e40\u0081", 1}//no swap128};129}130131@Test(dataProvider = "ThaiLaoVowelConsonantSwapping")132public void testThaiLaoVowelConsonantSwapping(String sData, String tData,133int expected) throws ParseException {134String thai_rule = "& Z < \u0e01 < \u0e2e <\u0e40 < \u0e44!";135String rules = US_RULES + thai_rule;136RuleBasedCollator rc = new RuleBasedCollator(rules);137int result = rc.compare(sData, tData);138assertEquals(expected, result);139}140141@Test142public void testIgnorableCharacter() throws ParseException {143String rule = "=f<a<c";144RuleBasedCollator rc = new RuleBasedCollator(rule);145CollationElementIterator iter = rc.getCollationElementIterator("f");146int element = iter.next();147int primary = iter.primaryOrder(element);148assertEquals(primary, 0);149}150151@DataProvider(name = "Normalization")152Object[][] Normalization() {153return new Object[][] {154//micro sign has no canonical decomp mapping155// 0:NO_Decomposition;156// 1:CANONICAL_Decomposition;157// 2:FULL_Decomposition158{"\u00b5", "\u03BC", 0, -1},159{"\u00b5", "\u03BC", 1, -1},160{"\u00b5", "\u03BC", 2, 0}161};162}163164@Test(dataProvider = "Normalization")165public void testNormalization(String sData, String tData, int decomp,166int result) {167RuleBasedCollator rc = (RuleBasedCollator)USC.clone();168rc.setDecomposition(decomp);169assertEquals(rc.compare(sData, tData), result);170}171172@Test173public void testEquality() throws ParseException {174String rule1 = "<a=b";175RuleBasedCollator rc1= new RuleBasedCollator(rule1);176//test equals()177assertTrue(rc1.equals(new RuleBasedCollator(rule1)));178179//test semantic equality180String[] array1 = {"b", "c", "a"};181String[] array2 = Arrays.copyOf(array1, array1.length);182String[] expected = {"b", "a", "c"};183String rule2 = "<b=a";184RuleBasedCollator rc2= new RuleBasedCollator(rule2);185186Arrays.sort(array1, rc1);187Arrays.sort(array2, rc2);188assertEquals(array1, array2);189assertEquals(array1, expected);190}191192@Test193public void testBasicParsingOrder() throws ParseException {194String rule1 = "< a < b & a < c";195String rule2 = "< a < c & a < b";196String rule3 = "< a < b < c";197String s = "abc";198RuleBasedCollator c1 = new RuleBasedCollator(rule1);199RuleBasedCollator c2 = new RuleBasedCollator(rule2);200RuleBasedCollator c3 = new RuleBasedCollator(rule3);201CollationKey k1 = c1.getCollationKey(s);202CollationKey k2 = c2.getCollationKey(s);203CollationKey k3 = c3.getCollationKey(s);204//rule1 should not equals to rule2205assertEquals(k1.compareTo(k2) == 0, false);206207//rule2 should equals to rule3208assertEquals(k2.compareTo(k3) == 0, true);209}210211@DataProvider(name = "ParseData")212Object[][] ParseData() {213return new Object[][] {214{""},215{"a < b"},216{"< a-b < c"},217{"< ,a"},218{"< a < b & c < d"}219};220}221222@Test(dataProvider = "ParseData",223expectedExceptions = ParseException.class)224public void testParseException(String rule) throws ParseException{225new RuleBasedCollator(rule);226}227228@Test(expectedExceptions = NullPointerException.class)229public void testNullParseException() throws ParseException{230new RuleBasedCollator(null);231}232}233234235