1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.overwritingproperties;
17
18 import java.sql.Connection;
19 import java.sql.SQLException;
20
21 import org.apache.ibatis.BaseDataTest;
22 import org.apache.ibatis.io.Resources;
23 import org.apache.ibatis.session.SqlSession;
24 import org.apache.ibatis.session.SqlSessionFactory;
25 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
26 import org.junit.jupiter.api.AfterAll;
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31
32
33
34
35 class FooMapperTest {
36
37 private final static String SQL_MAP_CONFIG = "org/apache/ibatis/submitted/overwritingproperties/sqlmap.xml";
38 private static SqlSession session;
39 private static Connection conn;
40
41 @BeforeAll
42 static void setUpBeforeClass() throws Exception {
43 final SqlSessionFactory factory = new SqlSessionFactoryBuilder()
44 .build(Resources.getResourceAsReader(SQL_MAP_CONFIG));
45 session = factory.openSession();
46 conn = session.getConnection();
47
48 BaseDataTest.runScript(factory.getConfiguration().getEnvironment().getDataSource(),
49 "org/apache/ibatis/submitted/overwritingproperties/create-schema-mysql.sql");
50 }
51
52 @BeforeEach
53 void setUp() {
54 final FooMapper mapper = session.getMapper(FooMapper.class);
55 mapper.deleteAllFoo();
56 session.commit();
57 }
58
59 @Test
60 void testOverwriteWithDefault() {
61 final FooMapper mapper = session.getMapper(FooMapper.class);
62 final Bar bar = new Bar(2L);
63 final Foo inserted = new Foo(1L, bar, 3, 4);
64 mapper.insertFoo(inserted);
65
66 final Foo selected = mapper.selectFoo();
67
68
69
70 Assertions.assertEquals(inserted.getField1(), selected.getField1());
71
72
73
74 Assertions.assertEquals(inserted.getField3(), selected.getField4());
75
76
77
78 Assertions.assertEquals(inserted.getField4(), selected.getField3());
79
80
81
82
83
84
85 Assertions.assertEquals(inserted.getField2().getField1(), selected.getField2().getField1());
86 }
87
88 @AfterAll
89 static void tearDownAfterClass() {
90 try {
91 conn.close();
92 } catch (SQLException e) {
93 Assertions.fail(e.getMessage());
94 }
95 session.close();
96 }
97
98 }