Skip to content

Apache Lucene open-source search software

LUCENE-2318: Remove inconsistent system property code for retrieving temp and data directories inside test cases

Uwe Schindlerb13167eefdd9
diff --git a/CHANGES.txt b/CHANGES.txt
index ecf4a04f297..8ab623bd977 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -263,6 +263,13 @@ Test Cases
other than errors to System.(out|err). The setting can be changed with
-Dtests.verbose=true on test invokation. (Shai Erera via Uwe Schindler)
+* LUCENE-2318: Remove inconsistent system property code for retrieving
+ temp and data directories inside test cases. It is now centralized in
+ LuceneTestCase(J4). Also changed lots of tests to use
+ getClass().getResourceAsStream() to retrieve test data. Tests needing
+ access to "real" files from the test folder itsself, can use
+ LuceneTestCase(J4).getDataFile(). (Uwe Schindler)
+
================== Release 2.9.2 / 3.0.1 2010-02-26 ====================
Changes in backwards compatibility policy
diff --git a/common-build.xml b/common-build.xml
index d713155412b..d728290f55c 100644
--- a/common-build.xml
+++ b/common-build.xml
@@ -413,7 +413,6 @@
<sysproperty key="tests.verbose" value="${tests.verbose}"/>
<!-- TODO: create propertyset for test properties, so each project can have its own set -->
- <sysproperty key="dataDir" file="@{dataDir}"/>
<sysproperty key="tempDir" file="@{tempDir}"/>
<sysproperty key="java.io.tmpdir" file="@{tempDir}"/>
<sysproperty key="lucene.version" value="${dev.version}"/>
diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/compound/TestCompoundWordTokenFilter.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/compound/TestCompoundWordTokenFilter.java
index a342c86b90e..a3142d20400 100644
--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/compound/TestCompoundWordTokenFilter.java
+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/compound/TestCompoundWordTokenFilter.java
@@ -30,9 +30,6 @@ import org.apache.lucene.analysis.compound.hyphenation.HyphenationTree;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
public class TestCompoundWordTokenFilter extends BaseTokenStreamTestCase {
- static final File dataDir = new File(System.getProperty("dataDir", "./bin"));
- static final File testFile = new File(dataDir, "org/apache/lucene/analysis/compound/da_UTF8.xml");
-
public void testHyphenationCompoundWordsDA() throws Exception {
String[] dict = { "læse", "hest" };
@@ -143,6 +140,6 @@ public class TestCompoundWordTokenFilter extends BaseTokenStreamTestCase {
}
private Reader getHyphenationReader() throws Exception {
- return new InputStreamReader(new FileInputStream(testFile), "UTF-8");
+ return new InputStreamReader(getClass().getResourceAsStream("da_UTF8.xml"), "UTF-8");
}
}
diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/cz/TestCzechAnalyzer.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/cz/TestCzechAnalyzer.java
index 35d52266e92..eb3a6a60d77 100644
--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/cz/TestCzechAnalyzer.java
+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/cz/TestCzechAnalyzer.java
@@ -35,8 +35,6 @@ import org.apache.lucene.util.Version;
*
*/
public class TestCzechAnalyzer extends BaseTokenStreamTestCase {
- File dataDir = new File(System.getProperty("dataDir", "./bin"));
- File customStopFile = new File(dataDir, "org/apache/lucene/analysis/cz/customStopWordFile.txt");
/**
* @deprecated Remove this test when support for 3.0 indexes is no longer needed.
@@ -105,7 +103,7 @@ public class TestCzechAnalyzer extends BaseTokenStreamTestCase {
assertAnalyzesToReuse(cz, "Česká Republika",
new String[] { "česká", "republika" });
- InputStream stopwords = new FileInputStream(customStopFile);
+ InputStream stopwords = getClass().getResourceAsStream("customStopWordFile.txt");
cz.loadStopWords(stopwords, "UTF-8");
assertAnalyzesToReuse(cz, "Česká Republika", new String[] { "česká" });
diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/de/TestGermanStemFilter.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/de/TestGermanStemFilter.java
index 0b0cb78d4ea..213be5e6bb9 100644
--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/de/TestGermanStemFilter.java
+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/de/TestGermanStemFilter.java
@@ -41,10 +41,7 @@ public class TestGermanStemFilter extends BaseTokenStreamTestCase {
Tokenizer tokenizer = new KeywordTokenizer(new StringReader(""));
TokenFilter filter = new GermanStemFilter(new LowerCaseFilter(TEST_VERSION_CURRENT, tokenizer));
// read test cases from external file:
- File dataDir = new File(System.getProperty("dataDir", "./bin"));
- File testFile = new File(dataDir, "org/apache/lucene/analysis/de/data.txt");
- FileInputStream fis = new FileInputStream(testFile);
- InputStreamReader isr = new InputStreamReader(fis, "iso-8859-1");
+ InputStreamReader isr = new InputStreamReader(getClass().getResourceAsStream("data.txt"), "iso-8859-1");
BufferedReader breader = new BufferedReader(isr);
while(true) {
String line = breader.readLine();
@@ -61,6 +58,5 @@ public class TestGermanStemFilter extends BaseTokenStreamTestCase {
}
breader.close();
isr.close();
- fis.close();
}
}
diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/nl/TestDutchStemmer.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/nl/TestDutchStemmer.java
index 6d5357c95bf..dd61e525bd5 100644
--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/nl/TestDutchStemmer.java
+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/nl/TestDutchStemmer.java
@@ -32,8 +32,6 @@ import org.apache.lucene.util.Version;
*
*/
public class TestDutchStemmer extends BaseTokenStreamTestCase {
- File dataDir = new File(System.getProperty("dataDir", "./bin"));
- File customDictFile = new File(dataDir, "org/apache/lucene/analysis/nl/customStemDict.txt");
public void testWithSnowballExamples() throws Exception {
check("lichaamsziek", "lichaamsziek");
@@ -172,6 +170,7 @@ public class TestDutchStemmer extends BaseTokenStreamTestCase {
public void testStemDictionaryReuse() throws Exception {
DutchAnalyzer a = new DutchAnalyzer(TEST_VERSION_CURRENT);
checkOneTermReuse(a, "lichamelijk", "licham");
+ File customDictFile = getDataFile("customStemDict.txt");
a.setStemDictionary(customDictFile);
checkOneTermReuse(a, "lichamelijk", "somethingentirelydifferent");
}
diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ru/TestRussianAnalyzer.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ru/TestRussianAnalyzer.java
index 1e529842a3d..af6e6a852ef 100644
--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ru/TestRussianAnalyzer.java
+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ru/TestRussianAnalyzer.java
@@ -41,14 +41,6 @@ public class TestRussianAnalyzer extends BaseTokenStreamTestCase
private InputStreamReader sampleUnicode;
- private File dataDir;
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- dataDir = new File(System.getProperty("dataDir", "./bin"));
- }
-
/**
* @deprecated remove this test and its datafiles in Lucene 4.0
* the Snowball version has its own data tests.
@@ -59,12 +51,12 @@ public class TestRussianAnalyzer extends BaseTokenStreamTestCase
RussianAnalyzer ra = new RussianAnalyzer(Version.LUCENE_30);
inWords =
new InputStreamReader(
- new FileInputStream(new File(dataDir, "/org/apache/lucene/analysis/ru/testUTF8.txt")),
+ getClass().getResourceAsStream("testUTF8.txt"),
"UTF-8");
sampleUnicode =
new InputStreamReader(
- new FileInputStream(new File(dataDir, "/org/apache/lucene/analysis/ru/resUTF8.htm")),
+ getClass().getResourceAsStream("resUTF8.htm"),
"UTF-8");
TokenStream in = ra.tokenStream("all", inWords);
diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ru/TestRussianStem.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ru/TestRussianStem.java
index c61f45a8a16..632391e88ea 100644
--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ru/TestRussianStem.java
+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/ru/TestRussianStem.java
@@ -47,13 +47,11 @@ public class TestRussianStem extends LuceneTestCase
//System.out.println(new java.util.Date());
String str;
- File dataDir = new File(System.getProperty("dataDir", "./bin"));
-
// open and read words into an array list
BufferedReader inWords =
new BufferedReader(
new InputStreamReader(
- new FileInputStream(new File(dataDir, "/org/apache/lucene/analysis/ru/wordsUTF8.txt")),
+ getClass().getResourceAsStream("wordsUTF8.txt"),
"UTF-8"));
while ((str = inWords.readLine()) != null)
{
@@ -65,7 +63,7 @@ public class TestRussianStem extends LuceneTestCase
BufferedReader inStems =
new BufferedReader(
new InputStreamReader(
- new FileInputStream(new File(dataDir, "/org/apache/lucene/analysis/ru/stemsUTF8.txt")),
+ getClass().getResourceAsStream("stemsUTF8.txt"),
"UTF-8"));
while ((str = inStems.readLine()) != null)
{
diff --git a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/snowball/TestSnowballVocab.java b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/snowball/TestSnowballVocab.java
index b349e771e01..4cf3b9b34a2 100644
--- a/contrib/analyzers/common/src/test/org/apache/lucene/analysis/snowball/TestSnowballVocab.java
+++ b/contrib/analyzers/common/src/test/org/apache/lucene/analysis/snowball/TestSnowballVocab.java
@@ -35,18 +35,24 @@ import org.apache.lucene.analysis.Tokenizer;
*/
public class TestSnowballVocab extends BaseTokenStreamTestCase {
private Tokenizer tokenizer = new KeywordTokenizer(new StringReader(""));
- static final File dataDir = new File(System.getProperty("dataDir", "./bin"));
- static final File dataRoot = new File(dataDir,
- "org/apache/lucene/analysis/snowball/data");
+ File dataRoot = null;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ try {
+ dataRoot = getDataFile("data");
+ } catch (IOException ioe) {
+ dataRoot = null;
+ System.err.println("WARN: This test was disabled, as the svn checkout of snowball test files is not supported on your system!");
+ }
+ }
/**
* Run all languages against their snowball vocabulary tests.
*/
public void testStemmers() throws IOException {
- if (!dataRoot.exists()) {
- System.err.println("WARN: This test was disabled, as the svn checkout of snowball test files is not supported on your system!");
- return;
- }
+ if (dataRoot == null) return;
assertCorrectOutput("Danish", "danish");
assertCorrectOutput("Dutch", "dutch");
assertCorrectOutput("English", "english");
diff --git a/contrib/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java b/contrib/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java
index 3112046cdfc..834f67df12d 100755
--- a/contrib/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java
+++ b/contrib/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java
@@ -378,7 +378,7 @@ public class TestPerfTasksLogic extends LuceneTestCase {
* Test WriteLineDoc and LineDocSource.
*/
public void testLineDocFile() throws Exception {
- File lineFile = new File(System.getProperty("tempDir"), "test.reuters.lines.txt");
+ File lineFile = new File(TEMP_DIR, "test.reuters.lines.txt");
// We will call WriteLineDocs this many times
final int NUM_TRY_DOCS = 50;
diff --git a/contrib/db/bdb-je/src/test/org/apache/lucene/store/je/JEStoreTest.java b/contrib/db/bdb-je/src/test/org/apache/lucene/store/je/JEStoreTest.java
index d03cff24c74..eaf022b6001 100644
--- a/contrib/db/bdb-je/src/test/org/apache/lucene/store/je/JEStoreTest.java
+++ b/contrib/db/bdb-je/src/test/org/apache/lucene/store/je/JEStoreTest.java
@@ -46,7 +46,7 @@ import com.sleepycat.je.Transaction;
*
*/
public class JEStoreTest extends LuceneTestCase {
- protected File dbHome = new File(System.getProperty("java.io.tmpdir"),"index");
+ protected File dbHome = new File(TEMP_DIR,"index");
protected Environment env;
diff --git a/contrib/db/bdb/src/test/org/apache/lucene/store/db/DbStoreTest.java b/contrib/db/bdb/src/test/org/apache/lucene/store/db/DbStoreTest.java
index e3140883a35..586c7cf65dd 100644
--- a/contrib/db/bdb/src/test/org/apache/lucene/store/db/DbStoreTest.java
+++ b/contrib/db/bdb/src/test/org/apache/lucene/store/db/DbStoreTest.java
@@ -43,7 +43,7 @@ import org.apache.lucene.util.LuceneTestCase;
* Adapted from org.apache.lucene.StoreTest with larger files and random bytes.
*/
public class DbStoreTest extends LuceneTestCase {
- protected File dbHome = new File(System.getProperty("java.io.tmpdir"),"index");
+ protected File dbHome = new File(TEMP_DIR,"index");
protected Environment env;
protected Database index, blocks;
diff --git a/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java b/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java
index a04e57c5ea3..35c5a58bfb6 100644
--- a/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java
+++ b/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java
@@ -51,6 +51,7 @@ import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.store.RAMDirectory; // for javadocs
+import org.apache.lucene.util.Constants; // for javadocs
/**
* High-performance single-document main memory Apache Lucene fulltext search index.
@@ -1207,7 +1208,7 @@ public class MemoryIndex implements Serializable {
///////////////////////////////////////////////////////////////////////////////
private static final class VM {
- public static final int PTR = is64BitVM() ? 8 : 4;
+ public static final int PTR = Constants.JRE_IS_64BIT ? 8 : 4;
// bytes occupied by primitive data types
public static final int BOOLEAN = 1;
@@ -1272,18 +1273,6 @@ public class MemoryIndex implements Serializable {
return sizeOfObject(PTR + INT) + sizeOfIntArray(len);
}
- private static boolean is64BitVM() {
- try {
- int bits = Integer.getInteger("sun.arch.data.model", 0).intValue();
- if (bits != 0) return bits == 64;
-
- // fallback if sun.arch.data.model isn't available
- return System.getProperty("java.vm.name").toLowerCase().indexOf("64") >= 0;
- } catch (Throwable t) {
- return false; // better safe than sorry (applets, security managers, etc.) ...
- }
- }
-
/** logarithm to the base 2. Example: log2(4) == 2, log2(8) == 3 */
private static double log2(double value) {
return Math.log(value) / Math.log(2);
diff --git a/contrib/misc/src/test/org/apache/lucene/index/TestIndexSplitter.java b/contrib/misc/src/test/org/apache/lucene/index/TestIndexSplitter.java
index bfe56b79299..e3fc97b5776 100644
--- a/contrib/misc/src/test/org/apache/lucene/index/TestIndexSplitter.java
+++ b/contrib/misc/src/test/org/apache/lucene/index/TestIndexSplitter.java
@@ -27,11 +27,10 @@ import org.apache.lucene.util._TestUtil;
public class TestIndexSplitter extends LuceneTestCase {
public void test() throws Exception {
- String tmpDir = System.getProperty("java.io.tmpdir");
- File dir = new File(tmpDir, "testfilesplitter");
+ File dir = new File(TEMP_DIR, "testfilesplitter");
_TestUtil.rmDir(dir);
dir.mkdirs();
- File destDir = new File(tmpDir, "testfilesplitterdest");
+ File destDir = new File(TEMP_DIR, "testfilesplitterdest");
_TestUtil.rmDir(destDir);
destDir.mkdirs();
FSDirectory fsDir = FSDirectory.open(dir);
@@ -61,7 +60,7 @@ public class TestIndexSplitter extends LuceneTestCase {
assertEquals(50, r.maxDoc());
// now test cmdline
- File destDir2 = new File(tmpDir, "testfilesplitterdest2");
+ File destDir2 = new File(TEMP_DIR, "testfilesplitterdest2");
_TestUtil.rmDir(destDir2);
destDir2.mkdirs();
IndexSplitter.main(new String[] {dir.getAbsolutePath(), destDir2.getAbsolutePath(), splitSegName});
diff --git a/contrib/wordnet/src/test/org/apache/lucene/wordnet/TestSynonymTokenFilter.java b/contrib/wordnet/src/test/org/apache/lucene/wordnet/TestSynonymTokenFilter.java
index 9e53cc307a8..27e6de4648c 100644
--- a/contrib/wordnet/src/test/org/apache/lucene/wordnet/TestSynonymTokenFilter.java
+++ b/contrib/wordnet/src/test/org/apache/lucene/wordnet/TestSynonymTokenFilter.java
@@ -30,11 +30,10 @@ import org.apache.lucene.analysis.WhitespaceTokenizer;
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
public class TestSynonymTokenFilter extends BaseTokenStreamTestCase {
- File dataDir = new File(System.getProperty("dataDir", "./bin"));
- File testFile = new File(dataDir, "org/apache/lucene/wordnet/testSynonyms.txt");
+ final String testFile = "testSynonyms.txt";
public void testSynonyms() throws Exception {
- SynonymMap map = new SynonymMap(new FileInputStream(testFile));
+ SynonymMap map = new SynonymMap(getClass().getResourceAsStream(testFile));
/* all expansions */
Analyzer analyzer = new SynonymWhitespaceAnalyzer(map, Integer.MAX_VALUE);
assertAnalyzesTo(analyzer, "Lost in the woods",
@@ -45,7 +44,7 @@ public class TestSynonymTokenFilter extends BaseTokenStreamTestCase {
}
public void testSynonymsSingleQuote() throws Exception {
- SynonymMap map = new SynonymMap(new FileInputStream(testFile));
+ SynonymMap map = new SynonymMap(getClass().getResourceAsStream(testFile));
/* all expansions */
Analyzer analyzer = new SynonymWhitespaceAnalyzer(map, Integer.MAX_VALUE);
assertAnalyzesTo(analyzer, "king",
@@ -53,7 +52,7 @@ public class TestSynonymTokenFilter extends BaseTokenStreamTestCase {
}
public void testSynonymsLimitedAmount() throws Exception {
- SynonymMap map = new SynonymMap(new FileInputStream(testFile));
+ SynonymMap map = new SynonymMap(getClass().getResourceAsStream(testFile));
/* limit to one synonym expansion */
Analyzer analyzer = new SynonymWhitespaceAnalyzer(map, 1);
assertAnalyzesTo(analyzer, "Lost in the woods",
@@ -67,7 +66,7 @@ public class TestSynonymTokenFilter extends BaseTokenStreamTestCase {
}
public void testReusableTokenStream() throws Exception {
- SynonymMap map = new SynonymMap(new FileInputStream(testFile));
+ SynonymMap map = new SynonymMap(getClass().getResourceAsStream(testFile));
/* limit to one synonym expansion */
Analyzer analyzer = new SynonymWhitespaceAnalyzer(map, 1);
assertAnalyzesToReuse(analyzer, "Lost in the woods",
diff --git a/contrib/wordnet/src/test/org/apache/lucene/wordnet/TestWordnet.java b/contrib/wordnet/src/test/org/apache/lucene/wordnet/TestWordnet.java
index 497a8a00779..af3d9cea0e1 100644
--- a/contrib/wordnet/src/test/org/apache/lucene/wordnet/TestWordnet.java
+++ b/contrib/wordnet/src/test/org/apache/lucene/wordnet/TestWordnet.java
@@ -33,17 +33,15 @@ import org.apache.lucene.util.LuceneTestCase;
public class TestWordnet extends LuceneTestCase {
private Searcher searcher;
-
- File dataDir = new File(System.getProperty("dataDir", "./bin"));
- File testFile = new File(dataDir, "org/apache/lucene/wordnet/testSynonyms.txt");
-
+
String storePathName =
- new File(System.getProperty("tempDir"),"testLuceneWordnet").getAbsolutePath();
+ new File(TEMP_DIR,"testLuceneWordnet").getAbsolutePath();
@Override
protected void setUp() throws Exception {
super.setUp();
// create a temporary synonym index
+ File testFile = getDataFile("testSynonyms.txt");
String commandLineArgs[] = { testFile.getAbsolutePath(), storePathName };
try {
diff --git a/src/test/org/apache/lucene/analysis/TestPorterStemFilter.java b/src/test/org/apache/lucene/analysis/TestPorterStemFilter.java
index 30493d19f8e..f4fed3d0b15 100644
--- a/src/test/org/apache/lucene/analysis/TestPorterStemFilter.java
+++ b/src/test/org/apache/lucene/analysis/TestPorterStemFilter.java
@@ -36,8 +36,7 @@ public class TestPorterStemFilter extends BaseTokenStreamTestCase {
public void testPorterStemFilter() throws Exception {
Tokenizer tokenizer = new KeywordTokenizer(new StringReader(""));
TokenStream filter = new PorterStemFilter(tokenizer);
- ZipFile zipFile = new ZipFile(
- new File(getClass().getResource("porterTestData.zip").toURI()));
+ ZipFile zipFile = new ZipFile(getDataFile("porterTestData.zip"));
InputStream voc = zipFile.getInputStream(zipFile.getEntry("voc.txt"));
InputStream out = zipFile.getInputStream(zipFile.getEntry("output.txt"));
BufferedReader vocReader = new BufferedReader(new InputStreamReader(
diff --git a/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java b/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java
index 0fc6c4fd84f..35e109eb0e9 100644
--- a/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java
+++ b/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java
@@ -623,7 +623,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase {
}
public static String fullDir(String dirName) throws IOException {
- return new File(System.getProperty("tempDir"), dirName).getCanonicalPath();
+ return new File(TEMP_DIR, dirName).getCanonicalPath();
}
static final String TEXT_TO_COMPRESS = "this is a compressed field and should appear in 3.0 as an uncompressed field after merge";
diff --git a/src/test/org/apache/lucene/index/TestCompoundFile.java b/src/test/org/apache/lucene/index/TestCompoundFile.java
index c69c2761a8d..6f5e6e22cfb 100644
--- a/src/test/org/apache/lucene/index/TestCompoundFile.java
+++ b/src/test/org/apache/lucene/index/TestCompoundFile.java
@@ -57,7 +57,7 @@ public class TestCompoundFile extends LuceneTestCase
@Override
protected void setUp() throws Exception {
super.setUp();
- File file = new File(System.getProperty("tempDir"), "testIndex");
+ File file = new File(TEMP_DIR, "testIndex");
_TestUtil.rmDir(file);
// use a simple FSDir here, to be sure to have SimpleFSInputs
dir = new SimpleFSDirectory(file,null);
@@ -66,6 +66,7 @@ public class TestCompoundFile extends LuceneTestCase
@Override
protected void tearDown() throws Exception {
dir.close();
+ _TestUtil.rmDir(new File(TEMP_DIR, "testIndex"));
super.tearDown();
}
diff --git a/src/test/org/apache/lucene/index/TestDoc.java b/src/test/org/apache/lucene/index/TestDoc.java
index 5d5c19f80d4..a7ce98f4cf4 100644
--- a/src/test/org/apache/lucene/index/TestDoc.java
+++ b/src/test/org/apache/lucene/index/TestDoc.java
@@ -58,7 +58,7 @@ public class TestDoc extends LuceneTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
- workDir = new File(System.getProperty("tempDir"),"TestDoc");
+ workDir = new File(TEMP_DIR,"TestDoc");
workDir.mkdirs();
indexDir = new File(workDir, "testIndex");
diff --git a/src/test/org/apache/lucene/index/TestFieldsReader.java b/src/test/org/apache/lucene/index/TestFieldsReader.java
index f8f216f1f51..3892e2d59c8 100644
--- a/src/test/org/apache/lucene/index/TestFieldsReader.java
+++ b/src/test/org/apache/lucene/index/TestFieldsReader.java
@@ -210,10 +210,8 @@ public class TestFieldsReader extends LuceneTestCase {
* @throws Exception
*/
public void testLazyPerformance() throws Exception {
- String tmpIODir = System.getProperty("tempDir");
String userName = System.getProperty("user.name");
- String path = tmpIODir + File.separator + "lazyDir" + userName;
- File file = new File(path);
+ File file = new File(TEMP_DIR, "lazyDir" + userName);
_TestUtil.rmDir(file);
FSDirectory tmpDir = FSDirectory.open(file);
assertTrue(tmpDir != null);
@@ -391,10 +389,7 @@ public class TestFieldsReader extends LuceneTestCase {
// LUCENE-1262
public void testExceptions() throws Throwable {
- 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, "testfieldswriterexceptions");
+ File indexDir = new File(TEMP_DIR, "testfieldswriterexceptions");
try {
Directory dir = new FaultyFSDirectory(indexDir);
diff --git a/src/test/org/apache/lucene/index/TestIndexReader.java b/src/test/org/apache/lucene/index/TestIndexReader.java
index 296ad079db0..7f5a1f0f516 100644
--- a/src/test/org/apache/lucene/index/TestIndexReader.java
+++ b/src/test/org/apache/lucene/index/TestIndexReader.java
@@ -507,7 +507,7 @@ public class TestIndexReader extends LuceneTestCase
// Make sure you can set norms & commit even if a reader
// is open against the index:
public void testWritingNorms() throws IOException {
- File indexDir = new File(System.getProperty("tempDir"), "lucenetestnormwriter");
+ File indexDir = new File(TEMP_DIR, "lucenetestnormwriter");
Directory dir = FSDirectory.open(indexDir);
IndexWriter writer;
IndexReader reader;
@@ -685,12 +685,12 @@ public class TestIndexReader extends LuceneTestCase
}
private Directory getDirectory() throws IOException {
- return FSDirectory.open(new File(System.getProperty("tempDir"), "testIndex"));
+ return FSDirectory.open(new File(TEMP_DIR, "testIndex"));
}
public void testFilesOpenClose() throws IOException {
// Create initial data set
- File dirFile = new File(System.getProperty("tempDir"), "testIndex");
+ File dirFile = new File(TEMP_DIR, "testIndex");
Directory dir = getDirectory();
IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)));
addDoc(writer, "test");
@@ -719,7 +719,7 @@ public class TestIndexReader extends LuceneTestCase
}
public void testLastModified() throws Exception {
- final File fileDir = new File(System.getProperty("tempDir"), "testIndex");
+ final File fileDir = new File(TEMP_DIR, "testIndex");
for(int i=0;i<2;i++) {
try {
final Directory dir;
@@ -1148,8 +1148,7 @@ public class TestIndexReader extends LuceneTestCase
}
public void testOpenReaderAfterDelete() throws IOException {
- File dirFile = new File(System.getProperty("tempDir"),
- "deletetest");
+ File dirFile = new File(TEMP_DIR, "deletetest");
Directory dir = FSDirectory.open(dirFile);
try {
IndexReader.open(dir, false);
diff --git a/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java b/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java
index 04ca4b4f626..d9df6e612c0 100644
--- a/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java
+++ b/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java
@@ -82,14 +82,8 @@ public class TestIndexReaderCloneNorms extends LuceneTestCase {
* optimize.
*/
public void testNorms() throws IOException {
- // tmp dir
- String tempDir = System.getProperty("java.io.tmpdir");
- if (tempDir == null) {
- throw new IOException("java.io.tmpdir undefined, cannot run test");
- }
-
// test with a single index: index1
- File indexDir1 = new File(tempDir, "lucenetestindex1");
+ File indexDir1 = new File(TEMP_DIR, "lucenetestindex1");
Directory dir1 = FSDirectory.open(indexDir1);
IndexWriter.unlock(dir1);
@@ -108,14 +102,14 @@ public class TestIndexReaderCloneNorms extends LuceneTestCase {
modifiedNorms = new ArrayList<Float>();
numDocNorms = 0;
- File indexDir2 = new File(tempDir, "lucenetestindex2");
+ File indexDir2 = new File(TEMP_DIR, "lucenetestindex2");
Directory dir2 = FSDirectory.open(indexDir2);
createIndex(dir2);
doTestNorms(dir2);
// add index1 and index2 to a third index: index3
- File indexDir3 = new File(tempDir, "lucenetestindex3");
+ File indexDir3 = new File(TEMP_DIR, "lucenetestindex3");
Directory dir3 = FSDirectory.open(indexDir3);
createIndex(dir3);
diff --git a/src/test/org/apache/lucene/index/TestIndexReaderReopen.java b/src/test/org/apache/lucene/index/TestIndexReaderReopen.java
index 7f7a300e36c..6f1b5180642 100644
--- a/src/test/org/apache/lucene/index/TestIndexReaderReopen.java
+++ b/src/test/org/apache/lucene/index/TestIndexReaderReopen.java
@@ -1097,10 +1097,7 @@ public class TestIndexReaderReopen extends LuceneTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
- String tempDir = System.getProperty("java.io.tmpdir");
- if (tempDir == null)
- throw new IOException("java.io.tmpdir undefined, cannot run test");
- indexDir = new File(tempDir, "IndexReaderReopen");
+ indexDir = new File(TEMP_DIR, "IndexReaderReopen");
}
public void testCloseOrig() throws Throwable {
diff --git a/src/test/org/apache/lucene/index/TestIndexWriter.java b/src/test/org/apache/lucene/index/TestIndexWriter.java
index fa29a01343d..08a6f406b4e 100644
--- a/src/test/org/apache/lucene/index/TestIndexWriter.java
+++ b/src/test/org/apache/lucene/index/TestIndexWriter.java
@@ -4322,7 +4322,7 @@ public class TestIndexWriter extends LuceneTestCase {
// create=true does not remove non-index files
public void testOtherFiles() throws Throwable {
- File indexDir = new File(System.getProperty("tempDir"), "otherfiles");
+ File indexDir = new File(TEMP_DIR, "otherfiles");
Directory dir = FSDirectory.open(indexDir);
try {
// Create my own random file:
diff --git a/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java b/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java
index 80185ffd843..7fbc4c0fb8e 100644
--- a/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java
+++ b/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java
@@ -39,8 +39,7 @@ public class TestIndexWriterLockRelease extends LuceneTestCase {
protected void setUp() throws Exception {
super.setUp();
if (this.__test_dir == null) {
- String tmp_dir = System.getProperty("java.io.tmpdir", "tmp");
- this.__test_dir = new File(tmp_dir, "testIndexWriter");
+ this.__test_dir = new File(TEMP_DIR, "testIndexWriter");
if (this.__test_dir.exists()) {
throw new IOException("test directory \"" + this.__test_dir.getPath() + "\" already exists (please remove by hand)");
diff --git a/src/test/org/apache/lucene/index/TestThreadedOptimize.java b/src/test/org/apache/lucene/index/TestThreadedOptimize.java
index 2ab39ea089f..6284a10abe2 100644
--- a/src/test/org/apache/lucene/index/TestThreadedOptimize.java
+++ b/src/test/org/apache/lucene/index/TestThreadedOptimize.java
@@ -141,12 +141,8 @@ public class TestThreadedOptimize extends LuceneTestCase {
runTest(directory, new ConcurrentMergeScheduler());
directory.close();
- String tempDir = System.getProperty("tempDir");
- if (tempDir == null)
- throw new IOException("tempDir undefined, cannot run test");
-
- String dirName = tempDir + "/luceneTestThreadedOptimize";
- directory = FSDirectory.open(new File(dirName));
+ File dirName = new File(TEMP_DIR, "luceneTestThreadedOptimize");
+ directory = FSDirectory.open(dirName);
runTest(directory, new SerialMergeScheduler());
runTest(directory, new ConcurrentMergeScheduler());
directory.close();
diff --git a/src/test/org/apache/lucene/store/TestBufferedIndexInput.java b/src/test/org/apache/lucene/store/TestBufferedIndexInput.java
index d0737bea4bf..a9203217943 100755
--- a/src/test/org/apache/lucene/store/TestBufferedIndexInput.java
+++ b/src/test/org/apache/lucene/store/TestBufferedIndexInput.java
@@ -242,7 +242,7 @@ public class TestBufferedIndexInput extends LuceneTestCase {
}
public void testSetBufferSize() throws IOException {
- File indexDir = new File(System.getProperty("tempDir"), "testSetBufferSize");
+ File indexDir = new File(TEMP_DIR, "testSetBufferSize");
MockFSDirectory dir = new MockFSDirectory(indexDir, newRandom());
try {
IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(
diff --git a/src/test/org/apache/lucene/store/TestDirectory.java b/src/test/org/apache/lucene/store/TestDirectory.java
index 64149a9a6c8..bc5a9b1a5da 100644
--- a/src/test/org/apache/lucene/store/TestDirectory.java
+++ b/src/test/org/apache/lucene/store/TestDirectory.java
@@ -35,7 +35,7 @@ public class TestDirectory extends LuceneTestCase {
} catch (AlreadyClosedException ace) {
}
- dir = FSDirectory.open(new File(System.getProperty("tempDir")));
+ dir = FSDirectory.open(TEMP_DIR);
dir.close();
try {
dir.createOutput("test");
@@ -48,7 +48,7 @@ public class TestDirectory extends LuceneTestCase {
// Test that different instances of FSDirectory can coexist on the same
// path, can read, write, and lock files.
public void testDirectInstantiation() throws Exception {
- File path = new File(System.getProperty("tempDir"));
+ File path = new File(TEMP_DIR, "testDirectInstantiation");
int sz = 3;
Directory[] dirs = new Directory[sz];
@@ -116,11 +116,13 @@ public class TestDirectory extends LuceneTestCase {
dir.close();
assertFalse(dir.isOpen);
}
+
+ _TestUtil.rmDir(path);
}
// LUCENE-1464
public void testDontCreate() throws Throwable {
- File path = new File(System.getProperty("tempDir"), "doesnotexist");
+ File path = new File(TEMP_DIR, "doesnotexist");
try {
assertTrue(!path.exists());
Directory dir = new SimpleFSDirectory(path, null);
@@ -138,7 +140,7 @@ public class TestDirectory extends LuceneTestCase {
// LUCENE-1468
public void testFSDirectoryFilter() throws IOException {
- checkDirectoryFilter(FSDirectory.open(new File(System.getProperty("tempDir"),"test")));
+ checkDirectoryFilter(FSDirectory.open(new File(TEMP_DIR,"test")));
}
// LUCENE-1468
@@ -155,7 +157,7 @@ public class TestDirectory extends LuceneTestCase {
// LUCENE-1468
public void testCopySubdir() throws Throwable {
- File path = new File(System.getProperty("tempDir"), "testsubdir");
+ File path = new File(TEMP_DIR, "testsubdir");
try {
path.mkdirs();
new File(path, "subdir").mkdirs();
@@ -168,7 +170,7 @@ public class TestDirectory extends LuceneTestCase {
// LUCENE-1468
public void testNotDirectory() throws Throwable {
- File path = new File(System.getProperty("tempDir"), "testnotdir");
+ File path = new File(TEMP_DIR, "testnotdir");
Directory fsDir = new SimpleFSDirectory(path, null);
try {
IndexOutput out = fsDir.createOutput("afile");
diff --git a/src/test/org/apache/lucene/store/TestLockFactory.java b/src/test/org/apache/lucene/store/TestLockFactory.java
index b298e8426bd..c07ece490b0 100755
--- a/src/test/org/apache/lucene/store/TestLockFactory.java
+++ b/src/test/org/apache/lucene/store/TestLockFactory.java
@@ -171,7 +171,7 @@ public class TestLockFactory extends LuceneTestCase {
// Verify: NativeFSLockFactory works correctly
public void testNativeFSLockFactory() throws IOException {
- NativeFSLockFactory f = new NativeFSLockFactory(System.getProperty("tempDir"));
+ NativeFSLockFactory f = new NativeFSLockFactory(TEMP_DIR);
f.setLockPrefix("test");
Lock l = f.makeLock("commit");
@@ -195,7 +195,7 @@ public class TestLockFactory extends LuceneTestCase {
public void testNativeFSLockReleaseByOtherLock() throws IOException {
- NativeFSLockFactory f = new NativeFSLockFactory(System.getProperty("tempDir"));
+ NativeFSLockFactory f = new NativeFSLockFactory(TEMP_DIR);
f.setLockPrefix("test");
Lock l = f.makeLock("commit");
diff --git a/src/test/org/apache/lucene/store/TestRAMDirectory.java b/src/test/org/apache/lucene/store/TestRAMDirectory.java
index c4527492870..725c1eae09a 100644
--- a/src/test/org/apache/lucene/store/TestRAMDirectory.java
+++ b/src/test/org/apache/lucene/store/TestRAMDirectory.java
@@ -49,10 +49,7 @@ public class TestRAMDirectory extends LuceneTestCase {
@Override
protected void setUp () throws Exception {
super.setUp();
- String tempDir = System.getProperty("java.io.tmpdir");
- if (tempDir == null)
- throw new IOException("java.io.tmpdir undefined, cannot run test");
- indexDir = new File(tempDir, "RAMDirIndex");
+ indexDir = new File(TEMP_DIR, "RAMDirIndex");
Directory dir = FSDirectory.open(indexDir);
IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(
diff --git a/src/test/org/apache/lucene/store/TestWindowsMMap.java b/src/test/org/apache/lucene/store/TestWindowsMMap.java
index bb7882c2f1a..8ccc79aa47c 100644
--- a/src/test/org/apache/lucene/store/TestWindowsMMap.java
+++ b/src/test/org/apache/lucene/store/TestWindowsMMap.java
@@ -63,7 +63,7 @@ public class TestWindowsMMap extends LuceneTestCase {
}
private final static String storePathname =
- new File(System.getProperty("tempDir"),"testLuceneMmap").getAbsolutePath();
+ new File(TEMP_DIR,"testLuceneMmap").getAbsolutePath();
public void testMmapIndex() throws Exception {
FSDirectory storeDirectory;
diff --git a/src/test/org/apache/lucene/util/LuceneTestCase.java b/src/test/org/apache/lucene/util/LuceneTestCase.java
index f6aa79b2aea..9a4c1beb876 100644
--- a/src/test/org/apache/lucene/util/LuceneTestCase.java
+++ b/src/test/org/apache/lucene/util/LuceneTestCase.java
@@ -17,7 +17,9 @@ package org.apache.lucene.util;
* limitations under the License.
*/
+import java.io.File;
import java.io.PrintStream;
+import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Random;
@@ -60,8 +62,12 @@ public abstract class LuceneTestCase extends TestCase {
*/
public static final boolean VERBOSE = LuceneTestCaseJ4.VERBOSE;
+ /** Use this constant when creating Analyzers and any other version-dependent stuff. */
public static final Version TEST_VERSION_CURRENT = LuceneTestCaseJ4.TEST_VERSION_CURRENT;
+ /** Create indexes in this directory, optimally use a subdir, named after the test */
+ public static final File TEMP_DIR = LuceneTestCaseJ4.TEMP_DIR;
+
private int savedBoolMaxClauseCount;
private volatile Thread.UncaughtExceptionHandler savedUncaughtExceptionHandler = null;
@@ -250,6 +256,18 @@ public abstract class LuceneTestCase extends TestCase {
return new Random(seed);
}
+ /** Gets a resource from the classpath as {@link File}. This method should only be used,
+ * if a real file is needed. To get a stream, code should prefer
+ * {@link Class#getResourceAsStream} using {@code this.getClass()}.
+ */
+ protected File getDataFile(String name) throws IOException {
+ try {
+ return new File(this.getClass().getResource(name).toURI());
+ } catch (Exception e) {
+ throw new IOException("Cannot find resource: " + name);
+ }
+ }
+
@Override
public void runBare() throws Throwable {
//long t0 = System.currentTimeMillis();
diff --git a/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java b/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java
index b109e779b8e..f6ce1dfd1f2 100644
--- a/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java
+++ b/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java
@@ -28,7 +28,9 @@ import org.junit.Rule;
import org.junit.rules.TestWatchman;
import org.junit.runners.model.FrameworkMethod;
+import java.io.File;
import java.io.PrintStream;
+import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Random;
@@ -58,8 +60,7 @@ import static org.junit.Assert.fail;
* of its name
* <p>
* <p>
- * See Junit4 documentation for a complete list of features at
- * http://junit.org/junit/javadoc/4.7/
+ * See Junit4 <a href="http://junit.org/junit/javadoc/4.7/">documentation</a> for a complete list of features.
* <p>
* Import from org.junit rather than junit.framework.
* <p>
@@ -83,8 +84,19 @@ public class LuceneTestCaseJ4 {
*/
public static final boolean VERBOSE = Boolean.getBoolean("tests.verbose");
- /** Change this when development starts for new Lucene version: */
+ /** Use this constant when creating Analyzers and any other version-dependent stuff.
+ * <p><b>NOTE:</b> Change this when development starts for new Lucene version:
+ */
public static final Version TEST_VERSION_CURRENT = Version.LUCENE_31;
+
+ /** Create indexes in this directory, optimally use a subdir, named after the test */
+ public static final File TEMP_DIR;
+ static {
+ String s = System.getProperty("tempDir", System.getProperty("java.io.tmpdir"));
+ if (s == null)
+ throw new RuntimeException("To run tests, you need to define system property 'tempDir' or 'java.io.tmpdir'.");
+ TEMP_DIR = new File(s);
+ }
private int savedBoolMaxClauseCount;
@@ -100,7 +112,7 @@ public class LuceneTestCaseJ4 {
}
}
private List<UncaughtExceptionEntry> uncaughtExceptions = Collections.synchronizedList(new ArrayList<UncaughtExceptionEntry>());
-
+
// This is how we get control when errors occur.
// Think of this as start/end/success/failed
// events.
@@ -291,6 +303,18 @@ public class LuceneTestCaseJ4 {
public String getName() {
return this.name;
}
+
+ /** Gets a resource from the classpath as {@link File}. This method should only be used,
+ * if a real file is needed. To get a stream, code should prefer
+ * {@link Class#getResourceAsStream} using {@code this.getClass()}.
+ */
+ protected File getDataFile(String name) throws IOException {
+ try {
+ return new File(this.getClass().getResource(name).toURI());
+ } catch (Exception e) {
+ throw new IOException("Cannot find resource: " + name);
+ }
+ }
// We get here from InterceptTestCaseEvents on the 'failed' event....
public void reportAdditionalFailureInfo() {
diff --git a/src/test/org/apache/lucene/util/_TestUtil.java b/src/test/org/apache/lucene/util/_TestUtil.java
index 26e27e2d70f..0a61002a9e0 100644
--- a/src/test/org/apache/lucene/util/_TestUtil.java
+++ b/src/test/org/apache/lucene/util/_TestUtil.java
@@ -33,10 +33,7 @@ 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());
+ return new File(LuceneTestCaseJ4.TEMP_DIR, desc + "." + new Random().nextLong());
}
public static void rmDir(File dir) throws IOException {