1
package org.apache.solr.analysis;
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.
20
import org.apache.lucene.analysis.MockTokenizer;
21
import org.apache.lucene.analysis.TokenStream;
22
import org.apache.lucene.analysis.Tokenizer;
23
import org.apache.solr.common.ResourceLoader;
24
import org.apache.solr.core.SolrResourceLoader;
26
import java.io.StringReader;
29
import java.util.HashMap;
32
* Tests pretty much copied from StopFilterFactoryTest We use the test files
33
* used by the StopFilterFactoryTest TODO: consider creating separate test files
34
* so this won't break if stop filter test files change
36
public class CommonGramsFilterFactoryTest extends BaseTokenTestCase {
38
public void testInform() throws Exception {
39
ResourceLoader loader = new SolrResourceLoader(null, null);
40
assertTrue("loader is null and it shouldn't be", loader != null);
41
CommonGramsFilterFactory factory = new CommonGramsFilterFactory();
42
Map<String, String> args = new HashMap<String, String>(DEFAULT_VERSION_PARAM);
43
args.put("words", "stop-1.txt");
44
args.put("ignoreCase", "true");
46
factory.inform(loader);
47
Set<?> words = factory.getCommonWords();
48
assertTrue("words is null and it shouldn't be", words != null);
49
assertTrue("words Size: " + words.size() + " is not: " + 2,
51
assertTrue(factory.isIgnoreCase() + " does not equal: " + true, factory
52
.isIgnoreCase() == true);
54
factory = new CommonGramsFilterFactory();
55
args.put("words", "stop-1.txt, stop-2.txt");
57
factory.inform(loader);
58
words = factory.getCommonWords();
59
assertTrue("words is null and it shouldn't be", words != null);
60
assertTrue("words Size: " + words.size() + " is not: " + 4,
62
assertTrue(factory.isIgnoreCase() + " does not equal: " + true, factory
63
.isIgnoreCase() == true);
68
* If no words are provided, then a set of english default stopwords is used.
70
public void testDefaults() throws Exception {
71
ResourceLoader loader = new SolrResourceLoader(null, null);
72
assertTrue("loader is null and it shouldn't be", loader != null);
73
CommonGramsFilterFactory factory = new CommonGramsFilterFactory();
74
Map<String, String> args = new HashMap<String, String>(DEFAULT_VERSION_PARAM);
76
factory.inform(loader);
77
Set<?> words = factory.getCommonWords();
78
assertTrue("words is null and it shouldn't be", words != null);
79
assertTrue(words.contains("the"));
80
Tokenizer tokenizer = new MockTokenizer(new StringReader("testing the factory"), MockTokenizer.WHITESPACE, false);
81
TokenStream stream = factory.create(tokenizer);
82
assertTokenStreamContents(stream,
83
new String[] { "testing", "testing_the", "the", "the_factory", "factory" });