TestRAMDirectorypublic class TestRAMDirectory extends TestCase JUnit testcase to test RAMDirectory. RAMDirectory itself is used in many testcases,
but not one of them uses an different constructor other than the default constructor. |
Fields Summary |
---|
private File | indexDir | private final int | docsToAdd | private final int | numThreads | private final int | docsPerThread |
Methods Summary |
---|
private void | rmDir(java.io.File dir)
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
files[i].delete();
}
dir.delete();
| public void | setUp()
// setup the index
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");
IndexWriter writer = new IndexWriter(indexDir, new WhitespaceAnalyzer(), true);
// add some documents
Document doc = null;
for (int i = 0; i < docsToAdd; i++) {
doc = new Document();
doc.add(new Field("content", English.intToEnglish(i).trim(), Field.Store.YES, Field.Index.UN_TOKENIZED));
writer.addDocument(doc);
}
assertEquals(docsToAdd, writer.docCount());
writer.close();
| public void | tearDown()
// cleanup
if (indexDir != null && indexDir.exists()) {
rmDir (indexDir);
}
| public void | testRAMDirectory()
Directory dir = FSDirectory.getDirectory(indexDir);
MockRAMDirectory ramDir = new MockRAMDirectory(dir);
// close the underlaying directory
dir.close();
// Check size
assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
// open reader to test document count
IndexReader reader = IndexReader.open(ramDir);
assertEquals(docsToAdd, reader.numDocs());
// open search zo check if all doc's are there
IndexSearcher searcher = new IndexSearcher(reader);
// search for all documents
for (int i = 0; i < docsToAdd; i++) {
Document doc = searcher.doc(i);
assertTrue(doc.getField("content") != null);
}
// cleanup
reader.close();
searcher.close();
| public void | testRAMDirectoryFile()
MockRAMDirectory ramDir = new MockRAMDirectory(indexDir);
// Check size
assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
// open reader to test document count
IndexReader reader = IndexReader.open(ramDir);
assertEquals(docsToAdd, reader.numDocs());
// open search zo check if all doc's are there
IndexSearcher searcher = new IndexSearcher(reader);
// search for all documents
for (int i = 0; i < docsToAdd; i++) {
Document doc = searcher.doc(i);
assertTrue(doc.getField("content") != null);
}
// cleanup
reader.close();
searcher.close();
| public void | testRAMDirectorySize()
final MockRAMDirectory ramDir = new MockRAMDirectory(indexDir.getCanonicalPath());
final IndexWriter writer = new IndexWriter(ramDir, new WhitespaceAnalyzer(), false);
writer.optimize();
assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
Thread[] threads = new Thread[numThreads];
for (int i=0; i<numThreads; i++) {
final int num = i;
threads[i] = new Thread(){
public void run() {
for (int j=1; j<docsPerThread; j++) {
Document doc = new Document();
doc.add(new Field("sizeContent", English.intToEnglish(num*docsPerThread+j).trim(), Field.Store.YES, Field.Index.UN_TOKENIZED));
try {
writer.addDocument(doc);
} catch (IOException e) {
throw new RuntimeException(e);
}
synchronized (ramDir) {
assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
}
}
}
};
}
for (int i=0; i<numThreads; i++)
threads[i].start();
for (int i=0; i<numThreads; i++)
threads[i].join();
writer.optimize();
assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
writer.close();
| public void | testRAMDirectoryString()
MockRAMDirectory ramDir = new MockRAMDirectory(indexDir.getCanonicalPath());
// Check size
assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
// open reader to test document count
IndexReader reader = IndexReader.open(ramDir);
assertEquals(docsToAdd, reader.numDocs());
// open search zo check if all doc's are there
IndexSearcher searcher = new IndexSearcher(reader);
// search for all documents
for (int i = 0; i < docsToAdd; i++) {
Document doc = searcher.doc(i);
assertTrue(doc.getField("content") != null);
}
// cleanup
reader.close();
searcher.close();
| public void | testSerializable()
Directory dir = new RAMDirectory();
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
assertEquals("initially empty", 0, bos.size());
ObjectOutput out = new ObjectOutputStream(bos);
int headerSize = bos.size();
out.writeObject(dir);
out.close();
assertTrue("contains more then just header", headerSize < bos.size());
|
|