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

« back to all changes in this revision

Viewing changes to solr/solrj/src/test/org/apache/solr/client/solrj/response/AnlysisResponseBaseTest.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.client.solrj.response;
19
 
 
20
 
import org.apache.lucene.util.LuceneTestCase;
21
 
import org.apache.solr.common.util.NamedList;
22
 
import org.junit.Test;
23
 
 
24
 
import java.util.ArrayList;
25
 
import java.util.List;
26
 
 
27
 
/**
28
 
 * A Test case for the {@link AnalysisResponseBase} class.
29
 
 *
30
 
 * @version $Id: AnlysisResponseBaseTest.java 1052938 2010-12-26 20:21:48Z rmuir $
31
 
 * @since solr 1.4
32
 
 */
33
 
@SuppressWarnings("unchecked")
34
 
public class AnlysisResponseBaseTest extends LuceneTestCase {
35
 
 
36
 
  /**
37
 
   * Tests the {@link AnalysisResponseBase#buildTokenInfo(org.apache.solr.common.util.NamedList)} method.
38
 
   */
39
 
  @Test
40
 
  public void testBuildTokenInfo() throws Exception {
41
 
 
42
 
    NamedList tokenNL = new NamedList();
43
 
    tokenNL.add("text", "JUMPING");
44
 
    tokenNL.add("type", "word");
45
 
    tokenNL.add("start", 0);
46
 
    tokenNL.add("end", 7);
47
 
    tokenNL.add("position", 1);
48
 
 
49
 
    AnalysisResponseBase response = new AnalysisResponseBase();
50
 
 
51
 
    AnalysisResponseBase.TokenInfo tokenInfo = response.buildTokenInfo(tokenNL);
52
 
    assertEquals("JUMPING", tokenInfo.getText());
53
 
    assertEquals(null, tokenInfo.getRawText());
54
 
    assertEquals("word", tokenInfo.getType());
55
 
    assertEquals(0, tokenInfo.getStart());
56
 
    assertEquals(7, tokenInfo.getEnd());
57
 
    assertEquals(1, tokenInfo.getPosition());
58
 
    assertFalse(tokenInfo.isMatch());
59
 
 
60
 
    tokenNL.add("rawText", "JUMPING1");
61
 
    tokenNL.add("match", true);
62
 
 
63
 
    tokenInfo = response.buildTokenInfo(tokenNL);
64
 
    assertEquals("JUMPING", tokenInfo.getText());
65
 
    assertEquals("JUMPING1", tokenInfo.getRawText());
66
 
    assertEquals("word", tokenInfo.getType());
67
 
    assertEquals(0, tokenInfo.getStart());
68
 
    assertEquals(7, tokenInfo.getEnd());
69
 
    assertEquals(1, tokenInfo.getPosition());
70
 
    assertTrue(tokenInfo.isMatch());
71
 
  }
72
 
 
73
 
  /**
74
 
   * Tests the {@link AnalysisResponseBase#buildPhases(org.apache.solr.common.util.NamedList)} )} method.
75
 
   */
76
 
  @Test
77
 
  public void testBuildPhases() throws Exception {
78
 
 
79
 
    final AnalysisResponseBase.TokenInfo tokenInfo = new AnalysisResponseBase.TokenInfo("text", null, "type", 0, 3, 1, false);
80
 
    NamedList nl = new NamedList();
81
 
    nl.add("Tokenizer", buildFakeTokenInfoList(6));
82
 
    nl.add("Filter1", buildFakeTokenInfoList(5));
83
 
    nl.add("Filter2", buildFakeTokenInfoList(4));
84
 
    nl.add("Filter3", buildFakeTokenInfoList(3));
85
 
 
86
 
    AnalysisResponseBase response = new AnalysisResponseBase() {
87
 
      @Override
88
 
      protected TokenInfo buildTokenInfo(NamedList tokenNL) {
89
 
        return tokenInfo;
90
 
      }
91
 
    };
92
 
 
93
 
    List<AnalysisResponseBase.AnalysisPhase> phases = response.buildPhases(nl);
94
 
 
95
 
    assertEquals(4, phases.size());
96
 
    assertPhase(phases.get(0), "Tokenizer", 6, tokenInfo);
97
 
    assertPhase(phases.get(1), "Filter1", 5, tokenInfo);
98
 
    assertPhase(phases.get(2), "Filter2", 4, tokenInfo);
99
 
    assertPhase(phases.get(3), "Filter3", 3, tokenInfo);
100
 
  }
101
 
 
102
 
  //================================================ Helper Methods ==================================================
103
 
 
104
 
  private List<NamedList> buildFakeTokenInfoList(int numberOfTokens) {
105
 
    List<NamedList> list = new ArrayList<NamedList>(numberOfTokens);
106
 
    for (int i = 0; i < numberOfTokens; i++) {
107
 
      list.add(new NamedList());
108
 
    }
109
 
    return list;
110
 
  }
111
 
 
112
 
  private void assertPhase(AnalysisResponseBase.AnalysisPhase phase, String expectedClassName, int expectedTokenCount, AnalysisResponseBase.TokenInfo expectedToken) {
113
 
 
114
 
    assertEquals(expectedClassName, phase.getClassName());
115
 
    List<AnalysisResponseBase.TokenInfo> tokens = phase.getTokens();
116
 
    assertEquals(expectedTokenCount, tokens.size());
117
 
    for (AnalysisResponseBase.TokenInfo token : tokens) {
118
 
      assertSame(expectedToken, token);
119
 
    }
120
 
  }
121
 
}