LUCENE-3969: don't get caught by tokenizers that consume in ctor and throw IAE or UOE ever again

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene3969@1311351 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-04-09 17:32:39 +00:00
parent 2a01acc0e8
commit f41576a306
1 changed files with 77 additions and 1 deletions

View File

@ -18,6 +18,7 @@ package org.apache.lucene.analysis.core;
*/ */
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
@ -25,6 +26,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.net.URL; import java.net.URL;
import java.nio.CharBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -595,8 +597,12 @@ public class TestRandomChains extends BaseTokenStreamTestCase {
while (spec.tokenizer == null) { while (spec.tokenizer == null) {
final Constructor<? extends Tokenizer> ctor = tokenizers.get(random.nextInt(tokenizers.size())); final Constructor<? extends Tokenizer> ctor = tokenizers.get(random.nextInt(tokenizers.size()));
final StringBuilder descr = new StringBuilder(); final StringBuilder descr = new StringBuilder();
final Object args[] = newTokenizerArgs(random, reader, ctor.getParameterTypes()); CheckThatYouDidntReadAnythingReaderWrapper wrapper = new CheckThatYouDidntReadAnythingReaderWrapper(reader);
final Object args[] = newTokenizerArgs(random, wrapper, ctor.getParameterTypes());
spec.tokenizer = createComponent(ctor, args, descr); spec.tokenizer = createComponent(ctor, args, descr);
if (spec.tokenizer == null) {
assert wrapper.readSomething == false;
}
spec.toString = descr.toString(); spec.toString = descr.toString();
} }
return spec; return spec;
@ -643,6 +649,76 @@ public class TestRandomChains extends BaseTokenStreamTestCase {
} }
} }
// wants charfilter to be a filterreader...
static class CheckThatYouDidntReadAnythingReaderWrapper extends CharStream {
boolean readSomething;
CharStream in;
CheckThatYouDidntReadAnythingReaderWrapper(Reader in) {
this.in = CharReader.get(in);
}
@Override
public int correctOffset(int currentOff) {
return in.correctOffset(currentOff);
}
@Override
public void close() throws IOException {
in.close();
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
readSomething = true;
return in.read(cbuf, off, len);
}
@Override
public int read() throws IOException {
readSomething = true;
return in.read();
}
@Override
public int read(CharBuffer target) throws IOException {
readSomething = true;
return in.read(target);
}
@Override
public void mark(int readAheadLimit) throws IOException {
in.mark(readAheadLimit);
}
@Override
public boolean markSupported() {
return in.markSupported();
}
@Override
public int read(char[] cbuf) throws IOException {
readSomething = true;
return in.read(cbuf);
}
@Override
public boolean ready() throws IOException {
return in.ready();
}
@Override
public void reset() throws IOException {
in.reset();
}
@Override
public long skip(long n) throws IOException {
readSomething = true;
return in.skip(n);
}
}
static class TokenizerSpec { static class TokenizerSpec {
Tokenizer tokenizer; Tokenizer tokenizer;
String toString; String toString;