View Javadoc
1   /*
2    *    Copyright 2009-2022 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    *       https://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_type_handler;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNull;
20  
21  import java.io.Reader;
22  
23  import org.apache.ibatis.BaseDataTest;
24  import org.apache.ibatis.io.Resources;
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.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  class ArrayTypeHandlerTest {
32  
33    private SqlSessionFactory sqlSessionFactory;
34  
35    @BeforeEach
36    void setUp() throws Exception {
37      try (Reader reader = Resources
38          .getResourceAsReader("org/apache/ibatis/submitted/array_type_handler/mybatis-config.xml")) {
39        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40      }
41  
42      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
43          "org/apache/ibatis/submitted/array_type_handler/CreateDB.sql");
44    }
45  
46    @Test
47    void shouldInsertArrayValue() throws Exception {
48      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
49        User user = new User();
50        user.setId(1);
51        user.setName("User 1");
52        user.setNicknames(new String[] { "User", "one" });
53  
54        Mapper mapper = sqlSession.getMapper(Mapper.class);
55        mapper.insert(user);
56        sqlSession.commit();
57  
58        int usersInDatabase = mapper.getUserCount();
59        assertEquals(1, usersInDatabase);
60  
61        Integer nicknameCount = mapper.getNicknameCount();
62        assertEquals(2, nicknameCount);
63      }
64    }
65  
66    @Test
67    void shouldInsertNullValue() throws Exception {
68      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
69        User user = new User();
70        user.setId(1);
71        user.setName("User 1");
72        // note how the user does not have nicknames
73  
74        Mapper mapper = sqlSession.getMapper(Mapper.class);
75        mapper.insert(user);
76        sqlSession.commit();
77  
78        int usersInDatabase = mapper.getUserCount();
79        assertEquals(1, usersInDatabase);
80  
81        Integer nicknameCount = mapper.getNicknameCount();
82        assertNull(nicknameCount);
83      }
84    }
85  }