Skip to content

Commit e83bfec

Browse files
committed
Change status checking order in Cursor.inerator
See #1297
1 parent 2539245 commit e83bfec

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/main/java/org/apache/ibatis/cursor/defaults/DefaultCursor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ public int getCurrentIndex() {
9494

9595
@Override
9696
public Iterator<T> iterator() {
97-
if (isClosed()) {
98-
throw new IllegalStateException("A Cursor is already closed.");
99-
}
10097
if (iteratorRetrieved) {
10198
throw new IllegalStateException("Cannot open more than one iterator on a Cursor");
10299
}
100+
if (isClosed()) {
101+
throw new IllegalStateException("A Cursor is already closed.");
102+
}
103103
iteratorRetrieved = true;
104104
return cursorIterator;
105105
}

src/test/java/org/apache/ibatis/submitted/cursor_simple/CursorSimpleTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,19 @@ public void shouldThrowIllegalStateExceptionUsingIteratorOnSessionClosed() {
373373
} catch (IllegalStateException e) {
374374
Assert.assertEquals("A Cursor is already closed.", e.getMessage());
375375
}
376+
377+
// verify for checking order
378+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
379+
usersCursor = sqlSession.getMapper(Mapper.class).getAllUsers();
380+
usersCursor.iterator();
381+
}
382+
try {
383+
usersCursor.iterator();
384+
Assert.fail("Should throws the IllegalStateException when call the iterator already.");
385+
} catch (IllegalStateException e) {
386+
Assert.assertEquals("Cannot open more than one iterator on a Cursor", e.getMessage());
387+
}
388+
376389
}
377390

378391
}

0 commit comments

Comments
 (0)