使用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
- junit 使用
- junit使用
- junit 使用
- junit 使用
- Junit使用
- JUnit使用
- 使用JUnit
- Junit使用
- Junit使用
- junit 使用
- Junit使用
- junit使用
- junit使用
- Junit使用
- JUnit使用
- JUnit使用
- Junit 使用
- 【Java.JUnit】JUnit使用教程
- 计算机应用
- cString is deprecated :first deprecated in iOS 2.0
- 活性炭热再生法为工业上最成熟的再生法
- 跟着BOY 学习COCOS2D-X 网络篇---强联网(采用技术 BSD SOCKET+多线程技术 +protobuf)(环境搭建篇+服务器)
- 接近传感器(CM3652)在触摸屏手机中的应用
- 使用JUnit
- UITextFiled select改成中文
- Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
- 计算机应用系统分析
- Struts知识详解
- ReSharper 8--------NET开发利器
- phonegap导入工程报错解决办法 错误解析android
- [教程] MPICH2 Win7 VS2008环境搭建
- SIP穿越NAT的rport机制