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

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/analysis/TestTrimFilter.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
 
/**
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
package org.apache.solr.analysis;
19
 
 
20
 
import java.io.IOException;
21
 
import java.util.Collection;
22
 
import java.util.HashMap;
23
 
import java.util.Map;
24
 
 
25
 
import org.apache.lucene.analysis.Token;
26
 
import org.apache.lucene.analysis.TokenStream;
27
 
import org.apache.lucene.analysis.tokenattributes.FlagsAttribute;
28
 
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
29
 
import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
30
 
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
31
 
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
32
 
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
33
 
 
34
 
/**
35
 
 * @version $Id:$
36
 
 */
37
 
public class TestTrimFilter extends BaseTokenTestCase {
38
 
 
39
 
  public void testTrim() throws Exception {
40
 
    char[] a = " a ".toCharArray();
41
 
    char[] b = "b   ".toCharArray();
42
 
    char[] ccc = "cCc".toCharArray();
43
 
    char[] whitespace = "   ".toCharArray();
44
 
    char[] empty = "".toCharArray();
45
 
    TrimFilterFactory factory = new TrimFilterFactory();
46
 
    Map<String,String> args = new HashMap<String,String>();
47
 
    args.put("updateOffsets", "false");
48
 
    factory.init(args);
49
 
    TokenStream ts = factory.create(new IterTokenStream(new Token(a, 0, a.length, 1, 5),
50
 
                    new Token(b, 0, b.length, 6, 10),
51
 
                    new Token(ccc, 0, ccc.length, 11, 15),
52
 
                    new Token(whitespace, 0, whitespace.length, 16, 20),
53
 
                    new Token(empty, 0, empty.length, 21, 21)));
54
 
 
55
 
    assertTokenStreamContents(ts, new String[] { "a", "b", "cCc", "", ""});
56
 
 
57
 
    a = " a".toCharArray();
58
 
    b = "b ".toCharArray();
59
 
    ccc = " c ".toCharArray();
60
 
    whitespace = "   ".toCharArray();
61
 
    factory = new TrimFilterFactory();
62
 
    args = new HashMap<String,String>();
63
 
    args.put("updateOffsets", "true");
64
 
    factory.init(args);
65
 
    ts = factory.create(new IterTokenStream(
66
 
            new Token(a, 0, a.length, 0, 2),
67
 
            new Token(b, 0, b.length, 0, 2),
68
 
            new Token(ccc, 0, ccc.length, 0, 3),
69
 
            new Token(whitespace, 0, whitespace.length, 0, 3)));
70
 
    
71
 
    assertTokenStreamContents(ts, 
72
 
        new String[] { "a", "b", "c", "" },
73
 
        new int[] { 1, 0, 1, 3 },
74
 
        new int[] { 2, 1, 2, 3 },
75
 
        new int[] { 1, 1, 1, 1 });
76
 
  }
77
 
  
78
 
  /**
79
 
   * @deprecated does not support custom attributes
80
 
   */
81
 
  @Deprecated
82
 
  private static class IterTokenStream extends TokenStream {
83
 
    final Token tokens[];
84
 
    int index = 0;
85
 
    CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
86
 
    OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
87
 
    PositionIncrementAttribute posIncAtt = addAttribute(PositionIncrementAttribute.class);
88
 
    FlagsAttribute flagsAtt = addAttribute(FlagsAttribute.class);
89
 
    TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
90
 
    PayloadAttribute payloadAtt = addAttribute(PayloadAttribute.class);
91
 
    
92
 
    public IterTokenStream(Token... tokens) {
93
 
      super();
94
 
      this.tokens = tokens;
95
 
    }
96
 
    
97
 
    public IterTokenStream(Collection<Token> tokens) {
98
 
      this(tokens.toArray(new Token[tokens.size()]));
99
 
    }
100
 
    
101
 
    @Override
102
 
    public boolean incrementToken() throws IOException {
103
 
      if (index >= tokens.length)
104
 
        return false;
105
 
      else {
106
 
        clearAttributes();
107
 
        Token token = tokens[index++];
108
 
        termAtt.setEmpty().append(token);
109
 
        offsetAtt.setOffset(token.startOffset(), token.endOffset());
110
 
        posIncAtt.setPositionIncrement(token.getPositionIncrement());
111
 
        flagsAtt.setFlags(token.getFlags());
112
 
        typeAtt.setType(token.type());
113
 
        payloadAtt.setPayload(token.getPayload());
114
 
        return true;
115
 
      }
116
 
    }
117
 
  }
118
 
}