TestGdataIndexerpublic class TestGdataIndexer extends TestCase
Fields Summary |
---|
private GDataIndexer | indexer | private Directory | dir | private static org.apache.lucene.gdata.search.config.IndexSchema | config | private static String | FIELD_ID |
Methods Summary |
---|
protected void | setUp()
config = new IndexSchema();
config.setName("testService");
config.setCommitLockTimeout(-1);
config.setServiceAnalyzer(new StandardAnalyzer());
config.setMaxBufferedDocs(-1);
config.setMaxFieldLength(-1);
config.setMaxMergeDocs(-1);
config.setWriteLockTimeout(-1);
config.setMergeFactor(-1);
this.dir = new RAMDirectory();
this.indexer = GDataIndexer.createGdataIndexer(config, this.dir, true);
super.setUp();
| protected void | tearDown()
this.indexer.destroy();
| public void | testAddDocument()
String id = "myID";
Field f = new Field(FIELD_ID, id, Field.Store.YES,
Field.Index.UN_TOKENIZED);
Document doc = new Document();
doc.add(f);
Term delTerm = new Term(FIELD_ID, id);
IndexDocument iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.INSERT);
this.indexer.addDocument(iDoc);
assertEquals(1,this.indexer.docsAdded.get());
assertEquals(0,this.indexer.docsDeleted.get());
assertEquals(0,this.indexer.docsUpdated.get());
this.indexer.addDocument(iDoc);
this.indexer.commit(false);
IndexSearcher s = new IndexSearcher(this.dir);
Hits h = s.search(new TermQuery(delTerm));
assertEquals(1, h.length());
s.close();
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.UPDATE);
try{
this.indexer.addDocument(iDoc);
fail("document has not insert action ");
}catch (GdataIndexerException e) {
}
| public void | testAddIndexableDocumentTask()
String id = "myID";
Field f = new Field(FIELD_ID, id, Field.Store.YES,
Field.Index.UN_TOKENIZED);
Document doc = new Document();
doc.add(f);
Term delTerm = new Term(FIELD_ID, id);
/*
* Latch will be decremented in FutureStub#get() and
* IndexDocumentStub#getIndexable
*/
CountDownLatch l = new CountDownLatch(2);
IndexDocument iDoc = new IndexDocumentStub(doc, delTerm,
IndexAction.INSERT, l);
Future<IndexDocument> future = new FutureStub<IndexDocument>(iDoc, l);
this.indexer.addIndexableDocumentTask(future);
// wait for the latch do decrement
l.await(5000, TimeUnit.MILLISECONDS);
this.indexer.commit(false);
IndexSearcher s = new IndexSearcher(this.dir);
Hits h = s.search(new TermQuery(delTerm));
assertEquals(1, h.length());
s.close();
// test for update
/*
* Latch will be decremented in FutureStub#get() and
* IndexDocumentStub#getIndexable
*/
l = new CountDownLatch(2);
iDoc = new IndexDocumentStub(doc, delTerm, IndexAction.UPDATE, l);
future = new FutureStub<IndexDocument>(iDoc, l);
this.indexer.addIndexableDocumentTask(future);
l.await(5000, TimeUnit.MILLISECONDS);
this.indexer.commit(false);
s = new IndexSearcher(this.dir);
h = s.search(new TermQuery(delTerm));
assertEquals(1, h.length());
s.close();
// test for delete
/*
* Latch will be decremented in FutureStub#get()
*/
l = new CountDownLatch(1);
iDoc = new IndexDocumentStub(doc, delTerm, IndexAction.DELETE, l);
future = new FutureStub<IndexDocument>(iDoc, l);
this.indexer.addIndexableDocumentTask(future);
/*
* wait for the indexer task to add the deleted
*/
while (this.indexer.docsDeleted.get() == 0)
l.await(5000, TimeUnit.MILLISECONDS);
this.indexer.commit(false);
s = new IndexSearcher(this.dir);
h = s.search(new TermQuery(delTerm));
assertEquals(0, h.length());
s.close();
| public void | testCloseSearcher()
this.indexer.openSearcher();
assertNotNull(this.indexer.searcher);
this.indexer.closeSearcher();
assertNull(this.indexer.searcher);
| public void | testCloseWriter()
assertNotNull(this.indexer.writer);
this.indexer.closeWriter();
assertNull(this.indexer.writer);
| public void | testCommit()
String id = "myID";
Field f = new Field(FIELD_ID, id, Field.Store.YES,
Field.Index.UN_TOKENIZED);
Document doc = new Document();
doc.add(f);
Term delTerm = new Term(FIELD_ID, id);
IndexDocument iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.INSERT);
this.indexer.addDocument(iDoc);
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.UPDATE);
this.indexer.updateDocument(iDoc);
this.indexer.updateDocument(iDoc);
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.DELETE);
this.indexer.deleteDocument(iDoc);
IndexEventListenerStub evListener = new IndexEventListenerStub();
this.indexer.registerIndexEventListener(evListener);
assertEquals(1,this.indexer.docsAdded.get());
assertEquals(1,this.indexer.docsDeleted.get());
assertEquals(2,this.indexer.docsUpdated.get());
assertEquals(0,evListener.getCalledCount());
this.indexer.commit(true);
this.indexer.commit(false);
assertEquals(1,evListener.getCalledCount());
assertEquals(0,this.indexer.docsAdded.get());
assertEquals(0,this.indexer.docsDeleted.get());
assertEquals(0,this.indexer.docsUpdated.get());
IndexSearcher s = new IndexSearcher(this.dir);
Hits h = s.search(new TermQuery(delTerm));
assertEquals(0, h.length());
s.close();
| public void | testDeleteDocument()
String id = "myID";
Field f = new Field(FIELD_ID, id, Field.Store.YES,
Field.Index.UN_TOKENIZED);
Document doc = new Document();
doc.add(f);
Term delTerm = new Term(FIELD_ID, id);
IndexDocument iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.INSERT);
/*
* write doc to index
*/
this.indexer.writer.addDocument(doc);
this.indexer.closeWriter();
IndexSearcher s = new IndexSearcher(this.dir);
Hits h = s.search(new TermQuery(delTerm));
assertEquals(1, h.length());
s.close();
/*
* del doc via indexer
*/
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.DELETE);
this.indexer.deleteDocument(iDoc);
assertEquals(0,this.indexer.docsAdded.get());
assertEquals(1,this.indexer.docsDeleted.get());
assertEquals(0,this.indexer.docsUpdated.get());
this.indexer.commit(false);
s = new IndexSearcher(this.dir);
h = s.search(new TermQuery(delTerm));
assertEquals(0, h.length());
s.close();
/*
* test insert / del without optimize
*/
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.INSERT);
this.indexer.addDocument(iDoc);
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.DELETE);
this.indexer.deleteDocument(iDoc);
this.indexer.commit(false);
s = new IndexSearcher(this.dir);
h = s.search(new TermQuery(delTerm));
assertEquals(0, h.length());
s.close();
/*
* test insert / del / update without optimize
*/
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.INSERT);
this.indexer.addDocument(iDoc);
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.DELETE);
this.indexer.deleteDocument(iDoc);
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.INSERT);
this.indexer.addDocument(iDoc);
this.indexer.commit(false);
s = new IndexSearcher(this.dir);
h = s.search(new TermQuery(delTerm));
assertEquals(1, h.length());
s.close();
/*
* test insert / update / del without optimize
*/
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.INSERT);
this.indexer.addDocument(iDoc);
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.UPDATE);
this.indexer.updateDocument(iDoc);
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.DELETE);
this.indexer.deleteDocument(iDoc);
this.indexer.commit(false);
s = new IndexSearcher(this.dir);
h = s.search(new TermQuery(delTerm));
assertEquals(0, h.length());
s.close();
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.UPDATE);
try{
this.indexer.deleteDocument(iDoc);
fail("document has not delete action ");
}catch (GdataIndexerException e) {
}
| public void | testDestroy()
this.indexer.destroy();
String id = "myID";
Field f = new Field(FIELD_ID, id, Field.Store.YES,
Field.Index.UN_TOKENIZED);
Document doc = new Document();
doc.add(f);
Term delTerm = new Term(FIELD_ID, id);
IndexDocument iDoc = new IndexDocumentStub(doc, delTerm,
IndexAction.INSERT);
Future<IndexDocument> future = new FutureStub<IndexDocument>(iDoc);
try{
this.indexer.addIndexableDocumentTask(future);
fail("indexer already closed exc. expected");
}catch (IllegalStateException e) {}
this.indexer = GDataIndexer.createGdataIndexer(config, dir, true);
CountDownLatch documentLatch = new CountDownLatch(1);
iDoc = new IndexDocumentStub(doc, delTerm,
IndexAction.INSERT,documentLatch);
CountDownLatch latch = new CountDownLatch(1);
future = new FutureStub<IndexDocument>(iDoc,latch,true);
this.indexer.addIndexableDocumentTask(future);
this.indexer.destroy();
latch.countDown();
documentLatch.await(5000,TimeUnit.MILLISECONDS);
// wait active for the commit
while(this.indexer.writer != null){}
IndexSearcher s = new IndexSearcher(this.dir);
Hits h = s.search(new TermQuery(delTerm));
assertEquals(1, h.length());
s.close();
| public void | testGDataIndexer()
try {
new GDataIndexer(null, dir, true);
fail("config is null");
} catch (IllegalArgumentException e) {
//
}
try {
new GDataIndexer(config, null, true);
fail("dir is null");
} catch (IllegalArgumentException e) {
//
}
GDataIndexer in = new GDataIndexer(config,new RAMDirectory(),true);
in.setIndexTask(null);
in.init();
assertNotNull(in.indexTask);
assertEquals(IndexTask.class,in.indexTask.getClass());
| public void | testInnerClassFuture()
Future f = new GDataIndexer.FinishingFuture();
assertNull(f.get());
assertNull(f.get(0,TimeUnit.MICROSECONDS));
assertTrue(f.isDone());
assertFalse(f.isCancelled());
assertFalse(f.cancel(true));
| public void | testNotifyCommitListeners()
IndexEventListenerStub evListener = new IndexEventListenerStub();
IndexEventListenerStub evListener1 = new IndexEventListenerStub();
IndexEventListenerStub evListener2 = new IndexEventListenerStub();
this.indexer.registerIndexEventListener(evListener);
this.indexer.registerIndexEventListener(evListener1);
this.indexer.registerIndexEventListener(evListener2);
assertEquals(0,evListener.getCalledCount());
this.indexer.notifyCommitListeners("someId");
assertEquals(1,evListener.getCalledCount());
assertEquals(1,evListener1.getCalledCount());
assertEquals(1,evListener2.getCalledCount());
this.indexer.removeIndexEventListener(evListener);
this.indexer.notifyCommitListeners("someId");
assertEquals(1,evListener.getCalledCount());
assertEquals(2,evListener1.getCalledCount());
assertEquals(2,evListener2.getCalledCount());
| public void | testOpenSearcher()
this.indexer.searcher = null;
this.indexer.openSearcher();
assertNotNull(this.indexer.searcher);
| public void | testOpenWriter()
this.indexer.closeWriter();
assertNull(this.indexer.writer);
this.indexer.openWriter();
assertNotNull(this.indexer.writer);
| public void | testRegisterIndexEventListener()
IndexEventListenerStub evListener = new IndexEventListenerStub();
this.indexer.registerIndexEventListener(evListener);
this.indexer.registerIndexEventListener(evListener);
assertEquals(0,evListener.getCalledCount());
this.indexer.notifyCommitListeners("someId");
this.indexer.notifyCommitListeners("someId");
assertEquals(2,evListener.getCalledCount());
| public void | testRemoveIndexEventListener()
IndexEventListenerStub evListener = new IndexEventListenerStub();
this.indexer.registerIndexEventListener(evListener);
assertEquals(0,evListener.getCalledCount());
this.indexer.notifyCommitListeners("someId");
assertEquals(1,evListener.getCalledCount());
this.indexer.removeIndexEventListener(evListener);
this.indexer.removeIndexEventListener(evListener);
this.indexer.notifyCommitListeners("someId");
assertEquals(1,evListener.getCalledCount());
| public void | testStaticFactoryMethodes()
GDataIndexer i =GDataIndexer.createGdataIndexer(config, new RAMDirectory(), true);
assertNotNull(i);
assertEquals(IndexTask.class,i.indexTask.getClass());
i =GDataIndexer.createTimedGdataIndexer(config, new RAMDirectory(), true,60);
assertNotNull(i);
assertEquals(TimedIndexTask.class,i.indexTask.getClass());
assertEquals(60,((TimedIndexTask)i.indexTask).getIdleTime());
i.destroy();
| public void | testUpdateDocument()
String id = "myID";
Field f = new Field(FIELD_ID, id, Field.Store.YES,
Field.Index.UN_TOKENIZED);
Document doc = new Document();
doc.add(f);
Term delTerm = new Term(FIELD_ID, id);
IndexDocument iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.INSERT);
/*
* write doc to index
*/
this.indexer.writer.addDocument(doc);
this.indexer.closeWriter();
IndexSearcher s = new IndexSearcher(this.dir);
Hits h = s.search(new TermQuery(delTerm));
assertEquals(1, h.length());
s.close();
String testFieldName = "someTestFieldupdate";
doc.add(new Field(testFieldName,"someText",Field.Store.YES,Field.Index.TOKENIZED));
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.UPDATE);
/*
* updateDoc via indexer
*/
this.indexer.updateDocument(iDoc);
assertEquals(0,this.indexer.docsAdded.get());
assertEquals(0,this.indexer.docsDeleted.get());
assertEquals(1,this.indexer.docsUpdated.get());
this.indexer.commit(false);
s = new IndexSearcher(this.dir);
h = s.search(new TermQuery(delTerm));
assertEquals(1, h.length());
assertNotNull(h.doc(0).getField(testFieldName));
s.close();
iDoc = new IndexDocumentStub(doc,delTerm,
IndexAction.DELETE);
try{
this.indexer.updateDocument(iDoc);
fail("document has not update action ");
}catch (GdataIndexerException e) {
}
|
|