DATACMNS-867 - First draft.

This commit is contained in:
Oliver Gierke
2016-11-14 20:10:22 +01:00
parent 1b17271915
commit cc63e5b7a4
278 changed files with 4737 additions and 5714 deletions

View File

@@ -1,385 +0,0 @@
/*
* Copyright 2014-2017 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.convert;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.convert.BytecodeGeneratingEntityInstantiator.*;
import static org.springframework.data.util.ClassTypeInformation.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.convert.BytecodeGeneratingEntityInstantiatorUnitTests.Outer.Inner;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PreferredConstructor;
import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.mapping.model.MappingInstantiationException;
import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;
/**
* Unit tests for {@link BytecodeGeneratingEntityInstantiator}.
*
* @author Thomas Darimont
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
@Deprecated
public class BytecodeGeneratingEntityInstantiatorUnitTests<P extends PersistentProperty<P>> {
@Mock PersistentEntity<?, P> entity;
@Mock ParameterValueProvider<P> provider;
@Mock PreferredConstructor<?, P> constructor;
@Mock Parameter<?, P> parameter;
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiatesSimpleObjectCorrectly() {
when(entity.getType()).thenReturn((Class) Object.class);
INSTANCE.createInstance(entity, provider);
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiatesArrayCorrectly() {
when(entity.getType()).thenReturn((Class) String[][].class);
INSTANCE.createInstance(entity, provider);
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiatesTypeWithPreferredConstructorUsingParameterValueProvider() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<Foo, P>(Foo.class).getConstructor();
when(entity.getType()).thenReturn((Class) Foo.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
Object instance = INSTANCE.createInstance(entity, provider);
assertTrue(instance instanceof Foo);
verify(provider, times(1)).getParameterValue((Parameter) constructor.getParameters().iterator().next());
}
@Test(expected = MappingInstantiationException.class) // DATACMNS-300, DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
public void throwsExceptionOnBeanInstantiationException() {
when(entity.getPersistenceConstructor()).thenReturn(null);
when(entity.getType()).thenReturn((Class) PersistentEntity.class);
INSTANCE.createInstance(entity, provider);
}
@Test // DATACMNS-134, DATACMNS-578
public void createsInnerClassInstanceCorrectly() {
BasicPersistentEntity<Inner, P> entity = new BasicPersistentEntity<Inner, P>(from(Inner.class));
PreferredConstructor<Inner, P> constructor = entity.getPersistenceConstructor();
Parameter<Object, P> parameter = constructor.getParameters().iterator().next();
final Object outer = new Outer();
when(provider.getParameterValue(parameter)).thenReturn(outer);
final Inner instance = INSTANCE.createInstance(entity, provider);
assertThat(instance, is(notNullValue()));
// Hack to check syntheic field as compiles create different field names (e.g. this$0, this$1)
ReflectionUtils.doWithFields(Inner.class, new FieldCallback() {
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
if (field.isSynthetic() && field.getName().startsWith("this$")) {
ReflectionUtils.makeAccessible(field);
assertThat(ReflectionUtils.getField(field, instance), is(outer));
}
}
});
}
@Test // DATACMNS-283, DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
public void capturesContextOnInstantiationException() throws Exception {
PersistentEntity<Sample, P> entity = new BasicPersistentEntity<Sample, P>(from(Sample.class));
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn("FOO");
Constructor constructor = Sample.class.getConstructor(Long.class, String.class);
List<Object> parameters = Arrays.asList((Object) "FOO", (Object) "FOO");
try {
INSTANCE.createInstance(entity, provider);
fail("Expected MappingInstantiationException!");
} catch (MappingInstantiationException o_O) {
assertThat(o_O.getConstructor(), is(constructor));
assertThat(o_O.getConstructorArguments(), is(parameters));
assertEquals(Sample.class, o_O.getEntityType());
assertThat(o_O.getMessage(), containsString(Sample.class.getName()));
assertThat(o_O.getMessage(), containsString(Long.class.getName()));
assertThat(o_O.getMessage(), containsString(String.class.getName()));
assertThat(o_O.getMessage(), containsString("FOO"));
}
}
@Test // DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiateObjCtorDefault() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<ObjCtorDefault, P>(ObjCtorDefault.class)
.getConstructor();
when(entity.getType()).thenReturn((Class) ObjCtorDefault.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
for (int i = 0; i < 2; i++) {
Object instance = INSTANCE.createInstance(entity, provider);
assertTrue(instance instanceof ObjCtorDefault);
}
}
@Test // DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiateObjCtorNoArgs() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<ObjCtorNoArgs, P>(ObjCtorNoArgs.class)
.getConstructor();
when(entity.getType()).thenReturn((Class) ObjCtorNoArgs.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
for (int i = 0; i < 2; i++) {
Object instance = INSTANCE.createInstance(entity, provider);
assertTrue(instance instanceof ObjCtorNoArgs);
assertTrue(((ObjCtorNoArgs) instance).ctorInvoked);
}
}
@Test // DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiateObjCtor1ParamString() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<ObjCtor1ParamString, P>(
ObjCtor1ParamString.class).getConstructor();
when(entity.getType()).thenReturn((Class) ObjCtor1ParamString.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn("FOO");
for (int i = 0; i < 2; i++) {
Object instance = INSTANCE.createInstance(entity, provider);
assertTrue(instance instanceof ObjCtor1ParamString);
assertTrue(((ObjCtor1ParamString) instance).ctorInvoked);
assertThat(((ObjCtor1ParamString) instance).param1, is("FOO"));
}
}
@Test // DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiateObjCtor2ParamStringString() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<ObjCtor2ParamStringString, P>(
ObjCtor2ParamStringString.class).getConstructor();
when(entity.getType()).thenReturn((Class) ObjCtor2ParamStringString.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
for (int i = 0; i < 2; i++) {
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn("FOO").thenReturn("BAR");
Object instance = INSTANCE.createInstance(entity, provider);
assertTrue(instance instanceof ObjCtor2ParamStringString);
assertTrue(((ObjCtor2ParamStringString) instance).ctorInvoked);
assertThat(((ObjCtor2ParamStringString) instance).param1, is("FOO"));
assertThat(((ObjCtor2ParamStringString) instance).param2, is("BAR"));
}
}
@Test // DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiateObjectCtor1ParamInt() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<ObjectCtor1ParamInt, P>(
ObjectCtor1ParamInt.class).getConstructor();
when(entity.getType()).thenReturn((Class) ObjectCtor1ParamInt.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
for (int i = 0; i < 2; i++) {
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn(42);
Object instance = INSTANCE.createInstance(entity, provider);
assertTrue(instance instanceof ObjectCtor1ParamInt);
assertTrue("matches", ((ObjectCtor1ParamInt) instance).param1 == 42);
}
}
@Test // DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiateObjectCtor7ParamsString5IntsString() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<ObjectCtor7ParamsString5IntsString, P>(
ObjectCtor7ParamsString5IntsString.class).getConstructor();
when(entity.getType()).thenReturn((Class) ObjectCtor7ParamsString5IntsString.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
for (int i = 0; i < 2; i++) {
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn("A").thenReturn(1).thenReturn(2)
.thenReturn(3).thenReturn(4).thenReturn(5).thenReturn("B");
Object instance = INSTANCE.createInstance(entity, provider);
assertTrue(instance instanceof ObjectCtor7ParamsString5IntsString);
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param1, is("A"));
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param2, is(1));
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param3, is(2));
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param4, is(3));
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param5, is(4));
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param6, is(5));
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param7, is("B"));
}
}
static class Foo {
Foo(String foo) {
}
}
static class Outer {
class Inner {
}
}
static class Sample {
final Long id;
final String name;
public Sample(Long id, String name) {
this.id = id;
this.name = name;
}
}
/**
* @author Thomas Darimont
*/
public static class ObjCtorDefault {}
/**
* @author Thomas Darimont
*/
public static class ObjCtorNoArgs {
public boolean ctorInvoked;
public ObjCtorNoArgs() {
ctorInvoked = true;
}
}
/**
* @author Thomas Darimont
*/
public static class ObjCtor1ParamString {
public boolean ctorInvoked;
public String param1;
public ObjCtor1ParamString(String param1) {
this.param1 = param1;
this.ctorInvoked = true;
}
}
/**
* @author Thomas Darimont
*/
public static class ObjCtor2ParamStringString {
public boolean ctorInvoked;
public String param1;
public String param2;
public ObjCtor2ParamStringString(String param1, String param2) {
this.ctorInvoked = true;
this.param1 = param1;
this.param2 = param2;
}
}
/**
* @author Thomas Darimont
*/
public static class ObjectCtor1ParamInt {
public int param1;
public ObjectCtor1ParamInt(int param1) {
this.param1 = param1;
}
}
/**
* @author Thomas Darimont
*/
public static class ObjectCtor7ParamsString5IntsString {
public String param1;
public int param2;
public int param3;
public int param4;
public int param5;
public int param6;
public String param7;
public ObjectCtor7ParamsString5IntsString(String param1, int param2, int param3, int param4, int param5,
int param6, String param7) {
this.param1 = param1;
this.param2 = param2;
this.param3 = param3;
this.param4 = param4;
this.param5 = param5;
this.param6 = param6;
this.param7 = param7;
}
}
}

