add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / sinks / TokenTypeSinkTokenizerTest.java
1 package org.apache.lucene.analysis.sinks;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 import java.io.IOException;
20 import java.io.StringReader;
21
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;
30
31 public class TokenTypeSinkTokenizerTest extends BaseTokenStreamTestCase {
32
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";
36
37     TeeSinkTokenFilter ttf = new TeeSinkTokenFilter(new WordTokenFilter(new MockTokenizer(new StringReader(test), MockTokenizer.WHITESPACE, false)));
38     SinkTokenStream sink = ttf.newSinkTokenStream(sinkFilter);
39     
40     boolean seenDogs = false;
41
42     CharTermAttribute termAtt = ttf.addAttribute(CharTermAttribute.class);
43     TypeAttribute typeAtt = ttf.addAttribute(TypeAttribute.class);
44     ttf.reset();
45     while (ttf.incrementToken()) {
46       if (termAtt.toString().equals("dogs")) {
47         seenDogs = true;
48         assertTrue(typeAtt.type() + " is not equal to " + "D", typeAtt.type().equals("D") == true);
49       } else {
50         assertTrue(typeAtt.type() + " is not null and it should be", typeAtt.type().equals("word"));
51       }
52     }
53     assertTrue(seenDogs + " does not equal: " + true, seenDogs == true);
54     
55     int sinkCount = 0;
56     sink.reset();
57     while (sink.incrementToken()) {
58       sinkCount++;
59     }
60
61     assertTrue("sink Size: " + sinkCount + " is not: " + 1, sinkCount == 1);
62   }
63
64   private class WordTokenFilter extends TokenFilter {
65     private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
66     private final TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
67     
68     private WordTokenFilter(TokenStream input) {
69       super(input);
70     }
71
72     @Override
73     public final boolean incrementToken() throws IOException {
74       if (!input.incrementToken()) return false;
75       
76       if (termAtt.toString().equals("dogs")) {
77         typeAtt.setType("D");
78       }
79       return true;
80     }
81   }
82 }