committed by
Jens Schauder
parent
aecdf5001a
commit
2618221115
@@ -41,9 +41,10 @@ class InstantiationAwarePersistentPropertyAccessorUnitTests {
|
||||
|
||||
PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Sample.class);
|
||||
|
||||
PersistentPropertyAccessor<Sample> accessor = entity.getPropertyAccessor(new Sample("Dave", "Matthews", 42));
|
||||
Sample sample = new Sample("Dave", "Matthews", 42);
|
||||
|
||||
PersistentPropertyAccessor<Sample> wrapper = new InstantiationAwarePropertyAccessor<>(accessor,
|
||||
PersistentPropertyAccessor<Sample> wrapper = new InstantiationAwarePropertyAccessor<>(sample,
|
||||
entity::getPropertyAccessor,
|
||||
instantiators);
|
||||
|
||||
wrapper.setProperty(entity.getRequiredPersistentProperty("firstname"), "Oliver August");
|
||||
|
||||
@@ -37,12 +37,16 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.config.CustomRepositoryImplementationDetector;
|
||||
import org.springframework.data.repository.config.ImplementationLookupConfiguration;
|
||||
import org.springframework.data.repository.core.NamedQueries;
|
||||
import org.springframework.data.repository.core.RepositoryInformation;
|
||||
import org.springframework.data.repository.core.support.DummyRepositoryFactory;
|
||||
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
|
||||
import org.springframework.data.repository.core.support.QueryCreationListener;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
@@ -60,6 +64,7 @@ import org.springframework.data.repository.query.RepositoryQuery;
|
||||
* @author Kyrylo Merzlikin
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT)
|
||||
class CdiRepositoryBeanUnitTests {
|
||||
|
||||
static final String PASSIVATION_ID = "javax.enterprise.inject.Default:org.springframework.data.repository.cdi.CdiRepositoryBeanUnitTests$SampleRepository";
|
||||
@@ -148,8 +153,8 @@ class CdiRepositoryBeanUnitTests {
|
||||
|
||||
@Override
|
||||
protected SampleRepository create(CreationalContext<SampleRepository> creationalContext,
|
||||
Class<SampleRepository> repositoryType, Optional<Object> customImplementation) {
|
||||
return null;
|
||||
Class<SampleRepository> repositoryType) {
|
||||
return create(() -> new DummyRepositoryFactory(new Object()), repositoryType);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -158,9 +163,9 @@ class CdiRepositoryBeanUnitTests {
|
||||
ArgumentCaptor<ImplementationLookupConfiguration> captor = ArgumentCaptor
|
||||
.forClass(ImplementationLookupConfiguration.class);
|
||||
|
||||
verify(detector).detectCustomImplementation(captor.capture());
|
||||
verify(detector, times(2)).detectCustomImplementation(captor.capture());
|
||||
|
||||
ImplementationLookupConfiguration configuration = captor.getValue();
|
||||
ImplementationLookupConfiguration configuration = captor.getAllValues().get(0);
|
||||
|
||||
assertThat(configuration.getImplementationBeanName()).isEqualTo("cdiRepositoryBeanUnitTests.SampleRepositoryImpl");
|
||||
assertThat(configuration.getImplementationClassName()).isEqualTo("SampleRepositoryImpl");
|
||||
@@ -194,8 +199,7 @@ class CdiRepositoryBeanUnitTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType,
|
||||
Optional<Object> customImplementation) {
|
||||
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,118 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016-2021 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
|
||||
*
|
||||
* https://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.repository.support;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link PageableExecutionUtils}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class PageableExecutionUtilsUnitTests {
|
||||
|
||||
@Mock LongSupplier totalSupplierMock;
|
||||
|
||||
@Test // DATAMCNS-884
|
||||
void firstPageRequestIsLessThanOneFullPageDoesNotRequireTotal() {
|
||||
|
||||
Page<Integer> page = PageableExecutionUtils.getPage(Arrays.asList(1, 2, 3), PageRequest.of(0, 10),
|
||||
totalSupplierMock);
|
||||
|
||||
assertThat(page).contains(1, 2, 3);
|
||||
assertThat(page.getTotalElements()).isEqualTo(3L);
|
||||
verifyNoInteractions(totalSupplierMock);
|
||||
}
|
||||
|
||||
@Test // DATAMCNS-884
|
||||
void noPageableRequestDoesNotRequireTotal() {
|
||||
|
||||
Page<Integer> page = PageableExecutionUtils.getPage(Arrays.asList(1, 2, 3), Pageable.unpaged(), totalSupplierMock);
|
||||
|
||||
assertThat(page).contains(1, 2, 3);
|
||||
assertThat(page.getTotalElements()).isEqualTo(3L);
|
||||
|
||||
verifyNoInteractions(totalSupplierMock);
|
||||
}
|
||||
|
||||
@Test // DATAMCNS-884
|
||||
void subsequentPageRequestIsLessThanOneFullPageDoesNotRequireTotal() {
|
||||
|
||||
Page<Integer> page = PageableExecutionUtils.getPage(Arrays.asList(1, 2, 3), PageRequest.of(5, 10),
|
||||
totalSupplierMock);
|
||||
|
||||
assertThat(page).contains(1, 2, 3);
|
||||
assertThat(page.getTotalElements()).isEqualTo(53L);
|
||||
|
||||
verifyNoInteractions(totalSupplierMock);
|
||||
}
|
||||
|
||||
@Test // DATAMCNS-884
|
||||
void firstPageRequestHitsUpperBoundRequiresTotal() {
|
||||
|
||||
doReturn(4L).when(totalSupplierMock).getAsLong();
|
||||
|
||||
Page<Integer> page = PageableExecutionUtils.getPage(Arrays.asList(1, 2, 3), PageRequest.of(0, 3),
|
||||
totalSupplierMock);
|
||||
|
||||
assertThat(page).contains(1, 2, 3);
|
||||
assertThat(page.getTotalElements()).isEqualTo(4L);
|
||||
|
||||
verify(totalSupplierMock).getAsLong();
|
||||
}
|
||||
|
||||
@Test // DATAMCNS-884
|
||||
void subsequentPageRequestHitsUpperBoundRequiresTotal() {
|
||||
|
||||
doReturn(7L).when(totalSupplierMock).getAsLong();
|
||||
|
||||
Page<Integer> page = PageableExecutionUtils.getPage(Arrays.asList(1, 2, 3), PageRequest.of(1, 3),
|
||||
totalSupplierMock);
|
||||
|
||||
assertThat(page).contains(1, 2, 3);
|
||||
assertThat(page.getTotalElements()).isEqualTo(7L);
|
||||
|
||||
verify(totalSupplierMock).getAsLong();
|
||||
}
|
||||
|
||||
@Test // DATAMCNS-884
|
||||
void subsequentPageRequestWithoutResultRequiresRequireTotal() {
|
||||
|
||||
doReturn(7L).when(totalSupplierMock).getAsLong();
|
||||
Page<Integer> page = PageableExecutionUtils.getPage(Collections.<Integer> emptyList(), PageRequest.of(5, 10),
|
||||
totalSupplierMock);
|
||||
|
||||
assertThat(page.getTotalElements()).isEqualTo(7L);
|
||||
|
||||
verify(totalSupplierMock).getAsLong();
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,7 @@ import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Slice;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.NullableWrapper;
|
||||
import org.springframework.data.util.Streamable;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
import org.springframework.util.concurrent.ListenableFuture;
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.KotlinDetector;
|
||||
import org.springframework.data.mapping.model.TypeCreatingSyntheticClassKt;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
@@ -58,6 +59,21 @@ public class KotlinReflectionUtilsUnitTests {
|
||||
assertThat(KotlinReflectionUtils.isSupportedKotlinClass(TypeCreatingSyntheticClass.class)).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1171
|
||||
public void discoversKotlinClass() {
|
||||
|
||||
assertThat(KotlinDetector.isKotlinType(TypeCreatingSyntheticClass.class)).isTrue();
|
||||
assertThat(KotlinReflectionUtils.isSupportedKotlinClass(TypeCreatingSyntheticClass.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1171
|
||||
public void discoversUnsupportedKotlinClass() {
|
||||
|
||||
assertThat(KotlinDetector.isKotlinType(org.springframework.data.mapping.model.TypeCreatingSyntheticClassKt.class))
|
||||
.isTrue();
|
||||
assertThat(KotlinReflectionUtils.isSupportedKotlinClass(TypeCreatingSyntheticClassKt.class)).isFalse();
|
||||
}
|
||||
|
||||
void runTest(String testName)
|
||||
throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException {
|
||||
|
||||
|
||||
@@ -27,7 +27,6 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.data.mapping.model.TypeCreatingSyntheticClassKt;
|
||||
import org.springframework.data.repository.sample.User;
|
||||
import org.springframework.data.util.ReflectionUtils.DescribedFieldFilter;
|
||||
import org.springframework.util.ReflectionUtils.FieldFilter;
|
||||
@@ -38,46 +37,46 @@ import org.springframework.util.ReflectionUtils.FieldFilter;
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class ReflectionUtilsUnitTests {
|
||||
class ReflectionUtilsUnitTests {
|
||||
|
||||
@SuppressWarnings("rawtypes") Constructor constructor;
|
||||
Field reference;
|
||||
@SuppressWarnings("rawtypes") private Constructor constructor;
|
||||
private Field reference;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() throws Exception {
|
||||
void setUp() throws Exception {
|
||||
this.reference = Sample.class.getField("field");
|
||||
this.constructor = ConstructorDetection.class.getConstructor(int.class, String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsFieldByFilter() {
|
||||
void findsFieldByFilter() {
|
||||
|
||||
Field field = ReflectionUtils.findField(Sample.class, (FieldFilter) new FieldNameFieldFilter("field"));
|
||||
assertThat(field).isEqualTo(reference);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsNullIfNoFieldFound() {
|
||||
void returnsNullIfNoFieldFound() {
|
||||
|
||||
Field field = ReflectionUtils.findField(Sample.class, (FieldFilter) new FieldNameFieldFilter("foo"));
|
||||
assertThat(field).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rejectsNonUniqueField() {
|
||||
void rejectsNonUniqueField() {
|
||||
assertThatIllegalStateException().isThrownBy(
|
||||
() -> ReflectionUtils.findField(Sample.class, new ReflectionUtils.AnnotationFieldFilter(Autowired.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsUniqueField() {
|
||||
void findsUniqueField() {
|
||||
|
||||
Field field = ReflectionUtils.findField(Sample.class, new FieldNameFieldFilter("field"), false);
|
||||
assertThat(field).isEqualTo(reference);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsFieldInSuperclass() {
|
||||
void findsFieldInSuperclass() {
|
||||
|
||||
class Subclass extends Sample {
|
||||
|
||||
@@ -88,7 +87,7 @@ public class ReflectionUtilsUnitTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setsNonPublicField() {
|
||||
void setsNonPublicField() {
|
||||
|
||||
Sample sample = new Sample();
|
||||
Field field = ReflectionUtils.findField(Sample.class, new FieldNameFieldFilter("first"));
|
||||
@@ -97,48 +96,48 @@ public class ReflectionUtilsUnitTests {
|
||||
}
|
||||
|
||||
@Test // DATACMNS-542
|
||||
public void detectsConstructorForCompleteMatch() throws Exception {
|
||||
void detectsConstructorForCompleteMatch() throws Exception {
|
||||
assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, "test")).hasValue(constructor);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-542
|
||||
public void detectsConstructorForMatchWithNulls() throws Exception {
|
||||
void detectsConstructorForMatchWithNulls() throws Exception {
|
||||
assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, null)).hasValue(constructor);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-542
|
||||
public void rejectsConstructorIfNumberOfArgumentsDontMatch() throws Exception {
|
||||
void rejectsConstructorIfNumberOfArgumentsDontMatch() throws Exception {
|
||||
assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, "test", "test")).isNotPresent();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-542
|
||||
public void rejectsConstructorForNullForPrimitiveArgument() throws Exception {
|
||||
void rejectsConstructorForNullForPrimitiveArgument() throws Exception {
|
||||
assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, null, "test")).isNotPresent();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1154
|
||||
public void discoversNoReturnType() throws Exception {
|
||||
void discoversNoReturnType() throws Exception {
|
||||
|
||||
MethodParameter parameter = new MethodParameter(DummyInterface.class.getDeclaredMethod("noReturnValue"), -1);
|
||||
assertThat(ReflectionUtils.isNullable(parameter)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1154
|
||||
public void discoversNullableReturnType() throws Exception {
|
||||
void discoversNullableReturnType() throws Exception {
|
||||
|
||||
MethodParameter parameter = new MethodParameter(DummyInterface.class.getDeclaredMethod("nullableReturnValue"), -1);
|
||||
assertThat(ReflectionUtils.isNullable(parameter)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1154
|
||||
public void discoversNonNullableReturnType() throws Exception {
|
||||
void discoversNonNullableReturnType() throws Exception {
|
||||
|
||||
MethodParameter parameter = new MethodParameter(DummyInterface.class.getDeclaredMethod("mandatoryReturnValue"), -1);
|
||||
assertThat(ReflectionUtils.isNullable(parameter)).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1154
|
||||
public void discoversNullableParameter() throws Exception {
|
||||
void discoversNullableParameter() throws Exception {
|
||||
|
||||
MethodParameter parameter = new MethodParameter(
|
||||
DummyInterface.class.getDeclaredMethod("nullableParameter", User.class), 0);
|
||||
@@ -146,28 +145,14 @@ public class ReflectionUtilsUnitTests {
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1154
|
||||
public void discoversNonNullablePrimitiveParameter() throws Exception {
|
||||
void discoversNonNullablePrimitiveParameter() throws Exception {
|
||||
|
||||
MethodParameter parameter = new MethodParameter(DummyInterface.class.getDeclaredMethod("primitive", int.class), 0);
|
||||
assertThat(ReflectionUtils.isNullable(parameter)).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1171
|
||||
public void discoversKotlinClass() {
|
||||
|
||||
assertThat(ReflectionUtils.isKotlinClass(TypeCreatingSyntheticClass.class)).isTrue();
|
||||
assertThat(ReflectionUtils.isSupportedKotlinClass(TypeCreatingSyntheticClass.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1171
|
||||
public void discoversUnsupportedKotlinClass() {
|
||||
|
||||
assertThat(ReflectionUtils.isKotlinClass(TypeCreatingSyntheticClassKt.class)).isTrue();
|
||||
assertThat(ReflectionUtils.isSupportedKotlinClass(TypeCreatingSyntheticClassKt.class)).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1779
|
||||
public void shouldReportIsVoid() {
|
||||
void shouldReportIsVoid() {
|
||||
|
||||
assertThat(ReflectionUtils.isVoid(Void.class)).isTrue();
|
||||
assertThat(ReflectionUtils.isVoid(Void.TYPE)).isTrue();
|
||||
@@ -186,7 +171,7 @@ public class ReflectionUtilsUnitTests {
|
||||
|
||||
private final String name;
|
||||
|
||||
public FieldNameFieldFilter(String name) {
|
||||
FieldNameFieldFilter(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user