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.util.HashMap;
23
import org.apache.lucene.analysis.MockTokenizer;
24
import org.apache.lucene.analysis.TokenStream;
25
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
27
public class DoubleMetaphoneFilterFactoryTest extends BaseTokenTestCase {
29
public void testDefaults() throws Exception {
30
DoubleMetaphoneFilterFactory factory = new DoubleMetaphoneFilterFactory();
31
factory.init(new HashMap<String, String>());
32
TokenStream inputStream = new MockTokenizer(new StringReader("international"), MockTokenizer.WHITESPACE, false);
34
TokenStream filteredStream = factory.create(inputStream);
35
assertEquals(DoubleMetaphoneFilter.class, filteredStream.getClass());
36
assertTokenStreamContents(filteredStream, new String[] { "international", "ANTR" });
39
public void testSettingSizeAndInject() throws Exception {
40
DoubleMetaphoneFilterFactory factory = new DoubleMetaphoneFilterFactory();
41
Map<String, String> parameters = new HashMap<String, String>();
42
parameters.put("inject", "false");
43
parameters.put("maxCodeLength", "8");
44
factory.init(parameters);
46
TokenStream inputStream = new MockTokenizer(new StringReader("international"), MockTokenizer.WHITESPACE, false);
48
TokenStream filteredStream = factory.create(inputStream);
49
assertEquals(DoubleMetaphoneFilter.class, filteredStream.getClass());
50
assertTokenStreamContents(filteredStream, new String[] { "ANTRNXNL" });
54
* Ensure that reset() removes any state (buffered tokens)
56
public void testReset() throws Exception {
57
DoubleMetaphoneFilterFactory factory = new DoubleMetaphoneFilterFactory();
58
factory.init(new HashMap<String, String>());
59
TokenStream inputStream = new MockTokenizer(new StringReader("international"), MockTokenizer.WHITESPACE, false);
61
TokenStream filteredStream = factory.create(inputStream);
62
CharTermAttribute termAtt = filteredStream.addAttribute(CharTermAttribute.class);
63
assertEquals(DoubleMetaphoneFilter.class, filteredStream.getClass());
65
filteredStream.reset();
66
assertTrue(filteredStream.incrementToken());
67
assertEquals(13, termAtt.length());
68
assertEquals("international", termAtt.toString());
69
filteredStream.reset();
71
// ensure there are no more tokens, such as ANTRNXNL
72
assertFalse(filteredStream.incrementToken());