From 371e3a7ac0ef88e4da7f7a5dbdf845e66bbe0864 Mon Sep 17 00:00:00 2001 From: Jan Stamer Date: Mon, 25 Jun 2012 12:52:59 +0200 Subject: [PATCH 1/2] Add exception translator for EclipseLink exceptions Issue: SPR-9541 --- .../EclipseLinkExceptionTranslator.java | 51 ++++++++++++++++++ .../EclipseLinkSystemException.java | 42 +++++++++++++++ .../orm/eclipselink/EclipseLinkUtils.java | 42 +++++++++++++++ .../EclipseLinkExceptionTranslatorTests.java | 41 +++++++++++++++ .../EclipseLinkSytemExceptionTests.java | 52 +++++++++++++++++++ .../eclipselink/EclipseLinkUtilsTests.java | 38 ++++++++++++++ 6 files changed, 266 insertions(+) create mode 100644 spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslator.java create mode 100644 spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkSystemException.java create mode 100644 spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkUtils.java create mode 100644 spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslatorTests.java create mode 100644 spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkSytemExceptionTests.java create mode 100644 spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkUtilsTests.java diff --git a/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslator.java b/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslator.java new file mode 100644 index 0000000000..cde8ee3d27 --- /dev/null +++ b/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslator.java @@ -0,0 +1,51 @@ +/* + * Copyright 2002-2012 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.orm.eclipselink; + +import org.eclipse.persistence.exceptions.EclipseLinkException; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.support.PersistenceExceptionTranslator; + +/** + * {@link PersistenceExceptionTranslator} capable of translating {@link EclipseLinkException} + * instances to Spring's {@link DataAccessException} hierarchy. + * + * @author Jan Stamer + * @since 3.2 + * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor + */ +public class EclipseLinkExceptionTranslator implements PersistenceExceptionTranslator { + + public DataAccessException translateExceptionIfPossible(RuntimeException ex) { + if (ex instanceof EclipseLinkException) { + return convertEclipseLinkAccessException((EclipseLinkException) ex); + } + return null; + } + + /** + * Convert the given EclipseLinkException to an appropriate exception from + * the {@code org.springframework.dao} hierarchy. + * @param ex EclipseLinkException that occurred + * @return a corresponding DataAccessException + * @see SessionFactoryUtils#convertEclipseLinkAccessException + */ + protected DataAccessException convertEclipseLinkAccessException(EclipseLinkException ex) { + return EclipseLinkUtils.convertEclipseLinkAccessException(ex); + } + +} diff --git a/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkSystemException.java b/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkSystemException.java new file mode 100644 index 0000000000..27c223751c --- /dev/null +++ b/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkSystemException.java @@ -0,0 +1,42 @@ +/* + * Copyright 2002-2012 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.orm.eclipselink; + +import org.eclipse.persistence.exceptions.EclipseLinkException; +import org.springframework.dao.UncategorizedDataAccessException; + +/** + * EclipseLink-specific subclass of UncategorizedDataAccessException, for + * EclipseLink system errors that do not match any concrete + * org.springframework.dao exceptions. + * + * @author Jan Stamer + * @since 3.2 + * @see EclipseLinkUtils#convertEclipseLinkAccessException(EclipseLinkException) + */ +public class EclipseLinkSystemException extends UncategorizedDataAccessException { + + /** + * Create a new HibernateSystemException, wrapping an arbitrary + * HibernateException. + * @param cause the HibernateException thrown + */ + public EclipseLinkSystemException(EclipseLinkException cause) { + super(cause != null ? cause.getMessage() : null, cause); + } + +} diff --git a/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkUtils.java b/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkUtils.java new file mode 100644 index 0000000000..cdb225b58f --- /dev/null +++ b/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkUtils.java @@ -0,0 +1,42 @@ +/* + * Copyright 2002-2012 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.orm.eclipselink; + +import org.eclipse.persistence.exceptions.EclipseLinkException; +import org.springframework.dao.DataAccessException; + +/** + * Helper class featuring methods for Eclipse Link. Also provides support for + * exception translation. + * + * @author Jan Stamer + * @since 3.2 + */ +public abstract class EclipseLinkUtils { + + /** + * Convert the given EclipseLinkException to an appropriate exception from + * the org.springframework.dao hierarchy. + * @param ex EclipseLinkException that occured + * @return the corresponding DataAccessException instance + * @see EclipseLinkExceptionTranslator#convertEclipseLinkAccessException(EclipseLinkException) + */ + public static DataAccessException convertEclipseLinkAccessException(EclipseLinkException ex) { + return new EclipseLinkSystemException(ex); + } + +} diff --git a/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslatorTests.java b/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslatorTests.java new file mode 100644 index 0000000000..ff61607455 --- /dev/null +++ b/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslatorTests.java @@ -0,0 +1,41 @@ +/* + * Copyright 2002-2012 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.orm.eclipselink; + +import junit.framework.TestCase; + +import org.eclipse.persistence.exceptions.DatabaseException; +import org.springframework.dao.DataAccessException; + +/** + * @author Jan Stamer + * @since 3.2 + */ +public class EclipseLinkExceptionTranslatorTests extends TestCase { + + public void testWithWrongException() { + EclipseLinkExceptionTranslator exceptionTranslator = new EclipseLinkExceptionTranslator(); + assertNull(exceptionTranslator.translateExceptionIfPossible(new IllegalArgumentException())); + } + + public void testWithEclipseLinkException() { + EclipseLinkExceptionTranslator exceptionTranslator = new EclipseLinkExceptionTranslator(); + assertNotNull(exceptionTranslator.translateExceptionIfPossible(DatabaseException.databaseAccessorNotConnected())); + assertTrue(exceptionTranslator.translateExceptionIfPossible(DatabaseException.databaseAccessorNotConnected()) instanceof DataAccessException); + } + +} diff --git a/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkSytemExceptionTests.java b/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkSytemExceptionTests.java new file mode 100644 index 0000000000..79780b8393 --- /dev/null +++ b/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkSytemExceptionTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2002-2012 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.orm.eclipselink; + +import junit.framework.TestCase; + +import org.eclipse.persistence.exceptions.DatabaseException; +import org.hibernate.HibernateException; + +/** + * @author Jan Stamer + * @since 3.2 + */ +public class EclipseLinkSytemExceptionTests extends TestCase { + + public void testWithNull() { + EclipseLinkSystemException exception = new EclipseLinkSystemException(null); + assertNull(exception.getCause()); + assertNull(exception.getMessage()); + } + + public void testCreateWithCause() { + DatabaseException dbExceptionWithCause = new DatabaseException("my custom exception cause") { + }; + EclipseLinkSystemException elSystemException = new EclipseLinkSystemException(dbExceptionWithCause); + assertEquals(dbExceptionWithCause, elSystemException.getCause()); + assertTrue(elSystemException.getMessage().contains("my custom exception cause")); + } + + public void testCreateWithNullCause() throws HibernateException { + DatabaseException dbExceptionWithCause = new DatabaseException((String) null) { + }; + EclipseLinkSystemException elSystemException = new EclipseLinkSystemException(dbExceptionWithCause); + assertEquals(dbExceptionWithCause, elSystemException.getCause()); + assertTrue(elSystemException.getMessage().contains("null")); + } + +} diff --git a/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkUtilsTests.java b/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkUtilsTests.java new file mode 100644 index 0000000000..ee9141fa55 --- /dev/null +++ b/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkUtilsTests.java @@ -0,0 +1,38 @@ +/* + * Copyright 2002-2012 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.orm.eclipselink; + +import junit.framework.TestCase; + +import org.eclipse.persistence.exceptions.DatabaseException; +import org.springframework.dao.DataAccessException; + +/** + * @author Jan Stamer + * @since 3.2 + */ +public class EclipseLinkUtilsTests extends TestCase { + + public void testWithNull() { + assertTrue(EclipseLinkUtils.convertEclipseLinkAccessException(null) instanceof DataAccessException); + } + + public void testWithEclipseLinkException() { + assertTrue(EclipseLinkUtils.convertEclipseLinkAccessException(DatabaseException.databaseAccessorNotConnected()) instanceof DataAccessException); + } + +} From e18308851d3b9a3208d4394f3dfb9ff3b872f780 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 6 May 2014 16:04:39 +0200 Subject: [PATCH 2/2] polishing Updating pull request for SPR-9541 --- .../EclipseLinkExceptionTranslator.java | 6 +++--- .../EclipseLinkSystemException.java | 7 ++++--- .../orm/eclipselink/EclipseLinkUtils.java | 4 ++-- .../EclipseLinkExceptionTranslatorTests.java | 19 +++++++++++-------- ...a => EclipseLinkSystemExceptionTests.java} | 18 +++++++++++------- .../eclipselink/EclipseLinkUtilsTests.java | 18 ++++++++++-------- 6 files changed, 41 insertions(+), 31 deletions(-) rename spring-orm/src/test/java/org/springframework/orm/eclipselink/{EclipseLinkSytemExceptionTests.java => EclipseLinkSystemExceptionTests.java} (82%) diff --git a/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslator.java b/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslator.java index cde8ee3d27..6bfcf16983 100644 --- a/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslator.java +++ b/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -25,7 +25,7 @@ import org.springframework.dao.support.PersistenceExceptionTranslator; * instances to Spring's {@link DataAccessException} hierarchy. * * @author Jan Stamer - * @since 3.2 + * @since 4.1 * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor */ public class EclipseLinkExceptionTranslator implements PersistenceExceptionTranslator { @@ -42,7 +42,7 @@ public class EclipseLinkExceptionTranslator implements PersistenceExceptionTrans * the {@code org.springframework.dao} hierarchy. * @param ex EclipseLinkException that occurred * @return a corresponding DataAccessException - * @see SessionFactoryUtils#convertEclipseLinkAccessException + * @see EclipseLinkUtils#convertEclipseLinkAccessException */ protected DataAccessException convertEclipseLinkAccessException(EclipseLinkException ex) { return EclipseLinkUtils.convertEclipseLinkAccessException(ex); diff --git a/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkSystemException.java b/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkSystemException.java index 27c223751c..4392b3de20 100644 --- a/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkSystemException.java +++ b/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkSystemException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -25,14 +25,15 @@ import org.springframework.dao.UncategorizedDataAccessException; * org.springframework.dao exceptions. * * @author Jan Stamer - * @since 3.2 + * @since 4.1 * @see EclipseLinkUtils#convertEclipseLinkAccessException(EclipseLinkException) */ +@SuppressWarnings("serial") public class EclipseLinkSystemException extends UncategorizedDataAccessException { /** * Create a new HibernateSystemException, wrapping an arbitrary - * HibernateException. + * {@link EclipseLinkException}. * @param cause the HibernateException thrown */ public EclipseLinkSystemException(EclipseLinkException cause) { diff --git a/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkUtils.java b/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkUtils.java index cdb225b58f..0d5e9cef05 100644 --- a/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkUtils.java +++ b/spring-orm/src/main/java/org/springframework/orm/eclipselink/EclipseLinkUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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,7 +31,7 @@ public abstract class EclipseLinkUtils { /** * Convert the given EclipseLinkException to an appropriate exception from * the org.springframework.dao hierarchy. - * @param ex EclipseLinkException that occured + * @param ex EclipseLinkException that occurred * @return the corresponding DataAccessException instance * @see EclipseLinkExceptionTranslator#convertEclipseLinkAccessException(EclipseLinkException) */ diff --git a/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslatorTests.java b/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslatorTests.java index ff61607455..397c456222 100644 --- a/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslatorTests.java +++ b/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkExceptionTranslatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -16,26 +16,29 @@ package org.springframework.orm.eclipselink; -import junit.framework.TestCase; +import static org.junit.Assert.*; import org.eclipse.persistence.exceptions.DatabaseException; -import org.springframework.dao.DataAccessException; +import org.junit.Test; /** * @author Jan Stamer - * @since 3.2 */ -public class EclipseLinkExceptionTranslatorTests extends TestCase { +public class EclipseLinkExceptionTranslatorTests { - public void testWithWrongException() { + @Test + public void wrongException() { EclipseLinkExceptionTranslator exceptionTranslator = new EclipseLinkExceptionTranslator(); assertNull(exceptionTranslator.translateExceptionIfPossible(new IllegalArgumentException())); } - public void testWithEclipseLinkException() { + @SuppressWarnings("ThrowableResultOfMethodCallIgnored") + @Test + public void eclipseLinkException() { EclipseLinkExceptionTranslator exceptionTranslator = new EclipseLinkExceptionTranslator(); assertNotNull(exceptionTranslator.translateExceptionIfPossible(DatabaseException.databaseAccessorNotConnected())); - assertTrue(exceptionTranslator.translateExceptionIfPossible(DatabaseException.databaseAccessorNotConnected()) instanceof DataAccessException); + assertNotNull(exceptionTranslator.translateExceptionIfPossible( + DatabaseException.databaseAccessorNotConnected())); } } diff --git a/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkSytemExceptionTests.java b/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkSystemExceptionTests.java similarity index 82% rename from spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkSytemExceptionTests.java rename to spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkSystemExceptionTests.java index 79780b8393..0774dac0fe 100644 --- a/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkSytemExceptionTests.java +++ b/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkSystemExceptionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -16,24 +16,27 @@ package org.springframework.orm.eclipselink; -import junit.framework.TestCase; +import static org.junit.Assert.*; import org.eclipse.persistence.exceptions.DatabaseException; import org.hibernate.HibernateException; +import org.junit.Test; /** * @author Jan Stamer - * @since 3.2 */ -public class EclipseLinkSytemExceptionTests extends TestCase { +@SuppressWarnings("serial") +public class EclipseLinkSystemExceptionTests { - public void testWithNull() { + @Test + public void withNull() { EclipseLinkSystemException exception = new EclipseLinkSystemException(null); assertNull(exception.getCause()); assertNull(exception.getMessage()); } - public void testCreateWithCause() { + @Test + public void createWithCause() { DatabaseException dbExceptionWithCause = new DatabaseException("my custom exception cause") { }; EclipseLinkSystemException elSystemException = new EclipseLinkSystemException(dbExceptionWithCause); @@ -41,7 +44,8 @@ public class EclipseLinkSytemExceptionTests extends TestCase { assertTrue(elSystemException.getMessage().contains("my custom exception cause")); } - public void testCreateWithNullCause() throws HibernateException { + @Test + public void createWithNullCause() throws HibernateException { DatabaseException dbExceptionWithCause = new DatabaseException((String) null) { }; EclipseLinkSystemException elSystemException = new EclipseLinkSystemException(dbExceptionWithCause); diff --git a/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkUtilsTests.java b/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkUtilsTests.java index ee9141fa55..fa9d804efd 100644 --- a/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkUtilsTests.java +++ b/spring-orm/src/test/java/org/springframework/orm/eclipselink/EclipseLinkUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -16,23 +16,25 @@ package org.springframework.orm.eclipselink; -import junit.framework.TestCase; +import static org.junit.Assert.*; import org.eclipse.persistence.exceptions.DatabaseException; -import org.springframework.dao.DataAccessException; +import org.junit.Test; /** * @author Jan Stamer - * @since 3.2 */ -public class EclipseLinkUtilsTests extends TestCase { +public class EclipseLinkUtilsTests { - public void testWithNull() { - assertTrue(EclipseLinkUtils.convertEclipseLinkAccessException(null) instanceof DataAccessException); + @Test + public void withNull() { + assertNotNull(EclipseLinkUtils.convertEclipseLinkAccessException(null)); } + @Test public void testWithEclipseLinkException() { - assertTrue(EclipseLinkUtils.convertEclipseLinkAccessException(DatabaseException.databaseAccessorNotConnected()) instanceof DataAccessException); + assertNotNull(EclipseLinkUtils.convertEclipseLinkAccessException( + DatabaseException.databaseAccessorNotConnected())); } }