|
| 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