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