Merge branch '5.2.x'

This commit is contained in:
Juergen Hoeller
2020-09-03 19:21:44 +02:00
9 changed files with 152 additions and 72 deletions

View File

@@ -17,10 +17,15 @@
package org.springframework.jdbc.support;
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.DataTruncation;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.dao.CannotSerializeTransactionException;
@@ -35,6 +40,9 @@ import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* @author Rod Johnson
@@ -79,7 +87,7 @@ public class SQLErrorCodeSQLExceptionTranslatorTests {
SQLException dupKeyEx = new SQLException("", "", 10);
DataAccessException dksex = sext.translate("task", "SQL", dupKeyEx);
assertThat(DataIntegrityViolationException.class.isAssignableFrom(dksex.getClass())).as("Not instance of DataIntegrityViolationException").isTrue();
assertThat(DataIntegrityViolationException.class.isInstance(dksex)).as("Not instance of DataIntegrityViolationException").isTrue();
// Test fallback. We assume that no database will ever return this error code,
// but 07xxx will be bad grammar picked up by the fallback SQLState translator
@@ -152,14 +160,13 @@ public class SQLErrorCodeSQLExceptionTranslatorTests {
final SQLErrorCodes customErrorCodes = new SQLErrorCodes();
final CustomSQLErrorCodesTranslation customTranslation = new CustomSQLErrorCodesTranslation();
customErrorCodes.setBadSqlGrammarCodes(new String[] {"1", "2"});
customErrorCodes.setDataIntegrityViolationCodes(new String[] {"3", "4"});
customTranslation.setErrorCodes(new String[] {"1"});
customErrorCodes.setBadSqlGrammarCodes("1", "2");
customErrorCodes.setDataIntegrityViolationCodes("3", "4");
customTranslation.setErrorCodes("1");
customTranslation.setExceptionClass(CustomErrorCodeException.class);
customErrorCodes.setCustomTranslations(new CustomSQLErrorCodesTranslation[] {customTranslation});
customErrorCodes.setCustomTranslations(customTranslation);
SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
sext.setSqlErrorCodes(customErrorCodes);
SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(customErrorCodes);
// Should custom translate this
SQLException badSqlEx = new SQLException("", "", 1);
@@ -176,4 +183,28 @@ public class SQLErrorCodeSQLExceptionTranslatorTests {
customTranslation.setExceptionClass(String.class));
}
@Test
public void dataSourceInitialization() throws Exception {
SQLException connectionException = new SQLException();
SQLException duplicateKeyException = new SQLException("test", "", 1);
DataSource dataSource = mock(DataSource.class);
given(dataSource.getConnection()).willThrow(connectionException);
SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(dataSource);
assertThat(sext.translate("test", null, duplicateKeyException)).isNotInstanceOf(DuplicateKeyException.class);
DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
given(databaseMetaData.getDatabaseProductName()).willReturn("Oracle");
Connection connection = mock(Connection.class);
given(connection.getMetaData()).willReturn(databaseMetaData);
Mockito.reset(dataSource);
given(dataSource.getConnection()).willReturn(connection);
assertThat(sext.translate("test", null, duplicateKeyException)).isInstanceOf(DuplicateKeyException.class);
verify(connection).close();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -31,6 +31,7 @@ import org.springframework.core.io.Resource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
/**
@@ -39,6 +40,7 @@ import static org.mockito.Mockito.verify;
* @author Rod Johnson
* @author Thomas Risberg
* @author Stephane Nicoll
* @author Juergen Hoeller
*/
public class SQLErrorCodesFactoryTests {
@@ -239,7 +241,11 @@ public class SQLErrorCodesFactoryTests {
SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes(dataSource);
assertIsEmpty(sec);
verify(connection).close();
reset(connection);
sec = SQLErrorCodesFactory.getInstance().resolveErrorCodes(dataSource);
assertThat(sec).isNull();
verify(connection).close();
}
@@ -252,12 +258,9 @@ public class SQLErrorCodesFactoryTests {
SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes(dataSource);
assertIsEmpty(sec);
}
private void assertIsEmpty(SQLErrorCodes sec) {
// Codes should be empty
assertThat(sec.getBadSqlGrammarCodes().length).isEqualTo(0);
assertThat(sec.getDataIntegrityViolationCodes().length).isEqualTo(0);
sec = SQLErrorCodesFactory.getInstance().resolveErrorCodes(dataSource);
assertThat(sec).isNull();
}
private SQLErrorCodes getErrorCodesFromDataSource(String productName, SQLErrorCodesFactory factory) throws Exception {
@@ -270,17 +273,9 @@ public class SQLErrorCodesFactoryTests {
DataSource dataSource = mock(DataSource.class);
given(dataSource.getConnection()).willReturn(connection);
SQLErrorCodesFactory secf = null;
if (factory != null) {
secf = factory;
}
else {
secf = SQLErrorCodesFactory.getInstance();
}
SQLErrorCodesFactory secf = (factory != null ? factory : SQLErrorCodesFactory.getInstance());
SQLErrorCodes sec = secf.getErrorCodes(dataSource);
SQLErrorCodes sec2 = secf.getErrorCodes(dataSource);
assertThat(sec).as("Cached per DataSource").isSameAs(sec2);
@@ -375,4 +370,9 @@ public class SQLErrorCodesFactoryTests {
assertIsEmpty(sec);
}
private void assertIsEmpty(SQLErrorCodes sec) {
assertThat(sec.getBadSqlGrammarCodes().length).isEqualTo(0);
assertThat(sec.getDataIntegrityViolationCodes().length).isEqualTo(0);
}
}