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