Skip to content

Commit

Permalink
(#28) Added test for parameter ordering issues
Browse files Browse the repository at this point in the history
  • Loading branch information
svettwer committed Jun 13, 2019
1 parent bdc86d3 commit 6b1aca4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ void setParameter(final String parameterName, final Object value){
parameters.put(parameterName, value);
}

private String composeStatement() {
String composeStatement() {
return composeStatement(parameters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -246,6 +248,20 @@ public void setBlobFromInputStream() throws Exception {
verify(jdbcPreparedStatement).setParameter(5, citrusBlobMock);
}

@Test
public void testParametersAreOrderedCorrectly() {

//GIVEN
final List<String> 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
Expand Down

0 comments on commit 6b1aca4

Please sign in to comment.