pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / TestPositionBasedTermVectorMapper.java
1 package org.apache.lucene.index;
2 /**
3  * Copyright 2005 The Apache Software Foundation
4  *
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
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 import org.apache.lucene.util.LuceneTestCase;
19
20 import java.io.IOException;
21 import java.util.BitSet;
22 import java.util.Map;
23
24 public class TestPositionBasedTermVectorMapper extends LuceneTestCase {
25   protected String[] tokens;
26   protected int[][] thePositions;
27   protected TermVectorOffsetInfo[][] offsets;
28   protected int numPositions;
29
30   @Override
31   public void setUp() throws Exception {
32     super.setUp();
33     tokens = new String[]{"here", "is", "some", "text", "to", "test", "extra"};
34     thePositions = new int[tokens.length][];
35     offsets = new TermVectorOffsetInfo[tokens.length][];
36     numPositions = 0;
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++)
39     {
40       thePositions[i] = new int[2 * i + 1];//give 'em all some positions
41       for (int j = 0; j < thePositions[i].length; j++)
42       {
43         thePositions[i][j] = numPositions++;
44       }
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
48       }
49     }
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);
54   }
55
56   public void test() throws IOException {
57     PositionBasedTermVectorMapper mapper = new PositionBasedTermVectorMapper();
58     
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]);
64
65     }
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);
71     
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()) {
75     
76       PositionBasedTermVectorMapper.TVPositionInfo info = entry.getValue();
77       assertTrue("info is null and it shouldn't be", info != null);
78       int pos = entry.getKey().intValue();
79       bits.set(pos);
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);
82       if (pos == 0)
83       {
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);
86       }
87       else
88       {
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);
91       }
92     }
93     assertTrue("Bits are not all on", bits.cardinality() == numPositions);
94   }
95
96
97
98   
99 }