|
| 1 | +package test; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.io.Reader; |
| 6 | +import java.sql.Connection; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.apache.ibatis.io.Resources; |
| 10 | +import org.apache.ibatis.jdbc.ScriptRunner; |
| 11 | +import org.apache.ibatis.session.SqlSession; |
| 12 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 13 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 14 | +import org.junit.BeforeClass; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +public class SimpleTest { |
| 18 | + |
| 19 | + private static SqlSessionFactory sqlSessionFactory; |
| 20 | + |
| 21 | + @BeforeClass |
| 22 | + public static void setUp() throws Exception { |
| 23 | + // create an SqlSessionFactory |
| 24 | + try (Reader reader = Resources.getResourceAsReader("test/mybatis-config.xml")) { |
| 25 | + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); |
| 26 | + } |
| 27 | + // prepare in-memory database |
| 28 | + try (SqlSession session = sqlSessionFactory.openSession(); |
| 29 | + Connection conn = session.getConnection(); |
| 30 | + Reader reader = Resources.getResourceAsReader("test/CreateDB.sql")) { |
| 31 | + ScriptRunner runner = new ScriptRunner(conn); |
| 32 | + runner.setLogWriter(null); |
| 33 | + runner.runScript(reader); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void getOneDivideOut() { |
| 39 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 40 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 41 | + DivideOut out = mapper.selectMultiple(); |
| 42 | + List<Movie> movies = out.movie; |
| 43 | + assertEquals(3, movies.size()); |
| 44 | + assertEquals("Movie1", movies.get(0).getName()); |
| 45 | + assertEquals("Movie2", movies.get(1).getName()); |
| 46 | + assertEquals("Movie3", movies.get(2).getName()); |
| 47 | + List<Artist> artists = out.artist; |
| 48 | + assertEquals(2, artists.size()); |
| 49 | + assertEquals("John", artists.get(0).getName()); |
| 50 | + assertEquals("Jane", artists.get(1).getName()); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void getTwoLists() { |
| 56 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 57 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 58 | + List<List<?>> lists = mapper.selectTwoLists(); |
| 59 | + @SuppressWarnings("unchecked") |
| 60 | + List<Movie> movies = (List<Movie>) lists.get(0); |
| 61 | + assertEquals(3, movies.size()); |
| 62 | + assertEquals("Movie1", movies.get(0).getName()); |
| 63 | + assertEquals("Movie2", movies.get(1).getName()); |
| 64 | + assertEquals("Movie3", movies.get(2).getName()); |
| 65 | + @SuppressWarnings("unchecked") |
| 66 | + List<Artist> artists = (List<Artist>) lists.get(1); |
| 67 | + assertEquals(2, artists.size()); |
| 68 | + assertEquals("John", artists.get(0).getName()); |
| 69 | + assertEquals("Jane", artists.get(1).getName()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments