Skip to content

Commit 1ba8b91

Browse files
christophstroblmp911de
authored andcommitted
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. Original pull request: #367.
1 parent 185b85b commit 1ba8b91

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2015 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -180,8 +180,8 @@ private boolean isLazyDbRef(MongoPersistentProperty property) {
180180
* @author Oliver Gierke
181181
* @author Christoph Strobl
182182
*/
183-
static class LazyLoadingInterceptor implements MethodInterceptor, org.springframework.cglib.proxy.MethodInterceptor,
184-
Serializable {
183+
static class LazyLoadingInterceptor
184+
implements MethodInterceptor, org.springframework.cglib.proxy.MethodInterceptor, Serializable {
185185

186186
private static final Method INITIALIZE_METHOD, TO_DBREF_METHOD, FINALIZE_METHOD;
187187

@@ -387,7 +387,8 @@ private synchronized Object resolve() {
387387
} catch (RuntimeException ex) {
388388

389389
DataAccessException translatedException = this.exceptionTranslator.translateExceptionIfPossible(ex);
390-
throw new LazyLoadingException("Unable to lazily resolve DBRef!", translatedException);
390+
throw new LazyLoadingException("Unable to lazily resolve DBRef!",
391+
translatedException != null ? translatedException : ex);
391392
}
392393
}
393394

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.convert;
17+
18+
import static org.hamcrest.core.Is.*;
19+
import static org.hamcrest.core.IsEqual.*;
20+
import static org.mockito.Mockito.*;
21+
22+
import org.junit.Rule;
23+
import org.junit.Test;
24+
import org.junit.rules.ExpectedException;
25+
import org.junit.runner.RunWith;
26+
import org.mockito.Mock;
27+
import org.mockito.runners.MockitoJUnitRunner;
28+
import org.springframework.dao.DataAccessException;
29+
import org.springframework.dao.support.PersistenceExceptionTranslator;
30+
import org.springframework.data.mongodb.LazyLoadingException;
31+
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver.LazyLoadingInterceptor;
32+
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
33+
34+
import com.mongodb.DBRef;
35+
36+
@RunWith(MockitoJUnitRunner.class)
37+
public class LazyLoadingInterceptorUntiTests {
38+
39+
public @Rule ExpectedException exception = ExpectedException.none();
40+
41+
@Mock MongoPersistentProperty propertyMock;
42+
@Mock DBRef dbrefMock;
43+
@Mock DbRefResolverCallback callbackMock;
44+
45+
/**
46+
* @see DATAMONGO-1437
47+
*/
48+
@Test
49+
public void shouldPreserveCauseForNonTranslatableExceptions()
50+
throws NoSuchMethodException, SecurityException, Throwable {
51+
52+
NullPointerException npe = new NullPointerException("Some Exception we did not think about.");
53+
when(callbackMock.resolve(propertyMock)).thenThrow(npe);
54+
55+
exception.expect(LazyLoadingException.class);
56+
exception.expectCause(is(equalTo(npe)));
57+
58+
new LazyLoadingInterceptor(propertyMock, dbrefMock, new NullExceptionTranslator(), callbackMock).intercept(null,
59+
LazyLoadingProxy.class.getMethod("getTarget"), null, null);
60+
}
61+
62+
static class NullExceptionTranslator implements PersistenceExceptionTranslator {
63+
64+
@Override
65+
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
66+
return null;
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)