pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / icu / src / test / org / apache / lucene / collation / TestICUCollationKeyAnalyzer.java
1 package org.apache.lucene.collation;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20
21 import com.ibm.icu.text.Collator;
22
23 import org.apache.lucene.analysis.Analyzer;
24 import org.apache.lucene.analysis.CollationTestBase;
25
26 import java.util.Locale;
27
28
29 public class TestICUCollationKeyAnalyzer extends CollationTestBase {
30
31   private Collator collator = Collator.getInstance(new Locale("fa"));
32   private Analyzer analyzer = new ICUCollationKeyAnalyzer(collator);
33
34   private String firstRangeBeginning = encodeCollationKey
35     (collator.getCollationKey(firstRangeBeginningOriginal).toByteArray());
36   private String firstRangeEnd = encodeCollationKey
37     (collator.getCollationKey(firstRangeEndOriginal).toByteArray());
38   private String secondRangeBeginning = encodeCollationKey
39     (collator.getCollationKey(secondRangeBeginningOriginal).toByteArray());
40   private String secondRangeEnd = encodeCollationKey
41     (collator.getCollationKey(secondRangeEndOriginal).toByteArray());
42   
43   public void testFarsiRangeFilterCollating() throws Exception {
44     testFarsiRangeFilterCollating(analyzer, firstRangeBeginning, firstRangeEnd, 
45                                   secondRangeBeginning, secondRangeEnd);
46   }
47  
48   public void testFarsiRangeQueryCollating() throws Exception {
49     testFarsiRangeQueryCollating(analyzer, firstRangeBeginning, firstRangeEnd, 
50                                  secondRangeBeginning, secondRangeEnd);
51   }
52
53   public void testFarsiTermRangeQuery() throws Exception {
54     testFarsiTermRangeQuery
55       (analyzer, firstRangeBeginning, firstRangeEnd, 
56        secondRangeBeginning, secondRangeEnd);
57   }
58
59   // Test using various international locales with accented characters (which
60   // sort differently depending on locale)
61   //
62   // Copied (and slightly modified) from 
63   // org.apache.lucene.search.TestSort.testInternationalSort()
64   //  
65   public void testCollationKeySort() throws Exception {
66     Analyzer usAnalyzer = new ICUCollationKeyAnalyzer
67       (Collator.getInstance(Locale.US));
68     Analyzer franceAnalyzer = new ICUCollationKeyAnalyzer
69       (Collator.getInstance(Locale.FRANCE));
70     Analyzer swedenAnalyzer = new ICUCollationKeyAnalyzer
71       (Collator.getInstance(new Locale("sv", "se")));
72     Analyzer denmarkAnalyzer = new ICUCollationKeyAnalyzer
73       (Collator.getInstance(new Locale("da", "dk")));
74
75     // The ICU Collator and java.text.Collator implementations differ in their
76     // orderings - "BFJHD" is the ordering for the ICU Collator for Locale.US.
77     testCollationKeySort
78     (usAnalyzer, franceAnalyzer, swedenAnalyzer, denmarkAnalyzer, 
79      "BFJHD", "ECAGI", "BJDFH", "BJDHF");
80   }
81   
82   public void testThreadSafe() throws Exception {
83     int iters = 20 * RANDOM_MULTIPLIER;
84     for (int i = 0; i < iters; i++) {
85       Locale locale = Locale.GERMAN;
86       Collator collator = Collator.getInstance(locale);
87       collator.setStrength(Collator.IDENTICAL);
88       assertThreadSafe(new ICUCollationKeyAnalyzer(collator));
89     }
90   }
91 }