FileDocCategorySizeDatePackage
TestStandardGdataSearcher.javaAPI DocApache Lucene 2.1.08006Wed Feb 14 10:46:02 GMT 2007org.apache.lucene.gdata.search

TestStandardGdataSearcher

public class TestStandardGdataSearcher extends TestCase
author
Simon Willnauer

Fields Summary
private Directory
dir
private int
amountDocuments
private static final String
FIELDNAME
private static final String
FIELDVALUE
private org.apache.lucene.gdata.utils.ReferenceCounter
searcher
private org.apache.lucene.gdata.search.StandardGdataSearcher
gdataSearcher
private List
idlist
private String
feedId
Constructors Summary
Methods Summary
protected voidsetUp()


         
        this.dir = new RAMDirectory();
        IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
        this.idlist = new ArrayList<String>();
        for (int i = 0; i < this.amountDocuments; i++) {
            Document doc = new Document();
            doc.add(new Field(IndexDocument.FIELD_FEED_ID, this.feedId,
                    Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.add(new Field(IndexDocument.FIELD_ENTRY_ID, "" + i,
                    Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.add(new Field(FIELDNAME, FIELDVALUE, Field.Store.YES,
                    Field.Index.UN_TOKENIZED));
            writer.addDocument(doc);
            this.idlist.add("" + i);
        }
        writer.close();
        this.searcher = new TestRefcounter(new IndexSearcher(this.dir));
        this.searcher.increamentReference();
        this.gdataSearcher = new StandardGdataSearcher(this.searcher);
    
protected voidtearDown()

        this.searcher.decrementRef();
        StandardGdataSearcher.flushFilterCache();
    
public voidtestClose()

        StandardGdataSearcher s = new StandardGdataSearcher(new TestRefcounter(
                new IndexSearcher(this.dir)));
        s.close();
        try {
            s.search(null, 0, 0,this.feedId);
            fail("searcher is closed");
        } catch (IllegalStateException e) {
        }

    
public voidtestCollectHits()

        Query q = new TermQuery(new Term(FIELDNAME, FIELDVALUE));
        Hits hits = this.searcher.get().search(q);
        assertEquals(amountDocuments, hits.length());
        List<String> returnValue = this.gdataSearcher.collectHits(hits, 1, 0);
        assertEquals(hits.doc(0).getField(IndexDocument.FIELD_ENTRY_ID)
                .stringValue(), returnValue.get(0));

        returnValue = this.gdataSearcher.collectHits(hits, 1, 1);
        assertEquals(hits.doc(0).getField(IndexDocument.FIELD_ENTRY_ID)
                .stringValue(), returnValue.get(0));

        returnValue = this.gdataSearcher.collectHits(hits, 1,
                this.amountDocuments);
        assertEquals(1, returnValue.size());
        assertEquals(hits.doc(this.amountDocuments - 1).getField(
                IndexDocument.FIELD_ENTRY_ID).stringValue(), returnValue.get(0));

        returnValue = this.gdataSearcher.collectHits(hits, 10,
                this.amountDocuments);
        assertEquals(1, returnValue.size());
        assertEquals(hits.doc(this.amountDocuments - 1).getField(
                IndexDocument.FIELD_ENTRY_ID).stringValue(), returnValue.get(0));

        returnValue = this.gdataSearcher.collectHits(hits, 50, 0);
        assertEquals(this.amountDocuments, returnValue.size());
        assertTrue(returnValue.containsAll(this.idlist));

        returnValue = this.gdataSearcher.collectHits(hits, 1, 5);
        assertEquals(1, returnValue.size());
        assertEquals(hits.doc(4).getField(IndexDocument.FIELD_ENTRY_ID)
                .stringValue(), returnValue.get(0));

        returnValue = this.gdataSearcher.collectHits(hits, 50,
                this.amountDocuments);
        assertEquals(1, returnValue.size());
        assertEquals(hits.doc(this.amountDocuments - 1).getField(
                IndexDocument.FIELD_ENTRY_ID).stringValue(), returnValue.get(0));

        returnValue = this.gdataSearcher.collectHits(hits, 1,
                this.amountDocuments + 1);
        assertEquals(0, returnValue.size());

    
public voidtestSearch()

        Query q = new TermQuery(new Term(FIELDNAME, FIELDVALUE));
        Hits hits = this.searcher.get().search(q);
        assertEquals(amountDocuments, hits.length());
        List<String> returnValue = this.gdataSearcher.search(q,
                this.amountDocuments, 0,this.feedId);
        assertEquals(amountDocuments, returnValue.size());
        assertTrue(returnValue.containsAll(this.idlist));
        try {
            this.gdataSearcher.search(null, 1, 0,this.feedId);
            fail("searcher is null");
        } catch (RuntimeException e) {
        }

        try {
            this.gdataSearcher.search(q, -1, 5,this.feedId);
            fail("hitcount is less than 0");
        } catch (IllegalArgumentException e) {}
        try {
            this.gdataSearcher.search(q, 4, -1,this.feedId);
            fail("offset is less than 0");
        } catch (IllegalArgumentException e) {}
        try {
            this.gdataSearcher.search(q, 4, 5,null);
            fail("feed id is null");
        } catch (IllegalArgumentException e) {}
      
        returnValue = this.gdataSearcher.search(q,this.amountDocuments, 0,"SomeOtherFeed");
        assertEquals(0,returnValue.size());
        

    
public voidtestStandardGdataSearcher()

        try {
            new StandardGdataSearcher(null);
            fail("searcher ref is null");
        } catch (IllegalArgumentException e) {

        }
        new StandardGdataSearcher(this.searcher);