[#3241] Cursor.iterator() does not correctly implement Iterator.next() contract with respect to NoSuchElementException

This commit is contained in:
Lukas Eder 2014-05-07 10:48:18 +02:00
parent 6d38a164b1
commit c0a07a7833
2 changed files with 18 additions and 8 deletions

View File

@ -74,6 +74,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Queue;
import org.jooq.AttachableInternal;
@ -1726,8 +1727,18 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
assertFalse(cursor.hasNext());
assertTrue(cursor.isClosed());
assertEquals(null, it.next());
assertEquals(null, it.next());
try {
it.next();
fail();
}
catch (NoSuchElementException expected) {}
try {
it.next();
fail();
}
catch (NoSuchElementException expected) {}
assertEquals(null, cursor.fetchOne());
assertEquals(null, cursor.fetchOne());

View File

@ -66,6 +66,7 @@ import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import org.jooq.Cursor;
import org.jooq.ExecuteContext;
@ -194,13 +195,12 @@ class CursorImpl<R extends Record> implements Cursor<R> {
iterator();
ResultImpl<R> result = new ResultImpl<R>(ctx.configuration(), fields);
R record = null;
ctx.result(result);
listener.resultStart(ctx);
for (int i = 0; i < number && ((record = iterator().next()) != null); i++) {
result.addRecord(record);
for (int i = 0; i < number && iterator().hasNext(); i++) {
result.addRecord(iterator().next());
}
ctx.result(result);
@ -1385,9 +1385,8 @@ class CursorImpl<R extends Record> implements Cursor<R> {
@Override
public final R next() {
if (hasNext == null) {
return fetchOne();
}
if (!hasNext())
throw new NoSuchElementException("There are no more records to fetch from this Cursor");
R result = next;
hasNext = null;