From 61cb18a69c4201770e1a5bd8223ec94c475ac7f7 Mon Sep 17 00:00:00 2001 From: tfeiner Date: Fri, 6 May 2016 16:26:48 +0200 Subject: [PATCH 1/2] Use JOOQ's Spring DB name during exception translation The name of a JOOQ SQLDialect does not always match the name defined in sql-error-codes.xml. For example, the Postgres translator was not initialized correctly because in JOOQ the dialect is named SQLDialect.POSTGRES, but in sql-error-codes.xml the bean is named "PostgreSQL". This commit updates the translator to use the dialects third-party springDbName which ensures that it maps correctly to the entries in sql-error-codes.xml. Closes gh-5884 --- .../boot/autoconfigure/jooq/JooqExceptionTranslator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java index fbfa7192c69..ca0085a177f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java @@ -58,7 +58,7 @@ class JooqExceptionTranslator extends DefaultExecuteListener { private SQLExceptionTranslator getTranslator(ExecuteContext context) { SQLDialect dialect = context.configuration().dialect(); if (dialect != null) { - return new SQLErrorCodeSQLExceptionTranslator(dialect.name()); + return new SQLErrorCodeSQLExceptionTranslator(dialect.thirdParty().springDbName()); } return new SQLStateSQLExceptionTranslator(); } From 1ab835a0f7d608eb6bc767fe5f9975f5cddd7f9a Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 10 May 2016 12:51:05 +0100 Subject: [PATCH 2/2] Add tests for JooqExceptionTranslator See gh-5884 --- .../jooq/JooqExceptionTranslatorTests.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorTests.java diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorTests.java new file mode 100644 index 00000000000..65eb6564e57 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorTests.java @@ -0,0 +1,95 @@ +/* + * Copyright 2012-2016 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.jooq; + +import java.sql.SQLException; + +import org.jooq.Configuration; +import org.jooq.ExecuteContext; +import org.jooq.SQLDialect; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.mockito.ArgumentCaptor; + +import org.springframework.jdbc.BadSqlGrammarException; + +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +/** + * Tests for {@link JooqExceptionTranslator} + * + * @author Andy Wilkinson + */ + +@RunWith(Parameterized.class) +public class JooqExceptionTranslatorTests { + + private final JooqExceptionTranslator exceptionTranslator = new JooqExceptionTranslator(); + + private final SQLDialect dialect; + + private final SQLException sqlException; + + @Parameters(name = "{0}") + public static Object[] parameters() { + return new Object[] { new Object[] { SQLDialect.DERBY, sqlException("42802") }, + new Object[] { SQLDialect.H2, sqlException(42000) }, + new Object[] { SQLDialect.HSQLDB, sqlException(-22) }, + new Object[] { SQLDialect.MARIADB, sqlException(1054) }, + new Object[] { SQLDialect.MYSQL, sqlException(1054) }, + new Object[] { SQLDialect.POSTGRES, sqlException("03000") }, + new Object[] { SQLDialect.POSTGRES_9_3, sqlException("03000") }, + new Object[] { SQLDialect.POSTGRES_9_4, sqlException("03000") }, + new Object[] { SQLDialect.POSTGRES_9_5, sqlException("03000") } }; + } + + private static SQLException sqlException(String sqlState) { + return new SQLException(null, sqlState); + } + + private static SQLException sqlException(int vendorCode) { + return new SQLException(null, null, vendorCode); + + } + + public JooqExceptionTranslatorTests(SQLDialect dialect, SQLException sqlException) { + this.dialect = dialect; + this.sqlException = sqlException; + } + + @Test + public void exceptionTranslation() { + ExecuteContext context = mock(ExecuteContext.class); + Configuration configuration = mock(Configuration.class); + given(context.configuration()).willReturn(configuration); + given(configuration.dialect()).willReturn(this.dialect); + given(context.sqlException()).willReturn(this.sqlException); + this.exceptionTranslator.exception(context); + ArgumentCaptor captor = ArgumentCaptor + .forClass(RuntimeException.class); + verify(context).exception(captor.capture()); + assertThat(captor.getValue(), is(instanceOf(BadSqlGrammarException.class))); + } + +}