1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 * License for the specific language governing permissions and limitations under 15 * the License. 16 */ 17 18 package org.apache.harmony.tests.java.text; 19 20 import java.text.CharacterIterator; 21 import java.text.CollationElementIterator; 22 import java.text.CollationKey; 23 import java.text.Collator; 24 import java.text.ParseException; 25 import java.text.RuleBasedCollator; 26 import java.text.StringCharacterIterator; 27 import java.util.Locale; 28 29 import junit.framework.TestCase; 30 31 public class RuleBasedCollatorTest extends TestCase { 32 33 public void test_getCollationKeyLjava_lang_String() throws Exception { 34 // Regression test for HARMONY-28 35 String source = null; 36 String Simple = "&9 < a< b< c< d"; 37 RuleBasedCollator rbc = new RuleBasedCollator(Simple); 38 CollationKey ck = rbc.getCollationKey(source); 39 assertNull("Assert 1: getCollationKey (null) does not return null", ck); 40 } 41 42 public void testHashCode() throws ParseException { 43 String rule = "&9 < a < b < c < d"; 44 45 RuleBasedCollator coll = new RuleBasedCollator(rule); 46 RuleBasedCollator same = new RuleBasedCollator(rule); 47 assertEquals(coll.hashCode(), same.hashCode()); 48 } 49 50 public void testClone() throws ParseException { 51 RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(Locale.US); 52 RuleBasedCollator clone = (RuleBasedCollator) coll.clone(); 53 assertNotSame(coll, clone); 54 assertEquals(coll.getRules(), clone.getRules()); 55 assertEquals(coll.getDecomposition(), clone.getDecomposition()); 56 assertEquals(coll.getStrength(), clone.getStrength()); 57 } 58 59 public void testEqualsObject() throws ParseException { 60 String rule = "&9 < a < b < c < d < e"; 61 RuleBasedCollator coll = new RuleBasedCollator(rule); 62 63 assertEquals(Collator.TERTIARY, coll.getStrength()); 64 assertEquals(Collator.NO_DECOMPOSITION, coll.getDecomposition()); 65 RuleBasedCollator other = new RuleBasedCollator(rule); 66 assertTrue(coll.equals(other)); 67 68 coll.setStrength(Collator.PRIMARY); 69 assertFalse(coll.equals(other)); 70 71 coll.setStrength(Collator.TERTIARY); 72 coll.setDecomposition(Collator.CANONICAL_DECOMPOSITION); 73 assertFalse(coll.equals(other)); 74 } 75 76 public void testCompareStringString() throws ParseException { 77 String rule = "&9 < c < b < a"; 78 RuleBasedCollator coll = new RuleBasedCollator(rule); 79 assertEquals(-1, coll.compare("c", "a")); 80 } 81 82 public void testGetCollationKey() { 83 RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(Locale.GERMAN); 84 String source = "abc"; 85 CollationKey key1 = coll.getCollationKey(source); 86 assertEquals(source, key1.getSourceString()); 87 String source2 = "abb"; 88 CollationKey key2 = coll.getCollationKey(source2); 89 assertEquals(source2, key2.getSourceString()); 90 assertTrue(key1.compareTo(key2) > 0); 91 assertTrue(coll.compare(source, source2) > 0); 92 } 93 94 public void testGetRules() throws ParseException { 95 String rule = "&9 < a = b < c"; 96 RuleBasedCollator coll = new RuleBasedCollator(rule); 97 assertEquals(rule, coll.getRules()); 98 } 99 100 public void testGetCollationElementIteratorString() throws Exception { 101 { 102 Locale locale = Locale.forLanguageTag("es-u-co-trad"); 103 RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(locale); 104 String source = "cha"; 105 CollationElementIterator iterator = coll.getCollationElementIterator(source); 106 int[] e_offset = { 0, 2, 3 }; 107 int offset = iterator.getOffset(); 108 int i = 0; 109 assertEquals(e_offset[i++], offset); 110 while (offset != source.length()) { 111 iterator.next(); 112 offset = iterator.getOffset(); 113 assertEquals(e_offset[i++], offset); 114 } 115 assertEquals(CollationElementIterator.NULLORDER, iterator.next()); 116 } 117 118 { 119 Locale locale = new Locale("de", "DE"); 120 RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(locale); 121 String source = "\u00fcb"; 122 CollationElementIterator iterator = coll.getCollationElementIterator(source); 123 int[] e_offset = { 0, 1, 1, 2 }; 124 int offset = iterator.getOffset(); 125 int i = 0; 126 assertEquals(e_offset[i++], offset); 127 while (offset != source.length()) { 128 iterator.next(); 129 offset = iterator.getOffset(); 130 assertEquals(e_offset[i++], offset); 131 } 132 assertEquals(CollationElementIterator.NULLORDER, iterator.next()); 133 } 134 //Regression for HARMONY-1352 135 try { 136 new RuleBasedCollator("&9 < a< b< c< d").getCollationElementIterator((String)null); 137 fail(); 138 } catch (NullPointerException expected) { 139 } 140 } 141 142 public void testGetCollationElementIteratorCharacterIterator() throws Exception { 143 { 144 Locale locale = Locale.forLanguageTag("es-u-co-trad"); 145 RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(locale); 146 String text = "cha"; 147 StringCharacterIterator source = new StringCharacterIterator(text); 148 CollationElementIterator iterator = coll.getCollationElementIterator(source); 149 int[] e_offset = { 0, 2, 3 }; 150 int offset = iterator.getOffset(); 151 int i = 0; 152 assertEquals(e_offset[i++], offset); 153 while (offset != text.length()) { 154 iterator.next(); 155 offset = iterator.getOffset(); 156 // System.out.println(offset); 157 assertEquals(e_offset[i++], offset); 158 } 159 assertEquals(CollationElementIterator.NULLORDER, iterator.next()); 160 } 161 162 { 163 Locale locale = new Locale("de", "DE"); 164 RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(locale); 165 String text = "\u00fcb"; 166 StringCharacterIterator source = new StringCharacterIterator(text); 167 CollationElementIterator iterator = coll.getCollationElementIterator(source); 168 int[] e_offset = { 0, 1, 1, 2 }; 169 int offset = iterator.getOffset(); 170 int i = 0; 171 assertEquals(e_offset[i++], offset); 172 while (offset != text.length()) { 173 iterator.next(); 174 offset = iterator.getOffset(); 175 assertEquals(e_offset[i++], offset); 176 } 177 assertEquals(CollationElementIterator.NULLORDER, iterator.next()); 178 } 179 //Regression for HARMONY-1352 180 try { 181 new RuleBasedCollator("&9 < a< b< c< d").getCollationElementIterator((CharacterIterator)null); 182 fail(); 183 } catch (NullPointerException expected) { 184 } 185 } 186 187 public void testStrength() { 188 RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(Locale.US); 189 for (int i = 0; i < 4; i++) { 190 coll.setStrength(i); 191 assertEquals(i, coll.getStrength()); 192 } 193 } 194 195 public void testDecomposition() { 196 RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(Locale.US); 197 for (int i = 0; i < 2; i++) { 198 coll.setDecomposition(i); 199 assertEquals(i, coll.getDecomposition()); 200 } 201 } 202 203 public void testCollator_GetInstance() { 204 Collator coll = Collator.getInstance(); 205 Object obj1 = "a"; 206 Object obj2 = "b"; 207 assertEquals(-1, coll.compare(obj1, obj2)); 208 209 Collator.getInstance(); 210 assertFalse(coll.equals("A", "\uFF21")); 211 } 212 213 public void testGetAvailableLocales() { 214 assertTrue(Collator.getAvailableLocales().length > 0); 215 } 216 217 // Test CollationKey 218 public void testCollationKey() { 219 Collator coll = Collator.getInstance(Locale.US); 220 String text = "abc"; 221 CollationKey key = coll.getCollationKey(text); 222 key.hashCode(); 223 224 CollationKey key2 = coll.getCollationKey("abc"); 225 226 assertEquals(0, key.compareTo(key2)); 227 } 228 229 public void testNullPointerException() throws Exception { 230 //Regression for HARMONY-241 231 try { 232 new RuleBasedCollator(null); 233 fail(); 234 } catch (NullPointerException expected) { 235 } 236 } 237 238 public void testCompareNull() throws Exception { 239 //Regression for HARMONY-836 240 try { 241 new RuleBasedCollator("&9 < a").compare(null, null); 242 fail(); 243 } catch (NullPointerException expected) { 244 } 245 } 246 247 public void testEmptyRules() throws Exception { 248 new RuleBasedCollator(""); 249 new RuleBasedCollator(" "); 250 new RuleBasedCollator("# This is a comment."); 251 } 252 } 253