01: package org.apache.lucene.analysis.payloads;
02:
03: /**
04: * Copyright 2004 The Apache Software Foundation
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
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 junit.framework.TestCase;
20: import org.apache.lucene.analysis.Token;
21: import org.apache.lucene.analysis.TokenFilter;
22: import org.apache.lucene.analysis.TokenStream;
23: import org.apache.lucene.analysis.WhitespaceTokenizer;
24:
25: import java.io.IOException;
26: import java.io.StringReader;
27:
28: public class NumericPayloadTokenFilterTest extends TestCase {
29:
30: public NumericPayloadTokenFilterTest(String s) {
31: super (s);
32: }
33:
34: protected void setUp() {
35: }
36:
37: protected void tearDown() {
38:
39: }
40:
41: public void test() throws IOException {
42: String test = "The quick red fox jumped over the lazy brown dogs";
43:
44: NumericPayloadTokenFilter nptf = new NumericPayloadTokenFilter(
45: new WordTokenFilter(new WhitespaceTokenizer(
46: new StringReader(test))), 3, "D");
47: Token tok = new Token();
48: boolean seenDogs = false;
49: while ((tok = nptf.next(tok)) != null) {
50: if (tok.termText().equals("dogs")) {
51: seenDogs = true;
52: assertTrue(tok.type() + " is not equal to " + "D", tok
53: .type().equals("D") == true);
54: assertTrue(
55: "tok.getPayload() is null and it shouldn't be",
56: tok.getPayload() != null);
57: byte[] bytes = tok.getPayload().getData();//safe here to just use the bytes, otherwise we should use offset, length
58: assertTrue(bytes.length + " does not equal: "
59: + tok.getPayload().length(),
60: bytes.length == tok.getPayload().length());
61: assertTrue(tok.getPayload().getOffset()
62: + " does not equal: " + 0, tok.getPayload()
63: .getOffset() == 0);
64: float pay = PayloadHelper.decodeFloat(bytes);
65: assertTrue(pay + " does not equal: " + 3, pay == 3);
66: } else {
67: assertTrue(
68: tok.type() + " is not null and it should be",
69: tok.type().equals("word"));
70: }
71: }
72: assertTrue(seenDogs + " does not equal: " + true,
73: seenDogs == true);
74: }
75:
76: private class WordTokenFilter extends TokenFilter {
77: private WordTokenFilter(TokenStream input) {
78: super (input);
79: }
80:
81: public Token next(Token result) throws IOException {
82: result = input.next(result);
83: if (result != null && result.termText().equals("dogs")) {
84: result.setType("D");
85: }
86: return result;
87: }
88: }
89:
90: }
|