Polishing

This commit is contained in:
Sam Brannen 2023-11-06 15:29:48 +01:00
parent dc26d3b0ec
commit 5752e03d97
4 changed files with 130 additions and 136 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2023 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.
@ -16,7 +16,6 @@
package org.springframework.core; package org.springframework.core;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function; import java.util.function.Function;
@ -82,10 +81,7 @@ class AttributeAccessorSupportTests {
void attributeNames() { void attributeNames() {
this.attributeAccessor.setAttribute(NAME, VALUE); this.attributeAccessor.setAttribute(NAME, VALUE);
this.attributeAccessor.setAttribute("abc", "123"); this.attributeAccessor.setAttribute("abc", "123");
String[] attributeNames = this.attributeAccessor.attributeNames(); assertThat(this.attributeAccessor.attributeNames()).contains("abc", NAME);
Arrays.sort(attributeNames);
assertThat(Arrays.binarySearch(attributeNames, "abc")).isEqualTo(0);
assertThat(Arrays.binarySearch(attributeNames, NAME)).isEqualTo(1);
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")

View File

@ -19,7 +19,6 @@ package org.springframework.jdbc.support;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Arrays;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -41,108 +40,108 @@ import static org.mockito.Mockito.verify;
* @author Thomas Risberg * @author Thomas Risberg
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen
*/ */
public class SQLErrorCodesFactoryTests { class SQLErrorCodesFactoryTests {
/** /**
* Check that a default instance returns empty error codes for an unknown database. * Check that a default instance returns empty error codes for an unknown database.
*/ */
@Test @Test
public void testDefaultInstanceWithNoSuchDatabase() { void defaultInstanceWithNoSuchDatabase() {
SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes("xx"); SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes("xx");
assertThat(sec.getBadSqlGrammarCodes().length).isEqualTo(0); assertThat(sec.getBadSqlGrammarCodes()).isEmpty();
assertThat(sec.getDataIntegrityViolationCodes().length).isEqualTo(0); assertThat(sec.getDataIntegrityViolationCodes()).isEmpty();
} }
/** /**
* Check that a known database produces recognizable codes. * Check that a known database produces recognizable codes.
*/ */
@Test @Test
public void testDefaultInstanceWithOracle() { void defaultInstanceWithOracle() {
SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes("Oracle"); SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes("Oracle");
assertIsOracle(sec); assertIsOracle(sec);
} }
private void assertIsOracle(SQLErrorCodes sec) { private void assertIsOracle(SQLErrorCodes sec) {
assertThat(sec.getBadSqlGrammarCodes().length).isGreaterThan(0); assertThat(sec.getBadSqlGrammarCodes()).isNotEmpty();
assertThat(sec.getDataIntegrityViolationCodes().length).isGreaterThan(0); assertThat(sec.getDataIntegrityViolationCodes()).isNotEmpty();
// These had better be a Bad SQL Grammar code // These had better be a Bad SQL Grammar code
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "942")).isGreaterThanOrEqualTo(0); assertThat(sec.getBadSqlGrammarCodes()).contains("942");
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "6550")).isGreaterThanOrEqualTo(0); assertThat(sec.getBadSqlGrammarCodes()).contains("6550");
// This had better NOT be // This had better NOT be
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "9xx42")).isLessThan(0); assertThat(sec.getBadSqlGrammarCodes()).doesNotContain("9xx42");
} }
private void assertIsSQLServer(SQLErrorCodes sec) { private void assertIsSQLServer(SQLErrorCodes sec) {
assertThat(sec.getDatabaseProductName()).isEqualTo("Microsoft SQL Server"); assertThat(sec.getDatabaseProductName()).isEqualTo("Microsoft SQL Server");
assertThat(sec.getBadSqlGrammarCodes().length).isGreaterThan(0); assertThat(sec.getBadSqlGrammarCodes()).isNotEmpty();
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "156")).isGreaterThanOrEqualTo(0); assertThat(sec.getBadSqlGrammarCodes()).contains("156");
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "170")).isGreaterThanOrEqualTo(0); assertThat(sec.getBadSqlGrammarCodes()).contains("170");
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "207")).isGreaterThanOrEqualTo(0); assertThat(sec.getBadSqlGrammarCodes()).contains("207");
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "208")).isGreaterThanOrEqualTo(0); assertThat(sec.getBadSqlGrammarCodes()).contains("208");
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "209")).isGreaterThanOrEqualTo(0); assertThat(sec.getBadSqlGrammarCodes()).contains("209");
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "9xx42")).isLessThan(0); assertThat(sec.getBadSqlGrammarCodes()).doesNotContain("9xx42");
assertThat(sec.getPermissionDeniedCodes().length).isGreaterThan(0); assertThat(sec.getPermissionDeniedCodes()).isNotEmpty();
assertThat(Arrays.binarySearch(sec.getPermissionDeniedCodes(), "229")).isGreaterThanOrEqualTo(0); assertThat(sec.getPermissionDeniedCodes()).contains("229");
assertThat(sec.getDuplicateKeyCodes().length).isGreaterThan(0); assertThat(sec.getDuplicateKeyCodes()).isNotEmpty();
assertThat(Arrays.binarySearch(sec.getDuplicateKeyCodes(), "2601")).isGreaterThanOrEqualTo(0); assertThat(sec.getDuplicateKeyCodes()).contains("2601");
assertThat(Arrays.binarySearch(sec.getDuplicateKeyCodes(), "2627")).isGreaterThanOrEqualTo(0); assertThat(sec.getDuplicateKeyCodes()).contains("2627");
assertThat(sec.getDataIntegrityViolationCodes().length).isGreaterThan(0); assertThat(sec.getDataIntegrityViolationCodes()).isNotEmpty();
assertThat(Arrays.binarySearch(sec.getDataIntegrityViolationCodes(), "544")).isGreaterThanOrEqualTo(0); assertThat(sec.getDataIntegrityViolationCodes()).contains("544");
assertThat(Arrays.binarySearch(sec.getDataIntegrityViolationCodes(), "8114")).isGreaterThanOrEqualTo(0); assertThat(sec.getDataIntegrityViolationCodes()).contains("8114");
assertThat(Arrays.binarySearch(sec.getDataIntegrityViolationCodes(), "8115")).isGreaterThanOrEqualTo(0); assertThat(sec.getDataIntegrityViolationCodes()).contains("8115");
assertThat(sec.getDataAccessResourceFailureCodes().length).isGreaterThan(0); assertThat(sec.getDataAccessResourceFailureCodes()).isNotEmpty();
assertThat(Arrays.binarySearch(sec.getDataAccessResourceFailureCodes(), "4060")).isGreaterThanOrEqualTo(0); assertThat(sec.getDataAccessResourceFailureCodes()).contains("4060");
assertThat(sec.getCannotAcquireLockCodes().length).isGreaterThan(0); assertThat(sec.getCannotAcquireLockCodes()).isNotEmpty();
assertThat(Arrays.binarySearch(sec.getCannotAcquireLockCodes(), "1222")).isGreaterThanOrEqualTo(0); assertThat(sec.getCannotAcquireLockCodes()).contains("1222");
assertThat(sec.getDeadlockLoserCodes().length).isGreaterThan(0); assertThat(sec.getDeadlockLoserCodes()).isNotEmpty();
assertThat(Arrays.binarySearch(sec.getDeadlockLoserCodes(), "1205")).isGreaterThanOrEqualTo(0); assertThat(sec.getDeadlockLoserCodes()).contains("1205");
} }
private void assertIsHsql(SQLErrorCodes sec) { private void assertIsHsql(SQLErrorCodes sec) {
assertThat(sec.getBadSqlGrammarCodes().length).isGreaterThan(0); assertThat(sec.getBadSqlGrammarCodes()).isNotEmpty();
assertThat(sec.getDataIntegrityViolationCodes().length).isGreaterThan(0); assertThat(sec.getDataIntegrityViolationCodes()).isNotEmpty();
// This had better be a Bad SQL Grammar code // This had better be a Bad SQL Grammar code
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "-22")).isGreaterThanOrEqualTo(0); assertThat(sec.getBadSqlGrammarCodes()).contains("-22");
// This had better NOT be // This had better NOT be
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "-9")).isLessThan(0); assertThat(sec.getBadSqlGrammarCodes()).doesNotContain("-9");
} }
private void assertIsDB2(SQLErrorCodes sec) { private void assertIsDB2(SQLErrorCodes sec) {
assertThat(sec.getBadSqlGrammarCodes().length).isGreaterThan(0); assertThat(sec.getBadSqlGrammarCodes()).isNotEmpty();
assertThat(sec.getDataIntegrityViolationCodes().length).isGreaterThan(0); assertThat(sec.getDataIntegrityViolationCodes()).isNotEmpty();
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "942")).isLessThan(0); assertThat(sec.getBadSqlGrammarCodes()).doesNotContain("942");
// This had better NOT be // This had better NOT be
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "-204")).isGreaterThanOrEqualTo(0); assertThat(sec.getBadSqlGrammarCodes()).contains("-204");
} }
private void assertIsHana(SQLErrorCodes sec) { private void assertIsHana(SQLErrorCodes sec) {
assertThat(sec.getBadSqlGrammarCodes().length).isGreaterThan(0); assertThat(sec.getBadSqlGrammarCodes()).isNotEmpty();
assertThat(sec.getDataIntegrityViolationCodes().length).isGreaterThan(0); assertThat(sec.getDataIntegrityViolationCodes()).isNotEmpty();
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "368")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getPermissionDeniedCodes(), "10")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getDuplicateKeyCodes(), "301")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getDataIntegrityViolationCodes(), "461")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getDataAccessResourceFailureCodes(), "-813")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getInvalidResultSetAccessCodes(), "582")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getCannotAcquireLockCodes(), "131")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getCannotSerializeTransactionCodes(), "138")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getDeadlockLoserCodes(), "133")).isGreaterThanOrEqualTo(0);
assertThat(sec.getBadSqlGrammarCodes()).contains("368");
assertThat(sec.getPermissionDeniedCodes()).contains("10");
assertThat(sec.getDuplicateKeyCodes()).contains("301");
assertThat(sec.getDataIntegrityViolationCodes()).contains("461");
assertThat(sec.getDataAccessResourceFailureCodes()).contains("-813");
assertThat(sec.getInvalidResultSetAccessCodes()).contains("582");
assertThat(sec.getCannotAcquireLockCodes()).contains("131");
assertThat(sec.getCannotSerializeTransactionCodes()).contains("138");
assertThat(sec.getDeadlockLoserCodes()).contains("133");
} }
@Test @Test
public void testLookupOrder() { void lookupOrder() {
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory { class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
private int lookups = 0; private int lookups = 0;
@Override @Override
@ -163,15 +162,15 @@ public class SQLErrorCodesFactoryTests {
// Should have failed to load without error // Should have failed to load without error
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory(); TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory();
assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes().length).isEqualTo(0); assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes()).isEmpty();
assertThat(sf.getErrorCodes("Oracle").getDataIntegrityViolationCodes().length).isEqualTo(0); assertThat(sf.getErrorCodes("Oracle").getDataIntegrityViolationCodes()).isEmpty();
} }
/** /**
* Check that user defined error codes take precedence. * Check that user defined error codes take precedence.
*/ */
@Test @Test
public void testFindUserDefinedCodes() { void findUserDefinedCodes() {
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory { class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
@Override @Override
protected Resource loadResource(String path) { protected Resource loadResource(String path) {
@ -184,14 +183,12 @@ public class SQLErrorCodesFactoryTests {
// Should have loaded without error // Should have loaded without error
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory(); TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory();
assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes().length).isEqualTo(0); assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes()).isEmpty();
assertThat(sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()).hasSize(2); assertThat(sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()).containsExactly("1", "2");
assertThat(sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()[0]).isEqualTo("1");
assertThat(sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()[1]).isEqualTo("2");
} }
@Test @Test
public void testInvalidUserDefinedCodeFormat() { void invalidUserDefinedCodeFormat() {
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory { class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
@Override @Override
protected Resource loadResource(String path) { protected Resource loadResource(String path) {
@ -205,7 +202,7 @@ public class SQLErrorCodesFactoryTests {
// Should have failed to load without error // Should have failed to load without error
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory(); TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory();
assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes().length).isEqualTo(0); assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes()).isEmpty();
assertThat(sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()).isEmpty(); assertThat(sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()).isEmpty();
} }
@ -213,7 +210,7 @@ public class SQLErrorCodesFactoryTests {
* Check that custom error codes take precedence. * Check that custom error codes take precedence.
*/ */
@Test @Test
public void testFindCustomCodes() { void findCustomCodes() {
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory { class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
@Override @Override
protected Resource loadResource(String path) { protected Resource loadResource(String path) {
@ -227,14 +224,13 @@ public class SQLErrorCodesFactoryTests {
// Should have loaded without error // Should have loaded without error
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory(); TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory();
assertThat(sf.getErrorCodes("Oracle").getCustomTranslations()).hasSize(1); assertThat(sf.getErrorCodes("Oracle").getCustomTranslations()).hasSize(1);
CustomSQLErrorCodesTranslation translation = CustomSQLErrorCodesTranslation translation = sf.getErrorCodes("Oracle").getCustomTranslations()[0];
sf.getErrorCodes("Oracle").getCustomTranslations()[0];
assertThat(translation.getExceptionClass()).isEqualTo(CustomErrorCodeException.class); assertThat(translation.getExceptionClass()).isEqualTo(CustomErrorCodeException.class);
assertThat(translation.getErrorCodes()).hasSize(1); assertThat(translation.getErrorCodes()).hasSize(1);
} }
@Test @Test
public void testDataSourceWithNullMetadata() throws Exception { void dataSourceWithNullMetadata() throws Exception {
Connection connection = mock(); Connection connection = mock();
DataSource dataSource = mock(); DataSource dataSource = mock();
given(dataSource.getConnection()).willReturn(connection); given(dataSource.getConnection()).willReturn(connection);
@ -250,7 +246,7 @@ public class SQLErrorCodesFactoryTests {
} }
@Test @Test
public void testGetFromDataSourceWithSQLException() throws Exception { void getFromDataSourceWithSQLException() throws Exception {
SQLException expectedSQLException = new SQLException(); SQLException expectedSQLException = new SQLException();
DataSource dataSource = mock(); DataSource dataSource = mock();
@ -284,25 +280,25 @@ public class SQLErrorCodesFactoryTests {
} }
@Test @Test
public void testSQLServerRecognizedFromMetadata() throws Exception { void sqlServerRecognizedFromMetadata() throws Exception {
SQLErrorCodes sec = getErrorCodesFromDataSource("MS-SQL", null); SQLErrorCodes sec = getErrorCodesFromDataSource("MS-SQL", null);
assertIsSQLServer(sec); assertIsSQLServer(sec);
} }
@Test @Test
public void testOracleRecognizedFromMetadata() throws Exception { void oracleRecognizedFromMetadata() throws Exception {
SQLErrorCodes sec = getErrorCodesFromDataSource("Oracle", null); SQLErrorCodes sec = getErrorCodesFromDataSource("Oracle", null);
assertIsOracle(sec); assertIsOracle(sec);
} }
@Test @Test
public void testHsqlRecognizedFromMetadata() throws Exception { void hsqlRecognizedFromMetadata() throws Exception {
SQLErrorCodes sec = getErrorCodesFromDataSource("HSQL Database Engine", null); SQLErrorCodes sec = getErrorCodesFromDataSource("HSQL Database Engine", null);
assertIsHsql(sec); assertIsHsql(sec);
} }
@Test @Test
public void testDB2RecognizedFromMetadata() throws Exception { void dB2RecognizedFromMetadata() throws Exception {
SQLErrorCodes sec = getErrorCodesFromDataSource("DB2", null); SQLErrorCodes sec = getErrorCodesFromDataSource("DB2", null);
assertIsDB2(sec); assertIsDB2(sec);
sec = getErrorCodesFromDataSource("DB2/", null); sec = getErrorCodesFromDataSource("DB2/", null);
@ -312,7 +308,7 @@ public class SQLErrorCodesFactoryTests {
} }
@Test @Test
public void testHanaIsRecognizedFromMetadata() throws Exception { void hanaIsRecognizedFromMetadata() throws Exception {
SQLErrorCodes sec = getErrorCodesFromDataSource("SAP DB", null); SQLErrorCodes sec = getErrorCodesFromDataSource("SAP DB", null);
assertIsHana(sec); assertIsHana(sec);
} }
@ -321,7 +317,7 @@ public class SQLErrorCodesFactoryTests {
* Check that wild card database name works. * Check that wild card database name works.
*/ */
@Test @Test
public void testWildCardNameRecognized() throws Exception { void wildCardNameRecognized() throws Exception {
class WildcardSQLErrorCodesFactory extends SQLErrorCodesFactory { class WildcardSQLErrorCodesFactory extends SQLErrorCodesFactory {
@Override @Override
protected Resource loadResource(String path) { protected Resource loadResource(String path) {

View File

@ -34,86 +34,89 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/** /**
* Tests for {@link SQLStateSQLExceptionTranslator}.
*
* @author Rick Evans * @author Rick Evans
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams * @author Chris Beams
*/ */
public class SQLStateSQLExceptionTranslatorTests { class SQLStateSQLExceptionTranslatorTests {
private final SQLExceptionTranslator translator = new SQLStateSQLExceptionTranslator();
@Test @Test
public void translateNullException() { void translateNullException() {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() -> translator.translate("", "", null));
new SQLStateSQLExceptionTranslator().translate("", "", null));
} }
@Test @Test
public void translateBadSqlGrammar() { void translateBadSqlGrammar() {
doTest("07", BadSqlGrammarException.class); assertTranslation("07", BadSqlGrammarException.class);
} }
@Test @Test
public void translateDataIntegrityViolation() { void translateDataIntegrityViolation() {
doTest("23", DataIntegrityViolationException.class); assertTranslation("23", DataIntegrityViolationException.class);
} }
@Test @Test
public void translateDuplicateKey() { void translateDuplicateKey() {
doTest("23505", DuplicateKeyException.class); assertTranslation("23505", DuplicateKeyException.class);
} }
@Test @Test
public void translateDuplicateKeyOracle() { void translateDuplicateKeyOracle() {
doTest("23000", 1, DuplicateKeyException.class); assertTranslation("23000", 1, DuplicateKeyException.class);
} }
@Test @Test
public void translateDuplicateKeyMySQL() { void translateDuplicateKeyMySQL() {
doTest("23000", 1062, DuplicateKeyException.class); assertTranslation("23000", 1062, DuplicateKeyException.class);
} }
@Test @Test
public void translateDuplicateKeyMSSQL1() { void translateDuplicateKeyMSSQL1() {
doTest("23000", 2601, DuplicateKeyException.class); assertTranslation("23000", 2601, DuplicateKeyException.class);
} }
@Test @Test
public void translateDuplicateKeyMSSQL2() { void translateDuplicateKeyMSSQL2() {
doTest("23000", 2627, DuplicateKeyException.class); assertTranslation("23000", 2627, DuplicateKeyException.class);
} }
@Test @Test
public void translateDuplicateKeySapHana() { void translateDuplicateKeySapHana() {
doTest("23000", 301, DuplicateKeyException.class); assertTranslation("23000", 301, DuplicateKeyException.class);
} }
@Test @Test
public void translateDataAccessResourceFailure() { void translateDataAccessResourceFailure() {
doTest("53", DataAccessResourceFailureException.class); assertTranslation("53", DataAccessResourceFailureException.class);
} }
@Test @Test
public void translateTransientDataAccessResourceFailure() { void translateTransientDataAccessResourceFailure() {
doTest("S1", TransientDataAccessResourceException.class); assertTranslation("S1", TransientDataAccessResourceException.class);
} }
@Test @Test
public void translatePessimisticLockingFailure() { void translatePessimisticLockingFailure() {
doTest("40", PessimisticLockingFailureException.class); assertTranslation("40", PessimisticLockingFailureException.class);
} }
@Test @Test
public void translateCannotAcquireLock() { void translateCannotAcquireLock() {
doTest("40001", CannotAcquireLockException.class); assertTranslation("40001", CannotAcquireLockException.class);
} }
@Test @Test
public void translateUncategorized() { void translateUncategorized() {
doTest("00000000", null); assertTranslation("00000000", null);
} }
@Test @Test
public void invalidSqlStateCode() { void invalidSqlStateCode() {
doTest("NO SUCH CODE", null); assertTranslation("NO SUCH CODE", null);
} }
/** /**
@ -122,19 +125,18 @@ public class SQLStateSQLExceptionTranslatorTests {
* Bug 729170 * Bug 729170
*/ */
@Test @Test
public void malformedSqlStateCodes() { void malformedSqlStateCodes() {
doTest(null, null); assertTranslation(null, null);
doTest("", null); assertTranslation("", null);
doTest("I", null); assertTranslation("I", null);
} }
private void doTest(@Nullable String sqlState, @Nullable Class<?> dataAccessExceptionType) { private void assertTranslation(@Nullable String sqlState, @Nullable Class<?> dataAccessExceptionType) {
doTest(sqlState, 0, dataAccessExceptionType); assertTranslation(sqlState, 0, dataAccessExceptionType);
} }
private void doTest(@Nullable String sqlState, int errorCode, @Nullable Class<?> dataAccessExceptionType) { private void assertTranslation(@Nullable String sqlState, int errorCode, @Nullable Class<?> dataAccessExceptionType) {
SQLExceptionTranslator translator = new SQLStateSQLExceptionTranslator();
SQLException ex = new SQLException("reason", sqlState, errorCode); SQLException ex = new SQLException("reason", sqlState, errorCode);
DataAccessException dax = translator.translate("task", "SQL", ex); DataAccessException dax = translator.translate("task", "SQL", ex);

View File

@ -45,17 +45,17 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Mark Paluch * @author Mark Paluch
* @author Juergen Hoeller * @author Juergen Hoeller
*/ */
public class ConnectionFactoryUtilsUnitTests { class ConnectionFactoryUtilsUnitTests {
@Test @Test
public void shouldTranslateTransientResourceException() { void shouldTranslateTransientResourceException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "", Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcTransientResourceException("")); new R2dbcTransientResourceException(""));
assertThat(exception).isExactlyInstanceOf(TransientDataAccessResourceException.class); assertThat(exception).isExactlyInstanceOf(TransientDataAccessResourceException.class);
} }
@Test @Test
public void shouldTranslateRollbackException() { void shouldTranslateRollbackException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "", Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcRollbackException()); new R2dbcRollbackException());
assertThat(exception).isExactlyInstanceOf(PessimisticLockingFailureException.class); assertThat(exception).isExactlyInstanceOf(PessimisticLockingFailureException.class);
@ -66,28 +66,28 @@ public class ConnectionFactoryUtilsUnitTests {
} }
@Test @Test
public void shouldTranslateTimeoutException() { void shouldTranslateTimeoutException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "", Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcTimeoutException()); new R2dbcTimeoutException());
assertThat(exception).isExactlyInstanceOf(QueryTimeoutException.class); assertThat(exception).isExactlyInstanceOf(QueryTimeoutException.class);
} }
@Test @Test
public void shouldNotTranslateUnknownExceptions() { void shouldNotTranslateUnknownExceptions() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "", Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new MyTransientException()); new MyTransientException());
assertThat(exception).isExactlyInstanceOf(UncategorizedR2dbcException.class); assertThat(exception).isExactlyInstanceOf(UncategorizedR2dbcException.class);
} }
@Test @Test
public void shouldTranslateNonTransientResourceException() { void shouldTranslateNonTransientResourceException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "", Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcNonTransientResourceException()); new R2dbcNonTransientResourceException());
assertThat(exception).isExactlyInstanceOf(DataAccessResourceFailureException.class); assertThat(exception).isExactlyInstanceOf(DataAccessResourceFailureException.class);
} }
@Test @Test
public void shouldTranslateIntegrityViolationException() { void shouldTranslateIntegrityViolationException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "", Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException()); new R2dbcDataIntegrityViolationException());
assertThat(exception).isExactlyInstanceOf(DataIntegrityViolationException.class); assertThat(exception).isExactlyInstanceOf(DataIntegrityViolationException.class);
@ -98,7 +98,7 @@ public class ConnectionFactoryUtilsUnitTests {
exception = ConnectionFactoryUtils.convertR2dbcException("", "", exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 1)); new R2dbcDataIntegrityViolationException("reason", "23000", 1));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class); assertThat(exception).as("Oracle").isExactlyInstanceOf(DuplicateKeyException.class);
exception = ConnectionFactoryUtils.convertR2dbcException("", "", exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 301)); new R2dbcDataIntegrityViolationException("reason", "23000", 301));
@ -106,33 +106,33 @@ public class ConnectionFactoryUtilsUnitTests {
exception = ConnectionFactoryUtils.convertR2dbcException("", "", exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 1062)); new R2dbcDataIntegrityViolationException("reason", "23000", 1062));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class); assertThat(exception).as("MySQL/MariaDB").isExactlyInstanceOf(DuplicateKeyException.class);
exception = ConnectionFactoryUtils.convertR2dbcException("", "", exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 2601)); new R2dbcDataIntegrityViolationException("reason", "23000", 2601));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class); assertThat(exception).as("MS SQL Server").isExactlyInstanceOf(DuplicateKeyException.class);
exception = ConnectionFactoryUtils.convertR2dbcException("", "", exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 2627)); new R2dbcDataIntegrityViolationException("reason", "23000", 2627));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class); assertThat(exception).as("MS SQL Server").isExactlyInstanceOf(DuplicateKeyException.class);
} }
@Test @Test
public void shouldTranslatePermissionDeniedException() { void shouldTranslatePermissionDeniedException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "", Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcPermissionDeniedException()); new R2dbcPermissionDeniedException());
assertThat(exception).isExactlyInstanceOf(PermissionDeniedDataAccessException.class); assertThat(exception).isExactlyInstanceOf(PermissionDeniedDataAccessException.class);
} }
@Test @Test
public void shouldTranslateBadSqlGrammarException() { void shouldTranslateBadSqlGrammarException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "", Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcBadGrammarException()); new R2dbcBadGrammarException());
assertThat(exception).isExactlyInstanceOf(BadSqlGrammarException.class); assertThat(exception).isExactlyInstanceOf(BadSqlGrammarException.class);
} }
@Test @Test
public void messageGeneration() { void messageGeneration() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK", Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK",
"SOME-SQL", new R2dbcTransientResourceException("MESSAGE")); "SOME-SQL", new R2dbcTransientResourceException("MESSAGE"));
assertThat(exception).isExactlyInstanceOf( assertThat(exception).isExactlyInstanceOf(
@ -140,7 +140,7 @@ public class ConnectionFactoryUtilsUnitTests {
} }
@Test @Test
public void messageGenerationNullSQL() { void messageGenerationNullSQL() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK", null, Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK", null,
new R2dbcTransientResourceException("MESSAGE")); new R2dbcTransientResourceException("MESSAGE"));
assertThat(exception).isExactlyInstanceOf( assertThat(exception).isExactlyInstanceOf(
@ -148,7 +148,7 @@ public class ConnectionFactoryUtilsUnitTests {
} }
@Test @Test
public void messageGenerationNullMessage() { void messageGenerationNullMessage() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK", Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK",
"SOME-SQL", new R2dbcTransientResourceException()); "SOME-SQL", new R2dbcTransientResourceException());
assertThat(exception).isExactlyInstanceOf( assertThat(exception).isExactlyInstanceOf(