mirror of
https://github.com/apache/lucene.git
synced 2025-02-08 11:05:29 +00:00
- Initial checkin.
Converted to JUnit-based unit test from Christoph Goller's test class. git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150012 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8c69111508
commit
3527232b78
75
src/test/org/apache/lucene/index/TestIndexWriter.java
Normal file
75
src/test/org/apache/lucene/index/TestIndexWriter.java
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package org.apache.lucene.index;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
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.Directory;
|
||||||
|
import org.apache.lucene.store.RAMDirectory;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author goller
|
||||||
|
*/
|
||||||
|
public class TestIndexWriter extends TestCase
|
||||||
|
{
|
||||||
|
private int docCount = 0;
|
||||||
|
|
||||||
|
public void testDocCount()
|
||||||
|
{
|
||||||
|
Directory dir = new RAMDirectory();
|
||||||
|
|
||||||
|
IndexWriter writer = null;
|
||||||
|
IndexReader reader = null;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
try {
|
||||||
|
writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
|
||||||
|
|
||||||
|
// add 100 documents
|
||||||
|
for (i = 0; i < 100; i++) {
|
||||||
|
addDoc(writer);
|
||||||
|
}
|
||||||
|
assertEquals(100, writer.docCount());
|
||||||
|
writer.close();
|
||||||
|
|
||||||
|
// delete 50 documents
|
||||||
|
reader = IndexReader.open(dir);
|
||||||
|
for (i = 0; i < 50; i++) {
|
||||||
|
reader.delete(i);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
|
||||||
|
assertEquals(50, writer.docCount());
|
||||||
|
writer.optimize();
|
||||||
|
assertEquals(50, writer.docCount());
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addDoc(IndexWriter writer)
|
||||||
|
{
|
||||||
|
Document doc = new Document();
|
||||||
|
|
||||||
|
doc.add(Field.Keyword("id","id" + docCount));
|
||||||
|
doc.add(Field.UnStored("content","aaa"));
|
||||||
|
|
||||||
|
try {
|
||||||
|
writer.addDocument(doc);
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
docCount++;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user