|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2024 the original author or authors. |
| 2 | + * Copyright 2002-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.jdbc.core.simple;
|
18 | 18 |
|
19 |
| -import java.lang.reflect.Field; |
20 | 19 | import java.sql.CallableStatement;
|
21 | 20 | import java.sql.Connection;
|
22 | 21 | import java.sql.DatabaseMetaData;
|
23 | 22 | import java.sql.ResultSet;
|
24 | 23 | import java.sql.SQLException;
|
25 | 24 | import java.sql.Types;
|
26 |
| -import java.util.List; |
27 |
| -import java.util.Map; |
28 | 25 |
|
29 | 26 | import javax.sql.DataSource;
|
30 | 27 |
|
| 28 | +import org.assertj.core.api.InstanceOfAssertFactories; |
31 | 29 | import org.junit.jupiter.api.BeforeEach;
|
32 | 30 | import org.junit.jupiter.api.Test;
|
33 | 31 |
|
|
40 | 38 |
|
41 | 39 | import static org.assertj.core.api.Assertions.assertThat;
|
42 | 40 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 41 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
43 | 42 | import static org.mockito.BDDMockito.given;
|
44 | 43 | import static org.mockito.Mockito.atLeastOnce;
|
45 | 44 | import static org.mockito.Mockito.mock;
|
@@ -364,78 +363,37 @@ void correctSybaseFunctionStatementNamed() throws Exception {
|
364 | 363 | verifyStatement(adder, "{call ADD_INVOICE(@AMOUNT = ?, @CUSTID = ?)}");
|
365 | 364 | }
|
366 | 365 |
|
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 | 366 | @Test
|
390 |
| - void verifyWhenCompiledThenDeclareParameterIsIgnored() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { |
| 367 | + void declareParametersCannotBeInvokedWhenCompiled() { |
391 | 368 | SimpleJdbcCall call = new SimpleJdbcCall(dataSource)
|
392 | 369 | .withProcedureName("procedure_name")
|
393 | 370 | .declareParameters(new SqlParameter("PARAM", Types.VARCHAR));
|
394 | 371 | 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")); |
| 372 | + assertThatIllegalStateException() |
| 373 | + .isThrownBy(() -> call.declareParameters(new SqlParameter("Ignored Param", Types.VARCHAR))) |
| 374 | + .withMessage("SqlCall for procedure is already compiled"); |
402 | 375 | }
|
403 |
| - |
404 |
| - /** |
405 |
| - * When adding a declared row mapper, this verifies that the declaredRowMappers |
406 |
| - * gets the new mapper |
407 |
| - */ |
408 |
| - @SuppressWarnings("unchecked") |
| 376 | + |
409 | 377 | @Test
|
410 |
| - void verifyUncompiledDeclareRowMapperIsAdded() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { |
| 378 | + void addDeclaredRowMapperCanBeConfigured() { |
411 | 379 | SimpleJdbcCall call = new SimpleJdbcCall(dataSource)
|
412 | 380 | .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")); |
| 381 | + .returningResultSet("result_set", (rs, i) -> new Object()); |
| 382 | + |
| 383 | + assertThat(call).extracting("declaredRowMappers") |
| 384 | + .asInstanceOf(InstanceOfAssertFactories.map(String.class, RowMapper.class)) |
| 385 | + .containsOnlyKeys("result_set"); |
419 | 386 | }
|
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") |
| 387 | + |
426 | 388 | @Test
|
427 |
| - void verifyWhenCompiledThenDeclareRowMapperIsIgnored() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { |
| 389 | + void addDeclaredRowMapperWhenCompiled() { |
428 | 390 | SimpleJdbcCall call = new SimpleJdbcCall(dataSource)
|
429 | 391 | .withProcedureName("procedure_name")
|
430 |
| - .returningResultSet("result_set", (rs,i) -> new Object()); |
| 392 | + .returningResultSet("result_set", (rs, i) -> new Object()); |
431 | 393 | 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")); |
| 394 | + assertThatIllegalStateException() |
| 395 | + .isThrownBy(() -> call.returningResultSet("not added", (rs, i) -> new Object())) |
| 396 | + .withMessage("SqlCall for procedure is already compiled"); |
439 | 397 | }
|
440 |
| - |
| 398 | + |
441 | 399 | }
|
0 commit comments