diff --git a/driver/src/main/java/com/consol/citrus/db/driver/JdbcPreparedStatement.java b/driver/src/main/java/com/consol/citrus/db/driver/JdbcPreparedStatement.java index 0312719..746ddc7 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/JdbcPreparedStatement.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/JdbcPreparedStatement.java @@ -389,7 +389,7 @@ void setParameter(final String parameterName, final Object value){ parameters.put(parameterName, value); } - private String composeStatement() { + String composeStatement() { return composeStatement(parameters); } diff --git a/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java index 760d991..b8aec88 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java @@ -27,6 +27,8 @@ import java.sql.Time; import java.sql.Timestamp; import java.sql.Types; +import java.util.Arrays; +import java.util.List; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; @@ -946,6 +948,82 @@ public void testSetBlobFromInputStream() throws Exception { assertEquals(storedClob, expectedBlob); } + @Test + public void testIndexedParametersAreOrderedCorrectly() throws SQLException { + + //GIVEN + final JdbcCallableStatement callableStatement = generateCallableStatement(); + callableStatement.registerOutParameter(3, Types.VARCHAR); + callableStatement.registerOutParameter(11, Types.VARCHAR); + final List expectedParameter = Arrays.asList("foo", "bar"); + + //WHEN + callableStatement.setString(3,"foo"); + callableStatement.setString(11, "bar"); + + //THEN + assertEquals(callableStatement.getParameters().values(), expectedParameter); + } + + @Test + public void testNamedParametersAreOrderedCorrectly() { + + //GIVEN + final String parameter1 = "z-param"; + final String parameter2 = "a-param"; + final JdbcCallableStatement callableStatement = + generateCallableStatementWithParameter(parameter1, parameter2); + callableStatement.registerOutParameter(parameter1, Types.VARCHAR); + callableStatement.registerOutParameter(parameter2, Types.VARCHAR); + final String expectedStatement = "CALL myFunction(z-param=>?,a-param=>?) - (foo, bar)"; + + //WHEN + callableStatement.setString(parameter1,"foo"); + callableStatement.setString(parameter2,"bar"); + + //THEN + assertEquals(callableStatement.composeStatement(), expectedStatement); + + } + + @Test + public void testParametersMixedParametersAreOrderedCorrectly() throws SQLException { + + //GIVEN + final String parameterName = "foo"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + callableStatement.registerOutParameter(2, Types.VARCHAR); + callableStatement.registerOutParameter(parameterName, Types.VARCHAR); + + final String expectedStatement = "CALL myFunction(foo=>?,?) - (foobar, bar)"; + + //WHEN + callableStatement.setParameter(2, "bar"); + callableStatement.setParameter(parameterName, "foobar"); + + //THEN + assertEquals(callableStatement.composeStatement(), expectedStatement); + } + + @Test + public void testAnonymousNamedParametersAreOrderedCorrectly() { + + //GIVEN + final String parameter1 = "z-param"; + final String parameter2 = "a-param"; + final JdbcCallableStatement callableStatement = generateCallableStatement(); + callableStatement.registerOutParameter(parameter1, Types.VARCHAR); + callableStatement.registerOutParameter(parameter2, Types.VARCHAR); + final String expectedStatement = "CALL myFunction(?,?) - (foo, bar)"; + + //WHEN + callableStatement.setString(parameter1,"foo"); + callableStatement.setString(parameter2, "bar"); + + //THEN + assertEquals(callableStatement.composeStatement(), expectedStatement); + } + @Test public void testToString(){ ToStringVerifier @@ -971,9 +1049,14 @@ private JdbcCallableStatement generateCallableStatement() { final String statement = "CALL myFunction(?,?)"; return new JdbcCallableStatement(httpClient, statement, serverUrl, jdbcConnection, lobUtils); } + private JdbcCallableStatement generateCallableStatementWithParameter(final String parameterName) { - final String statement = "CALL myFunction("+parameterName+",?)"; + final String statement = "CALL myFunction("+parameterName+"=>?,?)"; return new JdbcCallableStatement(httpClient,statement, serverUrl, jdbcConnection, lobUtils); } + private JdbcCallableStatement generateCallableStatementWithParameter(final String parameterName, final String parameterNameTwo) { + final String statement = "CALL myFunction("+parameterName+"=>?,"+parameterNameTwo+"=>?)"; + return new JdbcCallableStatement(httpClient,statement, serverUrl, jdbcConnection, lobUtils); + } } \ No newline at end of file diff --git a/driver/src/test/java/com/consol/citrus/db/driver/JdbcPreparedStatementTest.java b/driver/src/test/java/com/consol/citrus/db/driver/JdbcPreparedStatementTest.java index 68bcaec..b3e1ea4 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcPreparedStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcPreparedStatementTest.java @@ -18,6 +18,8 @@ import java.sql.Blob; import java.sql.Clob; import java.sql.SQLException; +import java.util.Arrays; +import java.util.List; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; @@ -246,6 +248,20 @@ public void setBlobFromInputStream() throws Exception { verify(jdbcPreparedStatement).setParameter(5, citrusBlobMock); } + @Test + public void testParametersAreOrderedCorrectly() { + + //GIVEN + final List expectedParameter = Arrays.asList("foo", "bar"); + + //WHEN + jdbcPreparedStatement.setString(3,"foo"); + jdbcPreparedStatement.setString(11, "bar"); + + //THEN + assertEquals(jdbcPreparedStatement.getParameters().values(), expectedParameter); + } + @Test public void testToString(){ ToStringVerifier