FileDocCategorySizeDatePackage
TestGdataIndexer.javaAPI DocApache Lucene 2.1.020560Wed Feb 14 10:46:02 GMT 2007org.apache.lucene.gdata.search.index

TestGdataIndexer

public class TestGdataIndexer extends TestCase
author
Simon Willnauer

Fields Summary
private GDataIndexer
indexer
private Directory
dir
private static org.apache.lucene.gdata.search.config.IndexSchema
config
private static String
FIELD_ID
Constructors Summary
Methods Summary
protected voidsetUp()

see
junit.framework.TestCase#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 voidtearDown()

see
junit.framework.TestCase#tearDown()

        this.indexer.destroy();
    
public voidtestAddDocument()

        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 voidtestAddIndexableDocumentTask()

        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 voidtestCloseSearcher()

        this.indexer.openSearcher();
        assertNotNull(this.indexer.searcher);
        this.indexer.closeSearcher();
        assertNull(this.indexer.searcher);
    
public voidtestCloseWriter()

        assertNotNull(this.indexer.writer);
        this.indexer.closeWriter();
        assertNull(this.indexer.writer);

    
public voidtestCommit()

        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 voidtestDeleteDocument()

        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 voidtestDestroy()

        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 voidtestGDataIndexer()

        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 voidtestInnerClassFuture()

        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 voidtestNotifyCommitListeners()

        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 voidtestOpenSearcher()

        this.indexer.searcher = null;
        this.indexer.openSearcher();
         assertNotNull(this.indexer.searcher);
    
public voidtestOpenWriter()

        this.indexer.closeWriter();
        assertNull(this.indexer.writer);
       this.indexer.openWriter();
        assertNotNull(this.indexer.writer);
    
public voidtestRegisterIndexEventListener()

        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 voidtestRemoveIndexEventListener()

        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 voidtestStaticFactoryMethodes()

        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 voidtestUpdateDocument()

        
        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) {
            
        }