DATACMNS-525 - Add KeyValuePersistenceExceptionTranslator, fixing Todos.

Added key/value specific PersistenceExceptionTranslator and use it within KeyValueTemplate. Fixed some todos in JavaDoc and added missing tests. Renamed test class for SimpleKeyValueRepository according to the changed name.

Original pull request: #95.
This commit is contained in:
Christoph Strobl
2014-11-25 12:44:00 +01:00
committed by Oliver Gierke
parent 1f71a28408
commit 52108ed006
7 changed files with 261 additions and 12 deletions

View File

@@ -0,0 +1,96 @@
/*
* Copyright 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.
* 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.keyvalue.core;
import static org.hamcrest.core.IsInstanceOf.*;
import static org.hamcrest.core.IsNull.*;
import static org.junit.Assert.*;
import java.util.NoSuchElementException;
import org.junit.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.dao.DataRetrievalFailureException;
/**
* @author Christoph Strobl
*/
public class KeyValuePersistenceExceptionTranslatorUnitTests {
KeyValuePersistenceExceptionTranslator translator = new KeyValuePersistenceExceptionTranslator();
/**
* @see DATACMNS-525
*/
@Test
public void translateExeptionShouldReturnDataAccessExceptionWhenGivenOne() {
assertThat(translator.translateExceptionIfPossible(new DataRetrievalFailureException("booh")),
instanceOf(DataRetrievalFailureException.class));
}
/**
* @see DATACMNS-525
*/
@Test
public void translateExeptionShouldReturnNullWhenGivenNull() {
assertThat(translator.translateExceptionIfPossible(null), nullValue());
}
/**
* @see DATACMNS-525
*/
@Test
public void translateExeptionShouldTranslateNoSuchElementExceptionToDataRetrievalFailureException() {
assertThat(translator.translateExceptionIfPossible(new NoSuchElementException("")),
instanceOf(DataRetrievalFailureException.class));
}
/**
* @see DATACMNS-525
*/
@Test
public void translateExeptionShouldTranslateIndexOutOfBoundsExceptionToDataRetrievalFailureException() {
assertThat(translator.translateExceptionIfPossible(new IndexOutOfBoundsException("")),
instanceOf(DataRetrievalFailureException.class));
}
/**
* @see DATACMNS-525
*/
@Test
public void translateExeptionShouldTranslateIllegalStateExceptionToDataRetrievalFailureException() {
assertThat(translator.translateExceptionIfPossible(new IllegalStateException("")),
instanceOf(DataRetrievalFailureException.class));
}
/**
* @see DATACMNS-525
*/
@Test
public void translateExeptionShouldTranslateAnyJavaExceptionToUncategorizedKeyValueException() {
assertThat(translator.translateExceptionIfPossible(new UnsupportedOperationException("")),
instanceOf(UncategorizedKeyValueException.class));
}
/**
* @see DATACMNS-525
*/
@Test
public void translateExeptionShouldReturnNullForNonJavaExceptions() {
assertThat(translator.translateExceptionIfPossible(new NoSuchBeanDefinitionException("")), nullValue());
}
}

View File

@@ -404,6 +404,14 @@ public class KeyValueTemplateUnitTests {
assertThat(template.findById("1", SUBCLASS_OF_ALIASED.getClass()), nullValue());
}
/**
* @see DATACMNS-525
*/
@Test(expected = IllegalArgumentException.class)
public void setttingNullPersistenceExceptionTranslatorShouldThrowException() {
template.setExceptionTranslator(null);
}
static class Foo {
String foo;

View File

@@ -27,6 +27,9 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.keyvalue.repository.support.SimpleKeyValueRepository;
import org.springframework.data.repository.core.support.ReflectionEntityInformation;
@@ -35,7 +38,7 @@ import org.springframework.data.repository.core.support.ReflectionEntityInformat
* @author Christoph Strobl
*/
@RunWith(MockitoJUnitRunner.class)
public class BasicKeyValueRepositoryUnitTests {
public class SimpleKeyValueRepositoryUnitTests {
private SimpleKeyValueRepository<Foo, String> repo;
private @Mock KeyValueOperations opsMock;
@@ -58,7 +61,10 @@ public class BasicKeyValueRepositoryUnitTests {
SimpleKeyValueRepository<WithNumericId, Integer> temp = new SimpleKeyValueRepository<WithNumericId, Integer>(ei,
opsMock);
WithNumericId foo = temp.save(new WithNumericId());
WithNumericId withNumericId = new WithNumericId();
temp.save(withNumericId);
verify(opsMock, times(1)).insert(eq(withNumericId));
}
/**
@@ -136,6 +142,40 @@ public class BasicKeyValueRepositoryUnitTests {
verify(opsMock, times(3)).findById(anyString(), eq(Foo.class));
}
/**
* @see DATACMNS-525
*/
@Test
public void findAllWithPageableShouldDelegateToOperationsCorrectlyWhenPageableDoesNotContainSort() {
repo.findAll(new PageRequest(10, 15));
verify(opsMock, times(1)).findInRange(eq(150), eq(15), isNull(Sort.class), eq(Foo.class));
}
/**
* @see DATACMNS-525
*/
@Test
public void findAllWithPageableShouldDelegateToOperationsCorrectlyWhenPageableContainsSort() {
Sort sort = new Sort("for", "bar");
repo.findAll(new PageRequest(10, 15, sort));
verify(opsMock, times(1)).findInRange(eq(150), eq(15), eq(sort), eq(Foo.class));
}
/**
* @see DATACMNS-525
*/
@Test
public void findAllShouldFallbackToFindAllOfWhenGivenNullPageable() {
repo.findAll((Pageable) null);
verify(opsMock, times(1)).findAll(eq(Foo.class));
}
static class Foo {
private @Id String id;