Skip to content

Apache Lucene open-source search software

LUCENE-2397: SnapshotDeletionPolicy.snapshot() throws NPE if no commits happened

Shai Erera24260eb6daa0
diff --git a/lucene/src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java b/lucene/src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java
index 0851282f4b7..e067e70ecf1 100644
--- a/lucene/src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java
+++ b/lucene/src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java
@@ -71,6 +71,10 @@ public class SnapshotDeletionPolicy implements IndexDeletionPolicy {
* consume an extra 1X of your total index size, until
* you release the snapshot. */
public synchronized IndexCommit snapshot() {
+ if (lastCommit == null) {
+ throw new IllegalStateException("no index commits to snapshot !");
+ }
+
if (snapshot == null)
snapshot = lastCommit.getSegmentsFileName();
else
diff --git a/lucene/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java b/lucene/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.java
similarity index 93%
rename from lucene/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java
rename to lucene/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.java
index 4f29e74e506..9e9464364e0 100644
--- a/lucene/src/test/org/apache/lucene/TestSnapshotDeletionPolicy.java
+++ b/lucene/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.java
@@ -1,6 +1,4 @@
-package org.apache.lucene;
-// Intentionally not in org.apache.lucene.index, to assert
-// that we do not require any package private access.
+package org.apache.lucene.index;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -19,6 +17,8 @@ package org.apache.lucene;
* limitations under the License.
*/
+import static org.junit.Assert.*;
+
import java.util.Collection;
import java.io.File;
import java.io.IOException;
@@ -34,21 +34,22 @@ import org.apache.lucene.index.IndexCommit;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy;
import org.apache.lucene.index.IndexWriter;
-import org.apache.lucene.index.TestIndexWriter;
import org.apache.lucene.index.SnapshotDeletionPolicy;
+import org.apache.lucene.util.LuceneTestCaseJ4;
import org.apache.lucene.util.ThreadInterruptedException;
-import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
+import org.junit.Test;
//
// This was developed for Lucene In Action,
// http://lucenebook.com
//
-public class TestSnapshotDeletionPolicy extends LuceneTestCase {
+public class TestSnapshotDeletionPolicy extends LuceneTestCaseJ4 {
public static final String INDEX_PATH = "test.snapshots";
+ @Test
public void testSnapshotDeletionPolicy() throws Exception {
File dir = _TestUtil.getTempDir(INDEX_PATH);
try {
@@ -64,6 +65,7 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {
dir2.close();
}
+ @Test
public void testReuseAcrossWriters() throws Exception {
Directory dir = new MockRAMDirectory();
@@ -235,5 +237,13 @@ public class TestSnapshotDeletionPolicy extends LuceneTestCase {
input.close();
}
}
+
+ @Test(expected=IllegalStateException.class)
+ public void testNoCommits() throws Exception {
+ // Tests that if there were no commits when snapshot() is called, then
+ // IllegalStateException is thrown rather than NPE.
+ SnapshotDeletionPolicy sdp = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
+ sdp.snapshot();
+ }
+
}
-