Apache Lucene open-source search software
LUCENE-6125: add more safety checks to MockDirectoryWrapper
Robert Muir2bef294a0952
diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexInputWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexInputWrapper.javaindex 83d34ffd28f..64fbfdcc90b 100644--- a/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexInputWrapper.java+++ b/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexInputWrapper.java@@ -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 {@Overridepublic 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);+ }+@Overridepublic String toString() {return "MockIndexInputWrapper(" + delegate + ")";diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.javaindex 38e2d4613f8..870c154777c 100644--- a/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java+++ b/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java@@ -88,8 +88,16 @@ public class MockIndexOutputWrapper extends IndexOutput {}}+ private boolean closed;+@Overridepublic void close() throws IOException {+ if (closed) {+ delegate.close(); // don't mask double-close bugs+ return;+ }+ closed = true;+try {dir.maybeThrowDeterministicException();} finally {@@ -105,6 +113,12 @@ public class MockIndexOutputWrapper extends IndexOutput {dir.removeIndexOutput(this, name);}}++ private void ensureOpen() {+ if (closed) {+ throw new AlreadyClosedException("Already closed: " + this);+ }+ }@Overridepublic void writeByte(byte b) throws IOException {@@ -114,6 +128,7 @@ public class MockIndexOutputWrapper extends IndexOutput {@Overridepublic 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 {@Overridepublic void copyBytes(DataInput input, long numBytes) throws IOException {+ ensureOpen();checkCrashed();checkDiskFull(null, 0, input, numBytes);