Browse Source
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.pull/368/head
2 changed files with 74 additions and 4 deletions
@ -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; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue