1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore.java.text; 18 19 import java.text.CharacterIterator; 20 import java.text.CollationElementIterator; 21 import java.text.Collator; 22 import java.text.ParseException; 23 import java.text.RuleBasedCollator; 24 import java.text.StringCharacterIterator; 25 import java.util.Locale; 26 27 public class CollatorTest extends junit.framework.TestCase { 28 public void test_setStrengthI() throws Exception { 29 Collator collator = Collator.getInstance(); 30 collator.setStrength(Collator.PRIMARY); 31 assertEquals(Collator.PRIMARY, collator.getStrength()); 32 collator.setStrength(Collator.SECONDARY); 33 assertEquals(Collator.SECONDARY, collator.getStrength()); 34 collator.setStrength(Collator.TERTIARY); 35 assertEquals(Collator.TERTIARY, collator.getStrength()); 36 collator.setStrength(Collator.IDENTICAL); 37 assertEquals(Collator.IDENTICAL, collator.getStrength()); 38 try { 39 collator.setStrength(-1); 40 fail("IllegalArgumentException was not thrown."); 41 } catch (IllegalArgumentException expected) { 42 } 43 } 44 45 public void test_stackCorruption() throws Exception { 46 // This used to crash Android. 47 Collator mColl = Collator.getInstance(); 48 mColl.setStrength(Collator.PRIMARY); 49 mColl.getCollationKey("2d294f2d3739433565147655394f3762f3147312d3731641452f310"); 50 } 51 52 public void test_collationKeySize() throws Exception { 53 // Test to verify that very large collation keys are not truncated. 54 StringBuilder b = new StringBuilder(); 55 for (int i = 0; i < 1024; i++) { 56 b.append("0123456789ABCDEF"); 57 } 58 String sixteen = b.toString(); 59 b.append("_THE_END"); 60 String sixteenplus = b.toString(); 61 62 Collator mColl = Collator.getInstance(); 63 mColl.setStrength(Collator.PRIMARY); 64 65 byte [] arr = mColl.getCollationKey(sixteen).toByteArray(); 66 int len = arr.length; 67 assertTrue("Collation key not 0 terminated", arr[arr.length - 1] == 0); 68 len--; 69 String foo = new String(arr, 0, len, "iso8859-1"); 70 71 arr = mColl.getCollationKey(sixteen).toByteArray(); 72 len = arr.length; 73 assertTrue("Collation key not 0 terminated", arr[arr.length - 1] == 0); 74 len--; 75 String bar = new String(arr, 0, len, "iso8859-1"); 76 77 assertTrue("Collation keys should differ", foo.equals(bar)); 78 } 79 80 public void test_decompositionCompatibility() throws Exception { 81 Collator myCollator = Collator.getInstance(); 82 myCollator.setDecomposition(Collator.NO_DECOMPOSITION); 83 assertFalse("Error: \u00e0\u0325 should not equal to a\u0325\u0300 without decomposition", 84 myCollator.compare("\u00e0\u0325", "a\u0325\u0300") == 0); 85 myCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION); 86 assertTrue("Error: \u00e0\u0325 should equal to a\u0325\u0300 with decomposition", 87 myCollator.compare("\u00e0\u0325", "a\u0325\u0300") == 0); 88 } 89 90 public void testEqualsObject() throws ParseException { 91 String rule = "&9 < a < b < c < d < e"; 92 RuleBasedCollator coll = new RuleBasedCollator(rule); 93 94 assertEquals(Collator.TERTIARY, coll.getStrength()); 95 assertEquals(Collator.NO_DECOMPOSITION, coll.getDecomposition()); 96 RuleBasedCollator other = new RuleBasedCollator(rule); 97 assertTrue(coll.equals(other)); 98 99 coll.setStrength(Collator.PRIMARY); 100 assertFalse(coll.equals(other)); 101 102 coll.setStrength(Collator.TERTIARY); 103 coll.setDecomposition(Collator.CANONICAL_DECOMPOSITION); 104 other.setDecomposition(Collator.NO_DECOMPOSITION); // See comment above. 105 assertFalse(coll.equals(other)); 106 } 107 108 public void test_Harmony_1352() throws Exception { 109 // Regression test for HARMONY-1352, that doesn't get run in the harmony test suite because 110 // of an earlier failure. 111 try { 112 new RuleBasedCollator("&9 < a< b< c< d").getCollationElementIterator((CharacterIterator) null); 113 fail("NullPointerException expected"); 114 } catch (NullPointerException expected) { 115 } 116 } 117 118 private void assertCollationElementIterator(CollationElementIterator it, Integer... offsets) { 119 for (int offset : offsets) { 120 assertEquals(offset, it.getOffset()); 121 it.next(); 122 } 123 assertEquals(CollationElementIterator.NULLORDER, it.next()); 124 } 125 126 private void assertGetCollationElementIteratorString(Locale l, String s, Integer... offsets) { 127 RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(l); 128 assertCollationElementIterator(coll.getCollationElementIterator(s), offsets); 129 } 130 131 private void assertGetCollationElementIteratorCharacterIterator(Locale l, String s, Integer... offsets) { 132 RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(l); 133 CharacterIterator it = new StringCharacterIterator(s); 134 assertCollationElementIterator(coll.getCollationElementIterator(it), offsets); 135 } 136 137 public void testGetCollationElementIteratorString_es() throws Exception { 138 assertGetCollationElementIteratorString(new Locale("es", "", ""), "cha", 0, 1, 2, 3); 139 } 140 141 public void testGetCollationElementIteratorString_de_DE() throws Exception { 142 assertGetCollationElementIteratorString(new Locale("de", "DE", ""), "\u00e6b", 0, 1, 1, 2); 143 } 144 145 public void testGetCollationElementIteratorCharacterIterator_es() throws Exception { 146 assertGetCollationElementIteratorCharacterIterator(new Locale("es", "", ""), "cha", 0, 1, 2, 3); 147 } 148 149 public void testGetCollationElementIteratorCharacterIterator_de_DE() throws Exception { 150 assertGetCollationElementIteratorCharacterIterator(new Locale("de", "DE", ""), "\u00e6b", 0, 1, 1, 2); 151 } 152 } 153