View Javadoc
1   /*
2    *    Copyright 2009-2022 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  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   * @author jjensen
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      // field1 is explicitly mapped properly
69      // <result property="field1" column="field1" jdbcType="INTEGER"/>
70      Assertions.assertEquals(inserted.getField1(), selected.getField1());
71  
72      // field4 is not mapped in the result map
73      // <result property="field4" column="field3" jdbcType="INTEGER"/>
74      Assertions.assertEquals(inserted.getField3(), selected.getField4());
75  
76      // field4 is explicitly remapped to field3 in the resultmap
77      // <result property="field3" column="field4" jdbcType="INTEGER"/>
78      Assertions.assertEquals(inserted.getField4(), selected.getField3());
79  
80      // is automapped from the only column that matches... which is Field1
81      // probably not the intention, but it's working correctly given the code
82      // <association property="field2" javaType="Bar">
83      // <result property="field1" column="bar_field1" jdbcType="INTEGER"/>
84      // </association>
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  }