diff options
Diffstat (limited to 'container-search/src/test/java/com/yahoo/search/query/SortingTestCase.java')
-rw-r--r-- | container-search/src/test/java/com/yahoo/search/query/SortingTestCase.java | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/container-search/src/test/java/com/yahoo/search/query/SortingTestCase.java b/container-search/src/test/java/com/yahoo/search/query/SortingTestCase.java new file mode 100644 index 00000000000..e7a0c78aa88 --- /dev/null +++ b/container-search/src/test/java/com/yahoo/search/query/SortingTestCase.java @@ -0,0 +1,88 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.query; + +import com.ibm.icu.lang.UScript; +import com.ibm.icu.text.Collator; +import com.ibm.icu.text.RuleBasedCollator; +import com.ibm.icu.util.ULocale; +import org.junit.Ignore; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; + +/** + * @author balder + */ +public class SortingTestCase { + @Test + public void validAttributeName() { + assertNotNull(Sorting.fromString("a")); + assertNotNull(Sorting.fromString("_a")); + assertNotNull(Sorting.fromString("+a")); + assertNotNull(Sorting.fromString("-a")); + assertNotNull(Sorting.fromString("a.b")); + try { + assertNotNull(Sorting.fromString("-1")); + fail("'-1' should not be allowed as attribute name."); + } catch (IllegalArgumentException e) { + assertEquals(e.getMessage(), "Illegal attribute name '1' for sorting. Requires '[\\[]*[a-zA-Z_][\\.a-zA-Z0-9_-]*[\\]]*'"); + } catch (Exception e) { + fail("I only expect 'IllegalArgumentException', not: + " + e.toString()); + } + } + @Test + public void requireThatChineseSortCorrect() { + requireThatChineseHasCorrectRules(Collator.getInstance(new ULocale("zh"))); + Sorting ch = Sorting.fromString("uca(a,zh)"); + assertEquals(1, ch.fieldOrders().size()); + Sorting.FieldOrder fo = ch.fieldOrders().get(0); + assertTrue(fo.getSorter() instanceof Sorting.UcaSorter); + Sorting.UcaSorter uca = (Sorting.UcaSorter) fo.getSorter(); + requireThatChineseHasCorrectRules(uca.getCollator()); + Sorting.AttributeSorter sorter = fo.getSorter(); + assertTrue(sorter.compare("a", "b") < 0); + assertTrue(sorter.compare("a", "a\u81EA") < 0); + assertTrue(sorter.compare("\u81EA", "a") < 0); + } + + private void requireThatArabicHasCorrectRules(Collator col) { + final int reorderCodes [] = {UScript.ARABIC}; + assertEquals("6.2.0.0", col.getUCAVersion().toString()); + assertEquals("58.0.0.6", col.getVersion().toString()); + assertEquals(Arrays.toString(reorderCodes), Arrays.toString(col.getReorderCodes())); + assertTrue(col.compare("a", "b") < 0); + assertTrue(col.compare("a", "aس") < 0); + assertFalse(col.compare("س", "a") < 0); + + assertEquals(" [reorder Arab]&ت<<ة<<<ﺔ<<<ﺓ&ي<<ى<<<ﯨ<<<ﯩ<<<ﻰ<<<ﻯ<<<ﲐ<<<ﱝ", ((RuleBasedCollator) col).getRules()); + assertFalse(col.compare("س", "a") < 0); + } + + private void requireThatChineseHasCorrectRules(Collator col) { + final int reorderCodes [] = {UScript.HAN}; + assertEquals("8.0.0.0", col.getUCAVersion().toString()); + assertEquals("153.64.29.0", col.getVersion().toString()); + assertEquals(Arrays.toString(reorderCodes), Arrays.toString(col.getReorderCodes())); + + assertNotEquals("", ((RuleBasedCollator) col).getRules()); + } + @Test + @Ignore + public void requireThatArabicSortCorrect() { + requireThatArabicHasCorrectRules(Collator.getInstance(new ULocale("ar"))); + Sorting ar = Sorting.fromString("uca(a,ar)"); + assertEquals(1, ar.fieldOrders().size()); + Sorting.FieldOrder fo = ar.fieldOrders().get(0); + assertTrue(fo.getSorter() instanceof Sorting.UcaSorter); + Sorting.UcaSorter uca = (Sorting.UcaSorter) fo.getSorter(); + requireThatArabicHasCorrectRules(uca.getCollator()); + Sorting.AttributeSorter sorter = fo.getSorter(); + assertTrue(sorter.compare("a", "b") < 0); + assertTrue(sorter.compare("a", "aس") < 0); + assertTrue(sorter.compare("س", "a") < 0); + } + +} |