View Javadoc
1   /*
2    *    Copyright 2009-2024 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.overwritingproperties;
17  
18  import java.sql.Connection;
19  
20  import org.apache.ibatis.BaseDataTest;
21  import org.apache.ibatis.io.Resources;
22  import org.apache.ibatis.session.SqlSession;
23  import org.apache.ibatis.session.SqlSessionFactory;
24  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
25  import org.junit.jupiter.api.AfterAll;
26  import org.junit.jupiter.api.Assertions;
27  import org.junit.jupiter.api.BeforeAll;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  /*
32   * @author jjensen
33   */
34  class FooMapperTest {
35  
36    private static final String SQL_MAP_CONFIG = "org/apache/ibatis/submitted/overwritingproperties/sqlmap.xml";
37    private static SqlSession session;
38    private static Connection conn;
39  
40    @BeforeAll
41    static void setUpBeforeClass() throws Exception {
42      final SqlSessionFactory factory = new SqlSessionFactoryBuilder()
43          .build(Resources.getResourceAsReader(SQL_MAP_CONFIG));
44      session = factory.openSession();
45      conn = session.getConnection();
46  
47      BaseDataTest.runScript(factory.getConfiguration().getEnvironment().getDataSource(),
48          "org/apache/ibatis/submitted/overwritingproperties/create-schema-mysql.sql");
49    }
50  
51    @BeforeEach
52    void setUp() {
53      final FooMapper mapper = session.getMapper(FooMapper.class);
54      mapper.deleteAllFoo();
55      session.commit();
56    }
57  
58    @Test
59    void overwriteWithDefault() {
60      final FooMapper mapper = session.getMapper(FooMapper.class);
61      final Bar bar = new Bar(2L);
62      final Foo inserted = new Foo(1L, bar, 3, 4);
63      mapper.insertFoo(inserted);
64  
65      final Foo selected = mapper.selectFoo();
66  
67      // field1 is explicitly mapped properly
68      // <result property="field1" column="field1" jdbcType="INTEGER"/>
69      Assertions.assertEquals(inserted.getField1(), selected.getField1());
70  
71      // field4 is not mapped in the result map
72      // <result property="field4" column="field3" jdbcType="INTEGER"/>
73      Assertions.assertEquals(inserted.getField3(), selected.getField4());
74  
75      // field4 is explicitly remapped to field3 in the resultmap
76      // <result property="field3" column="field4" jdbcType="INTEGER"/>
77      Assertions.assertEquals(inserted.getField4(), selected.getField3());
78  
79      // is automapped from the only column that matches... which is Field1
80      // probably not the intention, but it's working correctly given the code
81      // <association property="field2" javaType="Bar">
82      // <result property="field1" column="bar_field1" jdbcType="INTEGER"/>
83      // </association>
84      Assertions.assertEquals(inserted.getField2().getField1(), selected.getField2().getField1());
85    }
86  
87    @AfterAll
88    static void tearDownAfterClass() {
89      Assertions.assertDoesNotThrow(() -> {
90        conn.close();
91      });
92      session.close();
93    }
94  
95  }