2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.solr.analysis;
19
import java.io.StringReader;
20
import java.lang.reflect.Field;
22
import org.apache.lucene.analysis.standard.StandardTokenizer;
23
import org.apache.solr.SolrTestCaseJ4;
24
import org.apache.solr.core.Config;
25
import org.apache.solr.schema.IndexSchema;
26
import org.apache.solr.schema.FieldType;
27
import org.apache.lucene.analysis.Analyzer;
28
import org.apache.lucene.analysis.standard.StandardAnalyzer;
29
import org.apache.lucene.util.Version;
30
import org.junit.BeforeClass;
33
* Tests for luceneMatchVersion property for analyzers
35
public class TestLuceneMatchVersion extends SolrTestCaseJ4 {
38
public static void beforeClass() throws Exception {
39
initCore("solrconfig.xml","schema-luceneMatchVersion.xml");
42
// this must match the solrconfig.xml version for this test
43
public static final Version DEFAULT_VERSION =
44
Config.parseLuceneVersionString(System.getProperty("tests.luceneMatchVersion", "LUCENE_CURRENT"));
46
public void testStandardTokenizerVersions() throws Exception {
47
assertEquals(DEFAULT_VERSION, solrConfig.luceneMatchVersion);
49
final IndexSchema schema = h.getCore().getSchema();
51
FieldType type = schema.getFieldType("textDefault");
52
TokenizerChain ana = (TokenizerChain) type.getAnalyzer();
53
assertEquals(DEFAULT_VERSION, ((BaseTokenizerFactory) ana.getTokenizerFactory()).luceneMatchVersion);
54
assertEquals(DEFAULT_VERSION, ((BaseTokenFilterFactory) ana.getTokenFilterFactories()[2]).luceneMatchVersion);
55
TokenizerChain.TokenStreamInfo tsi = ana.getStream("textDefault",new StringReader(""));
56
StandardTokenizer tok = (StandardTokenizer) tsi.getTokenizer();
57
assertTrue(tok.isReplaceInvalidAcronym());
59
type = schema.getFieldType("text20");
60
ana = (TokenizerChain) type.getAnalyzer();
61
assertEquals(Version.LUCENE_20, ((BaseTokenizerFactory) ana.getTokenizerFactory()).luceneMatchVersion);
62
assertEquals(Version.LUCENE_24, ((BaseTokenFilterFactory) ana.getTokenFilterFactories()[2]).luceneMatchVersion);
63
tsi = ana.getStream("text20",new StringReader(""));
64
tok = (StandardTokenizer) tsi.getTokenizer();
65
assertFalse(tok.isReplaceInvalidAcronym());
67
// this is a hack to get the private matchVersion field in StandardAnalyzer's superclass, may break in later lucene versions - we have no getter :(
68
final Field matchVersionField = StandardAnalyzer.class.getSuperclass().getDeclaredField("matchVersion");
69
matchVersionField.setAccessible(true);
71
type = schema.getFieldType("textStandardAnalyzerDefault");
72
Analyzer ana1 = type.getAnalyzer();
73
assertTrue(ana1 instanceof StandardAnalyzer);
74
assertEquals(DEFAULT_VERSION, matchVersionField.get(ana1));
76
type = schema.getFieldType("textStandardAnalyzer20");
77
ana1 = type.getAnalyzer();
78
assertTrue(ana1 instanceof StandardAnalyzer);
79
assertEquals(Version.LUCENE_20, matchVersionField.get(ana1));