Polishing
This commit is contained in:
parent
6fde28be98
commit
91d542e406
|
|
@ -432,7 +432,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
|
|||
|
||||
|
||||
/**
|
||||
* Inner class representing table meta-data.
|
||||
* Class representing table meta-data.
|
||||
*/
|
||||
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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -51,7 +51,6 @@ public final class TableMetaDataProviderFactory {
|
|||
try {
|
||||
return JdbcUtils.extractDatabaseMetaData(dataSource, databaseMetaData -> {
|
||||
String databaseProductName = JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
|
||||
boolean accessTableColumnMetaData = context.isAccessTableColumnMetaData();
|
||||
TableMetaDataProvider provider;
|
||||
|
||||
if ("Oracle".equals(databaseProductName)) {
|
||||
|
|
@ -70,15 +69,17 @@ public final class TableMetaDataProviderFactory {
|
|||
else {
|
||||
provider = new GenericTableMetaDataProvider(databaseMetaData);
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Using " + provider.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
provider.initializeWithMetaData(databaseMetaData);
|
||||
if (accessTableColumnMetaData) {
|
||||
|
||||
if (context.isAccessTableColumnMetaData()) {
|
||||
provider.initializeWithTableColumnMetaData(databaseMetaData,
|
||||
context.getCatalogName(), context.getSchemaName(), context.getTableName());
|
||||
}
|
||||
|
||||
return provider;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ public abstract class AbstractJdbcInsert {
|
|||
/** Context used to retrieve and manage database meta-data. */
|
||||
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<>();
|
||||
|
||||
/** 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");
|
||||
* 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 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
|
||||
public void setUp() throws Exception {
|
||||
connection = mock(Connection.class);
|
||||
databaseMetaData = mock(DatabaseMetaData.class);
|
||||
dataSource = mock(DataSource.class);
|
||||
callableStatement = mock(CallableStatement.class);
|
||||
void setUp() throws Exception {
|
||||
given(connection.getMetaData()).willReturn(databaseMetaData);
|
||||
given(dataSource.getConnection()).willReturn(connection);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNoSuchStoredProcedure() throws Exception {
|
||||
void noSuchStoredProcedure() throws Exception {
|
||||
final String NO_SUCH_PROC = "x";
|
||||
SQLException sqlException = new SQLException("Syntax error or access violation exception", "42000");
|
||||
given(databaseMetaData.getDatabaseProductName()).willReturn("MyDB");
|
||||
|
|
@ -81,8 +77,8 @@ public class SimpleJdbcCallTests {
|
|||
given(connection.prepareCall("{call " + NO_SUCH_PROC + "()}")).willReturn(callableStatement);
|
||||
SimpleJdbcCall sproc = new SimpleJdbcCall(dataSource).withProcedureName(NO_SUCH_PROC);
|
||||
try {
|
||||
assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(() ->
|
||||
sproc.execute())
|
||||
assertThatExceptionOfType(BadSqlGrammarException.class)
|
||||
.isThrownBy(() -> sproc.execute())
|
||||
.withCause(sqlException);
|
||||
}
|
||||
finally {
|
||||
|
|
@ -92,7 +88,7 @@ public class SimpleJdbcCallTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testUnnamedParameterHandling() throws Exception {
|
||||
void unnamedParameterHandling() throws Exception {
|
||||
final String MY_PROC = "my_proc";
|
||||
SimpleJdbcCall sproc = new SimpleJdbcCall(dataSource).withProcedureName(MY_PROC);
|
||||
// Shouldn't succeed in adding unnamed parameter
|
||||
|
|
@ -101,7 +97,7 @@ public class SimpleJdbcCallTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testAddInvoiceProcWithoutMetaDataUsingMapParamSource() throws Exception {
|
||||
void addInvoiceProcWithoutMetaDataUsingMapParamSource() throws Exception {
|
||||
initializeAddInvoiceWithoutMetaData(false);
|
||||
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice");
|
||||
adder.declareParameters(
|
||||
|
|
@ -117,7 +113,7 @@ public class SimpleJdbcCallTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testAddInvoiceProcWithoutMetaDataUsingArrayParams() throws Exception {
|
||||
void addInvoiceProcWithoutMetaDataUsingArrayParams() throws Exception {
|
||||
initializeAddInvoiceWithoutMetaData(false);
|
||||
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice");
|
||||
adder.declareParameters(
|
||||
|
|
@ -131,7 +127,7 @@ public class SimpleJdbcCallTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testAddInvoiceProcWithMetaDataUsingMapParamSource() throws Exception {
|
||||
void addInvoiceProcWithMetaDataUsingMapParamSource() throws Exception {
|
||||
initializeAddInvoiceWithMetaData(false);
|
||||
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice");
|
||||
Number newId = adder.executeObject(Number.class, new MapSqlParameterSource()
|
||||
|
|
@ -143,7 +139,7 @@ public class SimpleJdbcCallTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testAddInvoiceProcWithMetaDataUsingArrayParams() throws Exception {
|
||||
void addInvoiceProcWithMetaDataUsingArrayParams() throws Exception {
|
||||
initializeAddInvoiceWithMetaData(false);
|
||||
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice");
|
||||
Number newId = adder.executeObject(Number.class, 1103, 3);
|
||||
|
|
@ -153,7 +149,7 @@ public class SimpleJdbcCallTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testAddInvoiceFuncWithoutMetaDataUsingMapParamSource() throws Exception {
|
||||
void addInvoiceFuncWithoutMetaDataUsingMapParamSource() throws Exception {
|
||||
initializeAddInvoiceWithoutMetaData(true);
|
||||
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice");
|
||||
adder.declareParameters(
|
||||
|
|
@ -169,7 +165,7 @@ public class SimpleJdbcCallTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testAddInvoiceFuncWithoutMetaDataUsingArrayParams() throws Exception {
|
||||
void addInvoiceFuncWithoutMetaDataUsingArrayParams() throws Exception {
|
||||
initializeAddInvoiceWithoutMetaData(true);
|
||||
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice");
|
||||
adder.declareParameters(
|
||||
|
|
@ -183,7 +179,7 @@ public class SimpleJdbcCallTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testAddInvoiceFuncWithMetaDataUsingMapParamSource() throws Exception {
|
||||
void addInvoiceFuncWithMetaDataUsingMapParamSource() throws Exception {
|
||||
initializeAddInvoiceWithMetaData(true);
|
||||
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice");
|
||||
Number newId = adder.executeFunction(Number.class, new MapSqlParameterSource()
|
||||
|
|
@ -192,22 +188,20 @@ public class SimpleJdbcCallTests {
|
|||
assertThat(newId.intValue()).isEqualTo(4);
|
||||
verifyAddInvoiceWithMetaData(true);
|
||||
verify(connection, atLeastOnce()).close();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddInvoiceFuncWithMetaDataUsingArrayParams() throws Exception {
|
||||
void addInvoiceFuncWithMetaDataUsingArrayParams() throws Exception {
|
||||
initializeAddInvoiceWithMetaData(true);
|
||||
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice");
|
||||
Number newId = adder.executeFunction(Number.class, 1103, 3);
|
||||
assertThat(newId.intValue()).isEqualTo(4);
|
||||
verifyAddInvoiceWithMetaData(true);
|
||||
verify(connection, atLeastOnce()).close();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCorrectFunctionStatement() throws Exception {
|
||||
void correctFunctionStatement() throws Exception {
|
||||
initializeAddInvoiceWithMetaData(true);
|
||||
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withFunctionName("add_invoice");
|
||||
adder.compile();
|
||||
|
|
@ -215,7 +209,7 @@ public class SimpleJdbcCallTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testCorrectFunctionStatementNamed() throws Exception {
|
||||
void correctFunctionStatementNamed() throws Exception {
|
||||
initializeAddInvoiceWithMetaData(true);
|
||||
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withNamedBinding().withFunctionName("add_invoice");
|
||||
adder.compile();
|
||||
|
|
@ -223,7 +217,7 @@ public class SimpleJdbcCallTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testCorrectProcedureStatementNamed() throws Exception {
|
||||
void correctProcedureStatementNamed() throws Exception {
|
||||
initializeAddInvoiceWithMetaData(false);
|
||||
SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withNamedBinding().withProcedureName("add_invoice");
|
||||
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");
|
||||
* 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.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
|
|
@ -35,39 +35,36 @@ import static org.mockito.Mockito.mock;
|
|||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Mock object based tests for SimpleJdbcInsert.
|
||||
* Mock object based tests for {@link SimpleJdbcInsert}.
|
||||
*
|
||||
* @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
|
||||
public void setUp() throws Exception {
|
||||
connection = mock(Connection.class);
|
||||
databaseMetaData = mock(DatabaseMetaData.class);
|
||||
dataSource = mock(DataSource.class);
|
||||
void setUp() throws Exception {
|
||||
given(connection.getMetaData()).willReturn(databaseMetaData);
|
||||
given(dataSource.getConnection()).willReturn(connection);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void verifyClosed() throws Exception {
|
||||
void verifyClosed() throws Exception {
|
||||
verify(connection).close();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNoSuchTable() throws Exception {
|
||||
void noSuchTable() throws Exception {
|
||||
ResultSet resultSet = mock(ResultSet.class);
|
||||
given(resultSet.next()).willReturn(false);
|
||||
given(databaseMetaData.getDatabaseProductName()).willReturn("MyDB");
|
||||
|
||||
given(databaseMetaData.getDatabaseProductName()).willReturn("MyDB");
|
||||
given(databaseMetaData.getDatabaseProductVersion()).willReturn("1.0");
|
||||
given(databaseMetaData.getUserName()).willReturn("me");
|
||||
|
|
@ -76,8 +73,10 @@ public class SimpleJdbcInsertTests {
|
|||
|
||||
SimpleJdbcInsert insert = new SimpleJdbcInsert(dataSource).withTableName("x");
|
||||
// Shouldn't succeed in inserting into table which doesn't exist
|
||||
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() ->
|
||||
insert.execute(new HashMap<>()));
|
||||
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class)
|
||||
.isThrownBy(() -> insert.execute(Collections.emptyMap()))
|
||||
.withMessageStartingWith("Unable to locate columns for table 'x' so an insert statement can't be generated");
|
||||
|
||||
verify(resultSet).close();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue