pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / collation / TestCollationKeyAnalyzer.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 org.apache.lucene.analysis.Analyzer;
22 import org.apache.lucene.analysis.CollationTestBase;
23
24 import java.text.Collator;
25 import java.util.Locale;
26
27
28 public class TestCollationKeyAnalyzer extends CollationTestBase {
29   // the sort order of Ø versus U depends on the version of the rules being used
30   // for the inherited root locale: Ø's order isnt specified in Locale.US since 
31   // its not used in english.
32   private boolean oStrokeFirst = Collator.getInstance(new Locale("")).compare("Ø", "U") < 0;
33   
34   // Neither Java 1.4.2 nor 1.5.0 has Farsi Locale collation available in
35   // RuleBasedCollator.  However, the Arabic Locale seems to order the Farsi
36   // characters properly.
37   private Collator collator = Collator.getInstance(new Locale("ar"));
38   private Analyzer analyzer = new CollationKeyAnalyzer(collator);
39
40   private String firstRangeBeginning = encodeCollationKey
41     (collator.getCollationKey(firstRangeBeginningOriginal).toByteArray());
42   private String firstRangeEnd = encodeCollationKey
43     (collator.getCollationKey(firstRangeEndOriginal).toByteArray());
44   private String secondRangeBeginning = encodeCollationKey
45     (collator.getCollationKey(secondRangeBeginningOriginal).toByteArray());
46   private String secondRangeEnd = encodeCollationKey
47     (collator.getCollationKey(secondRangeEndOriginal).toByteArray());
48   
49   public void testFarsiRangeFilterCollating() throws Exception {
50     testFarsiRangeFilterCollating
51       (analyzer, firstRangeBeginning, firstRangeEnd, 
52        secondRangeBeginning, secondRangeEnd);
53   }
54  
55   public void testFarsiRangeQueryCollating() throws Exception {
56     testFarsiRangeQueryCollating
57       (analyzer, firstRangeBeginning, firstRangeEnd, 
58        secondRangeBeginning, secondRangeEnd);
59   }
60
61   public void testFarsiTermRangeQuery() throws Exception {
62     testFarsiTermRangeQuery
63       (analyzer, firstRangeBeginning, firstRangeEnd, 
64        secondRangeBeginning, secondRangeEnd);
65   }
66   
67   public void testCollationKeySort() throws Exception {
68     Analyzer usAnalyzer 
69       = new CollationKeyAnalyzer(Collator.getInstance(Locale.US));
70     Analyzer franceAnalyzer 
71       = new CollationKeyAnalyzer(Collator.getInstance(Locale.FRANCE));
72     Analyzer swedenAnalyzer 
73       = new CollationKeyAnalyzer(Collator.getInstance(new Locale("sv", "se")));
74     Analyzer denmarkAnalyzer 
75       = new CollationKeyAnalyzer(Collator.getInstance(new Locale("da", "dk")));
76     
77     // The ICU Collator and Sun java.text.Collator implementations differ in their
78     // orderings - "BFJDH" is the ordering for java.text.Collator for Locale.US.
79     testCollationKeySort
80     (usAnalyzer, franceAnalyzer, swedenAnalyzer, denmarkAnalyzer, 
81      oStrokeFirst ? "BFJHD" : "BFJDH", "EACGI", "BJDFH", "BJDHF");
82   }
83   
84   public void testThreadSafe() throws Exception {
85     int iters = 20 * RANDOM_MULTIPLIER;
86     for (int i = 0; i < iters; i++) {
87       Collator collator = Collator.getInstance(Locale.GERMAN);
88       collator.setStrength(Collator.PRIMARY);
89       assertThreadSafe(new CollationKeyAnalyzer(collator));
90     }
91   }
92 }