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.null_associations;
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  class FooMapperTest {
33  
34    private final static String SQL_MAP_CONFIG = "org/apache/ibatis/submitted/null_associations/sqlmap.xml";
35    private static SqlSession session;
36    private static Connection conn;
37  
38    @BeforeAll
39    static void setUpBeforeClass() throws Exception {
40      final SqlSessionFactory factory = new SqlSessionFactoryBuilder()
41          .build(Resources.getResourceAsReader(SQL_MAP_CONFIG));
42      session = factory.openSession();
43      conn = session.getConnection();
44  
45      BaseDataTest.runScript(factory.getConfiguration().getEnvironment().getDataSource(),
46          "org/apache/ibatis/submitted/null_associations/create-schema-mysql.sql");
47    }
48  
49    @BeforeEach
50    void setUp() {
51      final FooMapper mapper = session.getMapper(FooMapper.class);
52      mapper.deleteAllFoo();
53      session.commit();
54    }
55  
56    @Test
57    void testNullAssociation() {
58      final FooMapper mapper = session.getMapper(FooMapper.class);
59      final Foo foo = new Foo(1L, null, true);
60      mapper.insertFoo(foo);
61      session.commit();
62      final Foo read = mapper.selectFoo();
63      Assertions.assertEquals(1L, read.getField1(), "Invalid mapping");
64      Assertions.assertNull(read.getField2(), "Invalid mapping - field2 (Bar) should be null");
65      Assertions.assertTrue(read.isField3(), "Invalid mapping");
66    }
67  
68    @Test
69    void testNotNullAssociation() {
70      final FooMapper mapper = session.getMapper(FooMapper.class);
71      final Bar bar = new Bar(1L, 2L, 3L);
72      final Foo foo = new Foo(1L, bar, true);
73      mapper.insertFoo(foo);
74      session.commit();
75      final Foo read = mapper.selectFoo();
76      Assertions.assertEquals(1L, read.getField1(), "Invalid mapping");
77      Assertions.assertNotNull(read.getField2(), "Bar should be not null");
78      Assertions.assertTrue(read.isField3(), "Invalid mapping");
79      Assertions.assertEquals(1L, read.getField2().getField1(), "Invalid mapping");
80      Assertions.assertEquals(2L, read.getField2().getField2(), "Invalid mapping");
81      Assertions.assertEquals(3L, read.getField2().getField3(), "Invalid mapping");
82    }
83  
84    @AfterAll
85    static void tearDownAfterClass() throws SQLException {
86      conn.close();
87      session.close();
88    }
89  
90  }