1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }