Merge pull request #32164 from quaff

* pr/32164:
  Polish contribution
  Polish StatementCreatorUtilsTests

Closes gh-32164
This commit is contained in:
Stéphane Nicoll 2024-01-31 08:57:52 +01:00
commit a4db0e7448
1 changed files with 26 additions and 24 deletions

View File

@ -45,6 +45,8 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link StatementCreatorUtils}.
*
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 31.08.2004 * @since 31.08.2004
*/ */
@ -55,7 +57,7 @@ class StatementCreatorUtilsTests {
@Test @Test
void testSetParameterValueWithNullAndType() throws SQLException { void testSetParameterValueWithNullAndType() throws SQLException {
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.VARCHAR, null, null); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.VARCHAR, null);
verify(preparedStatement).setNull(1, Types.VARCHAR); verify(preparedStatement).setNull(1, Types.VARCHAR);
} }
@ -74,7 +76,7 @@ class StatementCreatorUtilsTests {
given(dbmd.getDatabaseProductName()).willReturn("Oracle"); given(dbmd.getDatabaseProductName()).willReturn("Oracle");
given(dbmd.getDriverName()).willReturn("Oracle Driver"); given(dbmd.getDriverName()).willReturn("Oracle Driver");
given(con.getMetaData()).willReturn(dbmd); given(con.getMetaData()).willReturn(dbmd);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, null); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null);
verify(preparedStatement).setNull(1, Types.NULL); verify(preparedStatement).setNull(1, Types.NULL);
StatementCreatorUtils.shouldIgnoreGetParameterType = false; StatementCreatorUtils.shouldIgnoreGetParameterType = false;
} }
@ -88,7 +90,7 @@ class StatementCreatorUtilsTests {
given(con.getMetaData()).willReturn(dbmd); given(con.getMetaData()).willReturn(dbmd);
given(dbmd.getDatabaseProductName()).willReturn("Informix Dynamic Server"); given(dbmd.getDatabaseProductName()).willReturn("Informix Dynamic Server");
given(dbmd.getDriverName()).willReturn("Informix Driver"); given(dbmd.getDriverName()).willReturn("Informix Driver");
StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, null); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null);
verify(dbmd).getDatabaseProductName(); verify(dbmd).getDatabaseProductName();
verify(dbmd).getDriverName(); verify(dbmd).getDriverName();
verify(preparedStatement).setObject(1, null); verify(preparedStatement).setObject(1, null);
@ -104,7 +106,7 @@ class StatementCreatorUtilsTests {
given(con.getMetaData()).willReturn(dbmd); given(con.getMetaData()).willReturn(dbmd);
given(dbmd.getDatabaseProductName()).willReturn("Apache Derby"); given(dbmd.getDatabaseProductName()).willReturn("Apache Derby");
given(dbmd.getDriverName()).willReturn("Apache Derby Embedded Driver"); given(dbmd.getDriverName()).willReturn("Apache Derby Embedded Driver");
StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, null); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null);
verify(dbmd).getDatabaseProductName(); verify(dbmd).getDatabaseProductName();
verify(dbmd).getDriverName(); verify(dbmd).getDriverName();
verify(preparedStatement).setNull(1, Types.VARCHAR); verify(preparedStatement).setNull(1, Types.VARCHAR);
@ -116,7 +118,7 @@ class StatementCreatorUtilsTests {
ParameterMetaData pmd = mock(); ParameterMetaData pmd = mock();
given(preparedStatement.getParameterMetaData()).willReturn(pmd); given(preparedStatement.getParameterMetaData()).willReturn(pmd);
given(pmd.getParameterType(1)).willReturn(Types.SMALLINT); given(pmd.getParameterType(1)).willReturn(Types.SMALLINT);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, null); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null);
verify(pmd).getParameterType(1); verify(pmd).getParameterType(1);
verify(preparedStatement, never()).getConnection(); verify(preparedStatement, never()).getConnection();
verify(preparedStatement).setNull(1, Types.SMALLINT); verify(preparedStatement).setNull(1, Types.SMALLINT);
@ -124,102 +126,102 @@ class StatementCreatorUtilsTests {
@Test @Test
void testSetParameterValueWithString() throws SQLException { void testSetParameterValueWithString() throws SQLException {
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.VARCHAR, null, "test"); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.VARCHAR, "test");
verify(preparedStatement).setString(1, "test"); verify(preparedStatement).setString(1, "test");
} }
@Test @Test
void testSetParameterValueWithStringAndSpecialType() throws SQLException { void testSetParameterValueWithStringAndSpecialType() throws SQLException {
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.CHAR, null, "test"); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.CHAR, "test");
verify(preparedStatement).setObject(1, "test", Types.CHAR); verify(preparedStatement).setObject(1, "test", Types.CHAR);
} }
@Test public void testSetParameterValueWithStringAndUnknownType() throws SQLException { @Test public void testSetParameterValueWithStringAndUnknownType() throws SQLException {
StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, "test"); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, "test");
verify(preparedStatement).setString(1, "test"); verify(preparedStatement).setString(1, "test");
} }
@Test @Test
void testSetParameterValueWithSqlDate() throws SQLException { void testSetParameterValueWithSqlDate() throws SQLException {
java.sql.Date date = new java.sql.Date(1000); java.sql.Date date = new java.sql.Date(1000);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.DATE, null, date); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.DATE, date);
verify(preparedStatement).setDate(1, date); verify(preparedStatement).setDate(1, date);
} }
@Test @Test
void testSetParameterValueWithDateAndUtilDate() throws SQLException { void testSetParameterValueWithDateAndUtilDate() throws SQLException {
java.util.Date date = new java.util.Date(1000); java.util.Date date = new java.util.Date(1000);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.DATE, null, date); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.DATE, date);
verify(preparedStatement).setDate(1, new java.sql.Date(1000)); verify(preparedStatement).setDate(1, new java.sql.Date(1000));
} }
@Test @Test
void testSetParameterValueWithDateAndCalendar() throws SQLException { void testSetParameterValueWithDateAndCalendar() throws SQLException {
java.util.Calendar cal = new GregorianCalendar(); java.util.Calendar cal = new GregorianCalendar();
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.DATE, null, cal); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.DATE, cal);
verify(preparedStatement).setDate(1, new java.sql.Date(cal.getTime().getTime()), cal); verify(preparedStatement).setDate(1, new java.sql.Date(cal.getTime().getTime()), cal);
} }
@Test @Test
void testSetParameterValueWithSqlTime() throws SQLException { void testSetParameterValueWithSqlTime() throws SQLException {
java.sql.Time time = new java.sql.Time(1000); java.sql.Time time = new java.sql.Time(1000);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIME, null, time); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIME, time);
verify(preparedStatement).setTime(1, time); verify(preparedStatement).setTime(1, time);
} }
@Test @Test
void testSetParameterValueWithTimeAndUtilDate() throws SQLException { void testSetParameterValueWithTimeAndUtilDate() throws SQLException {
java.util.Date date = new java.util.Date(1000); java.util.Date date = new java.util.Date(1000);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIME, null, date); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIME, date);
verify(preparedStatement).setTime(1, new java.sql.Time(1000)); verify(preparedStatement).setTime(1, new java.sql.Time(1000));
} }
@Test @Test
void testSetParameterValueWithTimeAndCalendar() throws SQLException { void testSetParameterValueWithTimeAndCalendar() throws SQLException {
java.util.Calendar cal = new GregorianCalendar(); java.util.Calendar cal = new GregorianCalendar();
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIME, null, cal); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIME, cal);
verify(preparedStatement).setTime(1, new java.sql.Time(cal.getTime().getTime()), cal); verify(preparedStatement).setTime(1, new java.sql.Time(cal.getTime().getTime()), cal);
} }
@Test @Test
void testSetParameterValueWithSqlTimestamp() throws SQLException { void testSetParameterValueWithSqlTimestamp() throws SQLException {
java.sql.Timestamp timestamp = new java.sql.Timestamp(1000); java.sql.Timestamp timestamp = new java.sql.Timestamp(1000);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIMESTAMP, null, timestamp); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIMESTAMP, timestamp);
verify(preparedStatement).setTimestamp(1, timestamp); verify(preparedStatement).setTimestamp(1, timestamp);
} }
@Test @Test
void testSetParameterValueWithTimestampAndUtilDate() throws SQLException { void testSetParameterValueWithTimestampAndUtilDate() throws SQLException {
java.util.Date date = new java.util.Date(1000); java.util.Date date = new java.util.Date(1000);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIMESTAMP, null, date); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIMESTAMP, date);
verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(1000)); verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(1000));
} }
@Test @Test
void testSetParameterValueWithTimestampAndCalendar() throws SQLException { void testSetParameterValueWithTimestampAndCalendar() throws SQLException {
java.util.Calendar cal = new GregorianCalendar(); java.util.Calendar cal = new GregorianCalendar();
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIMESTAMP, null, cal); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIMESTAMP, cal);
verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(cal.getTime().getTime()), cal); verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(cal.getTime().getTime()), cal);
} }
@Test @Test
void testSetParameterValueWithDateAndUnknownType() throws SQLException { void testSetParameterValueWithDateAndUnknownType() throws SQLException {
java.util.Date date = new java.util.Date(1000); java.util.Date date = new java.util.Date(1000);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, date); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, date);
verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(1000)); verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(1000));
} }
@Test @Test
void testSetParameterValueWithCalendarAndUnknownType() throws SQLException { void testSetParameterValueWithCalendarAndUnknownType() throws SQLException {
java.util.Calendar cal = new GregorianCalendar(); java.util.Calendar cal = new GregorianCalendar();
StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, cal); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, cal);
verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(cal.getTime().getTime()), cal); verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(cal.getTime().getTime()), cal);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("javaTimeTypes") @MethodSource("javaTimeTypes")
public void testSetParameterValueWithJavaTimeTypes(Object o, int sqlType) throws SQLException { public void testSetParameterValueWithJavaTimeTypes(Object o, int sqlType) throws SQLException {
StatementCreatorUtils.setParameterValue(preparedStatement, 1, sqlType, null, o); StatementCreatorUtils.setParameterValue(preparedStatement, 1, sqlType, o);
verify(preparedStatement).setObject(1, o, sqlType); verify(preparedStatement).setObject(1, o, sqlType);
} }
@ -248,7 +250,7 @@ class StatementCreatorUtilsTests {
public void testSetParameterValueWithOffsetDateTimeAndNotSupported() throws SQLException { public void testSetParameterValueWithOffsetDateTimeAndNotSupported() throws SQLException {
OffsetDateTime time = OffsetDateTime.now(); OffsetDateTime time = OffsetDateTime.now();
doThrow(new SQLFeatureNotSupportedException()).when(preparedStatement).setObject(1, time, Types.TIMESTAMP_WITH_TIMEZONE); doThrow(new SQLFeatureNotSupportedException()).when(preparedStatement).setObject(1, time, Types.TIMESTAMP_WITH_TIMEZONE);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIMESTAMP_WITH_TIMEZONE, null, time); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIMESTAMP_WITH_TIMEZONE, time);
verify(preparedStatement).setObject(1, time, Types.TIMESTAMP_WITH_TIMEZONE); verify(preparedStatement).setObject(1, time, Types.TIMESTAMP_WITH_TIMEZONE);
verify(preparedStatement).setObject(1, time); verify(preparedStatement).setObject(1, time);
} }
@ -256,7 +258,7 @@ class StatementCreatorUtilsTests {
@Test // gh-30556 @Test // gh-30556
public void testSetParameterValueWithNullAndNotSupported() throws SQLException { public void testSetParameterValueWithNullAndNotSupported() throws SQLException {
doThrow(new SQLFeatureNotSupportedException()).when(preparedStatement).setNull(1, Types.TIMESTAMP_WITH_TIMEZONE); doThrow(new SQLFeatureNotSupportedException()).when(preparedStatement).setNull(1, Types.TIMESTAMP_WITH_TIMEZONE);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIMESTAMP_WITH_TIMEZONE, null, null); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.TIMESTAMP_WITH_TIMEZONE, null);
verify(preparedStatement).setNull(1, Types.TIMESTAMP_WITH_TIMEZONE); verify(preparedStatement).setNull(1, Types.TIMESTAMP_WITH_TIMEZONE);
verify(preparedStatement).setNull(1, Types.NULL); verify(preparedStatement).setNull(1, Types.NULL);
} }
@ -268,7 +270,7 @@ class StatementCreatorUtilsTests {
given(preparedStatement.getConnection()).willReturn(con); given(preparedStatement.getConnection()).willReturn(con);
given(dbmd.getDatabaseProductName()).willReturn("Oracle"); given(dbmd.getDatabaseProductName()).willReturn("Oracle");
given(con.getMetaData()).willReturn(dbmd); given(con.getMetaData()).willReturn(dbmd);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.OTHER, null, "test"); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.OTHER, "test");
verify(preparedStatement).setString(1, "test"); verify(preparedStatement).setString(1, "test");
} }
@ -281,7 +283,7 @@ class StatementCreatorUtilsTests {
given(dbmd.getDatabaseProductName()).willReturn("Oracle"); given(dbmd.getDatabaseProductName()).willReturn("Oracle");
given(dbmd.getDriverName()).willReturn("Oracle Driver"); given(dbmd.getDriverName()).willReturn("Oracle Driver");
given(con.getMetaData()).willReturn(dbmd); given(con.getMetaData()).willReturn(dbmd);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.OTHER, null, null); StatementCreatorUtils.setParameterValue(preparedStatement, 1, Types.OTHER, null);
verify(preparedStatement).setNull(1, Types.NULL); verify(preparedStatement).setNull(1, Types.NULL);
StatementCreatorUtils.shouldIgnoreGetParameterType = false; StatementCreatorUtils.shouldIgnoreGetParameterType = false;
} }