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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.solr.client.solrj.response;
20
import org.apache.lucene.util.LuceneTestCase;
21
import org.apache.solr.common.util.NamedList;
22
import org.junit.Test;
24
import java.util.ArrayList;
25
import java.util.List;
28
* A Test case for the {@link AnalysisResponseBase} class.
30
* @version $Id: AnlysisResponseBaseTest.java 1052938 2010-12-26 20:21:48Z rmuir $
33
@SuppressWarnings("unchecked")
34
public class AnlysisResponseBaseTest extends LuceneTestCase {
37
* Tests the {@link AnalysisResponseBase#buildTokenInfo(org.apache.solr.common.util.NamedList)} method.
40
public void testBuildTokenInfo() throws Exception {
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);
49
AnalysisResponseBase response = new AnalysisResponseBase();
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());
60
tokenNL.add("rawText", "JUMPING1");
61
tokenNL.add("match", true);
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());
74
* Tests the {@link AnalysisResponseBase#buildPhases(org.apache.solr.common.util.NamedList)} )} method.
77
public void testBuildPhases() throws Exception {
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));
86
AnalysisResponseBase response = new AnalysisResponseBase() {
88
protected TokenInfo buildTokenInfo(NamedList tokenNL) {
93
List<AnalysisResponseBase.AnalysisPhase> phases = response.buildPhases(nl);
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);
102
//================================================ Helper Methods ==================================================
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());
112
private void assertPhase(AnalysisResponseBase.AnalysisPhase phase, String expectedClassName, int expectedTokenCount, AnalysisResponseBase.TokenInfo expectedToken) {
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);