View Javadoc
1   /*
2    *    Copyright 2009-2023 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.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      // create a SqlSessionFactory
37      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/record_type/mybatis-config.xml")) {
38        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39      }
40      // populate in-memory database
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  }