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.type;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  import static org.mockito.Mockito.verify;
22  import static org.mockito.Mockito.when;
23  
24  import java.io.*;
25  import java.sql.Clob;
26  
27  import javax.sql.DataSource;
28  
29  import org.apache.ibatis.BaseDataTest;
30  import org.apache.ibatis.annotations.Insert;
31  import org.apache.ibatis.annotations.Select;
32  import org.apache.ibatis.mapping.Environment;
33  import org.apache.ibatis.session.Configuration;
34  import org.apache.ibatis.session.SqlSession;
35  import org.apache.ibatis.session.SqlSessionFactory;
36  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
37  import org.apache.ibatis.transaction.TransactionFactory;
38  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
39  import org.junit.jupiter.api.BeforeAll;
40  import org.junit.jupiter.api.Test;
41  import org.mockito.Mock;
42  
43  /**
44   * Tests for {@link ClobReaderTypeHandler}.
45   *
46   * @since 3.4.0
47   * @author Kazuki Shimizu
48   */
49  class ClobReaderTypeHandlerTest extends BaseTypeHandlerTest {
50  
51    private static final TypeHandler<Reader> TYPE_HANDLER = new ClobReaderTypeHandler();
52  
53    private static SqlSessionFactory sqlSessionFactory;
54  
55    @Mock
56    protected Clob clob;
57  
58    @BeforeAll
59    static void setupSqlSessionFactory() throws Exception {
60      DataSource dataSource = BaseDataTest.createUnpooledDataSource("org/apache/ibatis/type/jdbc.properties");
61      TransactionFactory transactionFactory = new JdbcTransactionFactory();
62      Environment environment = new Environment("Production", transactionFactory, dataSource);
63      Configuration configuration = new Configuration(environment);
64      configuration.addMapper(Mapper.class);
65      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
66  
67      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
68          "org/apache/ibatis/type/ClobReaderTypeHandlerTest.sql");
69    }
70  
71    @Override
72    @Test
73    public void shouldSetParameter() throws Exception {
74      Reader reader = new StringReader("Hello");
75      TYPE_HANDLER.setParameter(ps, 1, reader, null);
76      verify(ps).setClob(1, reader);
77    }
78  
79    @Override
80    @Test
81    public void shouldGetResultFromResultSetByName() throws Exception {
82      Reader reader = new StringReader("Hello");
83      when(rs.getClob("column")).thenReturn(clob);
84      when(clob.getCharacterStream()).thenReturn(reader);
85      assertEquals(reader, TYPE_HANDLER.getResult(rs, "column"));
86    }
87  
88    @Override
89    @Test
90    public void shouldGetResultNullFromResultSetByName() throws Exception {
91      when(rs.getClob("column")).thenReturn(null);
92      assertNull(TYPE_HANDLER.getResult(rs, "column"));
93    }
94  
95    @Override
96    @Test
97    public void shouldGetResultFromResultSetByPosition() throws Exception {
98      when(rs.getClob(1)).thenReturn(clob);
99      assertNull(TYPE_HANDLER.getResult(rs, 1));
100   }
101 
102   @Override
103   @Test
104   public void shouldGetResultNullFromResultSetByPosition() throws Exception {
105     when(rs.getClob(1)).thenReturn(null);
106     assertNull(TYPE_HANDLER.getResult(rs, 1));
107   }
108 
109   @Override
110   @Test
111   public void shouldGetResultFromCallableStatement() throws Exception {
112     Reader reader = new StringReader("Hello");
113     when(cs.getClob(1)).thenReturn(clob);
114     when(clob.getCharacterStream()).thenReturn(reader);
115     assertEquals(reader, TYPE_HANDLER.getResult(cs, 1));
116   }
117 
118   @Override
119   @Test
120   public void shouldGetResultNullFromCallableStatement() throws Exception {
121     when(cs.getClob(1)).thenReturn(null);
122     assertNull(TYPE_HANDLER.getResult(cs, 1));
123   }
124 
125   @Test
126   void integrationTest() throws IOException {
127     try (SqlSession session = sqlSessionFactory.openSession()) {
128       Mapper mapper = session.getMapper(Mapper.class);
129       // insert (Reader -> Clob)
130       {
131         ClobContent clobContent = new ClobContent();
132         clobContent.setId(1);
133         clobContent.setContent(new StringReader("Hello"));
134         mapper.insert(clobContent);
135         session.commit();
136       }
137       // select (Clob -> Reader)
138       {
139         ClobContent clobContent = mapper.findOne(1);
140         assertThat(new BufferedReader(clobContent.getContent()).readLine()).isEqualTo("Hello");
141       }
142     }
143 
144   }
145 
146   interface Mapper {
147     @Select("SELECT ID, CONTENT FROM TEST_CLOB WHERE ID = #{id}")
148     ClobContent findOne(int id);
149 
150     @Insert("INSERT INTO TEST_CLOB (ID, CONTENT) VALUES(#{id}, #{content})")
151     void insert(ClobContent blobContent);
152   }
153 
154   static class ClobContent {
155     private int id;
156     private Reader content;
157 
158     public int getId() {
159       return id;
160     }
161 
162     public void setId(int id) {
163       this.id = id;
164     }
165 
166     public Reader getContent() {
167       return content;
168     }
169 
170     public void setContent(Reader content) {
171       this.content = content;
172     }
173   }
174 
175 }