View File

@@ -15,20 +15,23 @@
*/
package org.springframework.data.convert;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.util.ClassTypeInformation.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.convert.ClassGeneratingEntityInstantiatorUnitTests.Outer.Inner;
import org.springframework.data.mapping.PersistentEntity;
@@ -40,7 +43,6 @@ import org.springframework.data.mapping.model.MappingInstantiationException;
import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;
/**
* Unit tests for {@link ClassGeneratingEntityInstantiator}.
@@ -58,43 +60,49 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
@Mock PreferredConstructor<?, P> constructor;
@Mock Parameter<?, P> parameter;
@Before
public void setUp() {
doReturn(Optional.empty()).when(entity).getPersistenceConstructor();
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiatesSimpleObjectCorrectly() {
when(entity.getType()).thenReturn((Class) Object.class);
doReturn(Object.class).when(entity).getType();
this.instance.createInstance(entity, provider);
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiatesArrayCorrectly() {
when(entity.getType()).thenReturn((Class) String[][].class);
doReturn(String[][].class).when(entity).getType();
this.instance.createInstance(entity, provider);
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiatesTypeWithPreferredConstructorUsingParameterValueProvider() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<Foo, P>(Foo.class).getConstructor();
Optional<? extends PreferredConstructor<Foo, P>> constructor = new PreferredConstructorDiscoverer<Foo, P>(Foo.class)
.getConstructor();
when(entity.getType()).thenReturn((Class) Foo.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
doReturn(Foo.class).when(entity).getType();
doReturn(constructor).when(entity).getPersistenceConstructor();
Object instance = this.instance.createInstance(entity, provider);
assertThat(instance.createInstance(entity, provider)).isInstanceOf(Foo.class);
assertTrue(instance instanceof Foo);
verify(provider, times(1)).getParameterValue((Parameter) constructor.getParameters().iterator().next());
assertThat(constructor).hasValueSatisfying(it -> {
verify(provider, times(1)).getParameterValue(it.getParameters().iterator().next());
});
}
@Test(expected = MappingInstantiationException.class) // DATACMNS-300, DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
public void throwsExceptionOnBeanInstantiationException() {
when(entity.getPersistenceConstructor()).thenReturn(null);
when(entity.getType()).thenReturn((Class) PersistentEntity.class);
doReturn(Optional.empty()).when(entity).getPersistenceConstructor();
doReturn(PersistentEntity.class).when(entity).getType();
this.instance.createInstance(entity, provider);
}
@@ -103,24 +111,24 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
public void createsInnerClassInstanceCorrectly() {
BasicPersistentEntity<Inner, P> entity = new BasicPersistentEntity<Inner, P>(from(Inner.class));
PreferredConstructor<Inner, P> constructor = entity.getPersistenceConstructor();
Parameter<Object, P> parameter = constructor.getParameters().iterator().next();
assertThat(entity.getPersistenceConstructor()).hasValueSatisfying(constructor -> {
final Object outer = new Outer();
Parameter<Object, P> parameter = constructor.getParameters().iterator().next();
when(provider.getParameterValue(parameter)).thenReturn(outer);
final Inner instance = this.instance.createInstance(entity, provider);
Object outer = new Outer();
assertThat(instance, is(notNullValue()));
doReturn(Optional.of(outer)).when(provider).getParameterValue(parameter);
Inner instance = this.instance.createInstance(entity, provider);
// Hack to check syntheic field as compiles create different field names (e.g. this$0, this$1)
ReflectionUtils.doWithFields(Inner.class, new FieldCallback() {
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
assertThat(instance).isNotNull();
// Hack to check synthetic field as compiles create different field names (e.g. this$0, this$1)
ReflectionUtils.doWithFields(Inner.class, field -> {
if (field.isSynthetic() && field.getName().startsWith("this$")) {
ReflectionUtils.makeAccessible(field);
assertThat(ReflectionUtils.getField(field, instance), is(outer));
assertThat(ReflectionUtils.getField(field, instance)).isEqualTo(outer);
}
}
});
});
}
@@ -128,12 +136,12 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
@SuppressWarnings({ "unchecked", "rawtypes" })
public void capturesContextOnInstantiationException() throws Exception {
PersistentEntity<Sample, P> entity = new BasicPersistentEntity<Sample, P>(from(Sample.class));
PersistentEntity<Sample, P> entity = new BasicPersistentEntity<>(from(Sample.class));
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn("FOO");
doReturn(Optional.of("FOO")).when(provider).getParameterValue(any(Parameter.class));
Constructor constructor = Sample.class.getConstructor(Long.class, String.class);
List<Object> parameters = Arrays.asList((Object) "FOO", (Object) "FOO");
List<Object> parameters = Arrays.asList("FOO", "FOO");
try {
@@ -142,14 +150,14 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
} catch (MappingInstantiationException o_O) {
assertThat(o_O.getConstructor(), is(constructor));
assertThat(o_O.getConstructorArguments(), is(parameters));
assertEquals(Sample.class, o_O.getEntityType());
assertThat(o_O.getConstructor()).hasValue(constructor);
assertThat(o_O.getConstructorArguments()).isEqualTo(parameters);
assertThat(o_O.getEntityType()).hasValue(Sample.class);
assertThat(o_O.getMessage(), containsString(Sample.class.getName()));
assertThat(o_O.getMessage(), containsString(Long.class.getName()));
assertThat(o_O.getMessage(), containsString(String.class.getName()));
assertThat(o_O.getMessage(), containsString("FOO"));
assertThat(o_O.getMessage()).contains(Sample.class.getName());
assertThat(o_O.getMessage()).contains(Long.class.getName());
assertThat(o_O.getMessage()).contains(String.class.getName());
assertThat(o_O.getMessage()).contains("FOO");
}
}
@@ -157,120 +165,124 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiateObjCtorDefault() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<ObjCtorDefault, P>(ObjCtorDefault.class)
.getConstructor();
doReturn(ObjCtorDefault.class).when(entity).getType();
doReturn(new PreferredConstructorDiscoverer<>(ObjCtorDefault.class).getConstructor())//
.when(entity).getPersistenceConstructor();
when(entity.getType()).thenReturn((Class) ObjCtorDefault.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
for (int i = 0; i < 2; i++) {
Object instance = this.instance.createInstance(entity, provider);
assertTrue(instance instanceof ObjCtorDefault);
}
IntStream.range(0, 2).forEach(i -> {
assertThat(this.instance.createInstance(entity, provider)).isInstanceOf(ObjCtorDefault.class);
});
}
@Test // DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiateObjCtorNoArgs() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<ObjCtorNoArgs, P>(ObjCtorNoArgs.class)
.getConstructor();
doReturn(ObjCtorNoArgs.class).when(entity).getType();
doReturn(new PreferredConstructorDiscoverer<>(ObjCtorNoArgs.class).getConstructor())//
.when(entity).getPersistenceConstructor();
when(entity.getType()).thenReturn((Class) ObjCtorNoArgs.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
IntStream.range(0, 2).forEach(i -> {
for (int i = 0; i < 2; i++) {
Object instance = this.instance.createInstance(entity, provider);
assertTrue(instance instanceof ObjCtorNoArgs);
assertTrue(((ObjCtorNoArgs) instance).ctorInvoked);
}
assertThat(instance).isInstanceOf(ObjCtorNoArgs.class);
assertThat(((ObjCtorNoArgs) instance).ctorInvoked).isTrue();
});
}
@Test // DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings("unchecked")
public void instantiateObjCtor1ParamString() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<ObjCtor1ParamString, P>(
ObjCtor1ParamString.class).getConstructor();
doReturn(ObjCtor1ParamString.class).when(entity).getType();
doReturn(new PreferredConstructorDiscoverer<>(ObjCtor1ParamString.class).getConstructor())//
.when(entity).getPersistenceConstructor();
doReturn(Optional.of("FOO")).when(provider).getParameterValue(any());
when(entity.getType()).thenReturn((Class) ObjCtor1ParamString.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
IntStream.range(0, 2).forEach(i -> {
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn("FOO");
for (int i = 0; i < 2; i++) {
Object instance = this.instance.createInstance(entity, provider);
assertTrue(instance instanceof ObjCtor1ParamString);
assertTrue(((ObjCtor1ParamString) instance).ctorInvoked);
assertThat(((ObjCtor1ParamString) instance).param1, is("FOO"));
}
assertThat(instance).isInstanceOf(ObjCtor1ParamString.class);
assertThat(((ObjCtor1ParamString) instance).ctorInvoked).isTrue();
assertThat(((ObjCtor1ParamString) instance).param1).isEqualTo("FOO");
});
}
@Test // DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings("unchecked")
public void instantiateObjCtor2ParamStringString() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<ObjCtor2ParamStringString, P>(
ObjCtor2ParamStringString.class).getConstructor();
doReturn(ObjCtor2ParamStringString.class).when(entity).getType();
doReturn(new PreferredConstructorDiscoverer<>(ObjCtor2ParamStringString.class).getConstructor())//
.when(entity).getPersistenceConstructor();
when(entity.getType()).thenReturn((Class) ObjCtor2ParamStringString.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
IntStream.range(0, 2).forEach(i -> {
for (int i = 0; i < 2; i++) {
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn("FOO").thenReturn("BAR");
when(provider.getParameterValue(any())).thenReturn(Optional.of("FOO"), Optional.of("BAR"));
Object instance = this.instance.createInstance(entity, provider);
assertTrue(instance instanceof ObjCtor2ParamStringString);
assertTrue(((ObjCtor2ParamStringString) instance).ctorInvoked);
assertThat(((ObjCtor2ParamStringString) instance).param1, is("FOO"));
assertThat(((ObjCtor2ParamStringString) instance).param2, is("BAR"));
}
assertThat(instance).isInstanceOf(ObjCtor2ParamStringString.class);
assertThat(((ObjCtor2ParamStringString) instance).ctorInvoked).isTrue();
assertThat(((ObjCtor2ParamStringString) instance).param1).isEqualTo("FOO");
assertThat(((ObjCtor2ParamStringString) instance).param2).isEqualTo("BAR");
});
}
@Test // DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings("unchecked")
public void instantiateObjectCtor1ParamInt() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<ObjectCtor1ParamInt, P>(
ObjectCtor1ParamInt.class).getConstructor();
doReturn(ObjectCtor1ParamInt.class).when(entity).getType();
doReturn(new PreferredConstructorDiscoverer<>(ObjectCtor1ParamInt.class).getConstructor())//
.when(entity).getPersistenceConstructor();
when(entity.getType()).thenReturn((Class) ObjectCtor1ParamInt.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
IntStream.range(0, 2).forEach(i -> {
for (int i = 0; i < 2; i++) {
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn(42);
doReturn(Optional.of(42)).when(provider).getParameterValue(any());
Object instance = this.instance.createInstance(entity, provider);
assertTrue(instance instanceof ObjectCtor1ParamInt);
assertTrue("matches", ((ObjectCtor1ParamInt) instance).param1 == 42);
}
assertThat(instance).isInstanceOf(ObjectCtor1ParamInt.class);
assertThat(((ObjectCtor1ParamInt) instance).param1).isEqualTo(42);
});
}
@Test // DATACMNS-578
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings("unchecked")
public void instantiateObjectCtor7ParamsString5IntsString() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<ObjectCtor7ParamsString5IntsString, P>(
ObjectCtor7ParamsString5IntsString.class).getConstructor();
doReturn(ObjectCtor7ParamsString5IntsString.class).when(entity).getType();
doReturn(new PreferredConstructorDiscoverer<>(ObjectCtor7ParamsString5IntsString.class).getConstructor())//
.when(entity).getPersistenceConstructor();
when(entity.getType()).thenReturn((Class) ObjectCtor7ParamsString5IntsString.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
IntStream.range(0, 2).forEach(i -> {
for (int i = 0; i < 2; i++) {
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn("A").thenReturn(1).thenReturn(2)
.thenReturn(3).thenReturn(4).thenReturn(5).thenReturn("B");
when(provider.getParameterValue(any(Parameter.class))).thenReturn(Optional.of("A"), Optional.of(1),
Optional.of(2), Optional.of(3), Optional.of(4), Optional.of(5), Optional.of("B"));
Object instance = this.instance.createInstance(entity, provider);
assertTrue(instance instanceof ObjectCtor7ParamsString5IntsString);
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param1, is("A"));
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param2, is(1));
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param3, is(2));
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param4, is(3));
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param5, is(4));
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param6, is(5));
assertThat(((ObjectCtor7ParamsString5IntsString) instance).param7, is("B"));
}
assertThat(instance).isInstanceOf(ObjectCtor7ParamsString5IntsString.class);
ObjectCtor7ParamsString5IntsString toTest = (ObjectCtor7ParamsString5IntsString) instance;
assertThat(toTest.param1).isEqualTo("A");
assertThat(toTest.param2).isEqualTo(1);
assertThat(toTest.param3).isEqualTo(2);
assertThat(toTest.param4).isEqualTo(3);
assertThat(toTest.param5).isEqualTo(4);
assertThat(toTest.param6).isEqualTo(5);
assertThat(toTest.param7).isEqualTo("B");
});
}
@Test
public void testname() {
List<String> result = Stream.of("1", null).map(it -> (String) null).collect(Collectors.toList());
}
static class Foo {
@@ -371,8 +383,8 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
public int param6;
public String param7;
public ObjectCtor7ParamsString5IntsString(String param1, int param2, int param3, int param4, int param5,
int param6, String param7) {
public ObjectCtor7ParamsString5IntsString(String param1, int param2, int param3, int param4, int param5, int param6,
String param7) {
this.param1 = param1;
this.param2 = param2;
this.param3 = param3;

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.convert;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import java.util.Collections;
import java.util.HashMap;
@@ -63,15 +62,15 @@ public class ConfigurableTypeInformationMapperUnitTests<T extends PersistentProp
@Test
public void writesMapKeyForType() {
assertThat(mapper.createAliasFor(ClassTypeInformation.from(String.class)), is((Object) "1"));
assertThat(mapper.createAliasFor(ClassTypeInformation.from(Object.class)), is(nullValue()));
assertThat(mapper.createAliasFor(ClassTypeInformation.from(String.class))).isEqualTo("1");
assertThat(mapper.createAliasFor(ClassTypeInformation.from(Object.class))).isNull();
}
@Test
@SuppressWarnings("rawtypes")
public void readsTypeForMapKey() {
assertThat(mapper.resolveTypeFrom("1"), is((TypeInformation) ClassTypeInformation.from(String.class)));
assertThat(mapper.resolveTypeFrom("unmapped"), is(nullValue()));
assertThat(mapper.resolveTypeFrom("1")).isEqualTo((TypeInformation) ClassTypeInformation.from(String.class));
assertThat(mapper.resolveTypeFrom("unmapped")).isNull();
}
}

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.convert;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
@@ -64,7 +63,7 @@ public class DefaultTypeMapperUnitTests {
public void cachesResolvedTypeInformation() {
TypeInformation<?> information = typeMapper.readType(source);
assertThat(information, is((TypeInformation) STRING_TYPE_INFO));
assertThat(information).isEqualTo((TypeInformation) STRING_TYPE_INFO);
verify(mapper, times(1)).resolveTypeFrom(STRING);
typeMapper.readType(source);
@@ -77,7 +76,7 @@ public class DefaultTypeMapperUnitTests {
Object alias = "alias";
when(mapper.createAliasFor(STRING_TYPE_INFO)).thenReturn(alias);
assertThat(this.typeMapper.getAliasFor(STRING_TYPE_INFO), is(alias));
assertThat(this.typeMapper.getAliasFor(STRING_TYPE_INFO)).isEqualTo(alias);
}
@Test // DATACMNS-783
@@ -92,8 +91,8 @@ public class DefaultTypeMapperUnitTests {
TypeInformation<?> result = typeMapper.readType(source, propertyType);
assertThat(result.getType(), is((Object) Bar.class));
assertThat(result.getProperty("field").getType(), is((Object) Character.class));
assertThat(result.getType()).isEqualTo(Bar.class);
assertThat(result.getProperty("field").getType()).isEqualTo(Character.class);
}
static class TypeWithAbstractGenericType<T> {

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.convert;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Collections;
@@ -48,38 +47,36 @@ public class EntityInstantiatorsUnitTests {
public void usesReflectionEntityInstantiatorAsDefaultFallback() {
EntityInstantiators instantiators = new EntityInstantiators();
assertThat(instantiators.getInstantiatorFor(entity),
instanceOf(ClassGeneratingEntityInstantiator.class));
assertThat(instantiators.getInstantiatorFor(entity)).isInstanceOf(ClassGeneratingEntityInstantiator.class);
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void returnsCustomInstantiatorForTypeIfRegistered() {
when(entity.getType()).thenReturn((Class) String.class);
doReturn(String.class).when(entity).getType();
Map<Class<?>, EntityInstantiator> customInstantiators = Collections.<Class<?>, EntityInstantiator> singletonMap(
String.class, customInstantiator);
Map<Class<?>, EntityInstantiator> customInstantiators = Collections
.<Class<?>, EntityInstantiator> singletonMap(String.class, customInstantiator);
EntityInstantiators instantiators = new EntityInstantiators(customInstantiators);
assertThat(instantiators.getInstantiatorFor(entity), is(customInstantiator));
assertThat(instantiators.getInstantiatorFor(entity)).isEqualTo(customInstantiator);
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void usesCustomFallbackInstantiatorsIfConfigured() {
when(entity.getType()).thenReturn((Class) Object.class);
doReturn(Object.class).when(entity).getType();
Map<Class<?>, EntityInstantiator> customInstantiators = Collections.<Class<?>, EntityInstantiator> singletonMap(
String.class, ReflectionEntityInstantiator.INSTANCE);
Map<Class<?>, EntityInstantiator> customInstantiators = Collections
.<Class<?>, EntityInstantiator> singletonMap(String.class, ReflectionEntityInstantiator.INSTANCE);
EntityInstantiators instantiators = new EntityInstantiators(customInstantiator, customInstantiators);
instantiators.getInstantiatorFor(entity);
assertThat(instantiators.getInstantiatorFor(entity), is(customInstantiator));
assertThat(instantiators.getInstantiatorFor(entity)).isEqualTo(customInstantiator);
when(entity.getType()).thenReturn((Class) String.class);
assertThat(instantiators.getInstantiatorFor(entity), is((EntityInstantiator) ReflectionEntityInstantiator.INSTANCE));
doReturn(String.class).when(entity).getType();
assertThat(instantiators.getInstantiatorFor(entity))
.isEqualTo((EntityInstantiator) ReflectionEntityInstantiator.INSTANCE);
}
}

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.convert;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import java.text.SimpleDateFormat;
import java.time.Duration;
@@ -76,53 +75,54 @@ public class Jsr310ConvertersUnitTests {
@Test // DATACMNS-606
public void convertsDateToLocalDateTime() {
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDateTime.class).toString(),
is(format(NOW, "yyyy-MM-dd'T'HH:mm:ss.SSS")));
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDateTime.class).toString())
.isEqualTo(format(NOW, "yyyy-MM-dd'T'HH:mm:ss.SSS"));
}
@Test // DATACMNS-606
public void convertsLocalDateTimeToDate() {
LocalDateTime now = LocalDateTime.now();
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "yyyy-MM-dd'T'HH:mm:ss.SSS"), is(now.toString()));
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "yyyy-MM-dd'T'HH:mm:ss.SSS"))
.isEqualTo(now.toString());
}
@Test // DATACMNS-606
public void convertsDateToLocalDate() {
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDate.class).toString(), is(format(NOW, "yyyy-MM-dd")));
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDate.class).toString()).isEqualTo(format(NOW, "yyyy-MM-dd"));
}
@Test // DATACMNS-606
public void convertsLocalDateToDate() {
LocalDate now = LocalDate.now();
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "yyyy-MM-dd"), is(now.toString()));
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "yyyy-MM-dd")).isEqualTo(now.toString());
}
@Test // DATACMNS-606
public void convertsDateToLocalTime() {
assertThat(CONVERSION_SERVICE.convert(NOW, LocalTime.class).toString(), is(format(NOW, "HH:mm:ss.SSS")));
assertThat(CONVERSION_SERVICE.convert(NOW, LocalTime.class).toString()).isEqualTo(format(NOW, "HH:mm:ss.SSS"));
}
@Test // DATACMNS-606
public void convertsLocalTimeToDate() {
LocalTime now = LocalTime.now();
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "HH:mm:ss.SSS"), is(now.toString()));
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "HH:mm:ss.SSS")).isEqualTo(now.toString());
}
@Test // DATACMNS-623
public void convertsDateToInstant() {
Date now = new Date();
assertThat(CONVERSION_SERVICE.convert(now, Instant.class), is(now.toInstant()));
assertThat(CONVERSION_SERVICE.convert(now, Instant.class)).isEqualTo(now.toInstant());
}
@Test // DATACMNS-623
public void convertsInstantToDate() {
Date now = new Date();
assertThat(CONVERSION_SERVICE.convert(now.toInstant(), Date.class), is(now));
assertThat(CONVERSION_SERVICE.convert(now.toInstant(), Date.class)).isEqualTo(now);
}
@Test
@@ -133,8 +133,8 @@ public class Jsr310ConvertersUnitTests {
ids.put("+06:00", ZoneId.of("+06:00"));
for (Entry<String, ZoneId> entry : ids.entrySet()) {
assertThat(CONVERSION_SERVICE.convert(entry.getValue(), String.class), is(entry.getKey()));
assertThat(CONVERSION_SERVICE.convert(entry.getKey(), ZoneId.class), is(entry.getValue()));
assertThat(CONVERSION_SERVICE.convert(entry.getValue(), String.class)).isEqualTo(entry.getKey());
assertThat(CONVERSION_SERVICE.convert(entry.getKey(), ZoneId.class)).isEqualTo(entry.getValue());
}
}
@@ -186,8 +186,8 @@ public class Jsr310ConvertersUnitTests {
public void convertsPeriodToStringAndBack() {
ResolvableType type = ResolvableType.forClass(ConversionTest.class, this.getClass());
assertThat(CONVERSION_SERVICE.convert(target, String.class), is(string));
assertThat(CONVERSION_SERVICE.convert(string, type.getGeneric(0).getRawClass()), is((Object) target));
assertThat(CONVERSION_SERVICE.convert(target, String.class)).isEqualTo(string);
assertThat(CONVERSION_SERVICE.convert(string, type.getGeneric(0).getRawClass())).isEqualTo(target);
}
}
}

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.convert;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.util.ClassTypeInformation.*;
import java.util.Collections;
@@ -59,7 +58,7 @@ public class MappingContextTypeInformationMapperUnitTests {
mapper = new MappingContextTypeInformationMapper(mappingContext);
assertThat(mapper.createAliasFor(ClassTypeInformation.from(Entity.class)), is((Object) "foo"));
assertThat(mapper.createAliasFor(ClassTypeInformation.from(Entity.class))).isEqualTo("foo");
}
@Test
@@ -70,7 +69,7 @@ public class MappingContextTypeInformationMapperUnitTests {
mapper = new MappingContextTypeInformationMapper(mappingContext);
assertThat(mapper.createAliasFor(from(Entity.class)), is((Object) "foo"));
assertThat(mapper.createAliasFor(from(Entity.class))).isEqualTo("foo");
}
@Test
@@ -80,7 +79,7 @@ public class MappingContextTypeInformationMapperUnitTests {
mappingContext.initialize();
mapper = new MappingContextTypeInformationMapper(mappingContext);
assertThat(mapper.createAliasFor(from(String.class)), is(nullValue()));
assertThat(mapper.createAliasFor(from(String.class))).isNull();
}
@Test
@@ -91,12 +90,12 @@ public class MappingContextTypeInformationMapperUnitTests {
mappingContext.initialize();
mapper = new MappingContextTypeInformationMapper(mappingContext);
assertThat(mapper.resolveTypeFrom("foo"), is(nullValue()));
assertThat(mapper.resolveTypeFrom("foo")).isNull();
PersistentEntity<?, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Entity.class);
assertThat(entity, is(notNullValue()));
assertThat(mapper.resolveTypeFrom("foo"), is((TypeInformation) from(Entity.class)));
assertThat(entity).isNotNull();
assertThat(mapper.resolveTypeFrom("foo")).isEqualTo((TypeInformation) from(Entity.class));
}
@Test // DATACMNS-485

View File

@@ -15,21 +15,21 @@
*/
package org.springframework.data.convert;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.convert.ReflectionEntityInstantiator.*;
import static org.springframework.data.util.ClassTypeInformation.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.convert.ReflectionEntityInstantiatorUnitTests.Outer.Inner;
import org.springframework.data.mapping.PersistentEntity;
@@ -41,7 +41,6 @@ import org.springframework.data.mapping.model.MappingInstantiationException;
import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;
/**
* Unit tests for {@link ReflectionEntityInstantiator}.
@@ -52,52 +51,54 @@ import org.springframework.util.ReflectionUtils.FieldCallback;
@RunWith(MockitoJUnitRunner.class)
public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<P>> {
@Mock
PersistentEntity<?, P> entity;
@Mock
ParameterValueProvider<P> provider;
@Mock
PreferredConstructor<?, P> constructor;
@Mock
Parameter<?, P> parameter;
@Mock PersistentEntity<?, P> entity;
@Mock ParameterValueProvider<P> provider;
@Mock PreferredConstructor<?, P> constructor;
@Mock Parameter<?, P> parameter;
@Before
public void setUp() {
doReturn(Optional.empty()).when(entity).getPersistenceConstructor();
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiatesSimpleObjectCorrectly() {
when(entity.getType()).thenReturn((Class) Object.class);
doReturn(Object.class).when(entity).getType();
INSTANCE.createInstance(entity, provider);
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiatesArrayCorrectly() {
when(entity.getType()).thenReturn((Class) String[][].class);
doReturn(String[][].class).when(entity).getType();
INSTANCE.createInstance(entity, provider);
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void instantiatesTypeWithPreferredConstructorUsingParameterValueProvider() {
PreferredConstructor constructor = new PreferredConstructorDiscoverer<Foo, P>(Foo.class).getConstructor();
Optional<? extends PreferredConstructor<Foo, P>> constructor = new PreferredConstructorDiscoverer<Foo, P>(Foo.class)
.getConstructor();
when(entity.getType()).thenReturn((Class) Foo.class);
when(entity.getPersistenceConstructor()).thenReturn(constructor);
doReturn(Foo.class).when(entity).getType();
doReturn(constructor).when(entity).getPersistenceConstructor();
doReturn(Optional.empty()).when(provider).getParameterValue(any());
Object instance = INSTANCE.createInstance(entity, provider);
assertTrue(instance instanceof Foo);
verify(provider, times(1)).getParameterValue((Parameter) constructor.getParameters().iterator().next());
assertThat(instance).isInstanceOf(Foo.class);
assertThat(constructor).hasValueSatisfying(it -> {
verify(provider, times(1)).getParameterValue(it.getParameters().iterator().next());
});
}
@Test(expected = MappingInstantiationException.class) // DATACMNS-300
@SuppressWarnings({ "unchecked", "rawtypes" })
public void throwsExceptionOnBeanInstantiationException() {
when(entity.getPersistenceConstructor()).thenReturn(null);
when(entity.getType()).thenReturn((Class) PersistentEntity.class);
doReturn(Optional.empty()).when(entity).getPersistenceConstructor();
doReturn(PersistentEntity.class).when(entity).getType();
INSTANCE.createInstance(entity, provider);
}
@@ -106,24 +107,24 @@ public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<
public void createsInnerClassInstanceCorrectly() {
BasicPersistentEntity<Inner, P> entity = new BasicPersistentEntity<Inner, P>(from(Inner.class));
PreferredConstructor<Inner, P> constructor = entity.getPersistenceConstructor();
Parameter<Object, P> parameter = constructor.getParameters().iterator().next();
assertThat(entity.getPersistenceConstructor()).hasValueSatisfying(it -> {
final Object outer = new Outer();
Parameter<Object, P> parameter = it.getParameters().iterator().next();
when(provider.getParameterValue(parameter)).thenReturn(outer);
final Inner instance = INSTANCE.createInstance(entity, provider);
Object outer = new Outer();
assertThat(instance, is(notNullValue()));
when(provider.getParameterValue(parameter)).thenReturn(Optional.of(outer));
Inner instance = INSTANCE.createInstance(entity, provider);
// Hack to check syntheic field as compiles create different field names (e.g. this$0, this$1)
ReflectionUtils.doWithFields(Inner.class, new FieldCallback() {
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
assertThat(instance).isNotNull();
// Hack to check synthetic field as compiles create different field names (e.g. this$0, this$1)
ReflectionUtils.doWithFields(Inner.class, field -> {
if (field.isSynthetic() && field.getName().startsWith("this$")) {
ReflectionUtils.makeAccessible(field);
assertThat(ReflectionUtils.getField(field, instance), is(outer));
assertThat(ReflectionUtils.getField(field, instance)).isEqualTo(outer);
}
}
});
});
}
@@ -133,10 +134,10 @@ public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<
PersistentEntity<Sample, P> entity = new BasicPersistentEntity<Sample, P>(from(Sample.class));
when(provider.getParameterValue(Mockito.any(Parameter.class))).thenReturn("FOO");
doReturn(Optional.of("FOO")).when(provider).getParameterValue(any(Parameter.class));
Constructor constructor = Sample.class.getConstructor(Long.class, String.class);
List<Object> parameters = Arrays.asList((Object) "FOO", (Object) "FOO");
List<Object> parameters = Arrays.asList("FOO", "FOO");
try {
@@ -145,14 +146,14 @@ public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<
} catch (MappingInstantiationException o_O) {
assertThat(o_O.getConstructor(), is(constructor));
assertThat(o_O.getConstructorArguments(), is(parameters));
assertEquals(Sample.class, o_O.getEntityType());
assertThat(o_O.getConstructor()).hasValue(constructor);
assertThat(o_O.getConstructorArguments()).isEqualTo(parameters);
assertThat(o_O.getEntityType()).hasValue(Sample.class);
assertThat(o_O.getMessage(), containsString(Sample.class.getName()));
assertThat(o_O.getMessage(), containsString(Long.class.getName()));
assertThat(o_O.getMessage(), containsString(String.class.getName()));
assertThat(o_O.getMessage(), containsString("FOO"));
assertThat(o_O.getMessage()).contains(Sample.class.getName());
assertThat(o_O.getMessage()).contains(Long.class.getName());
assertThat(o_O.getMessage()).contains(String.class.getName());
assertThat(o_O.getMessage()).contains("FOO");
}
}

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.convert;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import org.junit.Test;
import org.springframework.data.util.ClassTypeInformation;
@@ -38,28 +37,28 @@ public class SimpleTypeInformationMapperUnitTests {
TypeInformation expected = ClassTypeInformation.from(String.class);
assertThat(type, is(expected));
assertThat(type).isEqualTo(expected);
}
@Test
public void returnsNullForNonStringKey() {
TypeInformationMapper mapper = new SimpleTypeInformationMapper();
assertThat(mapper.resolveTypeFrom(new Object()), is(nullValue()));
assertThat(mapper.resolveTypeFrom(new Object())).isNull();
}
@Test
public void returnsNullForEmptyTypeKey() {
TypeInformationMapper mapper = new SimpleTypeInformationMapper();
assertThat(mapper.resolveTypeFrom(""), is(nullValue()));
assertThat(mapper.resolveTypeFrom("")).isNull();
}
@Test
public void returnsNullForUnloadableClass() {
TypeInformationMapper mapper = new SimpleTypeInformationMapper();
assertThat(mapper.resolveTypeFrom("Foo"), is(nullValue()));
assertThat(mapper.resolveTypeFrom("Foo")).isNull();
}
@Test
@@ -68,7 +67,7 @@ public class SimpleTypeInformationMapperUnitTests {
TypeInformationMapper mapper = new SimpleTypeInformationMapper();
Object alias = mapper.createAliasFor(ClassTypeInformation.from(String.class));
assertTrue(alias instanceof String);
assertThat(alias, is((Object) String.class.getName()));
assertThat(alias).isInstanceOf(String.class);
assertThat(alias).isEqualTo(String.class.getName());
}
}

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.convert;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.threeten.bp.DateTimeUtils.*;
import java.text.SimpleDateFormat;
@@ -59,53 +58,54 @@ public class ThreeTenBackPortConvertersUnitTests {
@Test // DATACMNS-606
public void convertsDateToLocalDateTime() {
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDateTime.class).toString(),
is(format(NOW, "yyyy-MM-dd'T'HH:mm:ss.SSS")));
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDateTime.class).toString())
.isEqualTo(format(NOW, "yyyy-MM-dd'T'HH:mm:ss.SSS"));
}
@Test // DATACMNS-606
public void convertsLocalDateTimeToDate() {
LocalDateTime now = LocalDateTime.now();
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "yyyy-MM-dd'T'HH:mm:ss.SSS"), is(now.toString()));
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "yyyy-MM-dd'T'HH:mm:ss.SSS"))
.isEqualTo(now.toString());
}
@Test // DATACMNS-606
public void convertsDateToLocalDate() {
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDate.class).toString(), is(format(NOW, "yyyy-MM-dd")));
assertThat(CONVERSION_SERVICE.convert(NOW, LocalDate.class).toString()).isEqualTo(format(NOW, "yyyy-MM-dd"));
}
@Test // DATACMNS-606
public void convertsLocalDateToDate() {
LocalDate now = LocalDate.now();
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "yyyy-MM-dd"), is(now.toString()));
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "yyyy-MM-dd")).isEqualTo(now.toString());
}
@Test // DATACMNS-606
public void convertsDateToLocalTime() {
assertThat(CONVERSION_SERVICE.convert(NOW, LocalTime.class).toString(), is(format(NOW, "HH:mm:ss.SSS")));
assertThat(CONVERSION_SERVICE.convert(NOW, LocalTime.class).toString()).isEqualTo(format(NOW, "HH:mm:ss.SSS"));
}
@Test // DATACMNS-606
public void convertsLocalTimeToDate() {
LocalTime now = LocalTime.now();
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "HH:mm:ss.SSS"), is(now.toString()));
assertThat(format(CONVERSION_SERVICE.convert(now, Date.class), "HH:mm:ss.SSS")).isEqualTo(now.toString());
}
@Test // DATACMNS-623
public void convertsDateToInstant() {
Date now = new Date();
assertThat(CONVERSION_SERVICE.convert(now, Instant.class), is(toInstant(now)));
assertThat(CONVERSION_SERVICE.convert(now, Instant.class)).isEqualTo(toInstant(now));
}
@Test // DATACMNS-623
public void convertsInstantToDate() {
Date now = new Date();
assertThat(CONVERSION_SERVICE.convert(toInstant(now), Date.class), is(now));
assertThat(CONVERSION_SERVICE.convert(toInstant(now), Date.class)).isEqualTo(now);
}
@Test
@@ -116,8 +116,8 @@ public class ThreeTenBackPortConvertersUnitTests {
ids.put("+06:00", ZoneId.of("+06:00"));
for (Entry<String, ZoneId> entry : ids.entrySet()) {
assertThat(CONVERSION_SERVICE.convert(entry.getValue(), String.class), is(entry.getKey()));
assertThat(CONVERSION_SERVICE.convert(entry.getKey(), ZoneId.class), is(entry.getValue()));
assertThat(CONVERSION_SERVICE.convert(entry.getValue(), String.class)).isEqualTo(entry.getKey());
assertThat(CONVERSION_SERVICE.convert(entry.getKey(), ZoneId.class)).isEqualTo(entry.getValue());
}
}