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.TokenStream;
22 import org.apache.lucene.analysis.Analyzer;
23 import org.apache.lucene.analysis.KeywordTokenizer;
25 import java.text.Collator;
26 import java.util.Locale;
27 import java.io.Reader;
30 public class TestCollationKeyFilter extends CollationTestBase {
31 // the sort order of Ø versus U depends on the version of the rules being used
32 // for the inherited root locale: Ø's order isnt specified in Locale.US since
33 // its not used in english.
34 boolean oStrokeFirst = Collator.getInstance(new Locale("")).compare("Ø", "U") < 0;
36 // Neither Java 1.4.2 nor 1.5.0 has Farsi Locale collation available in
37 // RuleBasedCollator. However, the Arabic Locale seems to order the Farsi
38 // characters properly.
39 private Collator collator = Collator.getInstance(new Locale("ar"));
40 private Analyzer analyzer = new TestAnalyzer(collator);
42 private String firstRangeBeginning = encodeCollationKey
43 (collator.getCollationKey(firstRangeBeginningOriginal).toByteArray());
44 private String firstRangeEnd = encodeCollationKey
45 (collator.getCollationKey(firstRangeEndOriginal).toByteArray());
46 private String secondRangeBeginning = encodeCollationKey
47 (collator.getCollationKey(secondRangeBeginningOriginal).toByteArray());
48 private String secondRangeEnd = encodeCollationKey
49 (collator.getCollationKey(secondRangeEndOriginal).toByteArray());
52 public final class TestAnalyzer extends Analyzer {
53 private Collator _collator;
55 TestAnalyzer(Collator collator) {
60 public TokenStream tokenStream(String fieldName, Reader reader) {
61 TokenStream result = new KeywordTokenizer(reader);
62 result = new CollationKeyFilter(result, _collator);
67 public void testFarsiRangeFilterCollating() throws Exception {
68 testFarsiRangeFilterCollating
69 (analyzer, firstRangeBeginning, firstRangeEnd,
70 secondRangeBeginning, secondRangeEnd);
73 public void testFarsiRangeQueryCollating() throws Exception {
74 testFarsiRangeQueryCollating
75 (analyzer, firstRangeBeginning, firstRangeEnd,
76 secondRangeBeginning, secondRangeEnd);
79 public void testFarsiTermRangeQuery() throws Exception {
80 testFarsiTermRangeQuery
81 (analyzer, firstRangeBeginning, firstRangeEnd,
82 secondRangeBeginning, secondRangeEnd);
85 public void testCollationKeySort() throws Exception {
86 Analyzer usAnalyzer = new TestAnalyzer(Collator.getInstance(Locale.US));
87 Analyzer franceAnalyzer
88 = new TestAnalyzer(Collator.getInstance(Locale.FRANCE));
89 Analyzer swedenAnalyzer
90 = new TestAnalyzer(Collator.getInstance(new Locale("sv", "se")));
91 Analyzer denmarkAnalyzer
92 = new TestAnalyzer(Collator.getInstance(new Locale("da", "dk")));
94 // The ICU Collator and Sun java.text.Collator implementations differ in their
95 // orderings - "BFJDH" is the ordering for java.text.Collator for Locale.US.
97 (usAnalyzer, franceAnalyzer, swedenAnalyzer, denmarkAnalyzer,
98 oStrokeFirst ? "BFJHD" : "BFJDH", "EACGI", "BJDFH", "BJDHF");