1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.automapping;
17
18 import java.io.Reader;
19 import java.sql.Connection;
20 import java.util.List;
21
22 import org.apache.ibatis.io.Resources;
23 import org.apache.ibatis.jdbc.ScriptRunner;
24 import org.apache.ibatis.session.AutoMappingBehavior;
25 import org.apache.ibatis.session.SqlSession;
26 import org.apache.ibatis.session.SqlSessionFactory;
27 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28 import org.junit.Assert;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31
32 public class AutomappingTest {
33
34 private static SqlSessionFactory sqlSessionFactory;
35
36 @BeforeClass
37 public static void setUp() throws Exception {
38
39 Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/automapping/mybatis-config.xml");
40 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41 reader.close();
42
43
44 SqlSession session = sqlSessionFactory.openSession();
45 Connection conn = session.getConnection();
46 reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/automapping/CreateDB.sql");
47 ScriptRunner runner = new ScriptRunner(conn);
48 runner.setLogWriter(null);
49 runner.runScript(reader);
50 reader.close();
51 session.close();
52 }
53
54 @Test
55 public void shouldGetAUser() {
56 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
57 SqlSession sqlSession = sqlSessionFactory.openSession();
58 try {
59 Mapper mapper = sqlSession.getMapper(Mapper.class);
60 User user = mapper.getUser(1);
61 Assert.assertEquals("User1", user.getName());
62 } finally {
63 sqlSession.close();
64 }
65 }
66
67 @Test
68 public void shouldNotInheritAutoMappingInherited_InlineNestedResultMap() {
69 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
70 SqlSession sqlSession = sqlSessionFactory.openSession();
71 try {
72 Mapper mapper = sqlSession.getMapper(Mapper.class);
73 User user = mapper.getUserWithPets_Inline(2);
74 Assert.assertEquals(Integer.valueOf(2), user.getId());
75 Assert.assertEquals("User2", user.getName());
76 Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName());
77 Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
78 } finally {
79 sqlSession.close();
80 }
81 }
82
83 @Test
84 public void shouldNotInheritAutoMappingInherited_ExternalNestedResultMap() {
85 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
86 SqlSession sqlSession = sqlSessionFactory.openSession();
87 try {
88 Mapper mapper = sqlSession.getMapper(Mapper.class);
89 User user = mapper.getUserWithPets_External(2);
90 Assert.assertEquals(Integer.valueOf(2), user.getId());
91 Assert.assertEquals("User2", user.getName());
92 Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName());
93 Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
94 } finally {
95 sqlSession.close();
96 }
97 }
98
99 @Test
100 public void shouldIgnorePartialAutoMappingBehavior_InlineNestedResultMap() {
101
102 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
103 SqlSession sqlSession = sqlSessionFactory.openSession();
104 try {
105 Mapper mapper = sqlSession.getMapper(Mapper.class);
106 User user = mapper.getUserWithPets_Inline(2);
107 Assert.assertEquals(Integer.valueOf(2), user.getId());
108 Assert.assertEquals("User2", user.getName());
109 Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName());
110 Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
111 } finally {
112 sqlSession.close();
113 }
114 }
115
116 @Test
117 public void shouldRespectFullAutoMappingBehavior_InlineNestedResultMap() {
118 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.FULL);
119 SqlSession sqlSession = sqlSessionFactory.openSession();
120 try {
121 Mapper mapper = sqlSession.getMapper(Mapper.class);
122 User user = mapper.getUserWithPets_Inline(2);
123 Assert.assertEquals(Integer.valueOf(2), user.getId());
124 Assert.assertEquals("User2", user.getName());
125 Assert.assertEquals("Chien", user.getPets().get(0).getPetName());
126 Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
127 } finally {
128 sqlSession.close();
129 }
130 }
131
132 @Test
133 public void shouldIgnorePartialAutoMappingBehavior_ExternalNestedResultMap() {
134
135 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
136 SqlSession sqlSession = sqlSessionFactory.openSession();
137 try {
138 Mapper mapper = sqlSession.getMapper(Mapper.class);
139 User user = mapper.getUserWithPets_External(2);
140 Assert.assertEquals(Integer.valueOf(2), user.getId());
141 Assert.assertEquals("User2", user.getName());
142 Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName());
143 Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
144 } finally {
145 sqlSession.close();
146 }
147 }
148
149 @Test
150 public void shouldRespectFullAutoMappingBehavior_ExternalNestedResultMap() {
151 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.FULL);
152 SqlSession sqlSession = sqlSessionFactory.openSession();
153 try {
154 Mapper mapper = sqlSession.getMapper(Mapper.class);
155 User user = mapper.getUserWithPets_External(2);
156 Assert.assertEquals(Integer.valueOf(2), user.getId());
157 Assert.assertEquals("User2", user.getName());
158 Assert.assertEquals("Chien", user.getPets().get(0).getPetName());
159 Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
160 } finally {
161 sqlSession.close();
162 }
163 }
164
165 @Test
166 public void shouldGetBooks() {
167
168 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
169 SqlSession sqlSession = sqlSessionFactory.openSession();
170 try {
171 Mapper mapper = sqlSession.getMapper(Mapper.class);
172
173 List<Book> books = mapper.getBooks();
174 Assert.assertTrue("should return results,no errors throw", !books.isEmpty());
175 } finally {
176 sqlSession.close();
177 }
178 }
179
180 @Test
181 public void shouldUpdateFinalField() {
182
183 sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
184 SqlSession sqlSession = sqlSessionFactory.openSession();
185 try {
186 Mapper mapper = sqlSession.getMapper(Mapper.class);
187 Article article = mapper.getArticle();
188
189
190
191 Assert.assertTrue("should update version in mapping", article.version > 0);
192 } finally {
193 sqlSession.close();
194 }
195 }
196 }