~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/analyzers/common/src/test/org/apache/lucene/analysis/sinks/TokenTypeSinkTokenizerTest.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
}
 
 
b'\\ No newline at end of file'