mirror of https://github.com/apache/lucene.git
LUCENE-5635: add CrankyTokenFilter
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1591605 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1c77c9e5e9
commit
798624fe8b
|
@ -0,0 +1,71 @@
|
|||
package org.apache.lucene.analysis;
|
||||
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Throws IOException from random Tokenstream methods.
|
||||
* <p>
|
||||
* This can be used to simulate a buggy analyzer in IndexWriter,
|
||||
* where we must delete the document but not abort everything in the buffer.
|
||||
*/
|
||||
public class CrankyTokenFilter extends TokenFilter {
|
||||
final Random random;
|
||||
int thingToDo;
|
||||
|
||||
/** Creates a new CrankyTokenFilter */
|
||||
public CrankyTokenFilter(TokenStream input, Random random) {
|
||||
super(input);
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean incrementToken() throws IOException {
|
||||
if (thingToDo == 0 && random.nextBoolean()) {
|
||||
throw new IOException("Fake IOException from TokenStream.incrementToken()");
|
||||
}
|
||||
return input.incrementToken();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void end() throws IOException {
|
||||
super.end();
|
||||
if (thingToDo == 1 && random.nextBoolean()) {
|
||||
throw new IOException("Fake IOException from TokenStream.end()");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() throws IOException {
|
||||
super.reset();
|
||||
thingToDo = random.nextInt(10);
|
||||
if (thingToDo == 2 && random.nextBoolean()) {
|
||||
throw new IOException("Fake IOException from TokenStream.reset()");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
super.close();
|
||||
if (thingToDo == 3 && random.nextBoolean()) {
|
||||
throw new IOException("Fake IOException from TokenStream.close()");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue