1 /*
2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @library /java/text/testlib
27  * @summary test Collation API
28  */
29 /*
30 (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
31 (C) Copyright IBM Corp. 1996 - All Rights Reserved
32 
33   The original version of this source code and documentation is copyrighted and
34 owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
35 provided under terms of a License Agreement between Taligent and Sun. This
36 technology is protected by multiple US and International patents. This notice and
37 attribution to Taligent may not be removed.
38   Taligent is a registered trademark of Taligent, Inc.
39 */
40 
41 import java.util.Locale;
42 import java.text.Collator;
43 import java.text.RuleBasedCollator;
44 import java.text.CollationKey;
45 import java.text.CollationElementIterator;
46 
47 public class APITest extends CollatorTest {
48 
main(String[] args)49     public static void main(String[] args) throws Exception {
50         new APITest().run(args);
51     }
52 
doAssert(boolean condition, String message)53     final void doAssert(boolean condition, String message)
54     {
55         if (!condition) {
56             err("ERROR: ");
57             errln(message);
58         }
59     }
60 
TestProperty( )61     public final void TestProperty( )
62     {
63         Collator col = null;
64         try {
65             col = Collator.getInstance(Locale.ROOT);
66             logln("The property tests begin : ");
67             logln("Test ctors : ");
68             doAssert(col.compare("ab", "abc") < 0, "ab < abc comparison failed");
69             doAssert(col.compare("ab", "AB") < 0, "ab < AB comparison failed");
70             doAssert(col.compare("black-bird", "blackbird") > 0, "black-bird > blackbird comparison failed");
71             doAssert(col.compare("black bird", "black-bird") < 0, "black bird < black-bird comparison failed");
72             doAssert(col.compare("Hello", "hello") > 0, "Hello > hello comparison failed");
73 
74             logln("Test ctors ends.");
75             logln("testing Collator.getStrength() method ...");
76             doAssert(col.getStrength() == Collator.TERTIARY, "collation object has the wrong strength");
77             doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
78 
79             logln("testing Collator.setStrength() method ...");
80             col.setStrength(Collator.SECONDARY);
81             doAssert(col.getStrength() != Collator.TERTIARY, "collation object's strength is secondary difference");
82             doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
83             doAssert(col.getStrength() == Collator.SECONDARY, "collation object has the wrong strength");
84 
85             logln("testing Collator.setDecomposition() method ...");
86             col.setDecomposition(Collator.NO_DECOMPOSITION);
87             doAssert(col.getDecomposition() != Collator.FULL_DECOMPOSITION, "collation object's strength is secondary difference");
88             doAssert(col.getDecomposition() != Collator.CANONICAL_DECOMPOSITION, "collation object's strength is primary difference");
89             doAssert(col.getDecomposition() == Collator.NO_DECOMPOSITION, "collation object has the wrong strength");
90         } catch (Exception foo) {
91             errln("Error : " + foo.getMessage());
92             errln("Default Collator creation failed.");
93         }
94         logln("Default collation property test ended.");
95         logln("Collator.getRules() testing ...");
96         doAssert(((RuleBasedCollator)col).getRules().length() != 0, "getRules() result incorrect" );
97         logln("getRules tests end.");
98         try {
99             col = Collator.getInstance(Locale.FRENCH);
100             col.setStrength(Collator.PRIMARY);
101             logln("testing Collator.getStrength() method again ...");
102             doAssert(col.getStrength() != Collator.TERTIARY, "collation object has the wrong strength");
103             doAssert(col.getStrength() == Collator.PRIMARY, "collation object's strength is not primary difference");
104 
105             logln("testing French Collator.setStrength() method ...");
106             col.setStrength(Collator.TERTIARY);
107             doAssert(col.getStrength() == Collator.TERTIARY, "collation object's strength is not tertiary difference");
108             doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
109             doAssert(col.getStrength() != Collator.SECONDARY, "collation object's strength is secondary difference");
110 
111         } catch (Exception bar) {
112             errln("Error :  " + bar.getMessage());
113             errln("Creating French collation failed.");
114         }
115 
116         logln("Create junk collation: ");
117         Locale abcd = new Locale("ab", "CD", "");
118         Collator junk = null;
119         try {
120             junk = Collator.getInstance(abcd);
121         } catch (Exception err) {
122             errln("Error : " + err.getMessage());
123             errln("Junk collation creation failed, should at least return the collator for the base bundle.");
124         }
125         try {
126             col = Collator.getInstance(Locale.ROOT);
127             doAssert(col.equals(junk), "The base bundle's collation should be returned.");
128         } catch (Exception exc) {
129             errln("Error : " + exc.getMessage());
130             errln("Default collation comparison, caching not working.");
131         }
132 
133         logln("Collator property test ended.");
134     }
135 
136     public final void TestHashCode( )
137     {
138         logln("hashCode tests begin.");
139         Collator col1 = null;
140         try {
141             col1 = Collator.getInstance(Locale.ROOT);
142         } catch (Exception foo) {
143             errln("Error : " + foo.getMessage());
144             errln("Default collation creation failed.");
145         }
146         Collator col2 = null;
147         Locale dk = new Locale("da", "DK", "");
148         try {
149             col2 = Collator.getInstance(dk);
150         } catch (Exception bar) {
151             errln("Error : " + bar.getMessage());
152             errln("Danish collation creation failed.");
153             return;
154         }
155         Collator col3 = null;
156         try {
157             col3 = Collator.getInstance(Locale.ROOT);
158         } catch (Exception err) {
159             errln("Error : " + err.getMessage());
160             errln("2nd default collation creation failed.");
161         }
162         logln("Collator.hashCode() testing ...");
163 
164         if (col1 != null) {
165             doAssert(col1.hashCode() != col2.hashCode(), "Hash test1 result incorrect");
166             if (col3 != null) {
167                 doAssert(col1.hashCode() == col3.hashCode(), "Hash result not equal");
168             }
169         }
170 
171         logln("hashCode tests end.");
172     }
173 
174     //----------------------------------------------------------------------------
175     // ctor -- Tests the constructor methods
176     //
177     public final void TestCollationKey( )
178     {
179         logln("testing CollationKey begins...");
180         Collator col = null;
181         try {
182             col = Collator.getInstance(Locale.ROOT);
183         } catch (Exception foo) {
184             errln("Error : " + foo.getMessage());
185             errln("Default collation creation failed.");
186         }
187         if (col == null) {
188             return;
189         }
190 
191         String test1 = "Abcda", test2 = "abcda";
192         logln("Use tertiary comparison level testing ....");
193         CollationKey sortk1 = col.getCollationKey(test1);
194         CollationKey sortk2 = col.getCollationKey(test2);
195         doAssert(sortk1.compareTo(sortk2) > 0,
196                     "Result should be \"Abcda\" >>> \"abcda\"");
197         CollationKey sortk3 = sortk2;
198         CollationKey sortkNew = sortk1;
199         doAssert(sortk1 != sortk2, "The sort keys should be different");
200         doAssert(sortk1.hashCode() != sortk2.hashCode(), "sort key hashCode() failed");
201         doAssert(sortk2.compareTo(sortk3) == 0, "The sort keys should be the same");
202         doAssert(sortk1 == sortkNew, "The sort keys assignment failed");
203         doAssert(sortk1.hashCode() == sortkNew.hashCode(), "sort key hashCode() failed");
204         doAssert(sortkNew != sortk3, "The sort keys should be different");
205         doAssert(sortk1.compareTo(sortk3) > 0, "Result should be \"Abcda\" >>> \"abcda\"");
206         doAssert(sortk2.compareTo(sortk3) == 0, "Result should be \"abcda\" == \"abcda\"");
207         long    cnt1, cnt2;
208         byte byteArray1[] = sortk1.toByteArray();
209         byte byteArray2[] = sortk2.toByteArray();
210         doAssert(byteArray1 != null && byteArray2 != null, "CollationKey.toByteArray failed.");
211         logln("testing sortkey ends...");
212     }
213     //----------------------------------------------------------------------------
214     // ctor -- Tests the constructor methods
215     //
TestElemIter( )216     public final void TestElemIter( )
217     {
218         logln("testing sortkey begins...");
219         Collator col = null;
220         try {
221             col = Collator.getInstance();
222         } catch (Exception foo) {
223             errln("Error : " + foo.getMessage());
224             errln("Default collation creation failed.");
225         }
226         RuleBasedCollator rbCol;
227         if (col instanceof RuleBasedCollator) {
228             rbCol = (RuleBasedCollator) col;
229         } else {
230             return;
231         }
232         String testString1 = "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?";
233         String testString2 = "Xf ile What subset of all possible test cases has the lowest probability of detecting the least errors?";
234         logln("Constructors and comparison testing....");
235         CollationElementIterator iterator1 = rbCol.getCollationElementIterator(testString1);
236         CollationElementIterator iterator2 = rbCol.getCollationElementIterator(testString1);
237         CollationElementIterator iterator3 = rbCol.getCollationElementIterator(testString2);
238         int order1, order2, order3;
239         order1 = iterator1.next();
240         order2 = iterator2.next();
241         doAssert(order1 == order2, "The order result should be the same");
242 
243         order3 = iterator3.next();
244         doAssert(CollationElementIterator.primaryOrder(order1)
245                      == CollationElementIterator.primaryOrder(order3),
246                  "The primary orders should be the same");
247         doAssert(CollationElementIterator.secondaryOrder(order1)
248                      == CollationElementIterator.secondaryOrder(order3),
249                  "The secondary orders should be the same");
250         doAssert(CollationElementIterator.tertiaryOrder(order1)
251                      == CollationElementIterator.tertiaryOrder(order3),
252                  "The tertiary orders should be the same");
253 
254         order1 = iterator1.next();
255         order3 = iterator3.next();
256         doAssert(CollationElementIterator.primaryOrder(order1)
257                      == CollationElementIterator.primaryOrder(order3),
258                  "The primary orders should be identical");
259         doAssert(CollationElementIterator.tertiaryOrder(order1)
260                      != CollationElementIterator.tertiaryOrder(order3),
261                  "The tertiary orders should be different");
262 
263         order1 = iterator1.next();
264         order3 = iterator3.next();
265         doAssert(CollationElementIterator.secondaryOrder(order1)
266                      != CollationElementIterator.secondaryOrder(order3),
267                  "The secondary orders should be different");
268         doAssert(order1 != CollationElementIterator.NULLORDER,
269                  "Unexpected end of iterator reached");
270 
271         iterator1.reset();
272         iterator2.reset();
273         iterator3.reset();
274         order1 = iterator1.next();
275         order2 = iterator2.next();
276         doAssert(order1 == order2, "The order result should be the same");
277 
278         order3 = iterator3.next();
279         doAssert(CollationElementIterator.primaryOrder(order1)
280                      == CollationElementIterator.primaryOrder(order3),
281                  "The orders should be the same");
282         doAssert(CollationElementIterator.secondaryOrder(order1)
283                      == CollationElementIterator.secondaryOrder(order3),
284                  "The orders should be the same");
285         doAssert(CollationElementIterator.tertiaryOrder(order1)
286                      == CollationElementIterator.tertiaryOrder(order3),
287                  "The orders should be the same");
288 
289         order1 = iterator1.next();
290         order2 = iterator2.next();
291         order3 = iterator3.next();
292         doAssert(CollationElementIterator.primaryOrder(order1)
293                      == CollationElementIterator.primaryOrder(order3),
294                  "The primary orders should be identical");
295         doAssert(CollationElementIterator.tertiaryOrder(order1)
296                      != CollationElementIterator.tertiaryOrder(order3),
297                  "The tertiary orders should be different");
298 
299         order1 = iterator1.next();
300         order3 = iterator3.next();
301         doAssert(CollationElementIterator.secondaryOrder(order1)
302                      != CollationElementIterator.secondaryOrder(order3),
303                  "The secondary orders should be different");
304         doAssert(order1 != CollationElementIterator.NULLORDER, "Unexpected end of iterator reached");
305         logln("testing CollationElementIterator ends...");
306     }
307 
TestGetAll()308     public final void TestGetAll()
309     {
310         Locale[] list = Collator.getAvailableLocales();
311         for (int i = 0; i < list.length; ++i) {
312             log("Locale name: ");
313             log(list[i].toString());
314             log(" , the display name is : ");
315             logln(list[i].getDisplayName());
316         }
317     }
318 }
319