DATACMNS-1126 - Add class-generating entity instantiation support for Kotlin.
We now discover Kotlin constructors and apply parameter defaulting to allow construction of immutable value objects. Constructor discovery uses primary constructors by default and considers PersistenceConstructor annotations.
KotlinClassGeneratingEntityInstantiator can instantiate Kotlin classes with default parameters by resolving the synthetic constructor. Null values translate to parameter defaults.
class Person(val firstname: String = "Walter") {
}
class Address(val street: String, val city: String) {
@PersistenceConstructor
constructor(street: String = "Unknown", city: String = "Unknown", country: String) : this(street, city)
}
Original pull request: #233.
This commit is contained in:
committed by
Oliver Gierke
parent
e4e677d6b1
commit
22b37800ec
@@ -53,8 +53,6 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
|
||||
|
||||
@Mock PersistentEntity<?, P> entity;
|
||||
@Mock ParameterValueProvider<P> provider;
|
||||
@Mock PreferredConstructor<?, P> constructor;
|
||||
@Mock Parameter<?, P> parameter;
|
||||
|
||||
@Test
|
||||
public void instantiatesSimpleObjectCorrectly() {
|
||||
@@ -72,11 +70,10 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
|
||||
this.instance.createInstance(entity, provider);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test // DATACMNS-1126
|
||||
public void instantiatesTypeWithPreferredConstructorUsingParameterValueProvider() {
|
||||
|
||||
PreferredConstructor<Foo, P> constructor = new PreferredConstructorDiscoverer<Foo, P>(Foo.class)
|
||||
.getConstructor();
|
||||
PreferredConstructor<Foo, P> constructor = PreferredConstructorDiscoverer.discover(Foo.class);
|
||||
|
||||
doReturn(Foo.class).when(entity).getType();
|
||||
doReturn(constructor).when(entity).getPersistenceConstructor();
|
||||
@@ -150,23 +147,21 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
|
||||
}
|
||||
}
|
||||
|
||||
@Test // DATACMNS-578
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Test // DATACMNS-578, DATACMNS-1126
|
||||
public void instantiateObjCtorDefault() {
|
||||
|
||||
doReturn(ObjCtorDefault.class).when(entity).getType();
|
||||
doReturn(new PreferredConstructorDiscoverer<>(ObjCtorDefault.class).getConstructor())//
|
||||
doReturn(PreferredConstructorDiscoverer.discover(ObjCtorDefault.class))//
|
||||
.when(entity).getPersistenceConstructor();
|
||||
|
||||
IntStream.range(0, 2).forEach(i -> assertThat(this.instance.createInstance(entity, provider)).isInstanceOf(ObjCtorDefault.class));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-578
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Test // DATACMNS-578, DATACMNS-1126
|
||||
public void instantiateObjCtorNoArgs() {
|
||||
|
||||
doReturn(ObjCtorNoArgs.class).when(entity).getType();
|
||||
doReturn(new PreferredConstructorDiscoverer<>(ObjCtorNoArgs.class).getConstructor())//
|
||||
doReturn(PreferredConstructorDiscoverer.discover(ObjCtorNoArgs.class))//
|
||||
.when(entity).getPersistenceConstructor();
|
||||
|
||||
IntStream.range(0, 2).forEach(i -> {
|
||||
@@ -178,12 +173,11 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-578
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test // DATACMNS-578, DATACMNS-1126
|
||||
public void instantiateObjCtor1ParamString() {
|
||||
|
||||
doReturn(ObjCtor1ParamString.class).when(entity).getType();
|
||||
doReturn(new PreferredConstructorDiscoverer<>(ObjCtor1ParamString.class).getConstructor())//
|
||||
doReturn(PreferredConstructorDiscoverer.discover(ObjCtor1ParamString.class))//
|
||||
.when(entity).getPersistenceConstructor();
|
||||
doReturn("FOO").when(provider).getParameterValue(any());
|
||||
|
||||
@@ -197,12 +191,11 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-578
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test // DATACMNS-578, DATACMNS-1126
|
||||
public void instantiateObjCtor2ParamStringString() {
|
||||
|
||||
doReturn(ObjCtor2ParamStringString.class).when(entity).getType();
|
||||
doReturn(new PreferredConstructorDiscoverer<>(ObjCtor2ParamStringString.class).getConstructor())//
|
||||
doReturn(PreferredConstructorDiscoverer.discover(ObjCtor2ParamStringString.class))//
|
||||
.when(entity).getPersistenceConstructor();
|
||||
|
||||
IntStream.range(0, 2).forEach(i -> {
|
||||
@@ -218,12 +211,11 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-578
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test // DATACMNS-578, DATACMNS-1126
|
||||
public void instantiateObjectCtor1ParamInt() {
|
||||
|
||||
doReturn(ObjectCtor1ParamInt.class).when(entity).getType();
|
||||
doReturn(new PreferredConstructorDiscoverer<>(ObjectCtor1ParamInt.class).getConstructor())//
|
||||
doReturn(PreferredConstructorDiscoverer.discover(ObjectCtor1ParamInt.class))//
|
||||
.when(entity).getPersistenceConstructor();
|
||||
|
||||
IntStream.range(0, 2).forEach(i -> {
|
||||
@@ -237,12 +229,12 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-578
|
||||
@Test // DATACMNS-578, DATACMNS-1126
|
||||
@SuppressWarnings("unchecked")
|
||||
public void instantiateObjectCtor7ParamsString5IntsString() {
|
||||
|
||||
doReturn(ObjectCtor7ParamsString5IntsString.class).when(entity).getType();
|
||||
doReturn(new PreferredConstructorDiscoverer<>(ObjectCtor7ParamsString5IntsString.class).getConstructor())//
|
||||
doReturn(PreferredConstructorDiscoverer.discover(ObjectCtor7ParamsString5IntsString.class))//
|
||||
.when(entity).getPersistenceConstructor();
|
||||
|
||||
IntStream.range(0, 2).forEach(i -> {
|
||||
|
||||
@@ -51,8 +51,6 @@ public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<
|
||||
|
||||
@Mock PersistentEntity<?, P> entity;
|
||||
@Mock ParameterValueProvider<P> provider;
|
||||
@Mock PreferredConstructor<?, P> constructor;
|
||||
@Mock Parameter<?, P> parameter;
|
||||
|
||||
@Test
|
||||
public void instantiatesSimpleObjectCorrectly() {
|
||||
@@ -68,10 +66,10 @@ public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<
|
||||
INSTANCE.createInstance(entity, provider);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test // DATACMNS-1126
|
||||
public void instantiatesTypeWithPreferredConstructorUsingParameterValueProvider() {
|
||||
|
||||
PreferredConstructor<Foo, P> constructor = new PreferredConstructorDiscoverer<Foo, P>(Foo.class).getConstructor();
|
||||
PreferredConstructor<Foo, P> constructor = PreferredConstructorDiscoverer.discover(Foo.class);
|
||||
|
||||
doReturn(constructor).when(entity).getPersistenceConstructor();
|
||||
|
||||
|
||||
@@ -36,13 +36,10 @@ import org.springframework.data.util.ClassTypeInformation;
|
||||
*/
|
||||
public class PreferredConstructorDiscovererUnitTests<P extends PersistentProperty<P>> {
|
||||
|
||||
@Test
|
||||
@Test // DATACMNS-1126
|
||||
public void findsNoArgConstructorForClassWithoutExplicitConstructor() {
|
||||
|
||||
PreferredConstructorDiscoverer<EntityWithoutConstructor, P> discoverer = new PreferredConstructorDiscoverer<>(
|
||||
EntityWithoutConstructor.class);
|
||||
|
||||
assertThat(discoverer.getConstructor()).satisfies(constructor -> {
|
||||
assertThat(PreferredConstructorDiscoverer.discover(EntityWithoutConstructor.class)).satisfies(constructor -> {
|
||||
|
||||
assertThat(constructor).isNotNull();
|
||||
assertThat(constructor.isNoArgConstructor()).isTrue();
|
||||
@@ -50,13 +47,10 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test // DATACMNS-1126
|
||||
public void findsNoArgConstructorForClassWithMultipleConstructorsAndNoArgOne() {
|
||||
|
||||
PreferredConstructorDiscoverer<ClassWithEmptyConstructor, P> discoverer = new PreferredConstructorDiscoverer<>(
|
||||
ClassWithEmptyConstructor.class);
|
||||
|
||||
assertThat(discoverer.getConstructor()).satisfies(constructor -> {
|
||||
assertThat(PreferredConstructorDiscoverer.discover(ClassWithEmptyConstructor.class)).satisfies(constructor -> {
|
||||
|
||||
assertThat(constructor).isNotNull();
|
||||
assertThat(constructor.isNoArgConstructor()).isTrue();
|
||||
@@ -64,22 +58,19 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test // DATACMNS-1126
|
||||
public void doesNotThrowExceptionForMultipleConstructorsAndNoNoArgConstructorWithoutAnnotation() {
|
||||
|
||||
PreferredConstructorDiscoverer<ClassWithMultipleConstructorsWithoutEmptyOne, P> discoverer = new PreferredConstructorDiscoverer<>(
|
||||
ClassWithMultipleConstructorsWithoutEmptyOne.class);
|
||||
|
||||
assertThat(discoverer.getConstructor()).isNull();
|
||||
assertThat(PreferredConstructorDiscoverer.discover(ClassWithMultipleConstructorsWithoutEmptyOne.class)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test // DATACMNS-1126
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void usesConstructorWithAnnotationOverEveryOther() {
|
||||
|
||||
PreferredConstructorDiscoverer<ClassWithMultipleConstructorsAndAnnotation, P> discoverer = new PreferredConstructorDiscoverer<>(
|
||||
ClassWithMultipleConstructorsAndAnnotation.class);
|
||||
|
||||
assertThat(discoverer.getConstructor()).satisfies(constructor -> {
|
||||
assertThat(PreferredConstructorDiscoverer.discover(ClassWithMultipleConstructorsAndAnnotation.class))
|
||||
.satisfies(constructor -> {
|
||||
|
||||
assertThat(constructor).isNotNull();
|
||||
assertThat(constructor.isNoArgConstructor()).isFalse();
|
||||
@@ -87,7 +78,7 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
|
||||
|
||||
assertThat(constructor.hasParameters()).isTrue();
|
||||
|
||||
Iterator<Parameter<Object, P>> parameters = constructor.getParameters().iterator();
|
||||
Iterator<Parameter<Object, P>> parameters = (Iterator) constructor.getParameters().iterator();
|
||||
|
||||
Parameter<?, P> parameter = parameters.next();
|
||||
assertThat(parameter.getType().getType()).isEqualTo(Long.class);
|
||||
@@ -95,26 +86,25 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-134
|
||||
@Test // DATACMNS-134, DATACMNS-1126
|
||||
public void discoversInnerClassConstructorCorrectly() {
|
||||
|
||||
PersistentEntity<Inner, P> entity = new BasicPersistentEntity<>(ClassTypeInformation.from(Inner.class));
|
||||
PreferredConstructorDiscoverer<Inner, P> discoverer = new PreferredConstructorDiscoverer<>(entity);
|
||||
|
||||
assertThat(discoverer.getConstructor()).satisfies(constructor -> {
|
||||
assertThat(PreferredConstructorDiscoverer.discover(entity)).satisfies(constructor -> {
|
||||
|
||||
Parameter<?, P> parameter = constructor.getParameters().iterator().next();
|
||||
assertThat(constructor.isEnclosingClassParameter(parameter)).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1082
|
||||
@Test // DATACMNS-1082, DATACMNS-1126
|
||||
public void skipsSyntheticConstructor() {
|
||||
|
||||
PersistentEntity<SyntheticConstructor, P> entity = new BasicPersistentEntity<>(ClassTypeInformation.from(SyntheticConstructor.class));
|
||||
PreferredConstructorDiscoverer<SyntheticConstructor, P> discoverer = new PreferredConstructorDiscoverer<>(entity);
|
||||
PersistentEntity<SyntheticConstructor, P> entity = new BasicPersistentEntity<>(
|
||||
ClassTypeInformation.from(SyntheticConstructor.class));
|
||||
|
||||
assertThat(discoverer.getConstructor()).satisfies(constructor -> {
|
||||
assertThat(PreferredConstructorDiscoverer.discover(entity)).satisfies(constructor -> {
|
||||
|
||||
PersistenceConstructor annotation = constructor.getConstructor().getAnnotation(PersistenceConstructor.class);
|
||||
assertThat(annotation).isNotNull();
|
||||
@@ -124,8 +114,7 @@ public class PreferredConstructorDiscovererUnitTests<P extends PersistentPropert
|
||||
|
||||
static class SyntheticConstructor {
|
||||
@PersistenceConstructor
|
||||
private SyntheticConstructor(String x) {
|
||||
}
|
||||
private SyntheticConstructor(String x) {}
|
||||
|
||||
class InnerSynthetic {
|
||||
// Compiler will generate a synthetic constructor since
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 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 com.nhaarman.mockito_kotlin.any
|
||||
import com.nhaarman.mockito_kotlin.doReturn
|
||||
import com.nhaarman.mockito_kotlin.whenever
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mock
|
||||
import org.mockito.junit.MockitoJUnitRunner
|
||||
import org.springframework.data.mapping.PersistentEntity
|
||||
import org.springframework.data.mapping.context.SamplePersistentProperty
|
||||
import org.springframework.data.mapping.model.ParameterValueProvider
|
||||
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer
|
||||
|
||||
/**
|
||||
* Unit tests for [ClassGeneratingKotlinEntityInstantiator] creating instances using Kotlin data classes.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class ClassGeneratingKotlinEntityInstantiatorUnitTests {
|
||||
|
||||
@Mock lateinit var entity: PersistentEntity<*, *>
|
||||
@Mock lateinit var provider: ParameterValueProvider<SamplePersistentProperty>
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should create instance`() {
|
||||
|
||||
val entity = this.entity as PersistentEntity<Contact, SamplePersistentProperty>
|
||||
val constructor = PreferredConstructorDiscoverer.discover<Contact, SamplePersistentProperty>(Contact::class.java)
|
||||
|
||||
doReturn("Walter", "White").`when`(provider).getParameterValue<SamplePersistentProperty>(any())
|
||||
doReturn(constructor).whenever(entity).persistenceConstructor
|
||||
doReturn(constructor.constructor.declaringClass).whenever(entity).type
|
||||
|
||||
val instance: Contact = ClassGeneratingKotlinEntityInstantiator().createInstance(entity, provider)
|
||||
|
||||
Assertions.assertThat(instance.firstname).isEqualTo("Walter")
|
||||
Assertions.assertThat(instance.lastname).isEqualTo("White")
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should create instance and fill in defaults`() {
|
||||
|
||||
val entity = this.entity as PersistentEntity<ContactWithDefaulting, SamplePersistentProperty>
|
||||
val constructor = PreferredConstructorDiscoverer.discover<ContactWithDefaulting, SamplePersistentProperty>(ContactWithDefaulting::class.java)
|
||||
|
||||
doReturn("Walter", null, "Skyler", null, null, null, null, null, null, null, /* 0-9 */
|
||||
null, null, null, null, null, null, null, null, null, null, /* 10-19 */
|
||||
null, null, null, null, null, null, null, null, null, null, /* 20-29 */
|
||||
null, "Walter", null, "Junior", null).`when`(provider).getParameterValue<SamplePersistentProperty>(any())
|
||||
doReturn(constructor).whenever(entity).persistenceConstructor
|
||||
doReturn(constructor.constructor.declaringClass).whenever(entity).type
|
||||
|
||||
val instance: ContactWithDefaulting = ClassGeneratingKotlinEntityInstantiator().createInstance(entity, provider)
|
||||
|
||||
Assertions.assertThat(instance.prop0).isEqualTo("Walter")
|
||||
Assertions.assertThat(instance.prop2).isEqualTo("Skyler")
|
||||
Assertions.assertThat(instance.prop31).isEqualTo("Walter")
|
||||
Assertions.assertThat(instance.prop32).isEqualTo("White")
|
||||
Assertions.assertThat(instance.prop33).isEqualTo("Junior")
|
||||
Assertions.assertThat(instance.prop34).isEqualTo("White")
|
||||
}
|
||||
|
||||
data class Contact(val firstname: String, val lastname: String)
|
||||
|
||||
data class ContactWithDefaulting(val prop0: String, val prop1: String = "White", val prop2: String,
|
||||
val prop3: String = "White", val prop4: String = "White", val prop5: String = "White",
|
||||
val prop6: String = "White", val prop7: String = "White", val prop8: String = "White",
|
||||
val prop9: String = "White", val prop10: String = "White", val prop11: String = "White",
|
||||
val prop12: String = "White", val prop13: String = "White", val prop14: String = "White",
|
||||
val prop15: String = "White", val prop16: String = "White", val prop17: String = "White",
|
||||
val prop18: String = "White", val prop19: String = "White", val prop20: String = "White",
|
||||
val prop21: String = "White", val prop22: String = "White", val prop23: String = "White",
|
||||
val prop24: String = "White", val prop25: String = "White", val prop26: String = "White",
|
||||
val prop27: String = "White", val prop28: String = "White", val prop29: String = "White",
|
||||
val prop30: String = "White", val prop31: String = "White", val prop32: String = "White",
|
||||
val prop33: String, val prop34: String = "White"
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 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 com.nhaarman.mockito_kotlin.any
|
||||
import com.nhaarman.mockito_kotlin.doReturn
|
||||
import com.nhaarman.mockito_kotlin.whenever
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mock
|
||||
import org.mockito.junit.MockitoJUnitRunner
|
||||
import org.springframework.data.mapping.PersistentEntity
|
||||
import org.springframework.data.mapping.context.SamplePersistentProperty
|
||||
import org.springframework.data.mapping.model.ParameterValueProvider
|
||||
import org.springframework.data.mapping.model.PreferredConstructorDiscoverer
|
||||
|
||||
/**
|
||||
* Unit tests for [ReflectionEntityInstantiator] creating instances using Kotlin data classes.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class ReflectionEntityInstantiatorDataClassUnitTests {
|
||||
|
||||
@Mock lateinit var entity: PersistentEntity<*, *>
|
||||
@Mock lateinit var provider: ParameterValueProvider<SamplePersistentProperty>
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should create instance`() {
|
||||
|
||||
val entity = this.entity as PersistentEntity<Contact, SamplePersistentProperty>
|
||||
val constructor = PreferredConstructorDiscoverer.discover<Contact, SamplePersistentProperty>(Contact::class.java)
|
||||
|
||||
doReturn("Walter", "White").`when`(provider).getParameterValue<SamplePersistentProperty>(any())
|
||||
doReturn(constructor).whenever(entity).persistenceConstructor
|
||||
|
||||
val instance: Contact = ReflectionEntityInstantiator.INSTANCE.createInstance(entity, provider)
|
||||
|
||||
Assertions.assertThat(instance.firstname).isEqualTo("Walter")
|
||||
Assertions.assertThat(instance.lastname).isEqualTo("White")
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should create instance and fill in defaults`() {
|
||||
|
||||
val entity = this.entity as PersistentEntity<ContactWithDefaulting, SamplePersistentProperty>
|
||||
val constructor = PreferredConstructorDiscoverer.discover<ContactWithDefaulting, SamplePersistentProperty>(ContactWithDefaulting::class.java)
|
||||
|
||||
doReturn("Walter", null).`when`(provider).getParameterValue<SamplePersistentProperty>(any())
|
||||
doReturn(constructor).whenever(entity).persistenceConstructor
|
||||
|
||||
val instance: ContactWithDefaulting = ReflectionEntityInstantiator.INSTANCE.createInstance(entity, provider)
|
||||
|
||||
Assertions.assertThat(instance.firstname).isEqualTo("Walter")
|
||||
Assertions.assertThat(instance.lastname).isEqualTo("White")
|
||||
}
|
||||
|
||||
data class Contact(val firstname: String, val lastname: String)
|
||||
|
||||
data class ContactWithDefaulting(val firstname: String, val lastname: String = "White")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 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.mapping.model
|
||||
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.junit.Test
|
||||
import org.springframework.data.annotation.PersistenceConstructor
|
||||
import org.springframework.data.mapping.model.AbstractPersistentPropertyUnitTests.*
|
||||
|
||||
/**
|
||||
* Unit tests for [PreferredConstructorDiscoverer].
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class PreferredConstructorDiscovererUnitTests {
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should discover simple constructor`() {
|
||||
|
||||
val constructor = PreferredConstructorDiscoverer.discover<Simple, SamplePersistentProperty>(Simple::class.java)
|
||||
|
||||
Assertions.assertThat(constructor.parameters.size).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should reject two constructors`() {
|
||||
|
||||
val constructor = PreferredConstructorDiscoverer.discover<TwoConstructors, SamplePersistentProperty>(TwoConstructors::class.java)
|
||||
|
||||
Assertions.assertThat(constructor.parameters.size).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should discover annotated constructor`() {
|
||||
|
||||
val constructor = PreferredConstructorDiscoverer.discover<AnnotatedConstructors, SamplePersistentProperty>(AnnotatedConstructors::class.java)
|
||||
|
||||
Assertions.assertThat(constructor.parameters.size).isEqualTo(2)
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should discover default constructor`() {
|
||||
|
||||
val constructor = PreferredConstructorDiscoverer.discover<DefaultConstructor, SamplePersistentProperty>(DefaultConstructor::class.java)
|
||||
|
||||
Assertions.assertThat(constructor.parameters.size).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1126
|
||||
fun `should discover default annotated constructor`() {
|
||||
|
||||
val constructor = PreferredConstructorDiscoverer.discover<TwoDefaultConstructorsAnnotated, SamplePersistentProperty>(TwoDefaultConstructorsAnnotated::class.java)
|
||||
|
||||
Assertions.assertThat(constructor.parameters.size).isEqualTo(3)
|
||||
}
|
||||
|
||||
data class Simple(val firstname: String)
|
||||
|
||||
class TwoConstructors(val firstname: String) {
|
||||
constructor(firstname: String, lastname: String) : this(firstname)
|
||||
}
|
||||
|
||||
class AnnotatedConstructors(val firstname: String) {
|
||||
|
||||
@PersistenceConstructor
|
||||
constructor(firstname: String, lastname: String) : this(firstname)
|
||||
}
|
||||
|
||||
class DefaultConstructor(val firstname: String = "foo") {
|
||||
}
|
||||
|
||||
class TwoDefaultConstructorsAnnotated(val firstname: String = "foo", val lastname: String = "bar") {
|
||||
|
||||
@PersistenceConstructor
|
||||
constructor(firstname: String = "foo", lastname: String = "bar", age: Int) : this(firstname, lastname)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user