add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / payloads / NumericPayloadTokenFilterTest.java
1 package org.apache.lucene.analysis.payloads;
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 org.apache.lucene.analysis.BaseTokenStreamTestCase;
20 import org.apache.lucene.analysis.MockTokenizer;
21 import org.apache.lucene.analysis.TokenFilter;
22 import org.apache.lucene.analysis.TokenStream;
23 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
24 import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
25 import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
26
27 import java.io.IOException;
28 import java.io.StringReader;
29
30 public class NumericPayloadTokenFilterTest extends BaseTokenStreamTestCase {
31
32   public void test() throws IOException {
33     String test = "The quick red fox jumped over the lazy brown dogs";
34
35     NumericPayloadTokenFilter nptf = new NumericPayloadTokenFilter(new WordTokenFilter(new MockTokenizer(new StringReader(test), MockTokenizer.WHITESPACE, false)), 3, "D");
36     boolean seenDogs = false;
37     CharTermAttribute termAtt = nptf.getAttribute(CharTermAttribute.class);
38     TypeAttribute typeAtt = nptf.getAttribute(TypeAttribute.class);
39     PayloadAttribute payloadAtt = nptf.getAttribute(PayloadAttribute.class);
40     nptf.reset();
41     while (nptf.incrementToken()) {
42       if (termAtt.toString().equals("dogs")) {
43         seenDogs = true;
44         assertTrue(typeAtt.type() + " is not equal to " + "D", typeAtt.type().equals("D") == true);
45         assertTrue("payloadAtt.getPayload() is null and it shouldn't be", payloadAtt.getPayload() != null);
46         byte [] bytes = payloadAtt.getPayload().getData();//safe here to just use the bytes, otherwise we should use offset, length
47         assertTrue(bytes.length + " does not equal: " + payloadAtt.getPayload().length(), bytes.length == payloadAtt.getPayload().length());
48         assertTrue(payloadAtt.getPayload().getOffset() + " does not equal: " + 0, payloadAtt.getPayload().getOffset() == 0);
49         float pay = PayloadHelper.decodeFloat(bytes);
50         assertTrue(pay + " does not equal: " + 3, pay == 3);
51       } else {
52         assertTrue(typeAtt.type() + " is not null and it should be", typeAtt.type().equals("word"));
53       }
54     }
55     assertTrue(seenDogs + " does not equal: " + true, seenDogs == true);
56   }
57
58   private final class WordTokenFilter extends TokenFilter {
59     private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
60     private final TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
61     
62     private WordTokenFilter(TokenStream input) {
63       super(input);
64     }
65     
66     @Override
67     public boolean incrementToken() throws IOException {
68       if (input.incrementToken()) {
69         if (termAtt.toString().equals("dogs"))
70           typeAtt.setType("D");
71         return true;
72       } else {
73         return false;
74       }
75     }
76   }
77
78 }