使用JUnit

来源:互联网 发布:刺激女生乳头知乎 编辑:程序博客网 时间:2024/06/09 23:44

使用junit测试lucene:

import junit.framework.*;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.index.Term;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.util.Version;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;


public class IndexTester {
  protected String[] ids = {"1", "2"};
  protected String[] unindexed = {"Netherlands", "Italy"};
  protected String[] unstored = { 
    "Amsterdam has lots of bridges",
    "Venice has lots of canals"
  };  
  protected String[] text = {"Amsterdam", "Venice"};
  private Directory directory;


  protected void setUp() throws Exception {
    System.err.println("JUnit Setup!");
    directory = new RAMDirectory();
    IndexWriter writer = getWriter();
    for (int i = 0; i < ids.length; i++) {
      Document doc = new Document();
      doc.add(new Field("id", ids[i],
      Field.Store.YES,
      Field.Index.NOT_ANALYZED));
      doc.add(new Field("country", unindexed[i], Field.Store.YES, Field.Index.NO));

      doc.add(new Field("contents", unstored[i], Field.Store.NO, Field.Index.ANALYZED));

      writer.addDocument(doc);
    }
    writer.close();
  }


  private IndexWriter getWriter() throws IOException {
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new WhitespaceAnalyzer(Version.LUCENE_46));
    return new IndexWriter(directory, config);
  }


  protected int getHitCount(String fieldName, String searchString) throws IOException {
    IndexReader reader = IndexReader.open(directory);
    IndexSearcher searcher = new IndexSearcher(reader);
    Term t = new Term(fieldName, searchString);
    Query query = new TermQuery(t);
    int hitCount = IndexTester.hitCount(searcher, query);
    reader.close();
    return hitCount;
  }


  @Test
  public void testIndexWriter() throws IOException {
    IndexWriter writer = getWriter();
    assertEquals(ids.length, writer.numDocs());
    writer.close();
  }


  @Test
  public void testIndexReader() throws IOException {
    IndexReader reader = IndexReader.open(directory);
    assertEquals(ids.length, reader.maxDoc());
    assertEquals(ids.length, reader.numDocs());
    reader.close();
  }


  public static int hitCount(IndexSearcher searcher, Query query)
    throws IOException {
    return searcher.search(query, 1).totalHits;
  }

}

不使用编译工具:

编译:

javac -classpath lib/lucene-core-4.6-SNAPSHOT.jar:lib/lucene-analyzers-common-4.6-SNAPSHOT.jar:lib/junit.jar IndexTester.java

运行:

java -cp lib/junit.jar:lib/lucene-analyzers-common-4.6-SNAPSHOT.jar:lib/lucene-core-4.6-SNAPSHOT.jar:IndexTester.jar:./ org.junit.runner.JUnitCore IndexTester

0 0
原创粉丝点击