1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.scripting.defaults;
17
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.ArgumentMatchers.anyInt;
20 import static org.mockito.Mockito.doThrow;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23
24 import java.sql.PreparedStatement;
25 import java.sql.SQLException;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
30
31 import org.apache.ibatis.builder.StaticSqlSource;
32 import org.apache.ibatis.mapping.*;
33 import org.apache.ibatis.session.Configuration;
34 import org.apache.ibatis.type.JdbcType;
35 import org.apache.ibatis.type.TypeException;
36 import org.apache.ibatis.type.TypeHandler;
37 import org.apache.ibatis.type.TypeHandlerRegistry;
38 import org.junit.jupiter.api.Assertions;
39 import org.junit.jupiter.api.Test;
40
41
42
43
44
45
46 class DefaultParameterHandlerTest {
47
48 @Test
49 void setParametersThrowsProperException() throws SQLException {
50 final MappedStatement mappedStatement = getMappedStatement();
51 final Object parameterObject = null;
52 final BoundSql boundSql = mock(BoundSql.class);
53
54 TypeHandler<Object> typeHandler = mock(TypeHandler.class);
55 doThrow(new SQLException("foo")).when(typeHandler).setParameter(any(PreparedStatement.class), anyInt(), any(), any(JdbcType.class));
56 ParameterMapping parameterMapping = new ParameterMapping.Builder(mappedStatement.getConfiguration(), "prop", typeHandler).build();
57 List<ParameterMapping> parameterMappings = Collections.singletonList(parameterMapping);
58 when(boundSql.getParameterMappings()).thenReturn(parameterMappings);
59
60 DefaultParameterHandler defaultParameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
61
62 PreparedStatement ps = mock(PreparedStatement.class);
63 try {
64 defaultParameterHandler.setParameters(ps);
65 Assertions.fail("Should have thrown TypeException");
66 } catch (Exception e) {
67 Assertions.assertTrue(e instanceof TypeException, "expected TypeException");
68 Assertions.assertTrue(e.getMessage().contains("mapping: ParameterMapping"));
69 }
70
71 }
72
73 MappedStatement getMappedStatement() {
74 final Configuration config = new Configuration();
75 final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
76 return new MappedStatement.Builder(config, "testSelect", new StaticSqlSource(config, "some select statement"), SqlCommandType.SELECT).resultMaps(
77 new ArrayList<ResultMap>() {
78 {
79 add(new ResultMap.Builder(config, "testMap", HashMap.class, new ArrayList<ResultMapping>() {
80 {
81 add(new ResultMapping.Builder(config, "cOlUmN1", "CoLuMn1", registry.getTypeHandler(Integer.class)).build());
82 }
83 }).build());
84 }
85 }).build();
86 }
87
88 }