1 package org.apache.lucene.analysis.payloads;
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 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.PayloadAttribute;
24 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
25 import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
27 import java.io.IOException;
28 import java.io.StringReader;
30 public class TypeAsPayloadTokenFilterTest extends BaseTokenStreamTestCase {
32 public void test() throws IOException {
33 String test = "The quick red fox jumped over the lazy brown dogs";
35 TypeAsPayloadTokenFilter nptf = new TypeAsPayloadTokenFilter(new WordTokenFilter(new MockTokenizer(new StringReader(test), MockTokenizer.WHITESPACE, false)));
37 CharTermAttribute termAtt = nptf.getAttribute(CharTermAttribute.class);
38 TypeAttribute typeAtt = nptf.getAttribute(TypeAttribute.class);
39 PayloadAttribute payloadAtt = nptf.getAttribute(PayloadAttribute.class);
41 while (nptf.incrementToken()) {
42 assertTrue(typeAtt.type() + " is not null and it should be", typeAtt.type().equals(String.valueOf(Character.toUpperCase(termAtt.buffer()[0]))));
43 assertTrue("nextToken.getPayload() is null and it shouldn't be", payloadAtt.getPayload() != null);
44 String type = new String(payloadAtt.getPayload().getData(), "UTF-8");
45 assertTrue(type + " is not equal to " + typeAtt.type(), type.equals(typeAtt.type()) == true);
49 assertTrue(count + " does not equal: " + 10, count == 10);
52 private final class WordTokenFilter extends TokenFilter {
53 private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
54 private final TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
56 private WordTokenFilter(TokenStream input) {
61 public boolean incrementToken() throws IOException {
62 if (input.incrementToken()) {
63 typeAtt.setType(String.valueOf(Character.toUpperCase(termAtt.buffer()[0])));