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.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	public void test_clone() {
27		Collator c = Collator.getInstance(Locale.GERMAN);
28		Collator c2 = (Collator) c.clone();
29		assertTrue("Clones answered false to equals", c.equals(c2));
30		assertTrue("Clones were equivalent", c != c2);
31	}
32
33	public void test_compareLjava_lang_ObjectLjava_lang_Object() {
34		Collator c = Collator.getInstance(Locale.FRENCH);
35		Object o, o2;
36
37		c.setStrength(Collator.IDENTICAL);
38		o = "E";
39		o2 = "F";
40		assertTrue("a) Failed on primary difference", c.compare(o, o2) < 0);
41		o = "e";
42		o2 = "\u00e9";
43		assertTrue("a) Failed on secondary difference", c.compare(o, o2) < 0);
44		o = "e";
45		o2 = "E";
46		assertTrue("a) Failed on tertiary difference", c.compare(o, o2) < 0);
47		o = "\u0001";
48		o2 = "\u0002";
49		assertTrue("a) Failed on identical", c.compare(o, o2) < 0);
50		o = "e";
51		o2 = "e";
52		assertEquals("a) Failed on equivalence", 0, c.compare(o, o2));
53		assertTrue("a) Failed on primary expansion",
54				c.compare("\u01db", "v") < 0);
55
56		c.setStrength(Collator.TERTIARY);
57		o = "E";
58		o2 = "F";
59		assertTrue("b) Failed on primary difference", c.compare(o, o2) < 0);
60		o = "e";
61		o2 = "\u00e9";
62		assertTrue("b) Failed on secondary difference", c.compare(o, o2) < 0);
63		o = "e";
64		o2 = "E";
65		assertTrue("b) Failed on tertiary difference", c.compare(o, o2) < 0);
66		o = "\u0001";
67		o2 = "\u0002";
68		assertEquals("b) Failed on identical", 0, c.compare(o, o2));
69		o = "e";
70		o2 = "e";
71		assertEquals("b) Failed on equivalence", 0, c.compare(o, o2));
72
73		c.setStrength(Collator.SECONDARY);
74		o = "E";
75		o2 = "F";
76		assertTrue("c) Failed on primary difference", c.compare(o, o2) < 0);
77		o = "e";
78		o2 = "\u00e9";
79		assertTrue("c) Failed on secondary difference", c.compare(o, o2) < 0);
80		o = "e";
81		o2 = "E";
82		assertEquals("c) Failed on tertiary difference", 0, c.compare(o, o2));
83		o = "\u0001";
84		o2 = "\u0002";
85		assertEquals("c) Failed on identical", 0, c.compare(o, o2));
86		o = "e";
87		o2 = "e";
88		assertEquals("c) Failed on equivalence", 0, c.compare(o, o2));
89
90		c.setStrength(Collator.PRIMARY);
91		o = "E";
92		o2 = "F";
93		assertTrue("d) Failed on primary difference", c.compare(o, o2) < 0);
94		o = "e";
95		o2 = "\u00e9";
96		assertEquals("d) Failed on secondary difference", 0, c.compare(o, o2));
97		o = "e";
98		o2 = "E";
99		assertEquals("d) Failed on tertiary difference", 0, c.compare(o, o2));
100		o = "\u0001";
101		o2 = "\u0002";
102		assertEquals("d) Failed on identical", 0, c.compare(o, o2));
103		o = "e";
104		o2 = "e";
105		assertEquals("d) Failed on equivalence", 0, c.compare(o, o2));
106
107		try {
108			c.compare("e", new StringBuffer("Blah"));
109		} catch (ClassCastException e) {
110			// correct
111			return;
112		}
113		fail("Failed to throw ClassCastException");
114	}
115
116	public void test_equalsLjava_lang_Object() {
117		Collator c = Collator.getInstance(Locale.ENGLISH);
118		Collator c2 = (Collator) c.clone();
119		assertTrue("Cloned collators not equal", c.equals(c2));
120		c2.setStrength(Collator.SECONDARY);
121		assertTrue("Collators with different strengths equal", !c.equals(c2));
122	}
123
124	public void test_equalsLjava_lang_StringLjava_lang_String() {
125		Collator c = Collator.getInstance(Locale.FRENCH);
126
127		c.setStrength(Collator.IDENTICAL);
128		assertTrue("a) Failed on primary difference", !c.equals("E", "F"));
129		assertTrue("a) Failed on secondary difference", !c
130				.equals("e", "\u00e9"));
131		assertTrue("a) Failed on tertiary difference", !c.equals("e", "E"));
132		assertTrue("a) Failed on identical", !c.equals("\u0001", "\u0002"));
133		assertTrue("a) Failed on equivalence", c.equals("e", "e"));
134
135		c.setStrength(Collator.TERTIARY);
136		assertTrue("b) Failed on primary difference", !c.equals("E", "F"));
137		assertTrue("b) Failed on secondary difference", !c
138				.equals("e", "\u00e9"));
139		assertTrue("b) Failed on tertiary difference", !c.equals("e", "E"));
140		assertTrue("b) Failed on identical", c.equals("\u0001", "\u0002"));
141		assertTrue("b) Failed on equivalence", c.equals("e", "e"));
142
143		c.setStrength(Collator.SECONDARY);
144		assertTrue("c) Failed on primary difference", !c.equals("E", "F"));
145		assertTrue("c) Failed on secondary difference", !c
146				.equals("e", "\u00e9"));
147		assertTrue("c) Failed on tertiary difference", c.equals("e", "E"));
148		assertTrue("c) Failed on identical", c.equals("\u0001", "\u0002"));
149		assertTrue("c) Failed on equivalence", c.equals("e", "e"));
150
151		c.setStrength(Collator.PRIMARY);
152		assertTrue("d) Failed on primary difference", !c.equals("E", "F"));
153		assertTrue("d) Failed on secondary difference", c.equals("e", "\u00e9"));
154		assertTrue("d) Failed on tertiary difference", c.equals("e", "E"));
155		assertTrue("d) Failed on identical", c.equals("\u0001", "\u0002"));
156		assertTrue("d) Failed on equivalence", c.equals("e", "e"));
157	}
158
159	public void failing_test_getAvailableLocales() {
160		Locale[] locales = Collator.getAvailableLocales();
161		assertTrue("No locales", locales.length > 0);
162		boolean english = false, german = false;
163		for (int i = locales.length; --i >= 0;) {
164			if (locales[i].equals(Locale.ENGLISH))
165				english = true;
166			if (locales[i].equals(Locale.GERMAN))
167				german = true;
168			// Output the working locale to help diagnose a hang
169			Collator c1 = Collator.getInstance(locales[i]);
170			assertTrue("Doesn't work", c1.compare("a", "b") < 0);
171			assertTrue("Wrong decomposition",
172					c1.getDecomposition() == Collator.NO_DECOMPOSITION);
173			assertTrue("Wrong strength", c1.getStrength() == Collator.TERTIARY);
174			if (c1 instanceof RuleBasedCollator) {
175				try {
176					new RuleBasedCollator(((RuleBasedCollator) c1).getRules());
177				} catch (ParseException e) {
178					fail("ParseException");
179				}
180				// assertTrue("Can't recreate: " + locales[i], temp.equals(c1));
181			}
182		}
183		assertTrue("Missing locales", english && german);
184	}
185
186	public void failing_test_getDecomposition() {
187		RuleBasedCollator collator;
188		try {
189			collator = new RuleBasedCollator("; \u0300 < a, A < b < c < d");
190		} catch (ParseException e) {
191			fail("ParseException");
192			return;
193		}
194		assertTrue("Wrong default",
195				collator.getDecomposition() == Collator.CANONICAL_DECOMPOSITION);
196	}
197
198	public void test_getInstance() {
199		Collator c1 = Collator.getInstance();
200		Collator c2 = Collator.getInstance(Locale.getDefault());
201		assertTrue("Wrong locale", c1.equals(c2));
202	}
203
204	public void test_getInstanceLjava_util_Locale() {
205		assertTrue("Used to test", true);
206	}
207
208	public void test_getStrength() throws ParseException {
209		RuleBasedCollator collator = new RuleBasedCollator("&9 ; \u0300 < a, A < b < c < d");
210		assertTrue("Wrong default", collator.getStrength() == Collator.TERTIARY);
211	}
212
213	public void failing_test_setDecompositionI() {
214		Collator c = Collator.getInstance(Locale.FRENCH);
215		c.setStrength(Collator.IDENTICAL);
216		c.setDecomposition(Collator.NO_DECOMPOSITION);
217		assertTrue("Collator should not be using decomposition", !c.equals(
218				"\u212B", "\u00C5")); // "ANGSTROM SIGN" and "LATIN CAPITAL
219		// LETTER A WITH RING ABOVE"
220		c.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
221		assertTrue("Collator should be using decomposition", c.equals("\u212B",
222				"\u00C5")); // "ANGSTROM SIGN" and "LATIN CAPITAL LETTER A WITH
223		// RING ABOVE"
224		assertTrue("Should not be equal under canonical decomposition", !c
225				.equals("\u2163", "IV")); // roman number "IV"
226		c.setDecomposition(Collator.FULL_DECOMPOSITION);
227		assertTrue("Should be equal under full decomposition", c.equals(
228				"\u2163", "IV")); // roman number "IV"
229	}
230
231	public void test_setStrengthI() {
232		assertTrue("Used to test", true);
233	}
234}
235