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