1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }