|
| 1 | +/** |
| 2 | + * Copyright 2009-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.apache.ibatis.submitted.array_result_type; |
| 17 | + |
| 18 | +import static org.junit.Assert.*; |
| 19 | + |
| 20 | +import java.io.Reader; |
| 21 | +import java.sql.Connection; |
| 22 | + |
| 23 | +import org.apache.ibatis.io.Resources; |
| 24 | +import org.apache.ibatis.jdbc.ScriptRunner; |
| 25 | +import org.apache.ibatis.session.SqlSession; |
| 26 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 27 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 28 | +import org.junit.BeforeClass; |
| 29 | +import org.junit.Ignore; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +public class ArrayResultTypeTest { |
| 33 | + |
| 34 | + private static SqlSessionFactory sqlSessionFactory; |
| 35 | + |
| 36 | + @BeforeClass |
| 37 | + public static void setUp() throws Exception { |
| 38 | + // create an SqlSessionFactory |
| 39 | + Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/array_result_type/mybatis-config.xml"); |
| 40 | + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); |
| 41 | + reader.close(); |
| 42 | + |
| 43 | + // populate in-memory database |
| 44 | + SqlSession session = sqlSessionFactory.openSession(); |
| 45 | + Connection conn = session.getConnection(); |
| 46 | + reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/array_result_type/CreateDB.sql"); |
| 47 | + ScriptRunner runner = new ScriptRunner(conn); |
| 48 | + runner.setLogWriter(null); |
| 49 | + runner.runScript(reader); |
| 50 | + reader.close(); |
| 51 | + session.close(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void shouldGetUserArray() { |
| 56 | + SqlSession sqlSession = sqlSessionFactory.openSession(); |
| 57 | + try { |
| 58 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 59 | + User[] users = mapper.getUsers(); |
| 60 | + assertEquals("User1", users[0].getName()); |
| 61 | + assertEquals("User2", users[1].getName()); |
| 62 | + } finally { |
| 63 | + sqlSession.close(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void shouldGetUserArrayXml() { |
| 69 | + SqlSession sqlSession = sqlSessionFactory.openSession(); |
| 70 | + try { |
| 71 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 72 | + User[] users = mapper.getUsersXml(); |
| 73 | + assertEquals("User1", users[0].getName()); |
| 74 | + assertEquals("User2", users[1].getName()); |
| 75 | + } finally { |
| 76 | + sqlSession.close(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void shouldGetSimpleTypeArray() { |
| 82 | + SqlSession sqlSession = sqlSessionFactory.openSession(); |
| 83 | + try { |
| 84 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 85 | + Integer[] ids = mapper.getUserIds(); |
| 86 | + assertEquals(Integer.valueOf(1), ids[0]); |
| 87 | + } finally { |
| 88 | + sqlSession.close(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Ignore("primitive array return type is not supported. #555") |
| 93 | + @Test |
| 94 | + public void shouldGetPrimitiveArray() { |
| 95 | + SqlSession sqlSession = sqlSessionFactory.openSession(); |
| 96 | + try { |
| 97 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 98 | + int[] ids = mapper.getUserIdsPrimitive(); |
| 99 | + assertEquals(1, ids[0]); |
| 100 | + } finally { |
| 101 | + sqlSession.close(); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments