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.TokenFilter;
21: import org.apache.lucene.analysis.TokenStream;
22: import org.apache.lucene.analysis.Token;
23: import org.apache.lucene.analysis.WhitespaceTokenizer;
24:
25: import java.io.IOException;
26: import java.io.StringReader;
27:
28: public class TypeAsPayloadTokenFilterTest extends TestCase {
29:
30: public TypeAsPayloadTokenFilterTest(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: TypeAsPayloadTokenFilter nptf = new TypeAsPayloadTokenFilter(
45: new WordTokenFilter(new WhitespaceTokenizer(
46: new StringReader(test))));
47: Token tok = new Token();
48: int count = 0;
49: while ((tok = nptf.next(tok)) != null) {
50: assertTrue(tok.type() + " is not null and it should be",
51: tok.type().equals(
52: String.valueOf(Character.toUpperCase(tok
53: .termBuffer()[0]))));
54: assertTrue("tok.getPayload() is null and it shouldn't be",
55: tok.getPayload() != null);
56: String type = new String(tok.getPayload().getData(),
57: "UTF-8");
58: assertTrue("type is null and it shouldn't be", type != null);
59: assertTrue(type + " is not equal to " + tok.type(), type
60: .equals(tok.type()) == true);
61: count++;
62: }
63: assertTrue(count + " does not equal: " + 10, count == 10);
64: }
65:
66: private class WordTokenFilter extends TokenFilter {
67: private WordTokenFilter(TokenStream input) {
68: super (input);
69: }
70:
71: public Token next(Token result) throws IOException {
72: result = input.next(result);
73: if (result != null) {
74: result.setType(String.valueOf(Character
75: .toUpperCase(result.termBuffer()[0])));
76: }
77: return result;
78: }
79: }
80:
81: }
|