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.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNull;
20  import static org.mockito.Mockito.*;
21  
22  import java.sql.SQLException;
23  
24  import org.apache.ibatis.executor.result.ResultMapException;
25  import org.apache.ibatis.session.Configuration;
26  import org.junit.jupiter.api.Assertions;
27  import org.junit.jupiter.api.Test;
28  
29  class UnknownTypeHandlerTest extends BaseTypeHandlerTest {
30  
31    private static final TypeHandler<Object> TYPE_HANDLER = spy(new UnknownTypeHandler(new Configuration()));
32  
33    @Override
34    @Test
35    public void shouldSetParameter() throws Exception {
36      TYPE_HANDLER.setParameter(ps, 1, "Hello", null);
37      verify(ps).setString(1, "Hello");
38    }
39  
40    @Override
41    @Test
42    public void shouldGetResultFromResultSetByName() throws Exception {
43      when(rs.getMetaData()).thenReturn(rsmd);
44      when(rsmd.getColumnCount()).thenReturn(1);
45      when(rsmd.getColumnLabel(1)).thenReturn("column");
46      when(rsmd.getColumnClassName(1)).thenReturn(String.class.getName());
47      when(rsmd.getColumnType(1)).thenReturn(JdbcType.VARCHAR.TYPE_CODE);
48      when(rs.getString("column")).thenReturn("Hello");
49      assertEquals("Hello", TYPE_HANDLER.getResult(rs, "column"));
50    }
51  
52    @Override
53    public void shouldGetResultNullFromResultSetByName() throws Exception {
54      // Unnecessary
55    }
56  
57    @Override
58    @Test
59    public void shouldGetResultFromResultSetByPosition() throws Exception {
60      when(rs.getMetaData()).thenReturn(rsmd);
61      when(rsmd.getColumnClassName(1)).thenReturn(String.class.getName());
62      when(rsmd.getColumnType(1)).thenReturn(JdbcType.VARCHAR.TYPE_CODE);
63      when(rs.getString(1)).thenReturn("Hello");
64      assertEquals("Hello", TYPE_HANDLER.getResult(rs, 1));
65    }
66  
67    @Override
68    public void shouldGetResultNullFromResultSetByPosition() throws Exception {
69      // Unnecessary
70    }
71  
72    @Override
73    @Test
74    public void shouldGetResultFromCallableStatement() throws Exception {
75      when(cs.getObject(1)).thenReturn("Hello");
76      assertEquals("Hello", TYPE_HANDLER.getResult(cs, 1));
77    }
78  
79    @Override
80    @Test
81    public void shouldGetResultNullFromCallableStatement() throws Exception {
82      when(cs.getObject(1)).thenReturn(null);
83      assertNull(TYPE_HANDLER.getResult(cs, 1));
84    }
85  
86    @Test
87    void setParameterWithNullParameter() throws Exception {
88      TYPE_HANDLER.setParameter(ps, 0, null, JdbcType.INTEGER);
89      verify(ps).setNull(0, JdbcType.INTEGER.TYPE_CODE);
90    }
91  
92    @Test
93    void setParameterWithNullParameterThrowsException() throws SQLException {
94      doThrow(new SQLException("invalid column")).when(ps).setNull(1, JdbcType.INTEGER.TYPE_CODE);
95      try {
96        TYPE_HANDLER.setParameter(ps, 1, null, JdbcType.INTEGER);
97        Assertions.fail("Should have thrown a TypeException");
98      } catch (Exception e) {
99        Assertions.assertTrue(e instanceof TypeException, "Expected TypedException");
100       Assertions.assertTrue(e.getMessage().contains("parameter #1"), "Parameter index is in exception");
101     }
102   }
103 
104   @Test
105   void setParameterWithNonNullParameterThrowsException() throws SQLException {
106     doThrow(new SQLException("invalid column")).when((UnknownTypeHandler) TYPE_HANDLER).setNonNullParameter(ps, 1, 99,
107         JdbcType.INTEGER);
108     try {
109       TYPE_HANDLER.setParameter(ps, 1, 99, JdbcType.INTEGER);
110       Assertions.fail("Should have thrown a TypeException");
111     } catch (Exception e) {
112       Assertions.assertTrue(e instanceof TypeException, "Expected TypedException");
113       Assertions.assertTrue(e.getMessage().contains("parameter #1"), "Parameter index is in exception");
114     }
115   }
116 
117   @Test
118   void getResultWithResultSetAndColumnNameThrowsException() throws SQLException {
119     doThrow(new SQLException("invalid column")).when((UnknownTypeHandler) TYPE_HANDLER).getNullableResult(rs, "foo");
120     try {
121       TYPE_HANDLER.getResult(rs, "foo");
122       Assertions.fail("Should have thrown a ResultMapException");
123     } catch (Exception e) {
124       Assertions.assertTrue(e instanceof ResultMapException, "Expected ResultMapException");
125       Assertions.assertTrue(e.getMessage().contains("column 'foo'"), "column name is not in exception");
126     }
127   }
128 
129   @Test
130   void getResultWithResultSetAndColumnIndexThrowsException() throws SQLException {
131     doThrow(new SQLException("invalid column")).when((UnknownTypeHandler) TYPE_HANDLER).getNullableResult(rs, 1);
132     try {
133       TYPE_HANDLER.getResult(rs, 1);
134       Assertions.fail("Should have thrown a ResultMapException");
135     } catch (Exception e) {
136       Assertions.assertTrue(e instanceof ResultMapException, "Expected ResultMapException");
137       Assertions.assertTrue(e.getMessage().contains("column #1"), "column index is not in exception");
138     }
139   }
140 
141   @Test
142   void getResultWithCallableStatementAndColumnIndexThrowsException() throws SQLException {
143     doThrow(new SQLException("invalid column")).when((UnknownTypeHandler) TYPE_HANDLER).getNullableResult(cs, 1);
144     try {
145       TYPE_HANDLER.getResult(cs, 1);
146       Assertions.fail("Should have thrown a ResultMapException");
147     } catch (Exception e) {
148       Assertions.assertTrue(e instanceof ResultMapException, "Expected ResultMapException");
149       Assertions.assertTrue(e.getMessage().contains("column #1"), "column index is not in exception");
150     }
151   }
152 
153 }