001: package org.apache.lucene.index;
002:
003: /**
004: * Copyright 2005 The Apache Software Foundation
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.apache.lucene.util.LuceneTestCase;
020:
021: import java.io.IOException;
022: import java.util.BitSet;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: public class TestPositionBasedTermVectorMapper extends LuceneTestCase {
027: protected String[] tokens;
028: protected int[][] thePositions;
029: protected TermVectorOffsetInfo[][] offsets;
030: protected int numPositions;
031:
032: public TestPositionBasedTermVectorMapper(String s) {
033: super (s);
034: }
035:
036: protected void setUp() throws Exception {
037: super .setUp();
038: tokens = new String[] { "here", "is", "some", "text", "to",
039: "test", "extra" };
040: thePositions = new int[tokens.length][];
041: offsets = new TermVectorOffsetInfo[tokens.length][];
042: numPositions = 0;
043: //save off the last one so we can add it with the same positions as some of the others, but in a predictable way
044: for (int i = 0; i < tokens.length - 1; i++) {
045: thePositions[i] = new int[2 * i + 1];//give 'em all some positions
046: for (int j = 0; j < thePositions[i].length; j++) {
047: thePositions[i][j] = numPositions++;
048: }
049: offsets[i] = new TermVectorOffsetInfo[thePositions[i].length];
050: for (int j = 0; j < offsets[i].length; j++) {
051: offsets[i][j] = new TermVectorOffsetInfo(j, j + 1);//the actual value here doesn't much matter
052: }
053: }
054: thePositions[tokens.length - 1] = new int[1];
055: thePositions[tokens.length - 1][0] = 0;//put this at the same position as "here"
056: offsets[tokens.length - 1] = new TermVectorOffsetInfo[1];
057: offsets[tokens.length - 1][0] = new TermVectorOffsetInfo(0, 1);
058: }
059:
060: public void test() throws IOException {
061: PositionBasedTermVectorMapper mapper = new PositionBasedTermVectorMapper();
062:
063: mapper.setExpectations("test", tokens.length, true, true);
064: //Test single position
065: for (int i = 0; i < tokens.length; i++) {
066: String token = tokens[i];
067: mapper.map(token, 1, null, thePositions[i]);
068:
069: }
070: Map map = mapper.getFieldToTerms();
071: assertTrue("map is null and it shouldn't be", map != null);
072: assertTrue("map Size: " + map.size() + " is not: " + 1, map
073: .size() == 1);
074: Map positions = (Map) map.get("test");
075: assertTrue("thePositions is null and it shouldn't be",
076: positions != null);
077:
078: assertTrue("thePositions Size: " + positions.size()
079: + " is not: " + numPositions,
080: positions.size() == numPositions);
081: BitSet bits = new BitSet(numPositions);
082: for (Iterator iterator = positions.entrySet().iterator(); iterator
083: .hasNext();) {
084: Map.Entry entry = (Map.Entry) iterator.next();
085: PositionBasedTermVectorMapper.TVPositionInfo info = (PositionBasedTermVectorMapper.TVPositionInfo) entry
086: .getValue();
087: assertTrue("info is null and it shouldn't be", info != null);
088: int pos = ((Integer) entry.getKey()).intValue();
089: bits.set(pos);
090: assertTrue(info.getPosition() + " does not equal: " + pos,
091: info.getPosition() == pos);
092: assertTrue("info.getOffsets() is null and it shouldn't be",
093: info.getOffsets() != null);
094: if (pos == 0) {
095: assertTrue("info.getTerms() Size: "
096: + info.getTerms().size() + " is not: " + 2,
097: info.getTerms().size() == 2);//need a test for multiple terms at one pos
098: assertTrue("info.getOffsets() Size: "
099: + info.getOffsets().size() + " is not: " + 2,
100: info.getOffsets().size() == 2);
101: } else {
102: assertTrue("info.getTerms() Size: "
103: + info.getTerms().size() + " is not: " + 1,
104: info.getTerms().size() == 1);//need a test for multiple terms at one pos
105: assertTrue("info.getOffsets() Size: "
106: + info.getOffsets().size() + " is not: " + 1,
107: info.getOffsets().size() == 1);
108: }
109: }
110: assertTrue("Bits are not all on",
111: bits.cardinality() == numPositions);
112: }
113:
114: }
|