LUCENE-6125: add more safety checks to MockDirectoryWrapper

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1647371 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2014-12-22 18:42:28 +00:00
parent f75adf5956
commit 2bef294a09
2 changed files with 47 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package org.apache.lucene.store;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
@ -42,12 +43,17 @@ public class MockIndexInputWrapper extends IndexInput {
@Override
public void close() throws IOException {
if (closed) {
delegate.close(); // don't mask double-close bugs
return;
}
closed = true;
try {
// turn on the following to look for leaks closing inputs,
// after fixing TestTransactions
// dir.maybeThrowDeterministicException();
} finally {
closed = true;
delegate.close();
// Pending resolution on LUCENE-686 we may want to
// remove the conditional check so we also track that
@ -183,6 +189,30 @@ public class MockIndexInputWrapper extends IndexInput {
return delegate.readVLong();
}
@Override
public int readZInt() throws IOException {
ensureOpen();
return delegate.readZInt();
}
@Override
public long readZLong() throws IOException {
ensureOpen();
return delegate.readZLong();
}
@Override
public Set<String> readStringSet() throws IOException {
ensureOpen();
return delegate.readStringSet();
}
@Override
public void skipBytes(long numBytes) throws IOException {
ensureOpen();
super.skipBytes(numBytes);
}
@Override
public String toString() {
return "MockIndexInputWrapper(" + delegate + ")";

View File

@ -88,8 +88,16 @@ public class MockIndexOutputWrapper extends IndexOutput {
}
}
private boolean closed;
@Override
public void close() throws IOException {
if (closed) {
delegate.close(); // don't mask double-close bugs
return;
}
closed = true;
try {
dir.maybeThrowDeterministicException();
} finally {
@ -106,6 +114,12 @@ public class MockIndexOutputWrapper extends IndexOutput {
}
}
private void ensureOpen() {
if (closed) {
throw new AlreadyClosedException("Already closed: " + this);
}
}
@Override
public void writeByte(byte b) throws IOException {
singleByte[0] = b;
@ -114,6 +128,7 @@ public class MockIndexOutputWrapper extends IndexOutput {
@Override
public void writeBytes(byte[] b, int offset, int len) throws IOException {
ensureOpen();
checkCrashed();
checkDiskFull(b, offset, null, len);
@ -143,6 +158,7 @@ public class MockIndexOutputWrapper extends IndexOutput {
@Override
public void copyBytes(DataInput input, long numBytes) throws IOException {
ensureOpen();
checkCrashed();
checkDiskFull(null, 0, input, numBytes);