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.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNull;
20  import static org.mockito.Mockito.verify;
21  import static org.mockito.Mockito.when;
22  
23  import java.io.Reader;
24  import java.sql.Clob;
25  
26  import org.junit.jupiter.api.Test;
27  import org.mockito.ArgumentMatchers;
28  import org.mockito.Mock;
29  
30  class ClobTypeHandlerTest extends BaseTypeHandlerTest {
31  
32    private static final TypeHandler<String> TYPE_HANDLER = new ClobTypeHandler();
33  
34    @Mock
35    protected Clob clob;
36  
37    @Override
38    @Test
39    public void shouldSetParameter() throws Exception {
40      TYPE_HANDLER.setParameter(ps, 1, "Hello", null);
41      verify(ps).setCharacterStream(ArgumentMatchers.eq(1), ArgumentMatchers.any(Reader.class), ArgumentMatchers.eq(5));
42    }
43  
44    @Override
45    @Test
46    public void shouldGetResultFromResultSetByName() throws Exception {
47      when(rs.getClob("column")).thenReturn(clob);
48      when(clob.length()).thenReturn(3L);
49      when(clob.getSubString(1, 3)).thenReturn("Hello");
50      assertEquals("Hello", TYPE_HANDLER.getResult(rs, "column"));
51    }
52  
53    @Override
54    @Test
55    public void shouldGetResultNullFromResultSetByName() throws Exception {
56      when(rs.getClob("column")).thenReturn(null);
57      assertNull(TYPE_HANDLER.getResult(rs, "column"));
58    }
59  
60    @Override
61    @Test
62    public void shouldGetResultFromResultSetByPosition() throws Exception {
63      when(rs.getClob(1)).thenReturn(clob);
64      when(clob.length()).thenReturn(3L);
65      when(clob.getSubString(1, 3)).thenReturn("Hello");
66      assertEquals("Hello", TYPE_HANDLER.getResult(rs, 1));
67    }
68  
69    @Override
70    @Test
71    public void shouldGetResultNullFromResultSetByPosition() throws Exception {
72      when(rs.getClob(1)).thenReturn(null);
73      assertNull(TYPE_HANDLER.getResult(rs, 1));
74    }
75  
76    @Override
77    @Test
78    public void shouldGetResultFromCallableStatement() throws Exception {
79      when(cs.getClob(1)).thenReturn(clob);
80      when(clob.length()).thenReturn(3L);
81      when(clob.getSubString(1, 3)).thenReturn("Hello");
82      assertEquals("Hello", TYPE_HANDLER.getResult(cs, 1));
83    }
84  
85    @Override
86    @Test
87    public void shouldGetResultNullFromCallableStatement() throws Exception {
88      when(cs.getClob(1)).thenReturn(null);
89      assertNull(TYPE_HANDLER.getResult(cs, 1));
90    }
91  
92  }