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
18package org.apache.harmony.tests.java.text;
19
20import java.text.CharacterIterator;
21import java.text.CollationElementIterator;
22import java.text.CollationKey;
23import java.text.Collator;
24import java.text.ParseException;
25import java.text.RuleBasedCollator;
26import java.text.StringCharacterIterator;
27import java.util.Locale;
28
29import junit.framework.TestCase;
30
31public 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    {
44      String rule = "&9 < a < b < c < d";
45      RuleBasedCollator coll = new RuleBasedCollator(rule);
46      assertEquals(rule.hashCode(), coll.hashCode());
47    }
48
49    {
50      String rule = "&9 < a < b < c < d < e";
51      RuleBasedCollator coll = new RuleBasedCollator(rule);
52      assertEquals(rule.hashCode(), coll.hashCode());
53    }
54  }
55
56  public void testClone() throws ParseException {
57    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(Locale.US);
58    RuleBasedCollator clone = (RuleBasedCollator) coll.clone();
59    assertNotSame(coll, clone);
60    assertEquals(coll.getRules(), clone.getRules());
61    assertEquals(coll.getDecomposition(), clone.getDecomposition());
62    assertEquals(coll.getStrength(), clone.getStrength());
63  }
64
65  public void testEqualsObject() throws ParseException {
66    String rule = "&9 < a < b < c < d < e";
67    RuleBasedCollator coll = new RuleBasedCollator(rule);
68
69    assertEquals(Collator.TERTIARY, coll.getStrength());
70    assertEquals(Collator.NO_DECOMPOSITION, coll.getDecomposition());
71    RuleBasedCollator other = new RuleBasedCollator(rule);
72    assertTrue(coll.equals(other));
73
74    coll.setStrength(Collator.PRIMARY);
75    assertFalse(coll.equals(other));
76
77    coll.setStrength(Collator.TERTIARY);
78    coll.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
79    assertFalse(coll.equals(other));
80  }
81
82  public void testCompareStringString() throws ParseException {
83    String rule = "&9 < c < b < a";
84    RuleBasedCollator coll = new RuleBasedCollator(rule);
85    assertEquals(-1, coll.compare("c", "a"));
86  }
87
88  public void testGetCollationKey() {
89    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(Locale.GERMAN);
90    String source = "abc";
91    CollationKey key1 = coll.getCollationKey(source);
92    assertEquals(source, key1.getSourceString());
93    String source2 = "abb";
94    CollationKey key2 = coll.getCollationKey(source2);
95    assertEquals(source2, key2.getSourceString());
96    assertTrue(key1.compareTo(key2) > 0);
97    assertTrue(coll.compare(source, source2) > 0);
98  }
99
100  public void testGetRules() throws ParseException {
101    String rule = "&9 < a = b < c";
102    RuleBasedCollator coll = new RuleBasedCollator(rule);
103    assertEquals(rule, coll.getRules());
104  }
105
106  public void testGetCollationElementIteratorString() throws Exception {
107    {
108      Locale locale = Locale.forLanguageTag("es-u-co-trad");
109      RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(locale);
110      String source = "cha";
111      CollationElementIterator iterator = coll.getCollationElementIterator(source);
112      int[] e_offset = { 0, 2, 3 };
113      int offset = iterator.getOffset();
114      int i = 0;
115      assertEquals(e_offset[i++], offset);
116      while (offset != source.length()) {
117        iterator.next();
118        offset = iterator.getOffset();
119        assertEquals(e_offset[i++], offset);
120      }
121      assertEquals(CollationElementIterator.NULLORDER, iterator.next());
122    }
123
124    {
125      Locale locale = new Locale("de", "DE");
126      RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(locale);
127      String source = "\u00fcb";
128      CollationElementIterator iterator = coll.getCollationElementIterator(source);
129      int[] e_offset = { 0, 1, 1, 2 };
130      int offset = iterator.getOffset();
131      int i = 0;
132      assertEquals(e_offset[i++], offset);
133      while (offset != source.length()) {
134        iterator.next();
135        offset = iterator.getOffset();
136        assertEquals(e_offset[i++], offset);
137      }
138      assertEquals(CollationElementIterator.NULLORDER, iterator.next());
139    }
140    //Regression for HARMONY-1352
141    try {
142      new RuleBasedCollator("&9 < a< b< c< d").getCollationElementIterator((String)null);
143      fail();
144    } catch (NullPointerException expected) {
145    }
146  }
147
148  public void testGetCollationElementIteratorCharacterIterator() throws Exception {
149    {
150      Locale locale = Locale.forLanguageTag("es-u-co-trad");
151      RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(locale);
152      String text = "cha";
153      StringCharacterIterator source = new StringCharacterIterator(text);
154      CollationElementIterator iterator = coll.getCollationElementIterator(source);
155      int[] e_offset = { 0, 2, 3 };
156      int offset = iterator.getOffset();
157      int i = 0;
158      assertEquals(e_offset[i++], offset);
159      while (offset != text.length()) {
160        iterator.next();
161        offset = iterator.getOffset();
162        // System.out.println(offset);
163        assertEquals(e_offset[i++], offset);
164      }
165      assertEquals(CollationElementIterator.NULLORDER, iterator.next());
166    }
167
168    {
169      Locale locale = new Locale("de", "DE");
170      RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(locale);
171      String text = "\u00fcb";
172      StringCharacterIterator source = new StringCharacterIterator(text);
173      CollationElementIterator iterator = coll.getCollationElementIterator(source);
174      int[] e_offset = { 0, 1, 1, 2 };
175      int offset = iterator.getOffset();
176      int i = 0;
177      assertEquals(e_offset[i++], offset);
178      while (offset != text.length()) {
179        iterator.next();
180        offset = iterator.getOffset();
181        assertEquals(e_offset[i++], offset);
182      }
183      assertEquals(CollationElementIterator.NULLORDER, iterator.next());
184    }
185    //Regression for HARMONY-1352
186    try {
187      new RuleBasedCollator("&9 < a< b< c< d").getCollationElementIterator((CharacterIterator)null);
188      fail();
189    } catch (NullPointerException expected) {
190    }
191  }
192
193  public void testStrength() {
194    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(Locale.US);
195    for (int i = 0; i < 4; i++) {
196      coll.setStrength(i);
197      assertEquals(i, coll.getStrength());
198    }
199  }
200
201  public void testDecomposition() {
202    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(Locale.US);
203    for (int i = 0; i < 2; i++) {
204      coll.setDecomposition(i);
205      assertEquals(i, coll.getDecomposition());
206    }
207  }
208
209  public void testCollator_GetInstance() {
210    Collator coll = Collator.getInstance();
211    Object obj1 = "a";
212    Object obj2 = "b";
213    assertEquals(-1, coll.compare(obj1, obj2));
214
215    Collator.getInstance();
216    assertFalse(coll.equals("A", "\uFF21"));
217  }
218
219  public void testGetAvailableLocales() {
220    assertTrue(Collator.getAvailableLocales().length > 0);
221  }
222
223  // Test CollationKey
224  public void testCollationKey() {
225    Collator coll = Collator.getInstance(Locale.US);
226    String text = "abc";
227    CollationKey key = coll.getCollationKey(text);
228    key.hashCode();
229
230    CollationKey key2 = coll.getCollationKey("abc");
231
232    assertEquals(0, key.compareTo(key2));
233  }
234
235  public void testNullPointerException() throws Exception {
236    //Regression for HARMONY-241
237    try {
238      new RuleBasedCollator(null);
239      fail();
240    } catch (NullPointerException expected) {
241    }
242  }
243
244  public void testCompareNull() throws Exception {
245    //Regression for HARMONY-836
246    try {
247      new RuleBasedCollator("&9 < a").compare(null, null);
248      fail();
249    } catch (NullPointerException expected) {
250    }
251  }
252
253  public void testEmptyRules() throws Exception {
254    new RuleBasedCollator("");
255    new RuleBasedCollator(" ");
256    new RuleBasedCollator("# This is a comment.");
257  }
258}
259