Apache Lucene open-source search software
LUCENE-1682: fix some unit tests to use private filesystem dir when testing
Michael McCandlesseb6fbfa3d5ed
diff --git a/src/test/org/apache/lucene/index/TestIndexReader.java b/src/test/org/apache/lucene/index/TestIndexReader.javaindex 8de439662dc..3f4d8c59170 100644--- a/src/test/org/apache/lucene/index/TestIndexReader.java+++ b/src/test/org/apache/lucene/index/TestIndexReader.java@@ -1539,10 +1539,7 @@ public class TestIndexReader extends LuceneTestCasepublic void testFalseDirectoryAlreadyClosed() throws Throwable {- String tempDir = System.getProperty("java.io.tmpdir");- if (tempDir == null)- throw new RuntimeException("java.io.tmpdir undefined");- File indexDir = new File(tempDir, "lucenetestdiralreadyclosed");+ File indexDir = _TestUtil.getTempDir("lucenetestdiralreadyclosed");try {FSDirectory dir = FSDirectory.getDirectory(indexDir);@@ -1625,8 +1622,7 @@ public class TestIndexReader extends LuceneTestCase// IndexReader on a non-existent directory, you get a// good exceptionpublic void testNoDir() throws Throwable {- String tempDir = System.getProperty("java.io.tmpdir");- Directory dir = FSDirectory.open(new File(tempDir, "doesnotexist"));+ Directory dir = FSDirectory.open(_TestUtil.getTempDir("doesnotexist"));try {IndexReader.open(dir);fail("did not hit expected exception");diff --git a/src/test/org/apache/lucene/index/TestIndexWriter.java b/src/test/org/apache/lucene/index/TestIndexWriter.javaindex f25b300d546..04a9225b31f 100644--- a/src/test/org/apache/lucene/index/TestIndexWriter.java+++ b/src/test/org/apache/lucene/index/TestIndexWriter.java@@ -741,10 +741,7 @@ public class TestIndexWriter extends LuceneTestCase// reader holds it open (this fails pre lock-less// commits on windows):public void testCreateWithReader() throws IOException {- String tempDir = System.getProperty("java.io.tmpdir");- if (tempDir == null)- throw new IOException("java.io.tmpdir undefined, cannot run test");- File indexDir = new File(tempDir, "lucenetestindexwriter");+ File indexDir = _TestUtil.getTempDir("lucenetestindexwriter");try {Directory dir = FSDirectory.open(indexDir);@@ -778,10 +775,7 @@ public class TestIndexWriter extends LuceneTestCase// Same test as above, but use IndexWriter constructor// that takes File:public void testCreateWithReader2() throws IOException {- String tempDir = System.getProperty("java.io.tmpdir");- if (tempDir == null)- throw new IOException("java.io.tmpdir undefined, cannot run test");- File indexDir = new File(tempDir, "lucenetestindexwriter");+ File indexDir = _TestUtil.getTempDir("lucenetestindexwriter");try {// add one document & close writerIndexWriter writer = new IndexWriter(indexDir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);@@ -811,11 +805,7 @@ public class TestIndexWriter extends LuceneTestCase// Same test as above, but use IndexWriter constructor// that takes String:public void testCreateWithReader3() throws IOException {- String tempDir = System.getProperty("tempDir");- if (tempDir == null)- throw new IOException("java.io.tmpdir undefined, cannot run test");-- String dirName = tempDir + "/lucenetestindexwriter";+ File dirName = _TestUtil.getTempDir("lucenetestindexwriter");try {// add one document & close writer@@ -839,7 +829,7 @@ public class TestIndexWriter extends LuceneTestCasereader.close();reader2.close();} finally {- rmDir(new File(dirName));+ rmDir(dirName);}}diff --git a/src/test/org/apache/lucene/index/TestPayloads.java b/src/test/org/apache/lucene/index/TestPayloads.javaindex 360e47dd6c7..2b5687114f0 100644--- a/src/test/org/apache/lucene/index/TestPayloads.java+++ b/src/test/org/apache/lucene/index/TestPayloads.java@@ -41,6 +41,7 @@ import org.apache.lucene.store.FSDirectory;import org.apache.lucene.store.RAMDirectory;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.UnicodeUtil;+import org.apache.lucene.util._TestUtil;public class TestPayloads extends LuceneTestCase {@@ -157,10 +158,10 @@ public class TestPayloads extends LuceneTestCase {performTest(dir);// now use a FSDirectory and repeat same test- String dirName = "test_payloads";- dir = FSDirectory.open(new File(dirName));+ File dirName = _TestUtil.getTempDir("test_payloads");+ dir = FSDirectory.open(dirName);performTest(dir);- rmDir(dirName);+ _TestUtil.rmDir(dirName);}// builds an index with payloads in the given Directory and performs@@ -364,21 +365,6 @@ public class TestPayloads extends LuceneTestCase {}- private void rmDir(String dir) {- File fileDir = new File(dir);- if (fileDir.exists()) {- File[] files = fileDir.listFiles();- if (files != null) {- for (int i = 0; i < files.length; i++) {- files[i].delete();- }- }- fileDir.delete();- }- }---void assertByteArrayEquals(byte[] b1, byte[] b2) {if (b1.length != b2.length) {fail("Byte arrays have different lengths: " + b1.length + ", " + b2.length);diff --git a/src/test/org/apache/lucene/index/TestStressIndexing.java b/src/test/org/apache/lucene/index/TestStressIndexing.javaindex 9de361140f4..8b267177d9b 100644--- a/src/test/org/apache/lucene/index/TestStressIndexing.java+++ b/src/test/org/apache/lucene/index/TestStressIndexing.java@@ -172,8 +172,7 @@ public class TestStressIndexing extends LuceneTestCase {directory.close();// FSDir- String tempDir = System.getProperty("java.io.tmpdir");- File dirPath = new File(tempDir, "lucene.test.stress");+ File dirPath = _TestUtil.getTempDir("lucene.test.stress");directory = FSDirectory.open(dirPath);runStressTest(directory, true, null);directory.close();diff --git a/src/test/org/apache/lucene/store/TestLockFactory.java b/src/test/org/apache/lucene/store/TestLockFactory.javaindex 3aaf43a6c82..06acc63b86f 100755--- a/src/test/org/apache/lucene/store/TestLockFactory.java+++ b/src/test/org/apache/lucene/store/TestLockFactory.java@@ -34,6 +34,7 @@ import org.apache.lucene.search.Query;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TermQuery;import org.apache.lucene.util.LuceneTestCase;+import org.apache.lucene.util._TestUtil;public class TestLockFactory extends LuceneTestCase {@@ -339,7 +340,8 @@ public class TestLockFactory extends LuceneTestCase {}public void _testStressLocks(LockFactory lockFactory, String indexDirName) throws Exception {- FSDirectory fs1 = FSDirectory.open(new File(indexDirName), lockFactory);+ File indexDir = _TestUtil.getTempDir(indexDirName);+ FSDirectory fs1 = FSDirectory.open(indexDir, lockFactory);// First create a 1 doc index:IndexWriter w = new IndexWriter(fs1, new WhitespaceAnalyzer(), true,@@ -360,7 +362,7 @@ public class TestLockFactory extends LuceneTestCase {assertTrue("IndexSearcher hit unexpected exceptions", !searcher.hitException);// Cleanup- rmDir(indexDirName);+ _TestUtil.rmDir(indexDir);}// Verify: NativeFSLockFactory works correctlydiff --git a/src/test/org/apache/lucene/util/_TestUtil.java b/src/test/org/apache/lucene/util/_TestUtil.javaindex 3a7f735f3f4..c4ac283dd7d 100644--- a/src/test/org/apache/lucene/util/_TestUtil.java+++ b/src/test/org/apache/lucene/util/_TestUtil.java@@ -30,6 +30,15 @@ import java.util.Random;public class _TestUtil {+ /** Returns temp dir, containing String arg in its name;+ * does not create the directory. */+ public static File getTempDir(String desc) {+ String tempDir = System.getProperty("java.io.tmpdir");+ if (tempDir == null)+ throw new RuntimeException("java.io.tmpdir undefined, cannot run test");+ return new File(tempDir, desc + "." + new Random().nextLong());+ }+public static void rmDir(File dir) throws IOException {if (dir.exists()) {File[] files = dir.listFiles();