|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2019 the original author or authors. |
|
|
|
* Copyright 2002-2021 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -47,30 +47,26 @@ import static org.mockito.Mockito.verify; |
|
|
|
* @author Thomas Risberg |
|
|
|
* @author Thomas Risberg |
|
|
|
* @author Kiril Nugmanov |
|
|
|
* @author Kiril Nugmanov |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class SimpleJdbcCallTests { |
|
|
|
class SimpleJdbcCallTests { |
|
|
|
|
|
|
|
|
|
|
|
private Connection connection; |
|
|
|
private final Connection connection = mock(Connection.class); |
|
|
|
|
|
|
|
|
|
|
|
private DatabaseMetaData databaseMetaData; |
|
|
|
private final DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class); |
|
|
|
|
|
|
|
|
|
|
|
private DataSource dataSource; |
|
|
|
private final DataSource dataSource = mock(DataSource.class); |
|
|
|
|
|
|
|
|
|
|
|
private CallableStatement callableStatement; |
|
|
|
private final CallableStatement callableStatement = mock(CallableStatement.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeEach |
|
|
|
@BeforeEach |
|
|
|
public void setUp() throws Exception { |
|
|
|
void setUp() throws Exception { |
|
|
|
connection = mock(Connection.class); |
|
|
|
|
|
|
|
databaseMetaData = mock(DatabaseMetaData.class); |
|
|
|
|
|
|
|
dataSource = mock(DataSource.class); |
|
|
|
|
|
|
|
callableStatement = mock(CallableStatement.class); |
|
|
|
|
|
|
|
given(connection.getMetaData()).willReturn(databaseMetaData); |
|
|
|
given(connection.getMetaData()).willReturn(databaseMetaData); |
|
|
|
given(dataSource.getConnection()).willReturn(connection); |
|
|
|
given(dataSource.getConnection()).willReturn(connection); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testNoSuchStoredProcedure() throws Exception { |
|
|
|
void noSuchStoredProcedure() throws Exception { |
|
|
|
final String NO_SUCH_PROC = "x"; |
|
|
|
final String NO_SUCH_PROC = "x"; |
|
|
|
SQLException sqlException = new SQLException("Syntax error or access violation exception", "42000"); |
|
|
|
SQLException sqlException = new SQLException("Syntax error or access violation exception", "42000"); |
|
|
|
given(databaseMetaData.getDatabaseProductName()).willReturn("MyDB"); |
|
|
|
given(databaseMetaData.getDatabaseProductName()).willReturn("MyDB"); |
|
|
|
@ -81,8 +77,8 @@ public class SimpleJdbcCallTests { |
|
|
|
given(connection.prepareCall("{call " + NO_SUCH_PROC + "()}")).willReturn(callableStatement); |
|
|
|
given(connection.prepareCall("{call " + NO_SUCH_PROC + "()}")).willReturn(callableStatement); |
|
|
|
SimpleJdbcCall sproc = new SimpleJdbcCall(dataSource).withProcedureName(NO_SUCH_PROC); |
|
|
|
SimpleJdbcCall sproc = new SimpleJdbcCall(dataSource).withProcedureName(NO_SUCH_PROC); |
|
|
|
try { |
|
|
|
try { |
|
|
|
assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(BadSqlGrammarException.class) |
|
|
|
sproc.execute()) |
|
|
|
.isThrownBy(() -> sproc.execute()) |
|
|
|
.withCause(sqlException); |
|
|
|
.withCause(sqlException); |
|
|
|
} |
|
|
|
} |
|
|
|
finally { |
|
|
|
finally { |
|
|
|
@ -92,7 +88,7 @@ public class SimpleJdbcCallTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testUnnamedParameterHandling() throws Exception { |
|
|
|
void unnamedParameterHandling() throws Exception { |
|
|
|
final String MY_PROC = "my_proc"; |
|
|
|
final String MY_PROC = "my_proc"; |
|
|
|
SimpleJdbcCall sproc = new SimpleJdbcCall(dataSource).withProcedureName(MY_PROC); |
|
|
|
SimpleJdbcCall sproc = new SimpleJdbcCall(dataSource).withProcedureName(MY_PROC); |
|
|
|
// Shouldn't succeed in adding unnamed parameter
|
|
|
|
// Shouldn't succeed in adding unnamed parameter
|
|
|
|
@ -101,7 +97,7 @@ public class SimpleJdbcCallTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testAddInvoiceProcWithoutMetaDataUsingMapParamSource() throws Exception { |
|
|
|
void addInvoiceProcWithoutMetaDataUsingMapParamSource() throws Exception { |
|
|
|
initializeAddInvoiceWithoutMetaData(false); |
|
|
|
initializeAddInvoiceWithoutMetaData(false); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice"); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice"); |
|
|
|
adder.declareParameters( |
|
|
|
adder.declareParameters( |
|
|
|
@ -117,7 +113,7 @@ public class SimpleJdbcCallTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testAddInvoiceProcWithoutMetaDataUsingArrayParams() throws Exception { |
|
|
|
void addInvoiceProcWithoutMetaDataUsingArrayParams() throws Exception { |
|
|
|
initializeAddInvoiceWithoutMetaData(false); |
|
|
|
initializeAddInvoiceWithoutMetaData(false); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice"); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice"); |
|
|
|
adder.declareParameters( |
|
|
|
adder.declareParameters( |
|
|
|
@ -131,7 +127,7 @@ public class SimpleJdbcCallTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testAddInvoiceProcWithMetaDataUsingMapParamSource() throws Exception { |
|
|
|
void addInvoiceProcWithMetaDataUsingMapParamSource() throws Exception { |
|
|
|
initializeAddInvoiceWithMetaData(false); |
|
|
|
initializeAddInvoiceWithMetaData(false); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice"); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice"); |
|
|
|
Number newId = adder.executeObject(Number.class, new MapSqlParameterSource() |
|
|
|
Number newId = adder.executeObject(Number.class, new MapSqlParameterSource() |
|
|
|
@ -143,7 +139,7 @@ public class SimpleJdbcCallTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testAddInvoiceProcWithMetaDataUsingArrayParams() throws Exception { |
|
|
|
void addInvoiceProcWithMetaDataUsingArrayParams() throws Exception { |
|
|
|
initializeAddInvoiceWithMetaData(false); |
|
|
|
initializeAddInvoiceWithMetaData(false); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice"); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice"); |
|
|
|
Number newId = adder.executeObject(Number.class, 1103, 3); |
|
|
|
Number newId = adder.executeObject(Number.class, 1103, 3); |
|
|
|
@ -153,7 +149,7 @@ public class SimpleJdbcCallTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testAddInvoiceFuncWithoutMetaDataUsingMapParamSource() throws Exception { |
|
|
|
void addInvoiceFuncWithoutMetaDataUsingMapParamSource() throws Exception { |
|
|
|
initializeAddInvoiceWithoutMetaData(true); |
|
|
|
initializeAddInvoiceWithoutMetaData(true); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice"); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice"); |
|
|
|
adder.declareParameters( |
|
|
|
adder.declareParameters( |
|
|
|
@ -169,7 +165,7 @@ public class SimpleJdbcCallTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testAddInvoiceFuncWithoutMetaDataUsingArrayParams() throws Exception { |
|
|
|
void addInvoiceFuncWithoutMetaDataUsingArrayParams() throws Exception { |
|
|
|
initializeAddInvoiceWithoutMetaData(true); |
|
|
|
initializeAddInvoiceWithoutMetaData(true); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice"); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice"); |
|
|
|
adder.declareParameters( |
|
|
|
adder.declareParameters( |
|
|
|
@ -183,7 +179,7 @@ public class SimpleJdbcCallTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testAddInvoiceFuncWithMetaDataUsingMapParamSource() throws Exception { |
|
|
|
void addInvoiceFuncWithMetaDataUsingMapParamSource() throws Exception { |
|
|
|
initializeAddInvoiceWithMetaData(true); |
|
|
|
initializeAddInvoiceWithMetaData(true); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice"); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice"); |
|
|
|
Number newId = adder.executeFunction(Number.class, new MapSqlParameterSource() |
|
|
|
Number newId = adder.executeFunction(Number.class, new MapSqlParameterSource() |
|
|
|
@ -192,22 +188,20 @@ public class SimpleJdbcCallTests { |
|
|
|
assertThat(newId.intValue()).isEqualTo(4); |
|
|
|
assertThat(newId.intValue()).isEqualTo(4); |
|
|
|
verifyAddInvoiceWithMetaData(true); |
|
|
|
verifyAddInvoiceWithMetaData(true); |
|
|
|
verify(connection, atLeastOnce()).close(); |
|
|
|
verify(connection, atLeastOnce()).close(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testAddInvoiceFuncWithMetaDataUsingArrayParams() throws Exception { |
|
|
|
void addInvoiceFuncWithMetaDataUsingArrayParams() throws Exception { |
|
|
|
initializeAddInvoiceWithMetaData(true); |
|
|
|
initializeAddInvoiceWithMetaData(true); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice"); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice"); |
|
|
|
Number newId = adder.executeFunction(Number.class, 1103, 3); |
|
|
|
Number newId = adder.executeFunction(Number.class, 1103, 3); |
|
|
|
assertThat(newId.intValue()).isEqualTo(4); |
|
|
|
assertThat(newId.intValue()).isEqualTo(4); |
|
|
|
verifyAddInvoiceWithMetaData(true); |
|
|
|
verifyAddInvoiceWithMetaData(true); |
|
|
|
verify(connection, atLeastOnce()).close(); |
|
|
|
verify(connection, atLeastOnce()).close(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testCorrectFunctionStatement() throws Exception { |
|
|
|
void correctFunctionStatement() throws Exception { |
|
|
|
initializeAddInvoiceWithMetaData(true); |
|
|
|
initializeAddInvoiceWithMetaData(true); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice"); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice"); |
|
|
|
adder.compile(); |
|
|
|
adder.compile(); |
|
|
|
@ -215,7 +209,7 @@ public class SimpleJdbcCallTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testCorrectFunctionStatementNamed() throws Exception { |
|
|
|
void correctFunctionStatementNamed() throws Exception { |
|
|
|
initializeAddInvoiceWithMetaData(true); |
|
|
|
initializeAddInvoiceWithMetaData(true); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withNamedBinding().withFunctionName("add_invoice"); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withNamedBinding().withFunctionName("add_invoice"); |
|
|
|
adder.compile(); |
|
|
|
adder.compile(); |
|
|
|
@ -223,7 +217,7 @@ public class SimpleJdbcCallTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testCorrectProcedureStatementNamed() throws Exception { |
|
|
|
void correctProcedureStatementNamed() throws Exception { |
|
|
|
initializeAddInvoiceWithMetaData(false); |
|
|
|
initializeAddInvoiceWithMetaData(false); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withNamedBinding().withProcedureName("add_invoice"); |
|
|
|
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withNamedBinding().withProcedureName("add_invoice"); |
|
|
|
adder.compile(); |
|
|
|
adder.compile(); |
|
|
|
|