1 package org.apache.lucene.analysis;
3 import java.io.IOException;
4 import java.io.StringReader;
5 import java.util.Arrays;
6 import java.util.HashSet;
7 import java.util.Locale;
10 import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;
11 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
12 import org.junit.Test;
15 * Licensed to the Apache Software Foundation (ASF) under one or more
16 * contributor license agreements. See the NOTICE file distributed with
17 * this work for additional information regarding copyright ownership.
18 * The ASF licenses this file to You under the Apache License, Version 2.0
19 * (the "License"); you may not use this file except in compliance with
20 * the License. You may obtain a copy of the License at
22 * http://www.apache.org/licenses/LICENSE-2.0
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS,
26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
32 * Testcase for {@link KeywordMarkerFilter}
34 public class TestKeywordMarkerFilter extends BaseTokenStreamTestCase {
37 public void testIncrementToken() throws IOException {
38 CharArraySet set = new CharArraySet(TEST_VERSION_CURRENT, 5, true);
40 String[] output = new String[] { "the", "quick", "brown", "LuceneFox",
42 assertTokenStreamContents(new LowerCaseFilterMock(
43 new KeywordMarkerFilter(new MockTokenizer(new StringReader(
44 "The quIck browN LuceneFox Jumps"), MockTokenizer.WHITESPACE, false), set)), output);
45 Set<String> jdkSet = new HashSet<String>();
46 jdkSet.add("LuceneFox");
47 assertTokenStreamContents(new LowerCaseFilterMock(
48 new KeywordMarkerFilter(new MockTokenizer(new StringReader(
49 "The quIck browN LuceneFox Jumps"), MockTokenizer.WHITESPACE, false), jdkSet)), output);
51 assertTokenStreamContents(new LowerCaseFilterMock(
52 new KeywordMarkerFilter(new MockTokenizer(new StringReader(
53 "The quIck browN LuceneFox Jumps"), MockTokenizer.WHITESPACE, false), set2)), output);
57 public void testComposition() throws Exception {
58 TokenStream ts = new LowerCaseFilterMock(
59 new KeywordMarkerFilter(
60 new KeywordMarkerFilter(
61 new MockTokenizer(new StringReader("Dogs Trees Birds Houses"), MockTokenizer.WHITESPACE, false),
62 new HashSet<String>(Arrays.asList(new String[] { "Birds", "Houses" }))),
63 new HashSet<String>(Arrays.asList(new String[] { "Dogs", "Trees" }))));
65 assertTokenStreamContents(ts, new String[] { "Dogs", "Trees", "Birds", "Houses" });
68 public static final class LowerCaseFilterMock extends TokenFilter {
70 private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
71 private final KeywordAttribute keywordAttr = addAttribute(KeywordAttribute.class);
73 public LowerCaseFilterMock(TokenStream in) {
78 public boolean incrementToken() throws IOException {
79 if (input.incrementToken()) {
80 if (!keywordAttr.isKeyword()) {
81 final String term = termAtt.toString().toLowerCase(Locale.ENGLISH);
82 termAtt.setEmpty().append(term);