Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

change tests to use new sys property

Erik Hatcherf538150c0b9c
diff --git a/src/test/org/apache/lucene/index/TestCompoundFile.java b/src/test/org/apache/lucene/index/TestCompoundFile.java
index c6fd731fb9c..faf821493ce 100644
--- a/src/test/org/apache/lucene/index/TestCompoundFile.java
+++ b/src/test/org/apache/lucene/index/TestCompoundFile.java
@@ -55,17 +55,16 @@ package org.apache.lucene.index;
*/
import java.io.IOException;
+import java.io.File;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
-
-import org.apache.lucene.store.*;
-import org.apache.lucene.analysis.WhitespaceAnalyzer;
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.store.OutputStream;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.InputStream;
+import org.apache.lucene.store.FSDirectory;
+import org.apache.lucene.store._TestHelper;
/**
@@ -92,20 +91,12 @@ public class TestCompoundFile extends TestCase
}
- public TestCompoundFile() {
- super();
- }
-
- public TestCompoundFile(String name) {
- super(name);
- }
-
private Directory dir;
public void setUp() throws IOException {
//dir = new RAMDirectory();
- dir = FSDirectory.getDirectory("testIndex", true);
+ dir = FSDirectory.getDirectory(new File(System.getProperty("tempDir"), "testIndex"), true);
}
diff --git a/src/test/org/apache/lucene/index/TestDoc.java b/src/test/org/apache/lucene/index/TestDoc.java
index f51a43bd91e..3b94ffe0033 100644
--- a/src/test/org/apache/lucene/index/TestDoc.java
+++ b/src/test/org/apache/lucene/index/TestDoc.java
@@ -75,79 +75,79 @@ import java.util.*;
* @version $Id$
*/
public class TestDoc extends TestCase {
-
+
/** Main for running test case by itself. */
public static void main(String args[]) {
TestRunner.run (new TestSuite(TestDoc.class));
}
-
-
+
+
private File workDir;
private File indexDir;
private LinkedList files;
-
-
- /** Set the test case. This test case needs
+
+
+ /** Set the test case. This test case needs
* a few text files created in the current working directory.
*/
public void setUp() throws IOException {
- workDir = new File("TestDoc");
+ workDir = new File(System.getProperty("tempDir"),"TestDoc");
workDir.mkdirs();
-
+
indexDir = new File(workDir, "testIndex");
indexDir.mkdirs();
-
+
Directory directory = FSDirectory.getDirectory(indexDir, true);
directory.close();
-
+
files = new LinkedList();
- files.add(createFile("test.txt",
+ files.add(createFile("test.txt",
"This is the first test file"
));
-
+
files.add(createFile("test2.txt",
"This is the second test file"
));
}
-
+
private File createFile(String name, String text) throws IOException {
FileWriter fw = null;
PrintWriter pw = null;
-
+
try {
File f = new File(workDir, name);
if (f.exists()) f.delete();
-
+
fw = new FileWriter(f);
pw = new PrintWriter(fw);
pw.println(text);
return f;
-
+
} finally {
if (pw != null) pw.close();
if (fw != null) fw.close();
}
}
-
-
+
+
/** This test executes a number of merges and compares the contents of
* the segments created when using compound file or not using one.
- *
+ *
* TODO: the original test used to print the segment contents to System.out
* for visual validation. To have the same effect, a new method
- * checkSegment(String name, ...) should be created that would
+ * checkSegment(String name, ...) should be created that would
* assert various things about the segment.
*/
public void testIndexAndMerge() throws Exception {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw, true);
-
+
Directory directory = FSDirectory.getDirectory(indexDir, true);
directory.close();
indexDoc("one", "test.txt");
printSegment(out, "one");
-
+
indexDoc("two", "test2.txt");
printSegment(out, "two");
@@ -164,16 +164,16 @@ public class TestDoc extends TestCase {
sw.close();
String multiFileOutput = sw.getBuffer().toString();
System.out.println(multiFileOutput);
-
+
sw = new StringWriter();
out = new PrintWriter(sw, true);
-
+
directory = FSDirectory.getDirectory(indexDir, true);
directory.close();
indexDoc("one", "test.txt");
printSegment(out, "one");
-
+
indexDoc("two", "test2.txt");
printSegment(out, "two");
@@ -193,9 +193,9 @@ public class TestDoc extends TestCase {
assertEquals(multiFileOutput, singleFileOutput);
}
-
+
private void indexDoc(String segment, String fileName)
- throws Exception
+ throws Exception
{
Directory directory = FSDirectory.getDirectory(indexDir, false);
Analyzer analyzer = new SimpleAnalyzer();
@@ -210,7 +210,7 @@ public class TestDoc extends TestCase {
directory.close();
}
-
+
private void merge(String seg1, String seg2, String merged, boolean useCompoundFile)
throws Exception {
Directory directory = FSDirectory.getDirectory(indexDir, false);
@@ -218,9 +218,9 @@ public class TestDoc extends TestCase {
SegmentReader r1 = new SegmentReader(new SegmentInfo(seg1, 1, directory));
SegmentReader r2 = new SegmentReader(new SegmentInfo(seg2, 1, directory));
- SegmentMerger merger =
+ SegmentMerger merger =
new SegmentMerger(directory, merged, useCompoundFile);
-
+
merger.add(r1);
merger.add(r2);
merger.merge();
@@ -228,7 +228,7 @@ public class TestDoc extends TestCase {
directory.close();
}
-
+
private void printSegment(PrintWriter out, String segment)
throws Exception {
Directory directory = FSDirectory.getDirectory(indexDir, false);
diff --git a/src/test/org/apache/lucene/index/TestIndexReader.java b/src/test/org/apache/lucene/index/TestIndexReader.java
index e55314b161c..2f191b61e23 100644
--- a/src/test/org/apache/lucene/index/TestIndexReader.java
+++ b/src/test/org/apache/lucene/index/TestIndexReader.java
@@ -58,12 +58,7 @@ package org.apache.lucene.index;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
-import junit.framework.TestResult;
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.search.Searcher;
-import org.apache.lucene.search.Hits;
-import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.store.FSDirectory;
@@ -74,6 +69,7 @@ import org.apache.lucene.document.Field;
import java.util.Collection;
import java.io.IOException;
+import java.io.File;
public class TestIndexReader extends TestCase
{
@@ -85,7 +81,7 @@ public class TestIndexReader extends TestCase
// TestRunner.run (new TestIndexReader("testDeleteReaderReaderConflict"));
// TestRunner.run (new TestIndexReader("testFilesOpenClose"));
}
-
+
public TestIndexReader(String name) {
super(name);
}
@@ -149,15 +145,15 @@ public class TestIndexReader extends TestCase
assertTrue(fieldNames.contains("unindexed"));
}
-
- private void assertTermDocsCount(String msg,
- IndexReader reader,
+
+ private void assertTermDocsCount(String msg,
+ IndexReader reader,
Term term,
- int expected)
+ int expected)
throws IOException
{
TermDocs tdocs = null;
-
+
try {
tdocs = reader.termDocs(term);
assertNotNull(msg + ", null TermDocs", tdocs);
@@ -166,21 +162,20 @@ public class TestIndexReader extends TestCase
count++;
}
assertEquals(msg + ", count mismatch", expected, count);
-
+
} finally {
- if (tdocs != null)
+ if (tdocs != null)
try { tdocs.close(); } catch (Exception e) { }
}
-
+
}
-
+
public void testBasicDelete() throws IOException
{
Directory dir = new RAMDirectory();
- //Directory dir = FSDirectory.getDirectory("testIndex", true);
-
+
IndexWriter writer = null;
IndexReader reader = null;
Term searchTerm = new Term("content", "aaa");
@@ -192,7 +187,7 @@ public class TestIndexReader extends TestCase
addDoc(writer, searchTerm.text());
}
writer.close();
-
+
// OPEN READER AT THIS POINT - this should fix the view of the
// index at the point of having 100 "aaa" documents and 0 "bbb"
reader = IndexReader.open(dir);
@@ -216,12 +211,12 @@ public class TestIndexReader extends TestCase
}
-
+
public void testDeleteReaderWriterConflict() throws IOException
{
//Directory dir = new RAMDirectory();
- Directory dir = FSDirectory.getDirectory("testIndex", true);
-
+ Directory dir = getDirectory(true);
+
Term searchTerm = new Term("content", "aaa");
Term searchTerm2 = new Term("content", "bbb");
@@ -232,7 +227,7 @@ public class TestIndexReader extends TestCase
addDoc(writer, searchTerm.text());
}
writer.close();
-
+
// OPEN READER AT THIS POINT - this should fix the view of the
// index at the point of having 100 "aaa" documents and 0 "bbb"
IndexReader reader = IndexReader.open(dir);
@@ -247,7 +242,7 @@ public class TestIndexReader extends TestCase
{
addDoc(writer, searchTerm2.text());
}
-
+
// REQUEST OPTIMIZATION
// This causes a new segment to become current for all subsequent
// searchers. Because of this, deletions made via a previously open
@@ -261,8 +256,8 @@ public class TestIndexReader extends TestCase
assertEquals("first docFreq", 0, reader.docFreq(searchTerm2));
assertTermDocsCount("first reader", reader, searchTerm, 100);
assertTermDocsCount("first reader", reader, searchTerm2, 0);
-
-
+
+
// DELETE DOCUMENTS CONTAINING TERM: aaa
// NOTE: the reader was created when only "aaa" documents were in
int deleted = 0;
@@ -272,8 +267,8 @@ public class TestIndexReader extends TestCase
} catch (IOException e) {
/* success */
}
-
- // Re-open index reader and try again. This time it should see
+
+ // Re-open index reader and try again. This time it should see
// the new data.
reader.close();
reader = IndexReader.open(dir);
@@ -281,7 +276,7 @@ public class TestIndexReader extends TestCase
assertEquals("first docFreq", 100, reader.docFreq(searchTerm2));
assertTermDocsCount("first reader", reader, searchTerm, 100);
assertTermDocsCount("first reader", reader, searchTerm2, 100);
-
+
deleted = reader.delete(searchTerm);
assertEquals("deleted count", 100, deleted);
assertEquals("deleted docFreq", 100, reader.docFreq(searchTerm));
@@ -299,39 +294,42 @@ public class TestIndexReader extends TestCase
reader.close();
}
- public void testFilesOpenClose() throws IOException
+ private Directory getDirectory(boolean create) throws IOException {
+ return FSDirectory.getDirectory(new File(System.getProperty("tempDir"), "testIndex"), create);
+ }
+
+ public void testFilesOpenClose() throws IOException
{
// Create initial data set
- Directory dir = FSDirectory.getDirectory("testIndex", true);
+ Directory dir = getDirectory(true);
IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
addDoc(writer, "test");
writer.close();
dir.close();
-
+
// Try to erase the data - this ensures that the writer closed all files
- dir = FSDirectory.getDirectory("testIndex", true);
-
- // Now create the data set again, just as before
+ dir = getDirectory(true);
+
+ // Now create the data set again, just as before
writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
addDoc(writer, "test");
writer.close();
dir.close();
-
+
// Now open existing directory and test that reader closes all files
- dir = FSDirectory.getDirectory("testIndex", false);
+ dir = getDirectory(false);
IndexReader reader1 = IndexReader.open(dir);
reader1.close();
dir.close();
-
+
// The following will fail if reader did not close all files
- dir = FSDirectory.getDirectory("testIndex", true);
+ dir = getDirectory(true);
}
-
-
+
+
public void testDeleteReaderReaderConflict() throws IOException
{
-// Directory dir = new RAMDirectory();
- Directory dir = FSDirectory.getDirectory("testIndex", true);
+ Directory dir = getDirectory(true);
Term searchTerm1 = new Term("content", "aaa");
Term searchTerm2 = new Term("content", "bbb");
@@ -359,7 +357,7 @@ public class TestIndexReader extends TestCase
assertTermDocsCount("first opened", reader1, searchTerm1, 100);
assertTermDocsCount("first opened", reader1, searchTerm2, 100);
assertTermDocsCount("first opened", reader1, searchTerm3, 100);
-
+
IndexReader reader2 = IndexReader.open(dir);
assertEquals("first opened", 100, reader2.docFreq(searchTerm1));
assertEquals("first opened", 100, reader2.docFreq(searchTerm2));
@@ -388,8 +386,8 @@ public class TestIndexReader extends TestCase
assertTermDocsCount("after delete 1", reader1, searchTerm1, 100);
assertTermDocsCount("after delete 1", reader1, searchTerm2, 100);
assertTermDocsCount("after delete 1", reader1, searchTerm3, 100);
-
-
+
+
// ATTEMPT TO DELETE FROM STALE READER
// delete documents containing term: bbb
try {
@@ -398,7 +396,7 @@ public class TestIndexReader extends TestCase
} catch (IOException e) {
/* success */
}
-
+
// RECREATE READER AND TRY AGAIN
reader1.close();
reader1 = IndexReader.open(dir);
@@ -417,7 +415,7 @@ public class TestIndexReader extends TestCase
assertTermDocsCount("deleted 2", reader1, searchTerm2, 0);
assertTermDocsCount("deleted 2", reader1, searchTerm3, 100);
reader1.close();
-
+
// Open another reader to confirm that everything is deleted
reader2 = IndexReader.open(dir);
assertEquals("reopened 2", 100, reader2.docFreq(searchTerm1));
@@ -427,7 +425,7 @@ public class TestIndexReader extends TestCase
assertTermDocsCount("reopened 2", reader2, searchTerm2, 0);
assertTermDocsCount("reopened 2", reader2, searchTerm3, 100);
reader2.close();
-
+
dir.close();
}