Skip to content

Commit 2e29049

Browse files
committed
Add tests for RowBounds on MapperMethod
1 parent 2995f6e commit 2e29049

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/test/java/org/apache/ibatis/binding/BindingTest.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.ArrayList;
2727
import java.util.Collections;
2828
import java.util.HashMap;
29+
import java.util.Iterator;
2930
import java.util.List;
3031
import java.util.Map;
3132

@@ -36,6 +37,7 @@
3637
import net.sf.cglib.proxy.Factory;
3738

3839
import org.apache.ibatis.BaseDataTest;
40+
import org.apache.ibatis.cursor.Cursor;
3941
import org.apache.ibatis.domain.blog.Author;
4042
import org.apache.ibatis.domain.blog.Blog;
4143
import org.apache.ibatis.domain.blog.DraftPost;
@@ -734,5 +736,55 @@ public void shouldGetBlogsWithAuthorsAndPostsEagerly() {
734736
session.close();
735737
}
736738
}
737-
739+
740+
@Test
741+
public void executeWithResultHandlerAndRowBounds() {
742+
SqlSession session = sqlSessionFactory.openSession();
743+
try {
744+
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
745+
final DefaultResultHandler handler = new DefaultResultHandler();
746+
mapper.collectRangeBlogs(handler, new RowBounds(1, 1));
747+
748+
assertEquals(1, handler.getResultList().size());
749+
Blog blog = (Blog) handler.getResultList().get(0);
750+
assertEquals(2, blog.getId());
751+
752+
} finally {
753+
session.close();
754+
}
755+
}
756+
757+
@Test
758+
public void executeWithMapKeyAndRowBounds() {
759+
SqlSession session = sqlSessionFactory.openSession();
760+
try {
761+
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
762+
Map<Integer, Blog> blogs = mapper.selectRangeBlogsAsMapById(new RowBounds(1, 1));
763+
764+
assertEquals(1, blogs.size());
765+
Blog blog = blogs.get(2);
766+
assertEquals(2, blog.getId());
767+
768+
} finally {
769+
session.close();
770+
}
771+
}
772+
773+
@Test
774+
public void executeWithCursorAndRowBounds() {
775+
SqlSession session = sqlSessionFactory.openSession();
776+
try {
777+
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
778+
Cursor<Blog> blogs = mapper.openRangeBlogs(new RowBounds(1, 1));
779+
780+
Iterator<Blog> blogIterator = blogs.iterator();
781+
Blog blog = blogIterator.next();
782+
assertEquals(2, blog.getId());
783+
assertFalse(blogIterator.hasNext());
784+
785+
} finally {
786+
session.close();
787+
}
788+
}
789+
738790
}

src/test/java/org/apache/ibatis/binding/BoundBlogMapper.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
package org.apache.ibatis.binding;
1717

1818
import org.apache.ibatis.annotations.*;
19+
import org.apache.ibatis.cursor.Cursor;
1920
import org.apache.ibatis.domain.blog.Author;
2021
import org.apache.ibatis.domain.blog.Blog;
2122
import org.apache.ibatis.domain.blog.DraftPost;
2223
import org.apache.ibatis.domain.blog.Post;
2324
import org.apache.ibatis.mapping.FetchType;
25+
import org.apache.ibatis.session.ResultHandler;
2426
import org.apache.ibatis.session.RowBounds;
2527

2628
import java.util.List;
@@ -43,6 +45,10 @@ public interface BoundBlogMapper {
4345
@MapKey("id")
4446
Map<Integer,Blog> selectBlogsAsMapById();
4547

48+
@Select({ "SELECT * FROM blog ORDER BY id"})
49+
@MapKey("id")
50+
Map<Integer,Blog> selectRangeBlogsAsMapById(RowBounds rowBounds);
51+
4652
//======================================================
4753

4854
@Select({
@@ -51,6 +57,22 @@ public interface BoundBlogMapper {
5157
})
5258
List<Blog> selectBlogs();
5359

60+
@Select({
61+
"SELECT *",
62+
"FROM blog",
63+
"ORDER BY id"
64+
})
65+
@ResultType(Blog.class)
66+
void collectRangeBlogs(ResultHandler<Object> blog, RowBounds rowBounds);
67+
68+
69+
@Select({
70+
"SELECT *",
71+
"FROM blog",
72+
"ORDER BY id"
73+
})
74+
Cursor<Blog> openRangeBlogs(RowBounds rowBounds);
75+
5476
//======================================================
5577

5678
List<Blog> selectBlogsFromXML();

0 commit comments

Comments
 (0)