From cfd5d55b7ecc9c757910189b59edc01f3f644e12 Mon Sep 17 00:00:00 2001 From: Erik Hatcher Date: Tue, 26 Apr 2005 03:19:07 +0000 Subject: [PATCH] Fix issue Otis identified with HitIterator, along with corresponding test case git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@164726 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/lucene/search/HitIterator.java | 11 +++++------ src/test/org/apache/lucene/TestHitIterator.java | 12 ++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/java/org/apache/lucene/search/HitIterator.java b/src/java/org/apache/lucene/search/HitIterator.java index b502598bb40..959d193cdf4 100644 --- a/src/java/org/apache/lucene/search/HitIterator.java +++ b/src/java/org/apache/lucene/search/HitIterator.java @@ -50,13 +50,12 @@ public class HitIterator implements Iterator { * @return Next {@link Hit}. */ public Object next() { - try { - Object next = new Hit(hits, hitNumber); - hitNumber++; - return next; - } catch (IndexOutOfBoundsException e) { + if (hitNumber == hits.length()) throw new NoSuchElementException(); - } + + Object next = new Hit(hits, hitNumber); + hitNumber++; + return next; } /** diff --git a/src/test/org/apache/lucene/TestHitIterator.java b/src/test/org/apache/lucene/TestHitIterator.java index a5efbdb546a..b5df624bec5 100644 --- a/src/test/org/apache/lucene/TestHitIterator.java +++ b/src/test/org/apache/lucene/TestHitIterator.java @@ -13,6 +13,8 @@ import org.apache.lucene.search.Hits; import org.apache.lucene.search.Hit; import org.apache.lucene.search.HitIterator; +import java.util.NoSuchElementException; + /** * This test intentionally not put in the search package in order * to test HitIterator and Hit package protection. @@ -46,5 +48,15 @@ public class TestHitIterator extends TestCase { assertEquals("iterator test doc 2", hit.getDocument().get("field")); assertFalse(iterator.hasNext()); + + boolean caughtException = false; + try { + iterator.next(); + } catch (NoSuchElementException e) { + assertTrue(true); + caughtException = true; + } + + assertTrue(caughtException); } }