scribbling
mybatis 날 코딩할 때 참조할 소스
'김용환'
2012. 8. 16. 20:42
http://www.mybatis.org/core/ja/xref-test/org/apache/ibatis/executor/BaseExecutorTest.html
spring 없이 mybatis 만 이용해서 객체를 만들어서 사용할 경우 참조할 만한 소스
1 /* 2 * Copyright 2009-2011 The MyBatis Team 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.executor; 17 18 import domain.blog.Author; 19 import domain.blog.Blog; 20 import domain.blog.Post; 21 import domain.blog.Section; 22 import org.apache.ibatis.BaseDataTest; 23 import org.apache.ibatis.logging.jdbc.ConnectionLogger; 24 import org.apache.ibatis.mapping.MappedStatement; 25 import org.apache.ibatis.session.Configuration; 26 import org.apache.ibatis.session.RowBounds; 27 import org.apache.ibatis.transaction.Transaction; 28 import org.apache.ibatis.transaction.jdbc.JdbcTransaction; 29 import static org.junit.Assert.*; 30 import org.junit.Test; 31 32 import javax.sql.DataSource; 33 import java.sql.Connection; 34 import java.util.HashMap; 35 import java.util.List; 36 import java.util.Map; 37 38 public class BaseExecutorTest extends BaseDataTest { 39 protected final Configuration config; 40 41 public BaseExecutorTest() { 42 config = new Configuration(); 43 config.setLazyLoadingEnabled(true); 44 config.setUseGeneratedKeys(false); 45 config.setMultipleResultSetsEnabled(true); 46 config.setUseColumnLabel(true); 47 config.setDefaultStatementTimeout(5000); 48 } 49 50 @Test 51 public void shouldInsertNewAuthorWithBeforeAutoKey() throws Exception { 52 DataSource ds = createBlogDataSource(); 53 Connection connection = ds.getConnection(); 54 Executor executor = createExecutor(new JdbcTransaction(connection)); 55 try { 56 Author author = new Author(-1, "someone", "******", "someone@apache.org", null, Section.NEWS); 57 MappedStatement insertStatement = ExecutorTestHelper.prepareInsertAuthorMappedStatementWithBeforeAutoKey(config); 58 MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config); 59 int rows = executor.update(insertStatement, author); 60 assertTrue(rows > 0 || rows == BatchExecutor.BATCH_UPDATE_RETURN_VALUE); 61 if (rows == BatchExecutor.BATCH_UPDATE_RETURN_VALUE) { 62 executor.flushStatements(); 63 } 64 assertEquals(123456, author.getId()); 65 if (author.getId() != BatchExecutor.BATCH_UPDATE_RETURN_VALUE) { 66 List<Author> authors = executor.query(selectStatement, author.getId(), RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 67 executor.rollback(true); 68 assertEquals(1, authors.size()); 69 assertEquals(author.toString(), authors.get(0).toString()); 70 assertTrue(author.getId() >= 10000); 71 } 72 } finally { 73 executor.rollback(true); 74 executor.close(false); 75 } 76 } 77 78 @Test 79 public void shouldInsertNewAuthor() throws Exception { 80 DataSource ds = createBlogDataSource(); 81 Connection connection = ds.getConnection(); 82 Executor executor = createExecutor(new JdbcTransaction(connection)); 83 try { 84 Author author = new Author(99, "someone", "******", "someone@apache.org", null, Section.NEWS); 85 MappedStatement insertStatement = ExecutorTestHelper.prepareInsertAuthorMappedStatement(config); 86 MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config); 87 int rows = executor.update(insertStatement, author); 88 List<Author> authors = executor.query(selectStatement, 99, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 89 executor.flushStatements(); 90 executor.rollback(true); 91 assertEquals(1, authors.size()); 92 assertEquals(author.toString(), authors.get(0).toString()); 93 assertTrue(1 == rows || BatchExecutor.BATCH_UPDATE_RETURN_VALUE == rows); 94 } finally { 95 executor.rollback(true); 96 executor.close(false); 97 } 98 } 99 100 @Test 101 public void shouldSelectAllAuthorsAutoMapped() throws Exception { 102 DataSource ds = createBlogDataSource(); 103 Connection connection = ds.getConnection(); 104 Executor executor = createExecutor(new JdbcTransaction(connection)); 105 try { 106 MappedStatement selectStatement = ExecutorTestHelper.prepareSelectAllAuthorsAutoMappedStatement(config); 107 List<Author> authors = executor.query(selectStatement, null, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 108 assertEquals(2, authors.size()); 109 Author author = authors.get(0); 110 // id,username, password, email, bio, favourite_section 111 // (101,'jim','********','jim@ibatis.apache.org','','NEWS'); 112 assertEquals(101, author.getId()); 113 assertEquals("jim", author.getUsername()); 114 assertEquals("jim@ibatis.apache.org", author.getEmail()); 115 assertEquals("", author.getBio()); 116 assertEquals(Section.NEWS, author.getFavouriteSection()); 117 } finally { 118 executor.rollback(true); 119 executor.close(false); 120 } 121 } 122 123 @Test 124 public void shouldInsertNewAuthorWithAutoKey() throws Exception { 125 DataSource ds = createBlogDataSource(); 126 Connection connection = ds.getConnection(); 127 Executor executor = createExecutor(new JdbcTransaction(connection)); 128 try { 129 Author author = new Author(-1, "someone", "******", "someone@apache.org", null, Section.NEWS); 130 MappedStatement insertStatement = ExecutorTestHelper.prepareInsertAuthorMappedStatementWithAutoKey(config); 131 MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config); 132 int rows = executor.update(insertStatement, author); 133 assertTrue(rows > 0 || rows == BatchExecutor.BATCH_UPDATE_RETURN_VALUE); 134 if (rows == BatchExecutor.BATCH_UPDATE_RETURN_VALUE) { 135 executor.flushStatements(); 136 } 137 assertTrue(-1 != author.getId()); 138 if (author.getId() != BatchExecutor.BATCH_UPDATE_RETURN_VALUE) { 139 List<Author> authors = executor.query(selectStatement, author.getId(), RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 140 executor.rollback(true); 141 assertEquals(1, authors.size()); 142 assertEquals(author.toString(), authors.get(0).toString()); 143 assertTrue(author.getId() >= 10000); 144 } 145 } finally { 146 executor.rollback(true); 147 executor.close(false); 148 } 149 } 150 151 @Test 152 public void shouldInsertNewAuthorByProc() throws Exception { 153 DataSource ds = createBlogDataSource(); 154 Connection connection = ds.getConnection(); 155 Executor executor = createExecutor(new JdbcTransaction(connection)); 156 try { 157 Author author = new Author(97, "someone", "******", "someone@apache.org", null, null); 158 MappedStatement insertStatement = ExecutorTestHelper.prepareInsertAuthorProc(config); 159 MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config); 160 int rows = executor.update(insertStatement, author); 161 List<Author> authors = executor.query(selectStatement, 97, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 162 executor.flushStatements(); 163 executor.rollback(true); 164 assertEquals(1, authors.size()); 165 assertEquals(author.toString(), authors.get(0).toString()); 166 } finally { 167 executor.rollback(true); 168 executor.close(false); 169 } 170 } 171 172 @Test 173 public void shouldInsertNewAuthorUsingSimpleNonPreparedStatements() throws Exception { 174 DataSource ds = createBlogDataSource(); 175 Connection connection = ds.getConnection(); 176 Executor executor = createExecutor(new JdbcTransaction(connection)); 177 try { 178 Author author = new Author(99, "someone", "******", "someone@apache.org", null, null); 179 MappedStatement insertStatement = ExecutorTestHelper.createInsertAuthorWithIDof99MappedStatement(config); 180 MappedStatement selectStatement = ExecutorTestHelper.createSelectAuthorWithIDof99MappedStatement(config); 181 int rows = executor.update(insertStatement, null); 182 List<Author> authors = executor.query(selectStatement, 99, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 183 executor.flushStatements(); 184 executor.rollback(true); 185 assertEquals(1, authors.size()); 186 assertEquals(author.toString(), authors.get(0).toString()); 187 assertTrue(1 == rows || BatchExecutor.BATCH_UPDATE_RETURN_VALUE == rows); 188 } finally { 189 executor.rollback(true); 190 executor.close(false); 191 } 192 } 193 194 @Test 195 public void shouldUpdateAuthor() throws Exception { 196 DataSource ds = createBlogDataSource(); 197 Connection connection = ds.getConnection(); 198 Executor executor = createExecutor(new JdbcTransaction(connection)); 199 try { 200 Author author = new Author(101, "someone", "******", "someone@apache.org", null, Section.NEWS); 201 MappedStatement updateStatement = ExecutorTestHelper.prepareUpdateAuthorMappedStatement(config); 202 MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config); 203 int rows = executor.update(updateStatement, author); 204 List<Author> authors = executor.query(selectStatement, 101, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 205 executor.flushStatements(); 206 executor.rollback(true); 207 assertEquals(1, authors.size()); 208 assertEquals(author.toString(), authors.get(0).toString()); 209 assertTrue(1 == rows || BatchExecutor.BATCH_UPDATE_RETURN_VALUE == rows); 210 } finally { 211 executor.rollback(true); 212 executor.close(false); 213 } 214 } 215 216 @Test 217 public void shouldDeleteAuthor() throws Exception { 218 DataSource ds = createBlogDataSource(); 219 Connection connection = ds.getConnection(); 220 Executor executor = createExecutor(new JdbcTransaction(connection)); 221 try { 222 Author author = new Author(101, null, null, null, null, null); 223 MappedStatement deleteStatement = ExecutorTestHelper.prepareDeleteAuthorMappedStatement(config); 224 MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatement(config); 225 int rows = executor.update(deleteStatement, author); 226 List<Author> authors = executor.query(selectStatement, 101, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 227 executor.flushStatements(); 228 executor.rollback(true); 229 assertEquals(0, authors.size()); 230 assertTrue(1 == rows || BatchExecutor.BATCH_UPDATE_RETURN_VALUE == rows); 231 } finally { 232 executor.rollback(true); 233 executor.close(false); 234 } 235 } 236 237 @Test 238 public void shouldSelectDiscriminatedProduct() throws Exception { 239 DataSource ds = createJPetstoreDataSource(); 240 Connection connection = ds.getConnection(); 241 Executor executor = createExecutor(new JdbcTransaction(connection)); 242 try { 243 MappedStatement selectStatement = ExecutorTestHelper.prepareSelectDiscriminatedProduct(config); 244 List<Map> products = executor.query(selectStatement, null, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 245 connection.rollback(); 246 assertEquals(16, products.size()); 247 for (Map m : products) { 248 if ("REPTILES".equals(m.get("category"))) { 249 assertNull(m.get("name")); 250 } else { 251 assertNotNull(m.get("name")); 252 } 253 } 254 } finally { 255 executor.rollback(true); 256 executor.close(false); 257 } 258 } 259 260 @Test 261 public void shouldSelect10DiscriminatedProducts() throws Exception { 262 DataSource ds = createJPetstoreDataSource(); 263 Connection connection = ds.getConnection(); 264 Executor executor = createExecutor(new JdbcTransaction(connection)); 265 try { 266 MappedStatement selectStatement = ExecutorTestHelper.prepareSelectDiscriminatedProduct(config); 267 List<Map> products = executor.query(selectStatement, null, new RowBounds(4, 10), Executor.NO_RESULT_HANDLER); 268 connection.rollback(); 269 assertEquals(10, products.size()); 270 for (Map m : products) { 271 if ("REPTILES".equals(m.get("category"))) { 272 assertNull(m.get("name")); 273 } else { 274 assertNotNull(m.get("name")); 275 } 276 } 277 } finally { 278 executor.rollback(true); 279 executor.close(false); 280 } 281 282 } 283 284 @Test 285 public void shouldSelectTwoSetsOfAuthorsViaProc() throws Exception { 286 DataSource ds = createBlogDataSource(); 287 Connection connection = ds.getConnection(); 288 connection.setAutoCommit(false); 289 Executor executor = createExecutor(new JdbcTransaction(connection)); 290 try { 291 MappedStatement selectStatement = ExecutorTestHelper.prepareSelectTwoSetsOfAuthorsProc(config); 292 List<List> authorSets = executor.query(selectStatement, new HashMap() { 293 { 294 put("id1", 101); 295 put("id2", 102); 296 } 297 }, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 298 connection.rollback(); 299 assertEquals(2, authorSets.size()); 300 for (List authors : authorSets) { 301 assertEquals(2, authors.size()); 302 for (Object author : authors) { 303 assertTrue(author instanceof Author); 304 } 305 } 306 } finally { 307 executor.rollback(true); 308 executor.close(false); 309 } 310 } 311 312 @Test 313 public void shouldSelectAuthorViaOutParams() throws Exception { 314 DataSource ds = createBlogDataSource(); 315 Connection connection = ds.getConnection(); 316 connection.setAutoCommit(false); 317 Executor executor = createExecutor(new JdbcTransaction(connection)); 318 try { 319 MappedStatement selectStatement = ExecutorTestHelper.prepareSelectAuthorViaOutParams(config); 320 Author author = new Author(102, null, null, null, null, null); 321 executor.query(selectStatement, author, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 322 connection.rollback(); 323 324 assertEquals("sally", author.getUsername()); 325 assertEquals("********", author.getPassword()); 326 assertEquals("sally@ibatis.apache.org", author.getEmail()); 327 assertEquals(null, author.getBio()); 328 } catch (ExecutorException e) { 329 if (executor instanceof CachingExecutor) { 330 // TODO see issue #464. Fail is OK. 331 assertTrue(e.getMessage().contains("OUT params is not supported")); 332 } else { 333 throw e; 334 } 335 } finally { 336 executor.rollback(true); 337 executor.close(false); 338 } 339 } 340 341 @Test 342 public void shouldFetchPostsForBlog() throws Exception { 343 DataSource ds = createBlogDataSource(); 344 Connection connection = ds.getConnection(); 345 // connection = ConnectionLogger.newInstance(connection); 346 Executor executor = createExecutor(new JdbcTransaction(connection)); 347 try { 348 MappedStatement selectBlog = ExecutorTestHelper.prepareComplexSelectBlogMappedStatement(config); 349 MappedStatement selectPosts = ExecutorTestHelper.prepareSelectPostsForBlogMappedStatement(config); 350 config.addMappedStatement(selectBlog); 351 config.addMappedStatement(selectPosts); 352 List<Post> posts = executor.query(selectPosts, 1, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 353 executor.flushStatements(); 354 assertEquals(2, posts.size()); 355 assertNotNull(posts.get(1).getBlog()); 356 assertEquals(1, posts.get(1).getBlog().getId()); 357 executor.rollback(true); 358 } finally { 359 executor.rollback(true); 360 executor.close(false); 361 } 362 } 363 364 @Test 365 public void shouldFetchOneOrphanedPostWithNoBlog() throws Exception { 366 DataSource ds = createBlogDataSource(); 367 Connection connection = ds.getConnection(); 368 Executor executor = createExecutor(new JdbcTransaction(connection)); 369 try { 370 MappedStatement selectBlog = ExecutorTestHelper.prepareComplexSelectBlogMappedStatement(config); 371 MappedStatement selectPost = ExecutorTestHelper.prepareSelectPostMappedStatement(config); 372 config.addMappedStatement(selectBlog); 373 config.addMappedStatement(selectPost); 374 List<Post> posts = executor.query(selectPost, 5, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 375 executor.flushStatements(); 376 executor.rollback(true); 377 assertEquals(1, posts.size()); 378 Post post = posts.get(0); 379 assertNull(post.getBlog()); 380 } finally { 381 executor.rollback(true); 382 executor.close(false); 383 } 384 } 385 386 @Test 387 public void shouldFetchPostWithBlogWithCompositeKey() throws Exception { 388 DataSource ds = createBlogDataSource(); 389 Connection connection = ds.getConnection(); 390 Executor executor = createExecutor(new JdbcTransaction(connection)); 391 try { 392 MappedStatement selectBlog = ExecutorTestHelper.prepareSelectBlogByIdAndAuthor(config); 393 MappedStatement selectPost = ExecutorTestHelper.prepareSelectPostWithBlogByAuthorMappedStatement(config); 394 config.addMappedStatement(selectBlog); 395 config.addMappedStatement(selectPost); 396 List<Post> posts = executor.query(selectPost, 2, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 397 executor.flushStatements(); 398 assertEquals(1, posts.size()); 399 Post post = posts.get(0); 400 assertNotNull(post.getBlog()); 401 assertEquals(101, post.getBlog().getAuthor().getId()); 402 executor.rollback(true); 403 } finally { 404 executor.rollback(true); 405 executor.close(false); 406 } 407 } 408 409 410 @Test 411 public void shouldFetchComplexBlogs() throws Exception { 412 DataSource ds = createBlogDataSource(); 413 Connection connection = ds.getConnection(); 414 Executor executor = createExecutor(new JdbcTransaction(connection)); 415 try { 416 MappedStatement selectBlog = ExecutorTestHelper.prepareComplexSelectBlogMappedStatement(config); 417 MappedStatement selectPosts = ExecutorTestHelper.prepareSelectPostsForBlogMappedStatement(config); 418 config.addMappedStatement(selectBlog); 419 config.addMappedStatement(selectPosts); 420 config.setLazyLoadingEnabled(true); 421 List<Blog> blogs = executor.query(selectBlog, 1, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 422 executor.flushStatements(); 423 assertEquals(1, blogs.size()); 424 assertNotNull(blogs.get(0).getPosts()); 425 assertEquals(2, blogs.get(0).getPosts().size()); 426 assertEquals(1, blogs.get(0).getPosts().get(1).getBlog().getPosts().get(1).getBlog().getId()); 427 executor.rollback(true); 428 } finally { 429 config.setLazyLoadingEnabled(true); 430 executor.rollback(true); 431 executor.close(false); 432 } 433 } 434 435 @Test 436 public void shouldMapConstructorResults() throws Exception { 437 DataSource ds = createBlogDataSource(); 438 Connection connection = ds.getConnection(); 439 Executor executor = createExecutor(new JdbcTransaction(connection)); 440 try { 441 MappedStatement selectStatement = ExecutorTestHelper.prepareSelectOneAuthorMappedStatementWithConstructorResults(config); 442 List<Author> authors = executor.query(selectStatement, 102, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER); 443 executor.flushStatements(); 444 executor.rollback(true); 445 assertEquals(1, authors.size()); 446 447 Author author = authors.get(0); 448 assertEquals(102, author.getId()); 449 } finally { 450 executor.rollback(true); 451 executor.close(false); 452 } 453 } 454 455 protected Executor createExecutor(Transaction transaction) { 456 return new SimpleExecutor(config,transaction); 457 } 458 459 } This page was automatically generated by Maven |