From 2ebdc5452cf3931c1f5104c40bd7b8ad28ef564b Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 24 May 2016 13:46:01 +0200 Subject: [PATCH 1/2] DATAMONGO-1437 - DefaultDbRefResolver swallows cause of non DataAccessException translatable Exception. Prepare issue branch. --- pom.xml | 2 +- spring-data-mongodb-cross-store/pom.xml | 4 ++-- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb-log4j/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 3a9b4f5ad7..885a681791 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 1.10.0.BUILD-SNAPSHOT + 1.10.0.DATAMONGO-1437-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-cross-store/pom.xml b/spring-data-mongodb-cross-store/pom.xml index 6554045e11..c28a879438 100644 --- a/spring-data-mongodb-cross-store/pom.xml +++ b/spring-data-mongodb-cross-store/pom.xml @@ -6,7 +6,7 @@ org.springframework.data spring-data-mongodb-parent - 1.10.0.BUILD-SNAPSHOT + 1.10.0.DATAMONGO-1437-SNAPSHOT ../pom.xml @@ -48,7 +48,7 @@ org.springframework.data spring-data-mongodb - 1.10.0.BUILD-SNAPSHOT + 1.10.0.DATAMONGO-1437-SNAPSHOT diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 2d02722262..2ce769959d 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 1.10.0.BUILD-SNAPSHOT + 1.10.0.DATAMONGO-1437-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-log4j/pom.xml b/spring-data-mongodb-log4j/pom.xml index ee5e3336db..ba1bb04709 100644 --- a/spring-data-mongodb-log4j/pom.xml +++ b/spring-data-mongodb-log4j/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 1.10.0.BUILD-SNAPSHOT + 1.10.0.DATAMONGO-1437-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index edfa519fad..8a0924ef10 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -11,7 +11,7 @@ org.springframework.data spring-data-mongodb-parent - 1.10.0.BUILD-SNAPSHOT + 1.10.0.DATAMONGO-1437-SNAPSHOT ../pom.xml From fa44b100e3fa5e4d7d389243e20021a48cc1392d Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 24 May 2016 14:41:43 +0200 Subject: [PATCH 2/2] DATAMONGO-1437 - Preserve non translatable Exception cause when lazily resolving DBRef. We now preserve the cause of Exceptions that cannot be translated into DataAccessExceptions when an error occurs during lazily loading DBRefs. --- .../core/convert/DefaultDbRefResolver.java | 9 +-- .../LazyLoadingInterceptorUntiTests.java | 69 +++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/LazyLoadingInterceptorUntiTests.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java index 7e543e857b..cbffcf1f53 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2015 the original author or authors. + * Copyright 2013-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. @@ -180,8 +180,8 @@ private boolean isLazyDbRef(MongoPersistentProperty property) { * @author Oliver Gierke * @author Christoph Strobl */ - static class LazyLoadingInterceptor implements MethodInterceptor, org.springframework.cglib.proxy.MethodInterceptor, - Serializable { + static class LazyLoadingInterceptor + implements MethodInterceptor, org.springframework.cglib.proxy.MethodInterceptor, Serializable { private static final Method INITIALIZE_METHOD, TO_DBREF_METHOD, FINALIZE_METHOD; @@ -387,7 +387,8 @@ private synchronized Object resolve() { } catch (RuntimeException ex) { DataAccessException translatedException = this.exceptionTranslator.translateExceptionIfPossible(ex); - throw new LazyLoadingException("Unable to lazily resolve DBRef!", translatedException); + throw new LazyLoadingException("Unable to lazily resolve DBRef!", + translatedException != null ? translatedException : ex); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/LazyLoadingInterceptorUntiTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/LazyLoadingInterceptorUntiTests.java new file mode 100644 index 0000000000..d6d1659ab2 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/LazyLoadingInterceptorUntiTests.java @@ -0,0 +1,69 @@ +/* + * Copyright 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.data.mongodb.core.convert; + +import static org.hamcrest.core.Is.*; +import static org.hamcrest.core.IsEqual.*; +import static org.mockito.Mockito.*; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.support.PersistenceExceptionTranslator; +import org.springframework.data.mongodb.LazyLoadingException; +import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver.LazyLoadingInterceptor; +import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; + +import com.mongodb.DBRef; + +@RunWith(MockitoJUnitRunner.class) +public class LazyLoadingInterceptorUntiTests { + + public @Rule ExpectedException exception = ExpectedException.none(); + + @Mock MongoPersistentProperty propertyMock; + @Mock DBRef dbrefMock; + @Mock DbRefResolverCallback callbackMock; + + /** + * @see DATAMONGO-1437 + */ + @Test + public void shouldPreserveCauseForNonTranslatableExceptions() + throws NoSuchMethodException, SecurityException, Throwable { + + NullPointerException npe = new NullPointerException("Some Exception we did not think about."); + when(callbackMock.resolve(propertyMock)).thenThrow(npe); + + exception.expect(LazyLoadingException.class); + exception.expectCause(is(equalTo(npe))); + + new LazyLoadingInterceptor(propertyMock, dbrefMock, new NullExceptionTranslator(), callbackMock).intercept(null, + LazyLoadingProxy.class.getMethod("getTarget"), null, null); + } + + static class NullExceptionTranslator implements PersistenceExceptionTranslator { + + @Override + public DataAccessException translateExceptionIfPossible(RuntimeException ex) { + return null; + } + } +}