From 0fa718244cbf7b6840d35166b228a5577cf49262 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Wed, 24 Sep 2008 09:45:25 +0000 Subject: [PATCH] LUCENE-1404: fixed NPE in NearSpansUnordered.isPayloadAvailable git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@698487 13f79535-47bb-0310-9956-ffa450edef68 --- .../search/spans/NearSpansUnordered.java | 18 +++--- .../apache/lucene/search/spans/TestSpans.java | 64 +++++++++++++++++++ 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java b/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java index d7a466677e5..226b827bcc1 100644 --- a/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java +++ b/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java @@ -225,16 +225,16 @@ class NearSpansUnordered implements PayloadSpans { } // TODO: Remove warning after API has been finalized - public boolean isPayloadAvailable() { - SpansCell pointer = min(); - do { - if(pointer.isPayloadAvailable()) { - return true; - } - pointer = pointer.next; - } while(pointer.next != null); + public boolean isPayloadAvailable() { + SpansCell pointer = min(); + while (pointer != null) { + if (pointer.isPayloadAvailable()) { + return true; + } + pointer = pointer.next; + } - return false; + return false; } public String toString() { diff --git a/src/test/org/apache/lucene/search/spans/TestSpans.java b/src/test/org/apache/lucene/search/spans/TestSpans.java index 127cf04d49a..24e7dbc4630 100644 --- a/src/test/org/apache/lucene/search/spans/TestSpans.java +++ b/src/test/org/apache/lucene/search/spans/TestSpans.java @@ -23,9 +23,14 @@ import org.apache.lucene.search.CheckHits; import org.apache.lucene.search.Similarity; import org.apache.lucene.search.DefaultSimilarity; import org.apache.lucene.search.Scorer; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.Searcher; import org.apache.lucene.store.RAMDirectory; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.MockRAMDirectory; +import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.analysis.WhitespaceAnalyzer; import org.apache.lucene.document.Document; @@ -389,4 +394,63 @@ public class TestSpans extends LuceneTestCase { assertTrue("first doc score should be zero, " + score, score == 0.0f); assertTrue("no second doc", ! spanScorer.next()); } + + // LUCENE-1404 + private void addDoc(IndexWriter writer, String id, String text) throws IOException { + final Document doc = new Document(); + doc.add( new Field("id", id, Field.Store.YES, Field.Index.UN_TOKENIZED) ); + doc.add( new Field("text", text, Field.Store.YES, Field.Index.TOKENIZED) ); + writer.addDocument(doc); + } + + // LUCENE-1404 + private int hitCount(Searcher searcher, String word) throws Throwable { + return searcher.search(new TermQuery(new Term("text", word)), 10).totalHits; + } + + // LUCENE-1404 + private SpanQuery createSpan(String value) { + return new SpanTermQuery(new Term("text", value)); + } + + // LUCENE-1404 + private SpanQuery createSpan(int slop, boolean ordered, SpanQuery[] clauses) { + return new SpanNearQuery(clauses, slop, ordered); + } + + // LUCENE-1404 + private SpanQuery createSpan(int slop, boolean ordered, String term1, String term2) { + return createSpan(slop, ordered, new SpanQuery[] {createSpan(term1), createSpan(term2)}); + } + + // LUCENE-1404 + public void testNPESpanQuery() throws Throwable { + final Directory dir = new MockRAMDirectory(); + final IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(new String[0]), IndexWriter.MaxFieldLength.LIMITED); + + // Add documents + addDoc(writer, "1", "the big dogs went running to the market"); + addDoc(writer, "2", "the cat chased the mouse, then the cat ate the mouse quickly"); + + // Commit + writer.close(); + + // Get searcher + final IndexReader reader = IndexReader.open(dir); + final IndexSearcher searcher = new IndexSearcher(reader); + + // Control (make sure docs indexed) + assertEquals(2, hitCount(searcher, "the")); + assertEquals(1, hitCount(searcher, "cat")); + assertEquals(1, hitCount(searcher, "dogs")); + assertEquals(0, hitCount(searcher, "rabbit")); + + // This throws exception (it shouldn't) + assertEquals(1, + searcher.search(createSpan(0, true, + new SpanQuery[] {createSpan(4, false, "chased", "cat"), + createSpan("ate")}), 10).totalHits); + reader.close(); + dir.close(); + } }