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