LUCENE-1682: fix some unit tests to use private filesystem dir when testing

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@783487 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2009-06-10 20:15:19 +00:00
parent 5ddf4961f3
commit eb6fbfa3d5
6 changed files with 24 additions and 42 deletions

View File

@ -1539,10 +1539,7 @@ public class TestIndexReader extends LuceneTestCase
public 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 exception
public 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");

View File

@ -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 writer
IndexWriter 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 LuceneTestCase
reader.close();
reader2.close();
} finally {
rmDir(new File(dirName));
rmDir(dirName);
}
}

View File

@ -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);

View File

@ -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();

View File

@ -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 correctly

View File

@ -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();