1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.record_type;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19
20 import java.io.Reader;
21
22 import org.apache.ibatis.BaseDataTest;
23 import org.apache.ibatis.io.Resources;
24 import org.apache.ibatis.session.SqlSession;
25 import org.apache.ibatis.session.SqlSessionFactory;
26 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27 import org.junit.jupiter.api.BeforeAll;
28 import org.junit.jupiter.api.Test;
29
30 class RecordTypeTest {
31
32 private static SqlSessionFactory sqlSessionFactory;
33
34 @BeforeAll
35 static void setUp() throws Exception {
36
37 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/record_type/mybatis-config.xml")) {
38 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39 }
40
41 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
42 "org/apache/ibatis/submitted/record_type/CreateDB.sql");
43 }
44
45 @Test
46 void testSelectRecord() {
47 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
48 RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
49 Property prop = mapper.selectProperty(1);
50 assertEquals("Val1!", prop.value());
51 assertEquals("https://www.google.com", prop.URL());
52 }
53 }
54
55 @Test
56 void testSelectRecordAutomapping() {
57 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
58 RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
59 Property prop = mapper.selectPropertyAutomapping(1);
60 assertEquals("Val1!", prop.value());
61 assertEquals("https://www.google.com", prop.URL());
62 }
63 }
64
65 @Test
66 void testInsertRecord() {
67 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
68 RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
69 assertEquals(1, mapper.insertProperty(new Property(2, "Val2", "https://mybatis.org")));
70 sqlSession.commit();
71 }
72 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
73 RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
74 Property prop = mapper.selectProperty(2);
75 assertEquals("Val2!!", prop.value());
76 assertEquals("https://mybatis.org", prop.URL());
77 }
78 }
79
80 @Test
81 void testSelectNestedRecord() {
82 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
83 RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
84 Item item = mapper.selectItem(100);
85 assertEquals(Integer.valueOf(100), item.id());
86 assertEquals(new Property(1, "Val1", "https://www.google.com"), item.property());
87 }
88 }
89 }