View Javadoc
1   /*
2    *    Copyright 2009-2021 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.blobtest;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertTrue;
20  
21  import java.io.Reader;
22  import java.util.List;
23  
24  import org.apache.ibatis.BaseDataTest;
25  import org.apache.ibatis.io.Resources;
26  import org.apache.ibatis.session.SqlSession;
27  import org.apache.ibatis.session.SqlSessionFactory;
28  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29  import org.junit.jupiter.api.BeforeAll;
30  import org.junit.jupiter.api.Test;
31  
32  class BlobTest {
33      private static SqlSessionFactory sqlSessionFactory;
34  
35      @BeforeAll
36      static void initDatabase() throws Exception {
37          try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/blobtest/MapperConfig.xml")) {
38              sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39          }
40  
41          BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
42                  "org/apache/ibatis/submitted/blobtest/CreateDB.sql");
43      }
44  
45      @Test
46      /*
47       * This test demonstrates the use of type aliases for primitive types
48       * in constructor based result maps
49       */
50      void testInsertBlobThenSelectAll() {
51          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
52              BlobMapper blobMapper = sqlSession.getMapper(BlobMapper.class);
53  
54              byte[] myblob = new byte[] {1, 2, 3, 4, 5};
55              BlobRecord blobRecord = new BlobRecord(1, myblob);
56              int rows = blobMapper.insert(blobRecord);
57              assertEquals(1, rows);
58  
59              // NPE here due to unresolved type handler
60              List<BlobRecord> results = blobMapper.selectAll();
61  
62              assertEquals(1, results.size());
63              BlobRecord result = results.get(0);
64              assertEquals (blobRecord.getId(), result.getId());
65              assertTrue (blobsAreEqual(blobRecord.getBlob(), result.getBlob()));
66          }
67      }
68  
69      @Test
70      /*
71       * This test demonstrates the use of type aliases for primitive types
72       * in constructor based result maps
73       */
74      void testInsertBlobObjectsThenSelectAll() {
75          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
76              BlobMapper blobMapper = sqlSession.getMapper(BlobMapper.class);
77  
78              Byte[] myblob = new Byte[] {1, 2, 3, 4, 5};
79              BlobRecord blobRecord = new BlobRecord(1, myblob);
80              int rows = blobMapper.insert(blobRecord);
81              assertEquals(1, rows);
82  
83              // NPE here due to unresolved type handler
84              List<BlobRecord> results = blobMapper.selectAllWithBlobObjects();
85  
86              assertEquals(1, results.size());
87              BlobRecord result = results.get(0);
88              assertEquals (blobRecord.getId(), result.getId());
89              assertTrue (blobsAreEqual(blobRecord.getBlob(), result.getBlob()));
90          }
91      }
92  
93      static boolean blobsAreEqual(byte[] blob1, byte[] blob2) {
94          if (blob1 == null) {
95              return blob2 == null;
96          }
97  
98          if (blob2 == null) {
99              return blob1 == null;
100         }
101 
102         boolean rc = blob1.length == blob2.length;
103 
104         if (rc) {
105             for (int i = 0; i < blob1.length; i++) {
106                 if (blob1[i] != blob2[i]) {
107                     rc = false;
108                     break;
109                 }
110             }
111         }
112 
113         return rc;
114     }
115 }