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.bringrags;
17  
18  import java.io.Reader;
19  import java.io.StringReader;
20  import java.sql.Connection;
21  
22  import org.apache.ibatis.io.Resources;
23  import org.apache.ibatis.jdbc.ScriptRunner;
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.AfterEach;
28  import org.junit.jupiter.api.Assertions;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  class SimpleObjectTest {
33    private SimpleChildObjectMapper simpleChildObjectMapper;
34    private SqlSession sqlSession;
35    private Connection conn;
36  
37    @BeforeEach
38    void setUp() throws Exception {
39      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/bringrags/mybatis-config.xml")) {
40        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41  
42        sqlSession = sqlSessionFactory.openSession();
43        conn = sqlSession.getConnection();
44        ScriptRunner runner = new ScriptRunner(conn);
45        runner.setLogWriter(null);
46        runner.runScript(new StringReader("DROP TABLE IF EXISTS SimpleObject;"));
47        runner.runScript(new StringReader("DROP TABLE IF EXISTS SimpleChildObject;"));
48        runner.runScript(new StringReader("CREATE TABLE SimpleObject (id VARCHAR(5) NOT NULL);"));
49        runner.runScript(new StringReader(
50            "CREATE TABLE SimpleChildObject (id VARCHAR(5) NOT NULL, simple_object_id VARCHAR(5) NOT NULL);"));
51        runner.runScript(new StringReader("INSERT INTO SimpleObject (id) values ('10000');"));
52        runner.runScript(
53            new StringReader("INSERT INTO SimpleChildObject (id, simple_object_id) values ('20000', '10000');"));
54        simpleChildObjectMapper = sqlSession.getMapper(SimpleChildObjectMapper.class);
55      }
56    }
57  
58    @AfterEach
59    void tearDown() throws Exception {
60      conn.close();
61      sqlSession.close();
62    }
63  
64    @Test
65    void testGetById() {
66      SimpleChildObject sc = simpleChildObjectMapper.getSimpleChildObjectById("20000");
67      Assertions.assertNotNull(sc);
68      Assertions.assertNotNull(sc.getSimpleObject());
69    }
70  
71  }