|
16 | 16 |
|
17 | 17 | package org.springframework.jdbc.core.simple;
|
18 | 18 |
|
| 19 | +import java.lang.reflect.Field; |
19 | 20 | import java.sql.CallableStatement;
|
20 | 21 | import java.sql.Connection;
|
21 | 22 | import java.sql.DatabaseMetaData;
|
22 | 23 | import java.sql.ResultSet;
|
23 | 24 | import java.sql.SQLException;
|
24 | 25 | import java.sql.Types;
|
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
25 | 28 |
|
26 | 29 | import javax.sql.DataSource;
|
27 | 30 |
|
|
30 | 33 |
|
31 | 34 | import org.springframework.dao.InvalidDataAccessApiUsageException;
|
32 | 35 | import org.springframework.jdbc.BadSqlGrammarException;
|
| 36 | +import org.springframework.jdbc.core.RowMapper; |
33 | 37 | import org.springframework.jdbc.core.SqlOutParameter;
|
34 | 38 | import org.springframework.jdbc.core.SqlParameter;
|
35 | 39 | import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
@@ -360,4 +364,78 @@ void correctSybaseFunctionStatementNamed() throws Exception {
|
360 | 364 | verifyStatement(adder, "{call ADD_INVOICE(@AMOUNT = ?, @CUSTID = ?)}");
|
361 | 365 | }
|
362 | 366 |
|
| 367 | + /** |
| 368 | + * This test verifies that when declaring a parameter for a SimpleJdbcCall, |
| 369 | + * then the parameter is added as expected. |
| 370 | + */ |
| 371 | + @SuppressWarnings("unchecked") |
| 372 | + @Test |
| 373 | + void verifyUncompiledDeclareParameterIsAdded() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { |
| 374 | + SimpleJdbcCall call = new SimpleJdbcCall(dataSource) |
| 375 | + .withProcedureName("procedure_name") |
| 376 | + .declareParameters(new SqlParameter("PARAM", Types.VARCHAR)); |
| 377 | + |
| 378 | + Field params = AbstractJdbcCall.class.getDeclaredField("declaredParameters"); |
| 379 | + params.setAccessible(true); |
| 380 | + List<SqlParameter> paramList = (List<SqlParameter>) params.get(call); |
| 381 | + assertThat(paramList).hasSize(1).allMatch(sqlParam -> sqlParam.getName().equals("PARAM")); |
| 382 | + } |
| 383 | + |
| 384 | + /** |
| 385 | + * This verifies that once the SimpleJdbcCall is compiled, then adding |
| 386 | + * a parameter is ignored |
| 387 | + */ |
| 388 | + @SuppressWarnings("unchecked") |
| 389 | + @Test |
| 390 | + void verifyWhenCompiledThenDeclareParameterIsIgnored() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { |
| 391 | + SimpleJdbcCall call = new SimpleJdbcCall(dataSource) |
| 392 | + .withProcedureName("procedure_name") |
| 393 | + .declareParameters(new SqlParameter("PARAM", Types.VARCHAR)); |
| 394 | + call.compile(); |
| 395 | + |
| 396 | + call.declareParameters(new SqlParameter("Ignored Param", Types.VARCHAR)); |
| 397 | + |
| 398 | + Field params = AbstractJdbcCall.class.getDeclaredField("declaredParameters"); |
| 399 | + params.setAccessible(true); |
| 400 | + List<SqlParameter> paramList = (List<SqlParameter>) params.get(call); |
| 401 | + assertThat(paramList).hasSize(1).allMatch(sqlParam -> sqlParam.getName().equals("PARAM")); |
| 402 | + } |
| 403 | + |
| 404 | + /** |
| 405 | + * When adding a declared row mapper, this verifies that the declaredRowMappers |
| 406 | + * gets the new mapper |
| 407 | + */ |
| 408 | + @SuppressWarnings("unchecked") |
| 409 | + @Test |
| 410 | + void verifyUncompiledDeclareRowMapperIsAdded() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { |
| 411 | + SimpleJdbcCall call = new SimpleJdbcCall(dataSource) |
| 412 | + .withProcedureName("procedure_name") |
| 413 | + .returningResultSet("result_set", (rs,i) -> new Object()); |
| 414 | + |
| 415 | + Field rowMappers = AbstractJdbcCall.class.getDeclaredField("declaredRowMappers"); |
| 416 | + rowMappers.setAccessible(true); |
| 417 | + Map<String, RowMapper<?>> mappers = (Map<String, RowMapper<?>>) rowMappers.get(call); |
| 418 | + assertThat(mappers).hasSize(1).allSatisfy((key,value) -> key.equals("result_set")); |
| 419 | + } |
| 420 | + |
| 421 | + /** |
| 422 | + * This verifies that when adding a row mapper after the call is compiled |
| 423 | + * then the request is ignored |
| 424 | + */ |
| 425 | + @SuppressWarnings("unchecked") |
| 426 | + @Test |
| 427 | + void verifyWhenCompiledThenDeclareRowMapperIsIgnored() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { |
| 428 | + SimpleJdbcCall call = new SimpleJdbcCall(dataSource) |
| 429 | + .withProcedureName("procedure_name") |
| 430 | + .returningResultSet("result_set", (rs,i) -> new Object()); |
| 431 | + call.compile(); |
| 432 | + |
| 433 | + call.returningResultSet("not added", (rs,i) -> new Object()); |
| 434 | + |
| 435 | + Field rowMappers = AbstractJdbcCall.class.getDeclaredField("declaredRowMappers"); |
| 436 | + rowMappers.setAccessible(true); |
| 437 | + Map<String, RowMapper<?>> mappers = (Map<String, RowMapper<?>>) rowMappers.get(call); |
| 438 | + assertThat(mappers).hasSize(1).allSatisfy((key,value) -> key.equals("result_set")); |
| 439 | + } |
| 440 | + |
363 | 441 | }
|
0 commit comments