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, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17package org.apache.harmony.text.tests.java.text; 18 19import java.text.Collator; 20import java.text.ParseException; 21import java.text.RuleBasedCollator; 22import java.util.Locale; 23 24public class CollatorTest extends junit.framework.TestCase { 25 26 /** 27 * @tests java.text.Collator#clone() 28 */ 29 public void test_clone() { 30 Collator c = Collator.getInstance(Locale.GERMAN); 31 Collator c2 = (Collator) c.clone(); 32 assertTrue("Clones answered false to equals", c.equals(c2)); 33 assertTrue("Clones were equivalent", c != c2); 34 } 35 36 /** 37 * @tests java.text.Collator#compare(java.lang.Object, java.lang.Object) 38 */ 39 public void test_compareLjava_lang_ObjectLjava_lang_Object() { 40 Collator c = Collator.getInstance(Locale.FRENCH); 41 Object o, o2; 42 43 c.setStrength(Collator.IDENTICAL); 44 o = "E"; 45 o2 = "F"; 46 assertTrue("a) Failed on primary difference", c.compare(o, o2) < 0); 47 o = "e"; 48 o2 = "\u00e9"; 49 assertTrue("a) Failed on secondary difference", c.compare(o, o2) < 0); 50 o = "e"; 51 o2 = "E"; 52 assertTrue("a) Failed on tertiary difference", c.compare(o, o2) < 0); 53 o = "\u0001"; 54 o2 = "\u0002"; 55 assertTrue("a) Failed on identical", c.compare(o, o2) < 0); 56 o = "e"; 57 o2 = "e"; 58 assertEquals("a) Failed on equivalence", 0, c.compare(o, o2)); 59 assertTrue("a) Failed on primary expansion", 60 c.compare("\u01db", "v") < 0); 61 62 c.setStrength(Collator.TERTIARY); 63 o = "E"; 64 o2 = "F"; 65 assertTrue("b) Failed on primary difference", c.compare(o, o2) < 0); 66 o = "e"; 67 o2 = "\u00e9"; 68 assertTrue("b) Failed on secondary difference", c.compare(o, o2) < 0); 69 o = "e"; 70 o2 = "E"; 71 assertTrue("b) Failed on tertiary difference", c.compare(o, o2) < 0); 72 o = "\u0001"; 73 o2 = "\u0002"; 74 assertEquals("b) Failed on identical", 0, c.compare(o, o2)); 75 o = "e"; 76 o2 = "e"; 77 assertEquals("b) Failed on equivalence", 0, c.compare(o, o2)); 78 79 c.setStrength(Collator.SECONDARY); 80 o = "E"; 81 o2 = "F"; 82 assertTrue("c) Failed on primary difference", c.compare(o, o2) < 0); 83 o = "e"; 84 o2 = "\u00e9"; 85 assertTrue("c) Failed on secondary difference", c.compare(o, o2) < 0); 86 o = "e"; 87 o2 = "E"; 88 assertEquals("c) Failed on tertiary difference", 0, c.compare(o, o2)); 89 o = "\u0001"; 90 o2 = "\u0002"; 91 assertEquals("c) Failed on identical", 0, c.compare(o, o2)); 92 o = "e"; 93 o2 = "e"; 94 assertEquals("c) Failed on equivalence", 0, c.compare(o, o2)); 95 96 c.setStrength(Collator.PRIMARY); 97 o = "E"; 98 o2 = "F"; 99 assertTrue("d) Failed on primary difference", c.compare(o, o2) < 0); 100 o = "e"; 101 o2 = "\u00e9"; 102 assertEquals("d) Failed on secondary difference", 0, c.compare(o, o2)); 103 o = "e"; 104 o2 = "E"; 105 assertEquals("d) Failed on tertiary difference", 0, c.compare(o, o2)); 106 o = "\u0001"; 107 o2 = "\u0002"; 108 assertEquals("d) Failed on identical", 0, c.compare(o, o2)); 109 o = "e"; 110 o2 = "e"; 111 assertEquals("d) Failed on equivalence", 0, c.compare(o, o2)); 112 113 try { 114 c.compare("e", new StringBuffer("Blah")); 115 } catch (ClassCastException e) { 116 // correct 117 return; 118 } 119 fail("Failed to throw ClassCastException"); 120 } 121 122 /** 123 * @tests java.text.Collator#equals(java.lang.Object) 124 */ 125 public void test_equalsLjava_lang_Object() { 126 Collator c = Collator.getInstance(Locale.ENGLISH); 127 Collator c2 = (Collator) c.clone(); 128 assertTrue("Cloned collators not equal", c.equals(c2)); 129 c2.setStrength(Collator.SECONDARY); 130 assertTrue("Collators with different strengths equal", !c.equals(c2)); 131 } 132 133 /** 134 * @tests java.text.Collator#equals(java.lang.String, java.lang.String) 135 */ 136 public void test_equalsLjava_lang_StringLjava_lang_String() { 137 Collator c = Collator.getInstance(Locale.FRENCH); 138 139 c.setStrength(Collator.IDENTICAL); 140 assertTrue("a) Failed on primary difference", !c.equals("E", "F")); 141 assertTrue("a) Failed on secondary difference", !c 142 .equals("e", "\u00e9")); 143 assertTrue("a) Failed on tertiary difference", !c.equals("e", "E")); 144 assertTrue("a) Failed on identical", !c.equals("\u0001", "\u0002")); 145 assertTrue("a) Failed on equivalence", c.equals("e", "e")); 146 147 c.setStrength(Collator.TERTIARY); 148 assertTrue("b) Failed on primary difference", !c.equals("E", "F")); 149 assertTrue("b) Failed on secondary difference", !c 150 .equals("e", "\u00e9")); 151 assertTrue("b) Failed on tertiary difference", !c.equals("e", "E")); 152 assertTrue("b) Failed on identical", c.equals("\u0001", "\u0002")); 153 assertTrue("b) Failed on equivalence", c.equals("e", "e")); 154 155 c.setStrength(Collator.SECONDARY); 156 assertTrue("c) Failed on primary difference", !c.equals("E", "F")); 157 assertTrue("c) Failed on secondary difference", !c 158 .equals("e", "\u00e9")); 159 assertTrue("c) Failed on tertiary difference", c.equals("e", "E")); 160 assertTrue("c) Failed on identical", c.equals("\u0001", "\u0002")); 161 assertTrue("c) Failed on equivalence", c.equals("e", "e")); 162 163 c.setStrength(Collator.PRIMARY); 164 assertTrue("d) Failed on primary difference", !c.equals("E", "F")); 165 assertTrue("d) Failed on secondary difference", c.equals("e", "\u00e9")); 166 assertTrue("d) Failed on tertiary difference", c.equals("e", "E")); 167 assertTrue("d) Failed on identical", c.equals("\u0001", "\u0002")); 168 assertTrue("d) Failed on equivalence", c.equals("e", "e")); 169 } 170 171 /** 172 * @tests java.text.Collator#getAvailableLocales() 173 */ 174 //FIXME This test fails on Harmony ClassLibrary 175 public void failing_test_getAvailableLocales() { 176 Locale[] locales = Collator.getAvailableLocales(); 177 assertTrue("No locales", locales.length > 0); 178 boolean english = false, german = false; 179 for (int i = locales.length; --i >= 0;) { 180 if (locales[i].equals(Locale.ENGLISH)) 181 english = true; 182 if (locales[i].equals(Locale.GERMAN)) 183 german = true; 184 // Output the working locale to help diagnose a hang 185 Collator c1 = Collator.getInstance(locales[i]); 186 assertTrue("Doesn't work", c1.compare("a", "b") < 0); 187 assertTrue("Wrong decomposition", 188 c1.getDecomposition() == Collator.NO_DECOMPOSITION); 189 assertTrue("Wrong strength", c1.getStrength() == Collator.TERTIARY); 190 if (c1 instanceof RuleBasedCollator) { 191 try { 192 new RuleBasedCollator(((RuleBasedCollator) c1).getRules()); 193 } catch (ParseException e) { 194 fail("ParseException"); 195 } 196 // assertTrue("Can't recreate: " + locales[i], temp.equals(c1)); 197 } 198 } 199 assertTrue("Missing locales", english && german); 200 } 201 202 /** 203 * @tests java.text.Collator#getDecomposition() 204 */ 205 //FIXME This test fails on Harmony ClassLibrary 206 public void failing_test_getDecomposition() { 207 RuleBasedCollator collator; 208 try { 209 collator = new RuleBasedCollator("; \u0300 < a, A < b < c < d"); 210 } catch (ParseException e) { 211 fail("ParseException"); 212 return; 213 } 214 assertTrue("Wrong default", 215 collator.getDecomposition() == Collator.CANONICAL_DECOMPOSITION); 216 } 217 218 /** 219 * @tests java.text.Collator#getInstance() 220 */ 221 public void test_getInstance() { 222 Collator c1 = Collator.getInstance(); 223 Collator c2 = Collator.getInstance(Locale.getDefault()); 224 assertTrue("Wrong locale", c1.equals(c2)); 225 } 226 227 /** 228 * @tests java.text.Collator#getInstance(java.util.Locale) 229 */ 230 public void test_getInstanceLjava_util_Locale() { 231 assertTrue("Used to test", true); 232 } 233 234 /** 235 * @tests java.text.Collator#getStrength() 236 */ 237 public void test_getStrength() { 238 RuleBasedCollator collator; 239 try { 240 collator = new RuleBasedCollator("; \u0300 < a, A < b < c < d"); 241 } catch (ParseException e) { 242 fail("ParseException"); 243 return; 244 } 245 assertTrue("Wrong default", collator.getStrength() == Collator.TERTIARY); 246 } 247 248 /** 249 * @tests java.text.Collator#setDecomposition(int) 250 */ 251 //FIXME This test fails on Harmony ClassLibrary 252 public void failing_test_setDecompositionI() { 253 Collator c = Collator.getInstance(Locale.FRENCH); 254 c.setStrength(Collator.IDENTICAL); 255 c.setDecomposition(Collator.NO_DECOMPOSITION); 256 assertTrue("Collator should not be using decomposition", !c.equals( 257 "\u212B", "\u00C5")); // "ANGSTROM SIGN" and "LATIN CAPITAL 258 // LETTER A WITH RING ABOVE" 259 c.setDecomposition(Collator.CANONICAL_DECOMPOSITION); 260 assertTrue("Collator should be using decomposition", c.equals("\u212B", 261 "\u00C5")); // "ANGSTROM SIGN" and "LATIN CAPITAL LETTER A WITH 262 // RING ABOVE" 263 assertTrue("Should not be equal under canonical decomposition", !c 264 .equals("\u2163", "IV")); // roman number "IV" 265 c.setDecomposition(Collator.FULL_DECOMPOSITION); 266 assertTrue("Should be equal under full decomposition", c.equals( 267 "\u2163", "IV")); // roman number "IV" 268 } 269 270 /** 271 * @tests java.text.Collator#setStrength(int) 272 */ 273 public void test_setStrengthI() { 274 assertTrue("Used to test", true); 275 } 276} 277