1
package org.apache.lucene.index;
3
* Copyright 2005 The Apache Software Foundation
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* 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
import org.apache.lucene.util.LuceneTestCase;
20
import java.io.IOException;
21
import java.util.BitSet;
24
public class TestPositionBasedTermVectorMapper extends LuceneTestCase {
25
protected String[] tokens;
26
protected int[][] thePositions;
27
protected TermVectorOffsetInfo[][] offsets;
28
protected int numPositions;
31
public void setUp() throws Exception {
33
tokens = new String[]{"here", "is", "some", "text", "to", "test", "extra"};
34
thePositions = new int[tokens.length][];
35
offsets = new TermVectorOffsetInfo[tokens.length][];
37
//save off the last one so we can add it with the same positions as some of the others, but in a predictable way
38
for (int i = 0; i < tokens.length - 1; i++)
40
thePositions[i] = new int[2 * i + 1];//give 'em all some positions
41
for (int j = 0; j < thePositions[i].length; j++)
43
thePositions[i][j] = numPositions++;
45
offsets[i] = new TermVectorOffsetInfo[thePositions[i].length];
46
for (int j = 0; j < offsets[i].length; j++) {
47
offsets[i][j] = new TermVectorOffsetInfo(j, j + 1);//the actual value here doesn't much matter
50
thePositions[tokens.length - 1] = new int[1];
51
thePositions[tokens.length - 1][0] = 0;//put this at the same position as "here"
52
offsets[tokens.length - 1] = new TermVectorOffsetInfo[1];
53
offsets[tokens.length - 1][0] = new TermVectorOffsetInfo(0, 1);
56
public void test() throws IOException {
57
PositionBasedTermVectorMapper mapper = new PositionBasedTermVectorMapper();
59
mapper.setExpectations("test", tokens.length, true, true);
60
//Test single position
61
for (int i = 0; i < tokens.length; i++) {
62
String token = tokens[i];
63
mapper.map(token, 1, null, thePositions[i]);
66
Map<String,Map<Integer,PositionBasedTermVectorMapper.TVPositionInfo>> map = mapper.getFieldToTerms();
67
assertTrue("map is null and it shouldn't be", map != null);
68
assertTrue("map Size: " + map.size() + " is not: " + 1, map.size() == 1);
69
Map<Integer,PositionBasedTermVectorMapper.TVPositionInfo> positions = map.get("test");
70
assertTrue("thePositions is null and it shouldn't be", positions != null);
72
assertTrue("thePositions Size: " + positions.size() + " is not: " + numPositions, positions.size() == numPositions);
73
BitSet bits = new BitSet(numPositions);
74
for (Map.Entry<Integer,PositionBasedTermVectorMapper.TVPositionInfo> entry : positions.entrySet()) {
76
PositionBasedTermVectorMapper.TVPositionInfo info = entry.getValue();
77
assertTrue("info is null and it shouldn't be", info != null);
78
int pos = entry.getKey().intValue();
80
assertTrue(info.getPosition() + " does not equal: " + pos, info.getPosition() == pos);
81
assertTrue("info.getOffsets() is null and it shouldn't be", info.getOffsets() != null);
84
assertTrue("info.getTerms() Size: " + info.getTerms().size() + " is not: " + 2, info.getTerms().size() == 2);//need a test for multiple terms at one pos
85
assertTrue("info.getOffsets() Size: " + info.getOffsets().size() + " is not: " + 2, info.getOffsets().size() == 2);
89
assertTrue("info.getTerms() Size: " + info.getTerms().size() + " is not: " + 1, info.getTerms().size() == 1);//need a test for multiple terms at one pos
90
assertTrue("info.getOffsets() Size: " + info.getOffsets().size() + " is not: " + 1, info.getOffsets().size() == 1);
93
assertTrue("Bits are not all on", bits.cardinality() == numPositions);