Skip to content

Commit

Permalink
Merge pull request mybatis#1298 from kazuki43zoo/mybatisgh-1297
Browse files Browse the repository at this point in the history
Add closed check at Cursor.iterator
  • Loading branch information
kazuki43zoo authored Sep 1, 2018
2 parents aba8cc5 + e9849bb commit 7a6921c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public Iterator<T> iterator() {
if (iteratorRetrieved) {
throw new IllegalStateException("Cannot open more than one iterator on a Cursor");
}
if (isClosed()) {
throw new IllegalStateException("A Cursor is already closed.");
}
iteratorRetrieved = true;
return cursorIterator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,31 @@ public void shouldGetAllUserUsingAnnotationBasedMapper() {
}
}

@Test
public void shouldThrowIllegalStateExceptionUsingIteratorOnSessionClosed() {
Cursor<User> usersCursor;
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
usersCursor = sqlSession.getMapper(Mapper.class).getAllUsers();
}
try {
usersCursor.iterator();
Assert.fail("Should throws the IllegalStateException when call the iterator method after session is closed.");
} catch (IllegalStateException e) {
Assert.assertEquals("A Cursor is already closed.", e.getMessage());
}

// verify for checking order
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
usersCursor = sqlSession.getMapper(Mapper.class).getAllUsers();
usersCursor.iterator();
}
try {
usersCursor.iterator();
Assert.fail("Should throws the IllegalStateException when call the iterator already.");
} catch (IllegalStateException e) {
Assert.assertEquals("Cannot open more than one iterator on a Cursor", e.getMessage());
}

}

}

0 comments on commit 7a6921c

Please sign in to comment.