strengthen analysis tests: extend Lucene's base testcase

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/branches/newtrunk@925682 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2010-03-20 20:59:55 +00:00
parent 733aabd082
commit db59c17a36
1 changed files with 3 additions and 128 deletions

View File

@ -17,142 +17,17 @@
package org.apache.solr.analysis;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import java.util.Collections;
import java.util.Map;
import junit.framework.TestCase;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
/**
* General token testing helper functions
*/
public abstract class BaseTokenTestCase extends TestCase
public abstract class BaseTokenTestCase extends BaseTokenStreamTestCase
{
protected static final Map<String,String> DEFAULT_VERSION_PARAM =
Collections.singletonMap("luceneMatchVersion", System.getProperty("tests.luceneMatchVersion", "LUCENE_CURRENT"));
// some helpers to test Analyzers and TokenStreams:
// these are taken from Lucene's BaseTokenStreamTestCase
public static void assertTokenStreamContents(TokenStream ts, String[] output,
int startOffsets[], int endOffsets[], String types[], int posIncrements[])
throws IOException {
assertNotNull(output);
assertTrue("has TermAttribute", ts.hasAttribute(TermAttribute.class));
TermAttribute termAtt = (TermAttribute) ts
.getAttribute(TermAttribute.class);
OffsetAttribute offsetAtt = null;
if (startOffsets != null || endOffsets != null) {
assertTrue("has OffsetAttribute", ts.hasAttribute(OffsetAttribute.class));
offsetAtt = (OffsetAttribute) ts.getAttribute(OffsetAttribute.class);
}
TypeAttribute typeAtt = null;
if (types != null) {
assertTrue("has TypeAttribute", ts.hasAttribute(TypeAttribute.class));
typeAtt = (TypeAttribute) ts.getAttribute(TypeAttribute.class);
}
PositionIncrementAttribute posIncrAtt = null;
if (posIncrements != null) {
assertTrue("has PositionIncrementAttribute", ts
.hasAttribute(PositionIncrementAttribute.class));
posIncrAtt = (PositionIncrementAttribute) ts
.getAttribute(PositionIncrementAttribute.class);
}
ts.reset();
for (int i = 0; i < output.length; i++) {
// extra safety to enforce, that the state is not preserved and also
// assign bogus values
ts.clearAttributes();
termAtt.setTermBuffer("bogusTerm");
if (offsetAtt != null) offsetAtt.setOffset(14584724, 24683243);
if (typeAtt != null) typeAtt.setType("bogusType");
if (posIncrAtt != null) posIncrAtt.setPositionIncrement(45987657);
assertTrue("token " + i + " exists", ts.incrementToken());
assertEquals("term " + i, output[i], termAtt.term());
if (startOffsets != null) assertEquals("startOffset " + i,
startOffsets[i], offsetAtt.startOffset());
if (endOffsets != null) assertEquals("endOffset " + i, endOffsets[i],
offsetAtt.endOffset());
if (types != null) assertEquals("type " + i, types[i], typeAtt.type());
if (posIncrements != null) assertEquals("posIncrement " + i,
posIncrements[i], posIncrAtt.getPositionIncrement());
}
assertFalse("end of stream", ts.incrementToken());
ts.end();
ts.close();
}
public static void assertTokenStreamContents(TokenStream ts, String[] output)
throws IOException {
assertTokenStreamContents(ts, output, null, null, null, null);
}
public static void assertTokenStreamContents(TokenStream ts, String[] output,
String[] types) throws IOException {
assertTokenStreamContents(ts, output, null, null, types, null);
}
public static void assertTokenStreamContents(TokenStream ts, String[] output,
int[] posIncrements) throws IOException {
assertTokenStreamContents(ts, output, null, null, null, posIncrements);
}
public static void assertTokenStreamContents(TokenStream ts, String[] output,
int startOffsets[], int endOffsets[]) throws IOException {
assertTokenStreamContents(ts, output, startOffsets, endOffsets, null, null);
}
public static void assertTokenStreamContents(TokenStream ts, String[] output,
int startOffsets[], int endOffsets[], int[] posIncrements)
throws IOException {
assertTokenStreamContents(ts, output, startOffsets, endOffsets, null,
posIncrements);
}
public static void assertAnalyzesTo(Analyzer a, String input,
String[] output, int startOffsets[], int endOffsets[], String types[],
int posIncrements[]) throws IOException {
assertTokenStreamContents(a.tokenStream("dummy", new StringReader(input)),
output, startOffsets, endOffsets, types, posIncrements);
}
public static void assertAnalyzesTo(Analyzer a, String input, String[] output)
throws IOException {
assertAnalyzesTo(a, input, output, null, null, null, null);
}
public static void assertAnalyzesTo(Analyzer a, String input,
String[] output, String[] types) throws IOException {
assertAnalyzesTo(a, input, output, null, null, types, null);
}
public static void assertAnalyzesTo(Analyzer a, String input,
String[] output, int[] posIncrements) throws IOException {
assertAnalyzesTo(a, input, output, null, null, null, posIncrements);
}
public static void assertAnalyzesTo(Analyzer a, String input,
String[] output, int startOffsets[], int endOffsets[]) throws IOException {
assertAnalyzesTo(a, input, output, startOffsets, endOffsets, null, null);
}
public static void assertAnalyzesTo(Analyzer a, String input,
String[] output, int startOffsets[], int endOffsets[], int[] posIncrements)
throws IOException {
assertAnalyzesTo(a, input, output, startOffsets, endOffsets, null,
posIncrements);
}
}