Polishing
This commit is contained in:
@@ -432,7 +432,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inner class representing table meta-data.
|
* Class representing table meta-data.
|
||||||
*/
|
*/
|
||||||
private static class TableMetaData {
|
private static class TableMetaData {
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 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.
|
||||||
@@ -51,7 +51,6 @@ public final class TableMetaDataProviderFactory {
|
|||||||
try {
|
try {
|
||||||
return JdbcUtils.extractDatabaseMetaData(dataSource, databaseMetaData -> {
|
return JdbcUtils.extractDatabaseMetaData(dataSource, databaseMetaData -> {
|
||||||
String databaseProductName = JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
|
String databaseProductName = JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
|
||||||
boolean accessTableColumnMetaData = context.isAccessTableColumnMetaData();
|
|
||||||
TableMetaDataProvider provider;
|
TableMetaDataProvider provider;
|
||||||
|
|
||||||
if ("Oracle".equals(databaseProductName)) {
|
if ("Oracle".equals(databaseProductName)) {
|
||||||
@@ -70,15 +69,17 @@ public final class TableMetaDataProviderFactory {
|
|||||||
else {
|
else {
|
||||||
provider = new GenericTableMetaDataProvider(databaseMetaData);
|
provider = new GenericTableMetaDataProvider(databaseMetaData);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug("Using " + provider.getClass().getSimpleName());
|
logger.debug("Using " + provider.getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
|
|
||||||
provider.initializeWithMetaData(databaseMetaData);
|
provider.initializeWithMetaData(databaseMetaData);
|
||||||
if (accessTableColumnMetaData) {
|
|
||||||
|
if (context.isAccessTableColumnMetaData()) {
|
||||||
provider.initializeWithTableColumnMetaData(databaseMetaData,
|
provider.initializeWithTableColumnMetaData(databaseMetaData,
|
||||||
context.getCatalogName(), context.getSchemaName(), context.getTableName());
|
context.getCatalogName(), context.getSchemaName(), context.getTableName());
|
||||||
}
|
}
|
||||||
|
|
||||||
return provider;
|
return provider;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ public abstract class AbstractJdbcInsert {
|
|||||||
/** Context used to retrieve and manage database meta-data. */
|
/** Context used to retrieve and manage database meta-data. */
|
||||||
private final TableMetaDataContext tableMetaDataContext = new TableMetaDataContext();
|
private final TableMetaDataContext tableMetaDataContext = new TableMetaDataContext();
|
||||||
|
|
||||||
/** List of columns objects to be used in insert statement. */
|
/** List of column names to be used in insert statement. */
|
||||||
private final List<String> declaredColumns = new ArrayList<>();
|
private final List<String> declaredColumns = new ArrayList<>();
|
||||||
|
|
||||||
/** The names of the columns holding the generated key. */
|
/** The names of the columns holding the generated key. */
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
@@ -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.
|
||||||
@@ -19,7 +19,7 @@ package org.springframework.jdbc.core.simple;
|
|||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DatabaseMetaData;
|
import java.sql.DatabaseMetaData;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.util.HashMap;
|
import java.util.Collections;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
@@ -35,39 +35,36 @@ import static org.mockito.Mockito.mock;
|
|||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mock object based tests for SimpleJdbcInsert.
|
* Mock object based tests for {@link SimpleJdbcInsert}.
|
||||||
*
|
*
|
||||||
* @author Thomas Risberg
|
* @author Thomas Risberg
|
||||||
*/
|
*/
|
||||||
public class SimpleJdbcInsertTests {
|
class SimpleJdbcInsertTests {
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setUp() throws Exception {
|
void setUp() throws Exception {
|
||||||
connection = mock(Connection.class);
|
|
||||||
databaseMetaData = mock(DatabaseMetaData.class);
|
|
||||||
dataSource = mock(DataSource.class);
|
|
||||||
given(connection.getMetaData()).willReturn(databaseMetaData);
|
given(connection.getMetaData()).willReturn(databaseMetaData);
|
||||||
given(dataSource.getConnection()).willReturn(connection);
|
given(dataSource.getConnection()).willReturn(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
public void verifyClosed() throws Exception {
|
void verifyClosed() throws Exception {
|
||||||
verify(connection).close();
|
verify(connection).close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoSuchTable() throws Exception {
|
void noSuchTable() throws Exception {
|
||||||
ResultSet resultSet = mock(ResultSet.class);
|
ResultSet resultSet = mock(ResultSet.class);
|
||||||
given(resultSet.next()).willReturn(false);
|
given(resultSet.next()).willReturn(false);
|
||||||
given(databaseMetaData.getDatabaseProductName()).willReturn("MyDB");
|
|
||||||
given(databaseMetaData.getDatabaseProductName()).willReturn("MyDB");
|
given(databaseMetaData.getDatabaseProductName()).willReturn("MyDB");
|
||||||
given(databaseMetaData.getDatabaseProductVersion()).willReturn("1.0");
|
given(databaseMetaData.getDatabaseProductVersion()).willReturn("1.0");
|
||||||
given(databaseMetaData.getUserName()).willReturn("me");
|
given(databaseMetaData.getUserName()).willReturn("me");
|
||||||
@@ -76,8 +73,10 @@ public class SimpleJdbcInsertTests {
|
|||||||
|
|
||||||
SimpleJdbcInsert insert = new SimpleJdbcInsert(dataSource).withTableName("x");
|
SimpleJdbcInsert insert = new SimpleJdbcInsert(dataSource).withTableName("x");
|
||||||
// Shouldn't succeed in inserting into table which doesn't exist
|
// Shouldn't succeed in inserting into table which doesn't exist
|
||||||
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() ->
|
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class)
|
||||||
insert.execute(new HashMap<>()));
|
.isThrownBy(() -> insert.execute(Collections.emptyMap()))
|
||||||
|
.withMessageStartingWith("Unable to locate columns for table 'x' so an insert statement can't be generated");
|
||||||
|
|
||||||
verify(resultSet).close();
|
verify(resultSet).close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user