added convenience execute method that takes vararg of objects in the order of the parameters plus the corresponding executeFunction/executeObject methods (SPR-4739)
This commit is contained in:
parent
7c053127dd
commit
fbd0be2d65
|
|
@ -540,6 +540,18 @@ public class CallMetaDataContext {
|
||||||
return matchedParameters;
|
return matchedParameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<String,?> matchInParameterValuesWithCallParameters(Object[] parameterValues) {
|
||||||
|
Map<String, Object> matchedParameters = new HashMap<String, Object>(parameterValues.length);
|
||||||
|
int i = 0;
|
||||||
|
for (SqlParameter parameter : this.callParameters) {
|
||||||
|
if (parameter.isInputValueProvided()) {
|
||||||
|
String parameterName = parameter.getName();
|
||||||
|
matchedParameters.put(parameterName, parameterValues[i++]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matchedParameters;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build the call string based on configuration and metadata information.
|
* Build the call string based on configuration and metadata information.
|
||||||
* @return the call string to be used
|
* @return the call string to be used
|
||||||
|
|
|
||||||
|
|
@ -341,6 +341,17 @@ public abstract class AbstractJdbcCall {
|
||||||
return executeCallInternal(params);
|
return executeCallInternal(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method that provides execution of the call using the passed in array of parameters
|
||||||
|
* @param args array of parameter values; order must match the order declared for the stored procedure
|
||||||
|
* @return Map of out parameters
|
||||||
|
*/
|
||||||
|
protected Map<String, Object> doExecute(Object[] args) {
|
||||||
|
checkCompiled();
|
||||||
|
Map<String, ?> params = matchInParameterValuesWithCallParameters(args);
|
||||||
|
return executeCallInternal(params);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method that provides execution of the call using the passed in Map of parameters
|
* Method that provides execution of the call using the passed in Map of parameters
|
||||||
* @param args Map of parameter name and values
|
* @param args Map of parameter name and values
|
||||||
|
|
@ -385,6 +396,16 @@ public abstract class AbstractJdbcCall {
|
||||||
return this.callMetaDataContext.matchInParameterValuesWithCallParameters(parameterSource);
|
return this.callMetaDataContext.matchInParameterValuesWithCallParameters(parameterSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Match the provided in parameter values with registered parameters and
|
||||||
|
* parameters defined via metadata processing.
|
||||||
|
* @param args the parameter values provided as an array
|
||||||
|
* @return Map with parameter names and values
|
||||||
|
*/
|
||||||
|
private Map<String,?> matchInParameterValuesWithCallParameters(Object[] args) {
|
||||||
|
return this.callMetaDataContext.matchInParameterValuesWithCallParameters(args);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Match the provided in parameter values with registered parameters and
|
* Match the provided in parameter values with registered parameters and
|
||||||
* parameters defined via metadata processing.
|
* parameters defined via metadata processing.
|
||||||
|
|
|
||||||
|
|
@ -131,6 +131,10 @@ public class SimpleJdbcCall extends AbstractJdbcCall implements SimpleJdbcCallOp
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> T executeFunction(Class<T> returnType, Object... args) {
|
||||||
|
return (T) doExecute(args).get(getScalarOutParameterName());
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> T executeFunction(Class<T> returnType, Map<String, Object> args) {
|
public <T> T executeFunction(Class<T> returnType, Map<String, Object> args) {
|
||||||
|
|
@ -142,6 +146,11 @@ public class SimpleJdbcCall extends AbstractJdbcCall implements SimpleJdbcCallOp
|
||||||
return (T) doExecute(args).get(getScalarOutParameterName());
|
return (T) doExecute(args).get(getScalarOutParameterName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> T executeObject(Class<T> returnType, Object... args) {
|
||||||
|
return (T) doExecute(args).get(getScalarOutParameterName());
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> T executeObject(Class<T> returnType, Map<String, Object> args) {
|
public <T> T executeObject(Class<T> returnType, Map<String, Object> args) {
|
||||||
return (T) doExecute(args).get(getScalarOutParameterName());
|
return (T) doExecute(args).get(getScalarOutParameterName());
|
||||||
|
|
@ -152,8 +161,8 @@ public class SimpleJdbcCall extends AbstractJdbcCall implements SimpleJdbcCallOp
|
||||||
return (T) doExecute(args).get(getScalarOutParameterName());
|
return (T) doExecute(args).get(getScalarOutParameterName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Object> execute() {
|
public Map<String, Object> execute(Object... args) {
|
||||||
return doExecute(new HashMap<String, Object>());
|
return doExecute(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Object> execute(Map<String, Object> args) {
|
public Map<String, Object> execute(Map<String, Object> args) {
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,14 @@ public interface SimpleJdbcCallOperations {
|
||||||
SimpleJdbcCallOperations withoutProcedureColumnMetaDataAccess();
|
SimpleJdbcCallOperations withoutProcedureColumnMetaDataAccess();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the stored function and return the results obtained as an Object of the specified return type.
|
||||||
|
* @param returnType the type of the value to return
|
||||||
|
* @param args optional array containing the in parameter values to be used in the call. Parameter values must
|
||||||
|
* be provided in the same order as the parameters are defined for the stored procedure.
|
||||||
|
*/
|
||||||
|
<T> T executeFunction(Class<T> returnType, Object... args);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the stored function and return the results obtained as an Object of the specified return type.
|
* Execute the stored function and return the results obtained as an Object of the specified return type.
|
||||||
* @param returnType the type of the value to return
|
* @param returnType the type of the value to return
|
||||||
|
|
@ -115,6 +123,16 @@ public interface SimpleJdbcCallOperations {
|
||||||
*/
|
*/
|
||||||
<T> T executeFunction(Class<T> returnType, SqlParameterSource args);
|
<T> T executeFunction(Class<T> returnType, SqlParameterSource args);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the stored procedure and return the single out parameter as an Object of the specified return type.
|
||||||
|
* In the case where there are multiple out parameters, the first one is returned and additional out parameters
|
||||||
|
* are ignored.
|
||||||
|
* @param returnType the type of the value to return
|
||||||
|
* @param args optional array containing the in parameter values to be used in the call. Parameter values must
|
||||||
|
* be provided in the same order as the parameters are defined for the stored procedure.
|
||||||
|
*/
|
||||||
|
<T> T executeObject(Class<T> returnType, Object... args);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the stored procedure and return the single out parameter as an Object of the specified return type.
|
* Execute the stored procedure and return the single out parameter as an Object of the specified return type.
|
||||||
* In the case where there are multiple out parameters, the first one is returned and additional out parameters
|
* In the case where there are multiple out parameters, the first one is returned and additional out parameters
|
||||||
|
|
@ -134,10 +152,12 @@ public interface SimpleJdbcCallOperations {
|
||||||
<T> T executeObject(Class<T> returnType, SqlParameterSource args);
|
<T> T executeObject(Class<T> returnType, SqlParameterSource args);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the stored procedure and return a map of output params, keyed by name as in parameter declarations..
|
* Execute the stored procedure and return a map of output params, keyed by name as in parameter declarations.
|
||||||
|
* @param args optional array containing the in parameter values to be used in the call. Parameter values must
|
||||||
|
* be provided in the same order as the parameters are defined for the stored procedure.
|
||||||
* @return map of output params.
|
* @return map of output params.
|
||||||
*/
|
*/
|
||||||
Map<String, Object> execute();
|
Map<String, Object> execute(Object... args);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the stored procedure and return a map of output params, keyed by name as in parameter declarations..
|
* Execute the stored procedure and return a map of output params, keyed by name as in parameter declarations..
|
||||||
|
|
|
||||||
|
|
@ -138,45 +138,11 @@ public class SimpleJdbcCallTests extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAddInvoiceProcWithoutMetaData() throws Exception {
|
public void testAddInvoiceProcWithoutMetaDataUsingMapParamSource() throws Exception {
|
||||||
final int amount = 1103;
|
final int amount = 1103;
|
||||||
final int custid = 3;
|
final int custid = 3;
|
||||||
|
|
||||||
mockDatabaseMetaData.getDatabaseProductName();
|
initializeAddInvoiceWithoutMetaData(false);
|
||||||
ctrlDatabaseMetaData.setReturnValue("MyDB");
|
|
||||||
mockDatabaseMetaData.getUserName();
|
|
||||||
ctrlDatabaseMetaData.setReturnValue("me");
|
|
||||||
mockDatabaseMetaData.supportsCatalogsInProcedureCalls();
|
|
||||||
ctrlDatabaseMetaData.setReturnValue(false);
|
|
||||||
mockDatabaseMetaData.supportsSchemasInProcedureCalls();
|
|
||||||
ctrlDatabaseMetaData.setReturnValue(false);
|
|
||||||
mockDatabaseMetaData.storesUpperCaseIdentifiers();
|
|
||||||
ctrlDatabaseMetaData.setReturnValue(false);
|
|
||||||
mockDatabaseMetaData.storesLowerCaseIdentifiers();
|
|
||||||
ctrlDatabaseMetaData.setReturnValue(true);
|
|
||||||
|
|
||||||
mockCallable.setObject(1, 1103, 4);
|
|
||||||
ctrlCallable.setVoidCallable();
|
|
||||||
mockCallable.setObject(2, 3, 4);
|
|
||||||
ctrlCallable.setVoidCallable();
|
|
||||||
mockCallable.registerOutParameter(3, 4);
|
|
||||||
ctrlCallable.setVoidCallable();
|
|
||||||
mockCallable.execute();
|
|
||||||
ctrlCallable.setReturnValue(false);
|
|
||||||
mockCallable.getUpdateCount();
|
|
||||||
ctrlCallable.setReturnValue(-1);
|
|
||||||
mockCallable.getObject(3);
|
|
||||||
ctrlCallable.setReturnValue(new Long(4));
|
|
||||||
if (debugEnabled) {
|
|
||||||
mockCallable.getWarnings();
|
|
||||||
ctrlCallable.setReturnValue(null);
|
|
||||||
}
|
|
||||||
mockCallable.close();
|
|
||||||
ctrlCallable.setVoidCallable();
|
|
||||||
|
|
||||||
mockConnection.prepareCall(
|
|
||||||
"{call add_invoice(?, ?, ?)}");
|
|
||||||
ctrlConnection.setReturnValue(mockCallable);
|
|
||||||
|
|
||||||
replay();
|
replay();
|
||||||
|
|
||||||
|
|
@ -190,105 +156,27 @@ public class SimpleJdbcCallTests extends TestCase {
|
||||||
assertEquals(4, newId.intValue());
|
assertEquals(4, newId.intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAddInvoiceProcWithMetaData() throws Exception {
|
public void testAddInvoiceProcWithoutMetaDataUsingArrayParams() throws Exception {
|
||||||
final int amount = 1103;
|
final int amount = 1103;
|
||||||
final int custid = 3;
|
final int custid = 3;
|
||||||
|
|
||||||
MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
|
initializeAddInvoiceWithoutMetaData(false);
|
||||||
ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
|
|
||||||
mockResultSet.next();
|
|
||||||
ctrlResultSet.setReturnValue(true);
|
|
||||||
mockResultSet.getString("PROCEDURE_CAT");
|
|
||||||
ctrlResultSet.setReturnValue(null);
|
|
||||||
mockResultSet.getString("PROCEDURE_SCHEM");
|
|
||||||
ctrlResultSet.setReturnValue(null);
|
|
||||||
mockResultSet.getString("PROCEDURE_NAME");
|
|
||||||
ctrlResultSet.setReturnValue("add_invoice");
|
|
||||||
mockResultSet.next();
|
|
||||||
ctrlResultSet.setReturnValue(false);
|
|
||||||
mockResultSet.close();
|
|
||||||
ctrlResultSet.setVoidCallable();
|
|
||||||
mockResultSet.next();
|
|
||||||
ctrlResultSet.setReturnValue(true);
|
|
||||||
mockResultSet.getString("COLUMN_NAME");
|
|
||||||
ctrlResultSet.setReturnValue("amount");
|
|
||||||
mockResultSet.getInt("COLUMN_TYPE");
|
|
||||||
ctrlResultSet.setReturnValue(1);
|
|
||||||
mockResultSet.getInt("DATA_TYPE");
|
|
||||||
ctrlResultSet.setReturnValue(4);
|
|
||||||
mockResultSet.getString("TYPE_NAME");
|
|
||||||
ctrlResultSet.setReturnValue(null);
|
|
||||||
mockResultSet.getBoolean("NULLABLE");
|
|
||||||
ctrlResultSet.setReturnValue(false);
|
|
||||||
mockResultSet.next();
|
|
||||||
ctrlResultSet.setReturnValue(true);
|
|
||||||
mockResultSet.getString("COLUMN_NAME");
|
|
||||||
ctrlResultSet.setReturnValue("custid");
|
|
||||||
mockResultSet.getInt("COLUMN_TYPE");
|
|
||||||
ctrlResultSet.setReturnValue(1);
|
|
||||||
mockResultSet.getInt("DATA_TYPE");
|
|
||||||
ctrlResultSet.setReturnValue(4);
|
|
||||||
mockResultSet.getString("TYPE_NAME");
|
|
||||||
ctrlResultSet.setReturnValue(null);
|
|
||||||
mockResultSet.getBoolean("NULLABLE");
|
|
||||||
ctrlResultSet.setReturnValue(false);
|
|
||||||
mockResultSet.next();
|
|
||||||
ctrlResultSet.setReturnValue(true);
|
|
||||||
mockResultSet.getString("COLUMN_NAME");
|
|
||||||
ctrlResultSet.setReturnValue("newid");
|
|
||||||
mockResultSet.getInt("COLUMN_TYPE");
|
|
||||||
ctrlResultSet.setReturnValue(4);
|
|
||||||
mockResultSet.getInt("DATA_TYPE");
|
|
||||||
ctrlResultSet.setReturnValue(4);
|
|
||||||
mockResultSet.getString("TYPE_NAME");
|
|
||||||
ctrlResultSet.setReturnValue(null);
|
|
||||||
mockResultSet.getBoolean("NULLABLE");
|
|
||||||
ctrlResultSet.setReturnValue(false);
|
|
||||||
mockResultSet.next();
|
|
||||||
ctrlResultSet.setReturnValue(false);
|
|
||||||
mockResultSet.close();
|
|
||||||
ctrlResultSet.setVoidCallable();
|
|
||||||
|
|
||||||
|
replay();
|
||||||
|
|
||||||
mockDatabaseMetaData.getDatabaseProductName();
|
SimpleJdbcCall adder = new SimpleJdbcCall(mockDataSource).withProcedureName("add_invoice");
|
||||||
ctrlDatabaseMetaData.setReturnValue("Oracle");
|
adder.declareParameters(new SqlParameter("amount", Types.INTEGER),
|
||||||
mockDatabaseMetaData.getUserName();
|
new SqlParameter("custid", Types.INTEGER),
|
||||||
ctrlDatabaseMetaData.setReturnValue("ME");
|
new SqlOutParameter("newid", Types.INTEGER));
|
||||||
mockDatabaseMetaData.supportsCatalogsInProcedureCalls();
|
Number newId = adder.executeObject(Number.class, amount, custid);
|
||||||
ctrlDatabaseMetaData.setReturnValue(false);
|
assertEquals(4, newId.intValue());
|
||||||
mockDatabaseMetaData.supportsSchemasInProcedureCalls();
|
}
|
||||||
ctrlDatabaseMetaData.setReturnValue(false);
|
|
||||||
mockDatabaseMetaData.storesUpperCaseIdentifiers();
|
|
||||||
ctrlDatabaseMetaData.setReturnValue(true);
|
|
||||||
mockDatabaseMetaData.storesLowerCaseIdentifiers();
|
|
||||||
ctrlDatabaseMetaData.setReturnValue(false);
|
|
||||||
mockDatabaseMetaData.getProcedures("", "ME", "ADD_INVOICE");
|
|
||||||
ctrlDatabaseMetaData.setReturnValue(mockResultSet);
|
|
||||||
mockDatabaseMetaData.getProcedureColumns("", "ME", "ADD_INVOICE", null);
|
|
||||||
ctrlDatabaseMetaData.setReturnValue(mockResultSet);
|
|
||||||
|
|
||||||
mockCallable.setObject(1, 1103, 4);
|
public void testAddInvoiceProcWithMetaDataUsingMapParamSource() throws Exception {
|
||||||
ctrlCallable.setVoidCallable();
|
final int amount = 1103;
|
||||||
mockCallable.setObject(2, 3, 4);
|
final int custid = 3;
|
||||||
ctrlCallable.setVoidCallable();
|
|
||||||
mockCallable.registerOutParameter(3, 4);
|
|
||||||
ctrlCallable.setVoidCallable();
|
|
||||||
mockCallable.execute();
|
|
||||||
ctrlCallable.setReturnValue(false);
|
|
||||||
mockCallable.getUpdateCount();
|
|
||||||
ctrlCallable.setReturnValue(-1);
|
|
||||||
mockCallable.getObject(3);
|
|
||||||
ctrlCallable.setReturnValue(new Long(4));
|
|
||||||
if (debugEnabled) {
|
|
||||||
mockCallable.getWarnings();
|
|
||||||
ctrlCallable.setReturnValue(null);
|
|
||||||
}
|
|
||||||
mockCallable.close();
|
|
||||||
ctrlCallable.setVoidCallable();
|
|
||||||
|
|
||||||
mockConnection.prepareCall(
|
MockControl ctrlResultSet = initializeAddInvoiceWithMetaData(false);
|
||||||
"{call ADD_INVOICE(?, ?, ?)}");
|
|
||||||
ctrlConnection.setReturnValue(mockCallable);
|
|
||||||
|
|
||||||
ctrlResultSet.replay();
|
ctrlResultSet.replay();
|
||||||
replay();
|
replay();
|
||||||
|
|
@ -302,45 +190,27 @@ public class SimpleJdbcCallTests extends TestCase {
|
||||||
ctrlResultSet.verify();
|
ctrlResultSet.verify();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAddInvoiceFuncWithoutMetaData() throws Exception {
|
public void testAddInvoiceProcWithMetaDataUsingArrayParams() throws Exception {
|
||||||
final int amount = 1103;
|
final int amount = 1103;
|
||||||
final int custid = 3;
|
final int custid = 3;
|
||||||
|
|
||||||
mockDatabaseMetaData.getDatabaseProductName();
|
MockControl ctrlResultSet = initializeAddInvoiceWithMetaData(false);
|
||||||
ctrlDatabaseMetaData.setReturnValue("MyDB");
|
|
||||||
mockDatabaseMetaData.getUserName();
|
|
||||||
ctrlDatabaseMetaData.setReturnValue("me");
|
|
||||||
mockDatabaseMetaData.supportsCatalogsInProcedureCalls();
|
|
||||||
ctrlDatabaseMetaData.setReturnValue(false);
|
|
||||||
mockDatabaseMetaData.supportsSchemasInProcedureCalls();
|
|
||||||
ctrlDatabaseMetaData.setReturnValue(false);
|
|
||||||
mockDatabaseMetaData.storesUpperCaseIdentifiers();
|
|
||||||
ctrlDatabaseMetaData.setReturnValue(false);
|
|
||||||
mockDatabaseMetaData.storesLowerCaseIdentifiers();
|
|
||||||
ctrlDatabaseMetaData.setReturnValue(true);
|
|
||||||
|
|
||||||
mockCallable.registerOutParameter(1, 4);
|
ctrlResultSet.replay();
|
||||||
ctrlCallable.setVoidCallable();
|
replay();
|
||||||
mockCallable.setObject(2, 1103, 4);
|
|
||||||
ctrlCallable.setVoidCallable();
|
|
||||||
mockCallable.setObject(3, 3, 4);
|
|
||||||
ctrlCallable.setVoidCallable();
|
|
||||||
mockCallable.execute();
|
|
||||||
ctrlCallable.setReturnValue(false);
|
|
||||||
mockCallable.getUpdateCount();
|
|
||||||
ctrlCallable.setReturnValue(-1);
|
|
||||||
mockCallable.getObject(1);
|
|
||||||
ctrlCallable.setReturnValue(new Long(4));
|
|
||||||
if (debugEnabled) {
|
|
||||||
mockCallable.getWarnings();
|
|
||||||
ctrlCallable.setReturnValue(null);
|
|
||||||
}
|
|
||||||
mockCallable.close();
|
|
||||||
ctrlCallable.setVoidCallable();
|
|
||||||
|
|
||||||
mockConnection.prepareCall(
|
SimpleJdbcCall adder = new SimpleJdbcCall(mockDataSource).withProcedureName("add_invoice");
|
||||||
"{? = call add_invoice(?, ?)}");
|
Number newId = adder.executeObject(Number.class, amount, custid);
|
||||||
ctrlConnection.setReturnValue(mockCallable);
|
assertEquals(4, newId.intValue());
|
||||||
|
|
||||||
|
ctrlResultSet.verify();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testAddInvoiceFuncWithoutMetaDataUsingMapParamSource() throws Exception {
|
||||||
|
final int amount = 1103;
|
||||||
|
final int custid = 3;
|
||||||
|
|
||||||
|
initializeAddInvoiceWithoutMetaData(true);
|
||||||
|
|
||||||
replay();
|
replay();
|
||||||
|
|
||||||
|
|
@ -354,10 +224,117 @@ public class SimpleJdbcCallTests extends TestCase {
|
||||||
assertEquals(4, newId.intValue());
|
assertEquals(4, newId.intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAddInvoiceFuncWithMetaData() throws Exception {
|
public void testAddInvoiceFuncWithoutMetaDataUsingArrayParams() throws Exception {
|
||||||
final int amount = 1103;
|
final int amount = 1103;
|
||||||
final int custid = 3;
|
final int custid = 3;
|
||||||
|
|
||||||
|
initializeAddInvoiceWithoutMetaData(true);
|
||||||
|
|
||||||
|
replay();
|
||||||
|
|
||||||
|
SimpleJdbcCall adder = new SimpleJdbcCall(mockDataSource).withFunctionName("add_invoice");
|
||||||
|
adder.declareParameters(new SqlOutParameter("return", Types.INTEGER),
|
||||||
|
new SqlParameter("amount", Types.INTEGER),
|
||||||
|
new SqlParameter("custid", Types.INTEGER));
|
||||||
|
Number newId = adder.executeFunction(Number.class, amount, custid);
|
||||||
|
assertEquals(4, newId.intValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testAddInvoiceFuncWithMetaDataUsingMapParamSource() throws Exception {
|
||||||
|
final int amount = 1103;
|
||||||
|
final int custid = 3;
|
||||||
|
|
||||||
|
MockControl ctrlResultSet = initializeAddInvoiceWithMetaData(true);
|
||||||
|
|
||||||
|
ctrlResultSet.replay();
|
||||||
|
replay();
|
||||||
|
|
||||||
|
SimpleJdbcCall adder = new SimpleJdbcCall(mockDataSource).withFunctionName("add_invoice");
|
||||||
|
Number newId = adder.executeFunction(Number.class, new MapSqlParameterSource()
|
||||||
|
.addValue("amount", amount)
|
||||||
|
.addValue("custid", custid));
|
||||||
|
assertEquals(4, newId.intValue());
|
||||||
|
|
||||||
|
ctrlResultSet.verify();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testAddInvoiceFuncWithMetaDataUsingArrayParams() throws Exception {
|
||||||
|
final int amount = 1103;
|
||||||
|
final int custid = 3;
|
||||||
|
|
||||||
|
MockControl ctrlResultSet = initializeAddInvoiceWithMetaData(true);
|
||||||
|
|
||||||
|
ctrlResultSet.replay();
|
||||||
|
replay();
|
||||||
|
|
||||||
|
SimpleJdbcCall adder = new SimpleJdbcCall(mockDataSource).withFunctionName("add_invoice");
|
||||||
|
Number newId = adder.executeFunction(Number.class, amount, custid);
|
||||||
|
assertEquals(4, newId.intValue());
|
||||||
|
|
||||||
|
ctrlResultSet.verify();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeAddInvoiceWithoutMetaData(boolean isFunction) throws SQLException {
|
||||||
|
mockDatabaseMetaData.getDatabaseProductName();
|
||||||
|
ctrlDatabaseMetaData.setReturnValue("MyDB");
|
||||||
|
mockDatabaseMetaData.getUserName();
|
||||||
|
ctrlDatabaseMetaData.setReturnValue("me");
|
||||||
|
mockDatabaseMetaData.supportsCatalogsInProcedureCalls();
|
||||||
|
ctrlDatabaseMetaData.setReturnValue(false);
|
||||||
|
mockDatabaseMetaData.supportsSchemasInProcedureCalls();
|
||||||
|
ctrlDatabaseMetaData.setReturnValue(false);
|
||||||
|
mockDatabaseMetaData.storesUpperCaseIdentifiers();
|
||||||
|
ctrlDatabaseMetaData.setReturnValue(false);
|
||||||
|
mockDatabaseMetaData.storesLowerCaseIdentifiers();
|
||||||
|
ctrlDatabaseMetaData.setReturnValue(true);
|
||||||
|
|
||||||
|
if (isFunction) {
|
||||||
|
mockCallable.registerOutParameter(1, 4);
|
||||||
|
ctrlCallable.setVoidCallable();
|
||||||
|
mockCallable.setObject(2, 1103, 4);
|
||||||
|
ctrlCallable.setVoidCallable();
|
||||||
|
mockCallable.setObject(3, 3, 4);
|
||||||
|
ctrlCallable.setVoidCallable();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mockCallable.setObject(1, 1103, 4);
|
||||||
|
ctrlCallable.setVoidCallable();
|
||||||
|
mockCallable.setObject(2, 3, 4);
|
||||||
|
ctrlCallable.setVoidCallable();
|
||||||
|
mockCallable.registerOutParameter(3, 4);
|
||||||
|
ctrlCallable.setVoidCallable();
|
||||||
|
}
|
||||||
|
mockCallable.execute();
|
||||||
|
ctrlCallable.setReturnValue(false);
|
||||||
|
mockCallable.getUpdateCount();
|
||||||
|
ctrlCallable.setReturnValue(-1);
|
||||||
|
if (isFunction) {
|
||||||
|
mockCallable.getObject(1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mockCallable.getObject(3);
|
||||||
|
}
|
||||||
|
ctrlCallable.setReturnValue(new Long(4));
|
||||||
|
if (debugEnabled) {
|
||||||
|
mockCallable.getWarnings();
|
||||||
|
ctrlCallable.setReturnValue(null);
|
||||||
|
}
|
||||||
|
mockCallable.close();
|
||||||
|
ctrlCallable.setVoidCallable();
|
||||||
|
|
||||||
|
if (isFunction) {
|
||||||
|
mockConnection.prepareCall(
|
||||||
|
"{? = call add_invoice(?, ?)}");
|
||||||
|
ctrlConnection.setReturnValue(mockCallable);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mockConnection.prepareCall(
|
||||||
|
"{call add_invoice(?, ?, ?)}");
|
||||||
|
ctrlConnection.setReturnValue(mockCallable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private MockControl initializeAddInvoiceWithMetaData(boolean isFunction) throws SQLException {
|
||||||
MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
|
MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
|
||||||
ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
|
ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
|
||||||
mockResultSet.next();
|
mockResultSet.next();
|
||||||
|
|
@ -374,10 +351,18 @@ public class SimpleJdbcCallTests extends TestCase {
|
||||||
ctrlResultSet.setVoidCallable();
|
ctrlResultSet.setVoidCallable();
|
||||||
mockResultSet.next();
|
mockResultSet.next();
|
||||||
ctrlResultSet.setReturnValue(true);
|
ctrlResultSet.setReturnValue(true);
|
||||||
mockResultSet.getString("COLUMN_NAME");
|
if (isFunction) {
|
||||||
ctrlResultSet.setReturnValue(null);
|
mockResultSet.getString("COLUMN_NAME");
|
||||||
mockResultSet.getInt("COLUMN_TYPE");
|
ctrlResultSet.setReturnValue(null);
|
||||||
ctrlResultSet.setReturnValue(5);
|
mockResultSet.getInt("COLUMN_TYPE");
|
||||||
|
ctrlResultSet.setReturnValue(5);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mockResultSet.getString("COLUMN_NAME");
|
||||||
|
ctrlResultSet.setReturnValue("amount");
|
||||||
|
mockResultSet.getInt("COLUMN_TYPE");
|
||||||
|
ctrlResultSet.setReturnValue(1);
|
||||||
|
}
|
||||||
mockResultSet.getInt("DATA_TYPE");
|
mockResultSet.getInt("DATA_TYPE");
|
||||||
ctrlResultSet.setReturnValue(4);
|
ctrlResultSet.setReturnValue(4);
|
||||||
mockResultSet.getString("TYPE_NAME");
|
mockResultSet.getString("TYPE_NAME");
|
||||||
|
|
@ -386,10 +371,18 @@ public class SimpleJdbcCallTests extends TestCase {
|
||||||
ctrlResultSet.setReturnValue(false);
|
ctrlResultSet.setReturnValue(false);
|
||||||
mockResultSet.next();
|
mockResultSet.next();
|
||||||
ctrlResultSet.setReturnValue(true);
|
ctrlResultSet.setReturnValue(true);
|
||||||
mockResultSet.getString("COLUMN_NAME");
|
if (isFunction) {
|
||||||
ctrlResultSet.setReturnValue("amount");
|
mockResultSet.getString("COLUMN_NAME");
|
||||||
mockResultSet.getInt("COLUMN_TYPE");
|
ctrlResultSet.setReturnValue("amount");
|
||||||
ctrlResultSet.setReturnValue(1);
|
mockResultSet.getInt("COLUMN_TYPE");
|
||||||
|
ctrlResultSet.setReturnValue(1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mockResultSet.getString("COLUMN_NAME");
|
||||||
|
ctrlResultSet.setReturnValue("custid");
|
||||||
|
mockResultSet.getInt("COLUMN_TYPE");
|
||||||
|
ctrlResultSet.setReturnValue(1);
|
||||||
|
}
|
||||||
mockResultSet.getInt("DATA_TYPE");
|
mockResultSet.getInt("DATA_TYPE");
|
||||||
ctrlResultSet.setReturnValue(4);
|
ctrlResultSet.setReturnValue(4);
|
||||||
mockResultSet.getString("TYPE_NAME");
|
mockResultSet.getString("TYPE_NAME");
|
||||||
|
|
@ -398,10 +391,18 @@ public class SimpleJdbcCallTests extends TestCase {
|
||||||
ctrlResultSet.setReturnValue(false);
|
ctrlResultSet.setReturnValue(false);
|
||||||
mockResultSet.next();
|
mockResultSet.next();
|
||||||
ctrlResultSet.setReturnValue(true);
|
ctrlResultSet.setReturnValue(true);
|
||||||
mockResultSet.getString("COLUMN_NAME");
|
if (isFunction) {
|
||||||
ctrlResultSet.setReturnValue("custid");
|
mockResultSet.getString("COLUMN_NAME");
|
||||||
mockResultSet.getInt("COLUMN_TYPE");
|
ctrlResultSet.setReturnValue("custid");
|
||||||
ctrlResultSet.setReturnValue(1);
|
mockResultSet.getInt("COLUMN_TYPE");
|
||||||
|
ctrlResultSet.setReturnValue(1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mockResultSet.getString("COLUMN_NAME");
|
||||||
|
ctrlResultSet.setReturnValue("newid");
|
||||||
|
mockResultSet.getInt("COLUMN_TYPE");
|
||||||
|
ctrlResultSet.setReturnValue(4);
|
||||||
|
}
|
||||||
mockResultSet.getInt("DATA_TYPE");
|
mockResultSet.getInt("DATA_TYPE");
|
||||||
ctrlResultSet.setReturnValue(4);
|
ctrlResultSet.setReturnValue(4);
|
||||||
mockResultSet.getString("TYPE_NAME");
|
mockResultSet.getString("TYPE_NAME");
|
||||||
|
|
@ -413,7 +414,6 @@ public class SimpleJdbcCallTests extends TestCase {
|
||||||
mockResultSet.close();
|
mockResultSet.close();
|
||||||
ctrlResultSet.setVoidCallable();
|
ctrlResultSet.setVoidCallable();
|
||||||
|
|
||||||
|
|
||||||
mockDatabaseMetaData.getDatabaseProductName();
|
mockDatabaseMetaData.getDatabaseProductName();
|
||||||
ctrlDatabaseMetaData.setReturnValue("Oracle");
|
ctrlDatabaseMetaData.setReturnValue("Oracle");
|
||||||
mockDatabaseMetaData.getUserName();
|
mockDatabaseMetaData.getUserName();
|
||||||
|
|
@ -431,18 +431,34 @@ public class SimpleJdbcCallTests extends TestCase {
|
||||||
mockDatabaseMetaData.getProcedureColumns("", "ME", "ADD_INVOICE", null);
|
mockDatabaseMetaData.getProcedureColumns("", "ME", "ADD_INVOICE", null);
|
||||||
ctrlDatabaseMetaData.setReturnValue(mockResultSet);
|
ctrlDatabaseMetaData.setReturnValue(mockResultSet);
|
||||||
|
|
||||||
mockCallable.registerOutParameter(1, 4);
|
if (isFunction) {
|
||||||
ctrlCallable.setVoidCallable();
|
mockCallable.registerOutParameter(1, 4);
|
||||||
mockCallable.setObject(2, 1103, 4);
|
ctrlCallable.setVoidCallable();
|
||||||
ctrlCallable.setVoidCallable();
|
mockCallable.setObject(2, 1103, 4);
|
||||||
mockCallable.setObject(3, 3, 4);
|
ctrlCallable.setVoidCallable();
|
||||||
ctrlCallable.setVoidCallable();
|
mockCallable.setObject(3, 3, 4);
|
||||||
|
ctrlCallable.setVoidCallable();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mockCallable.setObject(1, 1103, 4);
|
||||||
|
ctrlCallable.setVoidCallable();
|
||||||
|
mockCallable.setObject(2, 3, 4);
|
||||||
|
ctrlCallable.setVoidCallable();
|
||||||
|
mockCallable.registerOutParameter(3, 4);
|
||||||
|
ctrlCallable.setVoidCallable();
|
||||||
|
}
|
||||||
mockCallable.execute();
|
mockCallable.execute();
|
||||||
ctrlCallable.setReturnValue(false);
|
ctrlCallable.setReturnValue(false);
|
||||||
mockCallable.getUpdateCount();
|
mockCallable.getUpdateCount();
|
||||||
ctrlCallable.setReturnValue(-1);
|
ctrlCallable.setReturnValue(-1);
|
||||||
mockCallable.getObject(1);
|
if (isFunction) {
|
||||||
ctrlCallable.setReturnValue(new Long(4));
|
mockCallable.getObject(1);
|
||||||
|
ctrlCallable.setReturnValue(new Long(4));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mockCallable.getObject(3);
|
||||||
|
ctrlCallable.setReturnValue(new Long(4));
|
||||||
|
}
|
||||||
if (debugEnabled) {
|
if (debugEnabled) {
|
||||||
mockCallable.getWarnings();
|
mockCallable.getWarnings();
|
||||||
ctrlCallable.setReturnValue(null);
|
ctrlCallable.setReturnValue(null);
|
||||||
|
|
@ -450,20 +466,17 @@ public class SimpleJdbcCallTests extends TestCase {
|
||||||
mockCallable.close();
|
mockCallable.close();
|
||||||
ctrlCallable.setVoidCallable();
|
ctrlCallable.setVoidCallable();
|
||||||
|
|
||||||
mockConnection.prepareCall(
|
if (isFunction) {
|
||||||
"{? = call ADD_INVOICE(?, ?)}");
|
mockConnection.prepareCall(
|
||||||
ctrlConnection.setReturnValue(mockCallable);
|
"{? = call ADD_INVOICE(?, ?)}");
|
||||||
|
ctrlConnection.setReturnValue(mockCallable);
|
||||||
ctrlResultSet.replay();
|
}
|
||||||
replay();
|
else {
|
||||||
|
mockConnection.prepareCall(
|
||||||
SimpleJdbcCall adder = new SimpleJdbcCall(mockDataSource).withFunctionName("add_invoice");
|
"{call ADD_INVOICE(?, ?, ?)}");
|
||||||
Number newId = adder.executeFunction(Number.class, new MapSqlParameterSource()
|
ctrlConnection.setReturnValue(mockCallable);
|
||||||
.addValue("amount", amount)
|
}
|
||||||
.addValue("custid", custid));
|
return ctrlResultSet;
|
||||||
assertEquals(4, newId.intValue());
|
|
||||||
|
|
||||||
ctrlResultSet.verify();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue