@Test public void shouldUpdateAuthorIfNecessary() throws Exception { SqlSession session = sqlMapper.openSession(); Author original; Author updated; try { original = session.selectOne("org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAuthor", 101); original.setEmail("*****@*****.**"); original.setBio(null); int updates = session.update( "org.apache.ibatis.domain.blog.mappers.AuthorMapper.updateAuthorIfNecessary", original); assertEquals(1, updates); updated = session.selectOne("org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAuthor", 101); assertEquals(original.getEmail(), updated.getEmail()); session.commit(); } finally { session.close(); } try { session = sqlMapper.openSession(); updated = session.selectOne("org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAuthor", 101); assertEquals(original.getEmail(), updated.getEmail()); } finally { session.close(); } }
@Test(expected = TooManyResultsException.class) public void shouldFailWithTooManyResultsException() throws Exception { SqlSession session = sqlMapper.openSession(TransactionIsolationLevel.SERIALIZABLE); try { session.selectOne("org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAllAuthors"); } finally { session.close(); } }
@Test public void shouldSelectCountOfPosts() throws Exception { SqlSession session = sqlMapper.openSession(); try { Integer count = session.selectOne("org.apache.ibatis.domain.blog.mappers.BlogMapper.selectCountOfPosts"); assertEquals(5, count.intValue()); } finally { session.close(); } }
@Test public void shouldSelectOneAuthorWithInlineParams() throws Exception { SqlSession session = sqlMapper.openSession(); try { Author author = session.selectOne( "org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAuthorWithInlineParams", new Author(101)); assertEquals(101, author.getId()); } finally { session.close(); } }
@Test public void shouldSelectOneImmutableAuthor() throws Exception { SqlSession session = sqlMapper.openSession(); try { ImmutableAuthor author = session.selectOne( "org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectImmutableAuthor", new Author(101)); assertEquals(101, author.getId()); assertEquals(Section.NEWS, author.getFavouriteSection()); } finally { session.close(); } }
@Test public void shouldSelectBlogWithPostsAndAuthorUsingSubSelects() throws Exception { SqlSession session = sqlMapper.openSession(); try { Blog blog = session.selectOne( "org.apache.ibatis.domain.blog.mappers.BlogMapper.selectBlogWithPostsUsingSubSelect", 1); assertEquals("Jim Business", blog.getTitle()); assertEquals(2, blog.getPosts().size()); assertEquals("Corn nuts", blog.getPosts().get(0).getSubject()); assertEquals(101, blog.getAuthor().getId()); assertEquals("jim", blog.getAuthor().getUsername()); } finally { session.close(); } }
@Test public void shouldInsertAuthor() throws Exception { SqlSession session = sqlMapper.openSession(); try { Author expected = new Author(500, "cbegin", "******", "*****@*****.**", "Something...", null); int updates = session.insert( "org.apache.ibatis.domain.blog.mappers.AuthorMapper.insertAuthor", expected); assertEquals(1, updates); Author actual = session.selectOne( "org.apache.ibatis.domain.blog.mappers.AuthorMapper.selectAuthor", new Author(500)); assertNotNull(actual); assertEquals(expected.getId(), actual.getId()); assertEquals(expected.getUsername(), actual.getUsername()); assertEquals(expected.getPassword(), actual.getPassword()); assertEquals(expected.getEmail(), actual.getEmail()); assertEquals(expected.getBio(), actual.getBio()); } finally { session.close(); } }
@Test public void shouldSelectNestedBlogWithPostsAndAuthorUsingJoin() throws Exception { SqlSession session = sqlMapper.openSession(); try { Blog blog = session.selectOne( "org.apache.ibatis.domain.blog.mappers.NestedBlogMapper.selectBlogJoinedWithPostsAndAuthor", 1); assertEquals("Jim Business", blog.getTitle()); final Author author = blog.getAuthor(); assertEquals(101, author.getId()); assertEquals("jim", author.getUsername()); final List<Post> posts = blog.getPosts(); assertEquals(2, posts.size()); final Post post = blog.getPosts().get(0); assertEquals(1, post.getId()); assertEquals("Corn nuts", post.getSubject()); final List<Comment> comments = post.getComments(); assertEquals(2, comments.size()); final List<Tag> tags = post.getTags(); assertEquals(3, tags.size()); final Comment comment = comments.get(0); assertEquals(1, comment.getId()); assertEquals(DraftPost.class, blog.getPosts().get(0).getClass()); assertEquals(Post.class, blog.getPosts().get(1).getClass()); } finally { session.close(); } }