DATACMNS-944 - Moved to more consistent naming scheme for CrudRepository methods.

We now follow a more consistent naming scheme for the methods in repository that are driven by the following guidelines:

* Methods referring to an identifier now all end on …ById(…).
* Methods taking or returning a collection are named …All(…)

That results in the following renames:

* findOne(…) -> findById(…)
* save(Iterable) -> saveAll(Iterable)
* findAll(Iterable<ID>) -> findAllById(…)
* delete(ID) -> deleteById(ID)
* delete(Iterable<T>) -> deleteAll(Iterable<T>)
* exists() -> existsById(…)

As a side-effect of that, we can now drop the Serializable requirement for identifiers.

Updated CRUD method detection to use the new naming scheme and moved the code to Java 8 streams and Optional. Adapted RepositoryInvoker API to reflect method name changes as well.
This commit is contained in:
Oliver Gierke
2017-04-28 15:28:28 +02:00
parent 382c912111
commit 727ab8384c
55 changed files with 324 additions and 397 deletions

View File

@@ -86,11 +86,11 @@ public class QuerydslRepositoryInvokerAdapterUnitTests {
adapter.hasSaveMethod();
verify(delegate, times(1)).hasSaveMethod();
adapter.invokeDelete(any(Serializable.class));
verify(delegate, times(1)).invokeDelete(any());
adapter.invokeDeleteById(any(Serializable.class));
verify(delegate, times(1)).invokeDeleteById(any());
adapter.invokeFindOne(any(Serializable.class));
verify(delegate, times(1)).invokeFindOne(any());
adapter.invokeFindById(any(Serializable.class));
verify(delegate, times(1)).invokeFindById(any());
adapter.invokeQueryMethod(any(), any(), any(), any());

View File

@@ -100,7 +100,7 @@ public class AbstractEntityInformationUnitTests {
@Id boolean id;
}
static class CustomEntityInformation<T, ID extends Serializable> extends AbstractEntityInformation<T, ID> {
static class CustomEntityInformation<T, ID> extends AbstractEntityInformation<T, ID> {
private final Class<T> type;

View File

@@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.springframework.core.ResolvableType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.User;
@@ -138,12 +139,14 @@ public class AbstractRepositoryMetadataUnitTests {
super(repositoryInterface);
}
@SuppressWarnings("unchecked")
public Class<? extends Serializable> getIdType() {
return null;
return (Class<? extends Serializable>) ResolvableType//
.forClass(Repository.class, getRepositoryInterface()).getGeneric(1).resolve();
}
public Class<?> getDomainType() {
return null;
return ResolvableType.forClass(Repository.class, getRepositoryInterface()).getGeneric(0).resolve();
}
}

View File

@@ -47,7 +47,7 @@ public class DefaultCrudMethodsUnitTests {
Class<DomainCrudRepository> type = DomainCrudRepository.class;
assertFindAllMethodOn(type, type.getMethod("findAll"));
assertDeleteMethodOn(type, type.getMethod("delete", Serializable.class));
assertDeleteMethodOn(type, type.getMethod("delete", Object.class));
assertSaveMethodPresent(type, true);
}
@@ -57,7 +57,7 @@ public class DefaultCrudMethodsUnitTests {
Class<DomainPagingAndSortingRepository> type = DomainPagingAndSortingRepository.class;
assertFindAllMethodOn(type, type.getMethod("findAll", Pageable.class));
assertDeleteMethodOn(type, type.getMethod("delete", Serializable.class));
assertDeleteMethodOn(type, type.getMethod("delete", Object.class));
assertSaveMethodPresent(type, true);
}
@@ -100,8 +100,8 @@ public class DefaultCrudMethodsUnitTests {
public void detectsOverloadedMethodsCorrectly() throws Exception {
Class<RepositoryWithAllCrudMethodOverloaded> type = RepositoryWithAllCrudMethodOverloaded.class;
assertFindOneMethodOn(type, type.getDeclaredMethod("findOne", Long.class));
assertDeleteMethodOn(type, type.getDeclaredMethod("delete", Long.class));
assertFindOneMethodOn(type, type.getDeclaredMethod("findById", Long.class));
assertDeleteMethodOn(type, type.getDeclaredMethod("deleteById", Long.class));
assertSaveMethodOn(type, type.getDeclaredMethod("save", Domain.class));
assertFindAllMethodOn(type, type.getDeclaredMethod("findAll"));
}
@@ -110,8 +110,8 @@ public class DefaultCrudMethodsUnitTests {
public void ignoresWrongOverloadedMethods() throws Exception {
Class<RepositoryWithAllCrudMethodOverloadedWrong> type = RepositoryWithAllCrudMethodOverloadedWrong.class;
assertFindOneMethodOn(type, CrudRepository.class.getDeclaredMethod("findOne", Serializable.class));
assertDeleteMethodOn(type, CrudRepository.class.getDeclaredMethod("delete", Serializable.class));
assertFindOneMethodOn(type, CrudRepository.class.getDeclaredMethod("findById", Object.class));
assertDeleteMethodOn(type, CrudRepository.class.getDeclaredMethod("delete", Object.class));
assertSaveMethodOn(type, CrudRepository.class.getDeclaredMethod("save", Object.class));
assertFindAllMethodOn(type, CrudRepository.class.getDeclaredMethod("findAll"));
}
@@ -241,9 +241,9 @@ public class DefaultCrudMethodsUnitTests {
<S extends Domain> S save(S entity);
void delete(Long id);
void deleteById(Long id);
Optional<Domain> findOne(Long id);
Optional<Domain> findById(Long id);
}
// DATACMNS-393
@@ -251,11 +251,11 @@ public class DefaultCrudMethodsUnitTests {
List<Domain> findAll(String s);
Domain save(Serializable entity);
Domain save(Long entity);
void delete(String o);
Domain findOne(Domain o);
Domain findById(Domain o);
}
// DATACMNS-539

View File

@@ -19,7 +19,6 @@ import static org.assertj.core.api.Assertions.*;
import lombok.experimental.Delegate;
import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -63,19 +62,19 @@ public class DefaultRepositoryInformationUnitTests {
@Test
public void discoversRepositoryBaseClassMethod() throws Exception {
Method method = FooRepository.class.getMethod("findOne", Integer.class);
Method method = FooRepository.class.getMethod("findById", Integer.class);
RepositoryMetadata metadata = new DefaultRepositoryMetadata(FooRepository.class);
DefaultRepositoryInformation information = new DefaultRepositoryInformation(metadata, REPOSITORY, Optional.empty());
Method reference = information.getTargetClassMethod(method);
assertThat(reference.getDeclaringClass()).isEqualTo(REPOSITORY);
assertThat(reference.getName()).isEqualTo("findOne");
assertThat(reference.getName()).isEqualTo("findById");
}
@Test
public void discoveresNonRepositoryBaseClassMethod() throws Exception {
Method method = FooRepository.class.getMethod("findOne", Long.class);
Method method = FooRepository.class.getMethod("findById", Long.class);
RepositoryMetadata metadata = new DefaultRepositoryMetadata(FooRepository.class);
DefaultRepositoryInformation information = new DefaultRepositoryInformation(metadata, CrudRepository.class,
@@ -117,7 +116,7 @@ public class DefaultRepositoryInformationUnitTests {
Method method = CustomRepository.class.getMethod("findAll", Pageable.class);
assertThat(information.isBaseClassMethod(method)).isTrue();
method = getMethodFrom(CustomRepository.class, "exists");
method = getMethodFrom(CustomRepository.class, "existsById");
assertThat(information.isBaseClassMethod(method)).isTrue();
assertThat(information.getQueryMethods()).isEmpty();
@@ -184,7 +183,7 @@ public class DefaultRepositoryInformationUnitTests {
RepositoryInformation information = new DefaultRepositoryInformation(metadata, CrudRepository.class,
Optional.empty());
Method method = BaseRepository.class.getMethod("findOne", Serializable.class);
Method method = BaseRepository.class.getMethod("findById", Object.class);
assertThat(information.getQueryMethods()).contains(method);
}
@@ -196,8 +195,8 @@ public class DefaultRepositoryInformationUnitTests {
RepositoryInformation information = new DefaultRepositoryInformation(metadata, CrudRepository.class,
Optional.empty());
Method method = BossRepository.class.getMethod("save", Iterable.class);
Method reference = CrudRepository.class.getMethod("save", Iterable.class);
Method method = BossRepository.class.getMethod("saveAll", Iterable.class);
Method reference = CrudRepository.class.getMethod("saveAll", Iterable.class);
assertThat(information.getTargetClassMethod(method)).isEqualTo(reference);
}
@@ -268,10 +267,10 @@ public class DefaultRepositoryInformationUnitTests {
RepositoryInformation information = new DefaultRepositoryInformation(metadata, DummyRepositoryImpl.class,
Optional.empty());
Method method = DummyRepository.class.getMethod("save", Iterable.class);
Method method = DummyRepository.class.getMethod("saveAll", Iterable.class);
assertThat(information.getTargetClassMethod(method))
.isEqualTo(DummyRepositoryImpl.class.getMethod("save", Iterable.class));
.isEqualTo(DummyRepositoryImpl.class.getMethod("saveAll", Iterable.class));
}
@Test // DATACMNS-1008, DATACMNS-912, DATACMNS-854
@@ -291,7 +290,7 @@ public class DefaultRepositoryInformationUnitTests {
return Arrays.stream(type.getMethods())//
.filter(method -> method.getName().equals(name))//
.findFirst()//
.orElseThrow(() -> new IllegalStateException("No method found wwith name ".concat(name).concat("!")));
.orElseThrow(() -> new IllegalStateException("No method found with name ".concat(name).concat("!")));
}
@Target(ElementType.METHOD)
@@ -303,10 +302,10 @@ public class DefaultRepositoryInformationUnitTests {
interface FooRepository extends CrudRepository<User, Integer>, FooRepositoryCustom {
// Redeclared method
Optional<User> findOne(Integer primaryKey);
Optional<User> findById(Integer primaryKey);
// Not a redeclared method
User findOne(Long primaryKey);
User findById(Long primaryKey);
static void staticMethod() {}
@@ -340,7 +339,7 @@ public class DefaultRepositoryInformationUnitTests {
}
}
interface BaseRepository<S, ID extends Serializable> extends CrudRepository<S, ID> {
interface BaseRepository<S, ID> extends CrudRepository<S, ID> {
S findBySomething(String something);
@@ -351,7 +350,7 @@ public class DefaultRepositoryInformationUnitTests {
void delete(S entity);
@MyQuery
Optional<S> findOne(ID id);
Optional<S> findById(ID id);
}
interface ConcreteRepository extends BaseRepository<User, Integer> {
@@ -361,9 +360,9 @@ public class DefaultRepositoryInformationUnitTests {
User genericMethodToOverride(String something);
}
interface ReadOnlyRepository<T, ID extends Serializable> extends Repository<T, ID> {
interface ReadOnlyRepository<T, ID> extends Repository<T, ID> {
T findOne(ID id);
T findById(ID id);
Iterable<T> findAll();
@@ -371,7 +370,7 @@ public class DefaultRepositoryInformationUnitTests {
List<T> findAll(Sort sort);
boolean exists(ID id);
boolean existsById(ID id);
long count();
}
@@ -405,10 +404,10 @@ public class DefaultRepositoryInformationUnitTests {
interface DummyRepository extends CrudRepository<User, Integer> {
@Override
<S extends User> List<S> save(Iterable<S> entites);
<S extends User> List<S> saveAll(Iterable<S> entites);
}
static class DummyRepositoryImpl<T, ID extends Serializable> implements CrudRepository<T, ID> {
static class DummyRepositoryImpl<T, ID> implements CrudRepository<T, ID> {
private @Delegate CrudRepository<T, ID> delegate;
}

View File

@@ -168,7 +168,7 @@ public class DefaultRepositoryMetadataUnitTests {
static abstract class DummyGenericRepositorySupport<T, ID extends Serializable> implements CrudRepository<T, ID> {
public java.util.Optional<T> findOne(ID id) {
public java.util.Optional<T> findById(ID id) {
return java.util.Optional.empty();
}
}

View File

@@ -17,7 +17,6 @@ package org.springframework.data.repository.core.support;
import static org.mockito.Mockito.*;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Optional;
@@ -61,7 +60,7 @@ public class DummyRepositoryFactory extends RepositoryFactorySupport {
*/
@Override
@SuppressWarnings("unchecked")
public <T, ID extends Serializable> EntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
return mock(EntityInformation.class);
}

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.data.repository.core.support;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Set;
@@ -36,7 +35,7 @@ public final class DummyRepositoryInformation implements RepositoryInformation {
this.metadata = metadata;
}
public Class<? extends Serializable> getIdType() {
public Class<?> getIdType() {
return metadata.getIdType();
}

View File

@@ -16,12 +16,12 @@
package org.springframework.data.repository.core.support;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import lombok.Getter;
import lombok.Value;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -122,7 +122,7 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests {
@Test // DATACMNS-928
public void doesNotInterceptNonSaveMethod() throws Throwable {
doReturn(SampleRepository.class.getMethod("findOne", Serializable.class)).when(invocation).getMethod();
doReturn(SampleRepository.class.getMethod("findById", Object.class)).when(invocation).getMethod();
EventPublishingMethodInterceptor//
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
@@ -162,9 +162,9 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests {
SomeEvent event = new SomeEvent();
MultipleEvents sample = MultipleEvents.of(Collections.singletonList(event));
doReturn(new Object[] {Collections.singletonList(sample)}).when(invocation).getArguments();
doReturn(new Object[] { Collections.singletonList(sample) }).when(invocation).getArguments();
doReturn(SampleRepository.class.getMethod("save", Iterable.class)).when(invocation).getMethod();
doReturn(SampleRepository.class.getMethod("saveAll", Iterable.class)).when(invocation).getMethod();
EventPublishingMethodInterceptor//
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//

View File

@@ -22,7 +22,6 @@ import io.reactivex.Flowable;
import reactor.core.publisher.Flux;
import rx.Observable;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Optional;
@@ -60,10 +59,11 @@ public class ReactiveRepositoryInformationUnitTests {
@Test // DATACMNS-836
public void discoversRxJava1MethodWithConvertibleArguments() throws Exception {
Method reference = extractTargetMethodFromRepository(RxJava1InterfaceWithGenerics.class, "save", Observable.class);
Method reference = extractTargetMethodFromRepository(RxJava1InterfaceWithGenerics.class, "saveAll",
Observable.class);
assertEquals(ReactiveCrudRepository.class, reference.getDeclaringClass());
assertThat(reference.getName(), is("save"));
assertThat(reference.getName(), is("saveAll"));
assertThat(reference.getParameterTypes()[0], is(equalTo(Publisher.class)));
}
@@ -79,31 +79,31 @@ public class ReactiveRepositoryInformationUnitTests {
@Test // DATACMNS-988
public void discoversRxJava2MethodWithConvertibleArguments() throws Exception {
Method reference = extractTargetMethodFromRepository(RxJava2InterfaceWithGenerics.class, "save", Flowable.class);
Method reference = extractTargetMethodFromRepository(RxJava2InterfaceWithGenerics.class, "saveAll", Flowable.class);
assertEquals(ReactiveCrudRepository.class, reference.getDeclaringClass());
assertThat(reference.getName(), is("save"));
assertThat(reference.getName(), is("saveAll"));
assertThat(reference.getParameterTypes()[0], is(equalTo(Publisher.class)));
}
@Test // DATACMNS-836
public void discoversMethodAssignableArguments() throws Exception {
Method reference = extractTargetMethodFromRepository(ReactiveSortingRepository.class, "save", Publisher.class);
Method reference = extractTargetMethodFromRepository(ReactiveSortingRepository.class, "saveAll", Publisher.class);
assertEquals(ReactiveCrudRepository.class, reference.getDeclaringClass());
assertThat(reference.getName(), is("save"));
assertThat(reference.getName(), is("saveAll"));
assertThat(reference.getParameterTypes()[0], is(equalTo(Publisher.class)));
}
@Test // DATACMNS-836
public void discoversMethodExactIterableArguments() throws Exception {
Method reference = extractTargetMethodFromRepository(ReactiveJavaInterfaceWithGenerics.class, "save",
Method reference = extractTargetMethodFromRepository(ReactiveJavaInterfaceWithGenerics.class, "saveAll",
Iterable.class);
assertEquals(ReactiveCrudRepository.class, reference.getDeclaringClass());
assertThat(reference.getName(), is("save"));
assertThat(reference.getName(), is("saveAll"));
assertThat(reference.getParameterTypes()[0], is(equalTo(Iterable.class)));
}
@@ -120,12 +120,12 @@ public class ReactiveRepositoryInformationUnitTests {
@Test // DATACMNS-1023
public void usesCorrectSaveOverload() throws Exception {
Method reference = extractTargetMethodFromRepository(DummyRepository.class, "save", Iterable.class);
Method reference = extractTargetMethodFromRepository(DummyRepository.class, "saveAll", Iterable.class);
assertThat(reference, is(ReactiveCrudRepository.class.getMethod("save", Iterable.class)));
assertThat(reference, is(ReactiveCrudRepository.class.getMethod("saveAll", Iterable.class)));
}
private Method extractTargetMethodFromRepository(Class<?> repositoryType, String methodName, Class... args)
private Method extractTargetMethodFromRepository(Class<?> repositoryType, String methodName, Class<?>... args)
throws NoSuchMethodException {
RepositoryInformation information = new ReactiveRepositoryInformation(new DefaultRepositoryMetadata(repositoryType),
@@ -139,15 +139,14 @@ public class ReactiveRepositoryInformationUnitTests {
interface ReactiveJavaInterfaceWithGenerics extends ReactiveCrudRepository<User, String> {}
static abstract class DummyGenericReactiveRepositorySupport<T, ID extends Serializable>
implements ReactiveCrudRepository<T, ID> {
static abstract class DummyGenericReactiveRepositorySupport<T, ID> implements ReactiveCrudRepository<T, ID> {
}
interface DummyRepository extends ReactiveCrudRepository<User, Integer> {
@Override
<S extends User> Flux<S> save(Iterable<S> entities);
<S extends User> Flux<S> saveAll(Iterable<S> entities);
}
static class User {

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.repository.core.support;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import io.reactivex.Completable;
@@ -59,10 +60,10 @@ public class ReactiveWrapperRepositoryFactorySupportUnitTests {
public void invokesCustomMethodIfItRedeclaresACRUDOne() {
ObjectRepository repository = factory.getRepository(ObjectRepository.class, customImplementation);
repository.findOne(1);
repository.findById(1);
verify(customImplementation, times(1)).findOne(1);
verify(backingRepo, times(0)).findOne(1);
verify(customImplementation, times(1)).findById(1);
verify(backingRepo, times(0)).findById(1);
}
@Test // DATACMNS-836
@@ -70,10 +71,10 @@ public class ReactiveWrapperRepositoryFactorySupportUnitTests {
Serializable id = 1L;
RxJava1ConvertingRepository repository = factory.getRepository(RxJava1ConvertingRepository.class);
repository.exists(id);
repository.exists((Long) id);
repository.existsById(id);
repository.existsById((Long) id);
verify(backingRepo, times(2)).exists(id);
verify(backingRepo, times(2)).existsById(id);
}
@Test // DATACMNS-836
@@ -83,9 +84,9 @@ public class ReactiveWrapperRepositoryFactorySupportUnitTests {
Single<Long> ids = Single.just(1L);
RxJava1ConvertingRepository repository = factory.getRepository(RxJava1ConvertingRepository.class);
repository.exists(ids);
repository.existsById(ids);
verify(backingRepo, times(1)).exists(any(Mono.class));
verify(backingRepo, times(1)).existsById(any(Mono.class));
}
@Test // DATACMNS-988
@@ -93,9 +94,9 @@ public class ReactiveWrapperRepositoryFactorySupportUnitTests {
Long id = 1L;
RxJava2ConvertingRepository repository = factory.getRepository(RxJava2ConvertingRepository.class);
repository.findOne(id);
repository.findById(id);
verify(backingRepo, times(1)).findOne(id);
verify(backingRepo, times(1)).findById(id);
}
@Test // DATACMNS-988
@@ -104,36 +105,36 @@ public class ReactiveWrapperRepositoryFactorySupportUnitTests {
Serializable id = 1L;
RxJava2ConvertingRepository repository = factory.getRepository(RxJava2ConvertingRepository.class);
repository.delete(id);
repository.deleteById(id);
verify(backingRepo, times(1)).delete(id);
verify(backingRepo, times(1)).deleteById(id);
}
interface RxJava1ConvertingRepository extends Repository<Object, Long> {
Single<Boolean> exists(Single<Long> id);
Single<Boolean> existsById(Single<Long> id);
Single<Boolean> exists(Serializable id);
Single<Boolean> existsById(Serializable id);
Single<Boolean> exists(Long id);
Single<Boolean> existsById(Long id);
}
interface RxJava2ConvertingRepository extends Repository<Object, Long> {
Maybe<Boolean> findOne(Serializable id);
Maybe<Boolean> findById(Serializable id);
Single<Boolean> exists(Long id);
Single<Boolean> existsById(Long id);
Completable delete(Serializable id);
Completable deleteById(Serializable id);
}
interface ObjectRepository
extends Repository<Object, Serializable>, RepositoryFactorySupportUnitTests.ObjectRepositoryCustom {
extends Repository<Object, Object>, RepositoryFactorySupportUnitTests.ObjectRepositoryCustom {
}
interface ObjectRepositoryCustom {
Object findOne(Serializable id);
Object findById(Object id);
}
}

View File

@@ -22,9 +22,9 @@ import static org.mockito.Mockito.*;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
@@ -77,7 +77,7 @@ public class RepositoryFactorySupportUnitTests {
DummyRepositoryFactory factory;
@Mock PagingAndSortingRepository<Object, Serializable> backingRepo;
@Mock PagingAndSortingRepository<Object, Object> backingRepo;
@Mock ObjectRepositoryCustom customImplementation;
@Mock MyQueryCreationListener listener;
@@ -119,10 +119,10 @@ public class RepositoryFactorySupportUnitTests {
public void invokesCustomMethodIfItRedeclaresACRUDOne() {
ObjectRepository repository = factory.getRepository(ObjectRepository.class, customImplementation);
repository.findOne(1);
repository.findById(1);
verify(customImplementation, times(1)).findOne(1);
verify(backingRepo, times(0)).findOne(1);
verify(customImplementation, times(1)).findById(1);
verify(backingRepo, times(0)).findById(1);
}
@Test
@@ -316,7 +316,7 @@ public class RepositoryFactorySupportUnitTests {
interface SimpleRepository extends Repository<Object, Serializable> {}
interface ObjectRepository extends Repository<Object, Serializable>, ObjectRepositoryCustom {
interface ObjectRepository extends Repository<Object, Object>, ObjectRepositoryCustom {
Object findByClass(Class<?> clazz);
@@ -335,7 +335,7 @@ public class RepositoryFactorySupportUnitTests {
interface ObjectRepositoryCustom {
Object findOne(Serializable id);
Object findById(Object id);
}
interface PlainQueryCreationListener extends QueryCreationListener<RepositoryQuery> {
@@ -352,7 +352,7 @@ public class RepositoryFactorySupportUnitTests {
interface ReadOnlyRepository<T, ID extends Serializable> extends Repository<T, ID> {
T findOne(ID id);
Optional<T> findById(ID id);
Iterable<T> findAll();
@@ -360,7 +360,7 @@ public class RepositoryFactorySupportUnitTests {
List<T> findAll(Sort sort);
boolean exists(ID id);
boolean existsById(ID id);
long count();
}

View File

@@ -4,7 +4,7 @@ import org.springframework.data.repository.Repository;
public interface ProductRepository extends Repository<Product, Long> {
Product findOne(Long id);
Product findById(Long id);
Product save(Product product);
}

View File

@@ -18,7 +18,6 @@ package org.springframework.data.repository.support;
import static org.mockito.Mockito.*;
import static org.springframework.data.repository.support.RepositoryInvocationTestUtils.*;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Date;
@@ -62,12 +61,12 @@ public class CrudRepositoryInvokerUnitTests {
@Test // DATACMNS-589, DATAREST-216
public void invokesRedeclaredFindOne() {
getInvokerFor(orderRepository, expectInvocationOnType(OrderRepository.class)).invokeFindOne(1L);
getInvokerFor(orderRepository, expectInvocationOnType(OrderRepository.class)).invokeFindById(1L);
}
@Test // DATACMNS-589
public void invokesRedeclaredDelete() throws Exception {
getInvokerFor(orderRepository, expectInvocationOnType(OrderRepository.class)).invokeDelete(1L);
getInvokerFor(orderRepository, expectInvocationOnType(OrderRepository.class)).invokeDeleteById(1L);
}
@Test // DATACMNS-589
@@ -80,15 +79,15 @@ public class CrudRepositoryInvokerUnitTests {
@Test // DATACMNS-589
public void invokesFindOneOnCrudRepository() throws Exception {
Method method = CrudRepository.class.getMethod("findOne", Serializable.class);
getInvokerFor(personRepository, expectInvocationOf(method)).invokeFindOne(1L);
Method method = CrudRepository.class.getMethod("findById", Object.class);
getInvokerFor(personRepository, expectInvocationOf(method)).invokeFindById(1L);
}
@Test // DATACMNS-589, DATAREST-216
public void invokesDeleteOnCrudRepository() throws Exception {
Method method = CrudRepository.class.getMethod("delete", Serializable.class);
getInvokerFor(personRepository, expectInvocationOf(method)).invokeDelete(1L);
Method method = CrudRepository.class.getMethod("deleteById", Object.class);
getInvokerFor(personRepository, expectInvocationOf(method)).invokeDeleteById(1L);
}
@Test // DATACMNS-589
@@ -142,10 +141,10 @@ public class CrudRepositoryInvokerUnitTests {
<S extends Order> S save(S entity);
@Override
Optional<Order> findOne(Long id);
Optional<Order> findById(Long id);
@Override
void delete(Long id);
void deleteById(Long id);
}
static class Person {}
@@ -172,6 +171,6 @@ public class CrudRepositoryInvokerUnitTests {
interface CrudWithRedeclaredDelete extends CrudRepository<Order, Long> {
void delete(Long id);
void deleteById(Long id);
}
}

View File

@@ -59,9 +59,9 @@ public class DefaultRepositoryInvokerFactoryIntegrationTests {
// Mockito.reset(productRepository);
Product product = new Product();
when(productRepository.findOne(4711L)).thenReturn(product);
when(productRepository.findById(4711L)).thenReturn(product);
Optional<Object> invokeFindOne = factory.getInvokerFor(Product.class).invokeFindOne(4711L);
Optional<Object> invokeFindOne = factory.getInvokerFor(Product.class).invokeFindById(4711L);
assertThat(invokeFindOne).isEqualTo(Optional.of(product));
}

View File

@@ -121,7 +121,7 @@ public class DomainClassConverterUnitTests {
UserRepository bean = context.getBean(UserRepository.class);
UserRepository repo = (UserRepository) ((Advised) bean).getTargetSource().getTarget();
verify(repo, times(1)).findOne(1L);
verify(repo, times(1)).findById(1L);
}
@Test // DATACMNS-133

View File

@@ -82,22 +82,22 @@ public class ReflectionRepositoryInvokerUnitTests {
public void invokesFindOneCorrectly() throws Exception {
ManualCrudRepository repository = mock(ManualCrudRepository.class);
Method method = ManualCrudRepository.class.getMethod("findOne", Long.class);
Method method = ManualCrudRepository.class.getMethod("findById", Long.class);
getInvokerFor(repository, expectInvocationOf(method)).invokeFindOne("1");
getInvokerFor(repository, expectInvocationOf(method)).invokeFindOne(1L);
getInvokerFor(repository, expectInvocationOf(method)).invokeFindById("1");
getInvokerFor(repository, expectInvocationOf(method)).invokeFindById(1L);
}
@Test // DATACMNS-589
public void invokesDeleteWithDomainCorrectly() throws Exception {
RepoWithDomainDeleteAndFindOne repository = mock(RepoWithDomainDeleteAndFindOne.class);
when(repository.findOne(1L)).thenReturn(new Domain());
when(repository.findById(1L)).thenReturn(new Domain());
Method findOneMethod = RepoWithDomainDeleteAndFindOne.class.getMethod("findOne", Long.class);
Method findOneMethod = RepoWithDomainDeleteAndFindOne.class.getMethod("findById", Long.class);
Method deleteMethod = RepoWithDomainDeleteAndFindOne.class.getMethod("delete", Domain.class);
getInvokerFor(repository, expectInvocationOf(findOneMethod, deleteMethod)).invokeDelete(1L);
getInvokerFor(repository, expectInvocationOf(findOneMethod, deleteMethod)).invokeDeleteById(1L);
}
@Test // DATACMNS-589
@@ -164,9 +164,9 @@ public class ReflectionRepositoryInvokerUnitTests {
public void invokesOverriddenDeleteMethodCorrectly() throws Exception {
MyRepo repository = mock(MyRepo.class);
Method method = CustomRepo.class.getMethod("delete", Long.class);
Method method = CustomRepo.class.getMethod("deleteById", Long.class);
getInvokerFor(repository, expectInvocationOf(method)).invokeDelete("1");
getInvokerFor(repository, expectInvocationOf(method)).invokeDeleteById("1");
}
@Test(expected = IllegalStateException.class) // DATACMNS-589
@@ -175,7 +175,7 @@ public class ReflectionRepositoryInvokerUnitTests {
RepositoryInvoker invoker = getInvokerFor(mock(EmptyRepository.class));
assertThat(invoker.hasDeleteMethod()).isFalse();
invoker.invokeDelete(1L);
invoker.invokeDeleteById(1L);
}
@Test(expected = IllegalStateException.class) // DATACMNS-589
@@ -184,7 +184,7 @@ public class ReflectionRepositoryInvokerUnitTests {
RepositoryInvoker invoker = getInvokerFor(mock(EmptyRepository.class));
assertThat(invoker.hasFindOneMethod()).isFalse();
invoker.invokeFindOne(1L);
invoker.invokeFindById(1L);
}
@Test(expected = IllegalStateException.class) // DATACMNS-589
@@ -245,11 +245,11 @@ public class ReflectionRepositoryInvokerUnitTests {
public void convertsWrapperTypeToJdkOptional() {
GuavaRepository mock = mock(GuavaRepository.class);
when(mock.findOne(any())).thenReturn(com.google.common.base.Optional.of(new Domain()));
when(mock.findById(any())).thenReturn(com.google.common.base.Optional.of(new Domain()));
RepositoryInvoker invoker = getInvokerFor(mock);
Optional<Object> invokeFindOne = invoker.invokeFindOne(1L);
Optional<Object> invokeFindOne = invoker.invokeFindById(1L);
assertThat(invokeFindOne).isPresent();
}
@@ -262,8 +262,8 @@ public class ReflectionRepositoryInvokerUnitTests {
Method method = ManualCrudRepository.class.getMethod("findAll");
Optional<Object> result = getInvokerFor(mock).invokeQueryMethod(method, new LinkedMultiValueMap<>(), Pageable.unpaged(),
Sort.unsorted());
Optional<Object> result = getInvokerFor(mock).invokeQueryMethod(method, new LinkedMultiValueMap<>(),
Pageable.unpaged(), Sort.unsorted());
assertThat(result).hasValueSatisfying(it -> {
assertThat(it).isInstanceOf(Collection.class);
@@ -287,20 +287,20 @@ public class ReflectionRepositoryInvokerUnitTests {
class Domain {}
interface CustomRepo {
void delete(Long id);
void deleteById(Long id);
}
interface EmptyRepository extends Repository<Domain, Long> {}
interface ManualCrudRepository extends Repository<Domain, Long> {
Domain findOne(Long id);
Domain findById(Long id);
Iterable<Domain> findAll();
<T extends Domain> T save(T entity);
void delete(Long id);
void deleteById(Long id);
}
interface RepoWithFindAllWithoutParameters extends Repository<Domain, Long> {
@@ -320,7 +320,7 @@ public class ReflectionRepositoryInvokerUnitTests {
interface RepoWithDomainDeleteAndFindOne extends Repository<Domain, Long> {
Domain findOne(Long id);
Domain findById(Long id);
void delete(Domain entity);
}
@@ -332,6 +332,6 @@ public class ReflectionRepositoryInvokerUnitTests {
interface GuavaRepository extends Repository<Domain, Long> {
com.google.common.base.Optional<Domain> findOne(Long id);
com.google.common.base.Optional<Domain> findById(Long id);
}
}