1 package org.apache.lucene.collation;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 import org.apache.lucene.analysis.Analyzer;
23 import java.text.Collator;
24 import java.util.Locale;
27 public class TestCollationKeyAnalyzer extends CollationTestBase {
28 // the sort order of Ø versus U depends on the version of the rules being used
29 // for the inherited root locale: Ø's order isnt specified in Locale.US since
30 // its not used in english.
31 private boolean oStrokeFirst = Collator.getInstance(new Locale("")).compare("Ø", "U") < 0;
33 // Neither Java 1.4.2 nor 1.5.0 has Farsi Locale collation available in
34 // RuleBasedCollator. However, the Arabic Locale seems to order the Farsi
35 // characters properly.
36 private Collator collator = Collator.getInstance(new Locale("ar"));
37 private Analyzer analyzer = new CollationKeyAnalyzer(collator);
39 private String firstRangeBeginning = encodeCollationKey
40 (collator.getCollationKey(firstRangeBeginningOriginal).toByteArray());
41 private String firstRangeEnd = encodeCollationKey
42 (collator.getCollationKey(firstRangeEndOriginal).toByteArray());
43 private String secondRangeBeginning = encodeCollationKey
44 (collator.getCollationKey(secondRangeBeginningOriginal).toByteArray());
45 private String secondRangeEnd = encodeCollationKey
46 (collator.getCollationKey(secondRangeEndOriginal).toByteArray());
48 public void testFarsiRangeFilterCollating() throws Exception {
49 testFarsiRangeFilterCollating
50 (analyzer, firstRangeBeginning, firstRangeEnd,
51 secondRangeBeginning, secondRangeEnd);
54 public void testFarsiRangeQueryCollating() throws Exception {
55 testFarsiRangeQueryCollating
56 (analyzer, firstRangeBeginning, firstRangeEnd,
57 secondRangeBeginning, secondRangeEnd);
60 public void testFarsiTermRangeQuery() throws Exception {
61 testFarsiTermRangeQuery
62 (analyzer, firstRangeBeginning, firstRangeEnd,
63 secondRangeBeginning, secondRangeEnd);
66 public void testCollationKeySort() throws Exception {
68 = new CollationKeyAnalyzer(Collator.getInstance(Locale.US));
69 Analyzer franceAnalyzer
70 = new CollationKeyAnalyzer(Collator.getInstance(Locale.FRANCE));
71 Analyzer swedenAnalyzer
72 = new CollationKeyAnalyzer(Collator.getInstance(new Locale("sv", "se")));
73 Analyzer denmarkAnalyzer
74 = new CollationKeyAnalyzer(Collator.getInstance(new Locale("da", "dk")));
76 // The ICU Collator and Sun java.text.Collator implementations differ in their
77 // orderings - "BFJDH" is the ordering for java.text.Collator for Locale.US.
79 (usAnalyzer, franceAnalyzer, swedenAnalyzer, denmarkAnalyzer,
80 oStrokeFirst ? "BFJHD" : "BFJDH", "EACGI", "BJDFH", "BJDHF");
83 public void testThreadSafe() throws Exception {
84 int iters = 20 * RANDOM_MULTIPLIER;
85 for (int i = 0; i < iters; i++) {
86 Collator collator = Collator.getInstance(Locale.GERMAN);
87 collator.setStrength(Collator.PRIMARY);
88 assertThreadSafe(new CollationKeyAnalyzer(collator));