1 package org.apache.lucene.analysis.sinks;
4 * Copyright 2004 The Apache Software Foundation
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import java.io.IOException;
20 import java.io.StringReader;
22 import org.apache.lucene.analysis.BaseTokenStreamTestCase;
23 import org.apache.lucene.analysis.TeeSinkTokenFilter;
24 import org.apache.lucene.analysis.MockTokenizer;
25 import org.apache.lucene.analysis.TokenFilter;
26 import org.apache.lucene.analysis.TokenStream;
27 import org.apache.lucene.analysis.TeeSinkTokenFilter.SinkTokenStream;
28 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
29 import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
31 public class TokenTypeSinkTokenizerTest extends BaseTokenStreamTestCase {
33 public void test() throws IOException {
34 TokenTypeSinkFilter sinkFilter = new TokenTypeSinkFilter("D");
35 String test = "The quick red fox jumped over the lazy brown dogs";
37 TeeSinkTokenFilter ttf = new TeeSinkTokenFilter(new WordTokenFilter(new MockTokenizer(new StringReader(test), MockTokenizer.WHITESPACE, false)));
38 SinkTokenStream sink = ttf.newSinkTokenStream(sinkFilter);
40 boolean seenDogs = false;
42 CharTermAttribute termAtt = ttf.addAttribute(CharTermAttribute.class);
43 TypeAttribute typeAtt = ttf.addAttribute(TypeAttribute.class);
45 while (ttf.incrementToken()) {
46 if (termAtt.toString().equals("dogs")) {
48 assertTrue(typeAtt.type() + " is not equal to " + "D", typeAtt.type().equals("D") == true);
50 assertTrue(typeAtt.type() + " is not null and it should be", typeAtt.type().equals("word"));
53 assertTrue(seenDogs + " does not equal: " + true, seenDogs == true);
57 while (sink.incrementToken()) {
61 assertTrue("sink Size: " + sinkCount + " is not: " + 1, sinkCount == 1);
64 private class WordTokenFilter extends TokenFilter {
65 private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
66 private final TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
68 private WordTokenFilter(TokenStream input) {
73 public final boolean incrementToken() throws IOException {
74 if (!input.incrementToken()) return false;
76 if (termAtt.toString().equals("dogs")) {