From aecdf5001a66a06f02eeec607af9ae8906052e1e Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 20 Sep 2021 13:44:15 +0200 Subject: [PATCH] Remove deprecated EntityInstantiator in convert package in favor of instantiators in mapping.model. See #2466 --- .../ClassGeneratingEntityInstantiator.java | 61 ------------ .../data/convert/EntityInstantiator.java | 27 ------ .../convert/EntityInstantiatorAdapter.java | 44 --------- .../data/convert/EntityInstantiators.java | 94 ------------------- ...tlinClassGeneratingEntityInstantiator.java | 48 ---------- .../convert/ReflectionEntityInstantiator.java | 41 -------- .../ClassGeneratingEntityInstantiator.java | 4 +- .../InternalEntityInstantiatorFactory.java | 40 -------- ...tlinClassGeneratingEntityInstantiator.java | 4 +- ...ssGeneratingEntityInstantiatorUnitTests.kt | 66 ++++++++----- ...ionEntityInstantiatorDataClassUnitTests.kt | 25 +++-- 11 files changed, 64 insertions(+), 390 deletions(-) delete mode 100644 src/main/java/org/springframework/data/convert/ClassGeneratingEntityInstantiator.java delete mode 100644 src/main/java/org/springframework/data/convert/EntityInstantiator.java delete mode 100644 src/main/java/org/springframework/data/convert/EntityInstantiatorAdapter.java delete mode 100644 src/main/java/org/springframework/data/convert/EntityInstantiators.java delete mode 100644 src/main/java/org/springframework/data/convert/KotlinClassGeneratingEntityInstantiator.java delete mode 100644 src/main/java/org/springframework/data/convert/ReflectionEntityInstantiator.java delete mode 100644 src/main/java/org/springframework/data/mapping/model/InternalEntityInstantiatorFactory.java rename src/test/kotlin/org/springframework/data/{convert => mapping/model}/KotlinClassGeneratingEntityInstantiatorUnitTests.kt (77%) rename src/test/kotlin/org/springframework/data/{convert => mapping/model}/ReflectionEntityInstantiatorDataClassUnitTests.kt (71%) diff --git a/src/main/java/org/springframework/data/convert/ClassGeneratingEntityInstantiator.java b/src/main/java/org/springframework/data/convert/ClassGeneratingEntityInstantiator.java deleted file mode 100644 index c1bb26736..000000000 --- a/src/main/java/org/springframework/data/convert/ClassGeneratingEntityInstantiator.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2015-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.convert; - -import org.springframework.data.mapping.PersistentEntity; -import org.springframework.data.mapping.PersistentProperty; -import org.springframework.data.mapping.PreferredConstructor; -import org.springframework.data.mapping.model.InternalEntityInstantiatorFactory; -import org.springframework.data.mapping.model.ParameterValueProvider; - -/** - * An {@link EntityInstantiator} that can generate byte code to speed-up dynamic object instantiation. Uses the - * {@link PersistentEntity}'s {@link PreferredConstructor} to instantiate an instance of the entity by dynamically - * generating factory methods with appropriate constructor invocations via ASM. If we cannot generate byte code for a - * type, we gracefully fall-back to the {@link ReflectionEntityInstantiator}. - * - * @author Thomas Darimont - * @author Oliver Gierke - * @author Phillip Webb - * @author Christoph Strobl - * @author Mark Paluch - * @since 1.11 - * @deprecated since 2.3, use {@code org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator} through - * {@link org.springframework.data.mapping.model.EntityInstantiators}. - */ -@Deprecated -public class ClassGeneratingEntityInstantiator implements EntityInstantiator { - - private final org.springframework.data.mapping.model.EntityInstantiator delegate = InternalEntityInstantiatorFactory - .getClassGeneratingEntityInstantiator(); - - /** - * Creates a new {@link ClassGeneratingEntityInstantiator}. - */ - public ClassGeneratingEntityInstantiator() { - super(); - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mapping.model.EntityInstantiator#createInstance(org.springframework.data.mapping.PersistentEntity, org.springframework.data.mapping.model.ParameterValueProvider) - */ - @Override - public , P extends PersistentProperty

> T createInstance(E entity, - ParameterValueProvider

provider) { - return delegate.createInstance(entity, provider); - } -} diff --git a/src/main/java/org/springframework/data/convert/EntityInstantiator.java b/src/main/java/org/springframework/data/convert/EntityInstantiator.java deleted file mode 100644 index 74ccec286..000000000 --- a/src/main/java/org/springframework/data/convert/EntityInstantiator.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2012-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.convert; - -import org.springframework.data.mapping.PersistentEntity; - -/** - * SPI to abstract strategies to create instances for {@link PersistentEntity}s. - * - * @author Oliver Gierke - * @since 2.3, use {@link org.springframework.data.mapping.model.EntityInstantiator} instead. - */ -@Deprecated -public interface EntityInstantiator extends org.springframework.data.mapping.model.EntityInstantiator {} diff --git a/src/main/java/org/springframework/data/convert/EntityInstantiatorAdapter.java b/src/main/java/org/springframework/data/convert/EntityInstantiatorAdapter.java deleted file mode 100644 index 84a34b4cf..000000000 --- a/src/main/java/org/springframework/data/convert/EntityInstantiatorAdapter.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2020-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.convert; - -import org.springframework.data.mapping.PersistentEntity; -import org.springframework.data.mapping.PersistentProperty; -import org.springframework.data.mapping.model.ParameterValueProvider; - -/** - * Adapter to bridge calls from the new APIs back to the legacy ones. - * - * @author Oliver Drotbohm - */ -class EntityInstantiatorAdapter implements EntityInstantiator { - - private final org.springframework.data.mapping.model.EntityInstantiator delegate; - - EntityInstantiatorAdapter(org.springframework.data.mapping.model.EntityInstantiator delegate) { - this.delegate = delegate; - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mapping.model.EntityInstantiator#createInstance(org.springframework.data.mapping.PersistentEntity, org.springframework.data.mapping.model.ParameterValueProvider) - */ - @Override - public , P extends PersistentProperty

> T createInstance(E entity, - ParameterValueProvider

provider) { - return delegate.createInstance(entity, provider); - } -} diff --git a/src/main/java/org/springframework/data/convert/EntityInstantiators.java b/src/main/java/org/springframework/data/convert/EntityInstantiators.java deleted file mode 100644 index f328a2553..000000000 --- a/src/main/java/org/springframework/data/convert/EntityInstantiators.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2012-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.convert; - -import java.util.Collections; -import java.util.Map; -import java.util.Map.Entry; -import java.util.stream.Collectors; - -import org.springframework.data.mapping.PersistentEntity; -import org.springframework.data.mapping.model.InternalEntityInstantiatorFactory; - -/** - * Simple value object allowing access to {@link EntityInstantiator} instances for a given type falling back to a - * default one. - * - * @author Oliver Gierke - * @author Thomas Darimont - * @author Christoph Strobl - * @author Mark Paluch - * @deprecated since 2.3, use {@link org.springframework.data.mapping.model.EntityInstantiators} instead. - */ -@Deprecated -public class EntityInstantiators extends org.springframework.data.mapping.model.EntityInstantiators { - - /** - * Creates a new {@link EntityInstantiators} using the default fallback instantiator and no custom ones. - */ - public EntityInstantiators() { - super(); - } - - /** - * Creates a new {@link EntityInstantiators} using the given {@link EntityInstantiator} as fallback. - * - * @param fallback must not be {@literal null}. - */ - public EntityInstantiators(EntityInstantiator fallback) { - super(fallback, Collections.emptyMap()); - } - - /** - * Creates a new {@link EntityInstantiators} using the default fallback instantiator and the given custom ones. - * - * @param customInstantiators must not be {@literal null}. - */ - public EntityInstantiators(Map, EntityInstantiator> customInstantiators) { - super(InternalEntityInstantiatorFactory.getKotlinClassGeneratingEntityInstantiator(), - adaptFromLegacy(customInstantiators)); - } - - /** - * Creates a new {@link EntityInstantiator} using the given fallback {@link EntityInstantiator} and the given custom - * ones. - * - * @param defaultInstantiator must not be {@literal null}. - * @param customInstantiators must not be {@literal null}. - */ - public EntityInstantiators(EntityInstantiator defaultInstantiator, - Map, EntityInstantiator> customInstantiators) { - super(defaultInstantiator, adaptFromLegacy(customInstantiators)); - } - - /* - * (non-Javadoc) - * @see org.springframework.data.mapping.model.EntityInstantiators#getInstantiatorFor(org.springframework.data.mapping.PersistentEntity) - */ - @Override - public EntityInstantiator getInstantiatorFor(PersistentEntity entity) { - return new EntityInstantiatorAdapter(super.getInstantiatorFor(entity)); - } - - private static Map, org.springframework.data.mapping.model.EntityInstantiator> adaptFromLegacy( - Map, EntityInstantiator> instantiators) { - - return instantiators == null // - ? null // - : instantiators.entrySet().stream() // - .collect(Collectors.toMap(Entry::getKey, e -> new EntityInstantiatorAdapter(e.getValue()))); - } -} diff --git a/src/main/java/org/springframework/data/convert/KotlinClassGeneratingEntityInstantiator.java b/src/main/java/org/springframework/data/convert/KotlinClassGeneratingEntityInstantiator.java deleted file mode 100644 index dbc98c04f..000000000 --- a/src/main/java/org/springframework/data/convert/KotlinClassGeneratingEntityInstantiator.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2017-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.convert; - -import org.springframework.data.mapping.PersistentEntity; -import org.springframework.data.mapping.PersistentProperty; -import org.springframework.data.mapping.model.InternalEntityInstantiatorFactory; -import org.springframework.data.mapping.model.ParameterValueProvider; - -/** - * Kotlin-specific extension to {@link ClassGeneratingEntityInstantiator} that adapts Kotlin constructors with - * defaulting. - * - * @author Mark Paluch - * @author Oliver Gierke - * @since 2.0 - * @deprecated since 2.3, use {@link org.springframework.data.mapping.model.KotlinClassGeneratingEntityInstantiator} - * instead. - */ -@Deprecated -public class KotlinClassGeneratingEntityInstantiator implements EntityInstantiator { - - private final org.springframework.data.mapping.model.EntityInstantiator delegate = InternalEntityInstantiatorFactory - .getKotlinClassGeneratingEntityInstantiator(); - - /* - * (non-Javadoc) - * @see org.springframework.data.mapping.model.EntityInstantiator#createInstance(org.springframework.data.mapping.PersistentEntity, org.springframework.data.mapping.model.ParameterValueProvider) - */ - @Override - public , P extends PersistentProperty

> T createInstance(E entity, - ParameterValueProvider

provider) { - return delegate.createInstance(entity, provider); - } -} diff --git a/src/main/java/org/springframework/data/convert/ReflectionEntityInstantiator.java b/src/main/java/org/springframework/data/convert/ReflectionEntityInstantiator.java deleted file mode 100644 index 7f1387429..000000000 --- a/src/main/java/org/springframework/data/convert/ReflectionEntityInstantiator.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2012-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.convert; - -import org.springframework.data.mapping.PersistentEntity; -import org.springframework.data.mapping.PersistentProperty; -import org.springframework.data.mapping.PreferredConstructor; -import org.springframework.data.mapping.model.InternalEntityInstantiatorFactory; -import org.springframework.data.mapping.model.ParameterValueProvider; - -/** - * {@link EntityInstantiator} that uses the {@link PersistentEntity}'s {@link PreferredConstructor} to instantiate an - * instance of the entity via reflection. - * - * @author Oliver Gierke - * @author Mark Paluch - * @deprecated since 2.3, use {@link org.springframework.data.mapping.model.ReflectionEntityInstantiator} instead. - */ -public enum ReflectionEntityInstantiator implements EntityInstantiator { - - INSTANCE; - - public , P extends PersistentProperty

> T createInstance(E entity, - ParameterValueProvider

provider) { - - return InternalEntityInstantiatorFactory.getReflectionEntityInstantiator().createInstance(entity, provider); - } -} diff --git a/src/main/java/org/springframework/data/mapping/model/ClassGeneratingEntityInstantiator.java b/src/main/java/org/springframework/data/mapping/model/ClassGeneratingEntityInstantiator.java index 11b04c496..d74391137 100644 --- a/src/main/java/org/springframework/data/mapping/model/ClassGeneratingEntityInstantiator.java +++ b/src/main/java/org/springframework/data/mapping/model/ClassGeneratingEntityInstantiator.java @@ -74,7 +74,7 @@ class ClassGeneratingEntityInstantiator implements EntityInstantiator { /* * (non-Javadoc) - * @see org.springframework.data.convert.EntityInstantiator#createInstance(org.springframework.data.mapping.PersistentEntity, org.springframework.data.mapping.model.ParameterValueProvider) + * @see org.springframework.data.mapping.model.EntityInstantiator#createInstance(org.springframework.data.mapping.PersistentEntity, org.springframework.data.mapping.model.ParameterValueProvider) */ @Override public , P extends PersistentProperty

> T createInstance(E entity, @@ -238,7 +238,7 @@ class ClassGeneratingEntityInstantiator implements EntityInstantiator { /* * (non-Javadoc) - * @see org.springframework.data.convert.EntityInstantiator#createInstance(org.springframework.data.mapping.PersistentEntity, org.springframework.data.mapping.model.ParameterValueProvider) + * @see org.springframework.data.mapping.model.EntityInstantiator#createInstance(org.springframework.data.mapping.PersistentEntity, org.springframework.data.mapping.model.ParameterValueProvider) */ @Override @SuppressWarnings("unchecked") diff --git a/src/main/java/org/springframework/data/mapping/model/InternalEntityInstantiatorFactory.java b/src/main/java/org/springframework/data/mapping/model/InternalEntityInstantiatorFactory.java deleted file mode 100644 index da11bdcdf..000000000 --- a/src/main/java/org/springframework/data/mapping/model/InternalEntityInstantiatorFactory.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2020-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.mapping.model; - -/** - * Factory to expose the new package-protected {@link EntityInstantiator} implementations for the legacy types. To be - * removed in 2.4. - * - * @author Oliver Drotbohm - * @since 2.3 - * @deprecated since 2.3 as it's only a bridge from the legacy types towards the new, package-protected implementation. - */ -@Deprecated -public class InternalEntityInstantiatorFactory { - - public static EntityInstantiator getClassGeneratingEntityInstantiator() { - return new ClassGeneratingEntityInstantiator(); - } - - public static EntityInstantiator getKotlinClassGeneratingEntityInstantiator() { - return new KotlinClassGeneratingEntityInstantiator(); - } - - public static EntityInstantiator getReflectionEntityInstantiator() { - return ReflectionEntityInstantiator.INSTANCE; - } -} diff --git a/src/main/java/org/springframework/data/mapping/model/KotlinClassGeneratingEntityInstantiator.java b/src/main/java/org/springframework/data/mapping/model/KotlinClassGeneratingEntityInstantiator.java index a89e71143..3c1b4f89a 100644 --- a/src/main/java/org/springframework/data/mapping/model/KotlinClassGeneratingEntityInstantiator.java +++ b/src/main/java/org/springframework/data/mapping/model/KotlinClassGeneratingEntityInstantiator.java @@ -44,7 +44,7 @@ class KotlinClassGeneratingEntityInstantiator extends ClassGeneratingEntityInsta /* * (non-Javadoc) - * @see org.springframework.data.convert.ClassGeneratingEntityInstantiator#doCreateEntityInstantiator(org.springframework.data.mapping.PersistentEntity) + * @see org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator#doCreateEntityInstantiator(org.springframework.data.mapping.PersistentEntity) */ @Override protected EntityInstantiator doCreateEntityInstantiator(PersistentEntity entity) { @@ -192,7 +192,7 @@ class KotlinClassGeneratingEntityInstantiator extends ClassGeneratingEntityInsta /* * (non-Javadoc) - * @see org.springframework.data.convert.EntityInstantiator#createInstance(org.springframework.data.mapping.PersistentEntity, org.springframework.data.mapping.model.ParameterValueProvider) + * @see org.springframework.data.mapping.model.EntityInstantiator#createInstance(org.springframework.data.mapping.PersistentEntity, org.springframework.data.mapping.model.ParameterValueProvider) */ @Override @SuppressWarnings("unchecked") diff --git a/src/test/kotlin/org/springframework/data/convert/KotlinClassGeneratingEntityInstantiatorUnitTests.kt b/src/test/kotlin/org/springframework/data/mapping/model/KotlinClassGeneratingEntityInstantiatorUnitTests.kt similarity index 77% rename from src/test/kotlin/org/springframework/data/convert/KotlinClassGeneratingEntityInstantiatorUnitTests.kt rename to src/test/kotlin/org/springframework/data/mapping/model/KotlinClassGeneratingEntityInstantiatorUnitTests.kt index fc997307e..c4e3000ac 100644 --- a/src/test/kotlin/org/springframework/data/convert/KotlinClassGeneratingEntityInstantiatorUnitTests.kt +++ b/src/test/kotlin/org/springframework/data/mapping/model/KotlinClassGeneratingEntityInstantiatorUnitTests.kt @@ -1,5 +1,5 @@ /* - * Copyright 2017-2021 the original author or authors. + * Copyright 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. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.data.convert +package org.springframework.data.mapping.model import io.mockk.every import io.mockk.mockk @@ -23,9 +23,6 @@ import org.junit.jupiter.api.Test import org.springframework.data.annotation.PersistenceConstructor import org.springframework.data.mapping.PersistentEntity import org.springframework.data.mapping.context.SamplePersistentProperty -import org.springframework.data.mapping.model.MappingInstantiationException -import org.springframework.data.mapping.model.ParameterValueProvider -import org.springframework.data.mapping.model.PreferredConstructorDiscoverer /** * Unit tests for [KotlinClassGeneratingEntityInstantiator] creating instances using Kotlin data classes. @@ -42,14 +39,18 @@ class KotlinClassGeneratingEntityInstantiatorUnitTests { fun `should create instance`() { val entity = mockk>() - val constructor = PreferredConstructorDiscoverer.discover(Contact::class.java) + val constructor = + PreferredConstructorDiscoverer.discover( + Contact::class.java + ) every { provider.getParameterValue(any()) }.returnsMany("Walter", "White") every { entity.persistenceConstructor } returns constructor every { entity.type } returns constructor.constructor.declaringClass every { entity.typeInformation } returns mockk() - val instance: Contact = KotlinClassGeneratingEntityInstantiator().createInstance(entity, provider) + val instance: Contact = + KotlinClassGeneratingEntityInstantiator().createInstance(entity, provider) assertThat(instance.firstname).isEqualTo("Walter") assertThat(instance.lastname).isEqualTo("White") @@ -58,14 +59,19 @@ class KotlinClassGeneratingEntityInstantiatorUnitTests { @Test // DATACMNS-1126 fun `should create instance and fill in defaults`() { - val entity = mockk>() - val constructor = PreferredConstructorDiscoverer.discover(ContactWithDefaulting::class.java) + val entity = + mockk>() + val constructor = + PreferredConstructorDiscoverer.discover( + ContactWithDefaulting::class.java + ) every { provider.getParameterValue(any()) }.returnsMany( - "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) + "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 + ) every { entity.persistenceConstructor } returns constructor every { entity.type } returns constructor.constructor.declaringClass @@ -85,24 +91,36 @@ class KotlinClassGeneratingEntityInstantiatorUnitTests { fun `absent primitive value should cause MappingInstantiationException`() { val entity = mockk>() - val constructor = PreferredConstructorDiscoverer.discover(WithBoolean::class.java) + val constructor = + PreferredConstructorDiscoverer.discover( + WithBoolean::class.java + ) every { provider.getParameterValue(any()) } returns null every { entity.persistenceConstructor } returns constructor every { entity.type } returns constructor.constructor.declaringClass every { entity.typeInformation } returns mockk() - assertThatThrownBy { KotlinClassGeneratingEntityInstantiator().createInstance(entity, provider) } // - .isInstanceOf(MappingInstantiationException::class.java) // - .hasMessageContaining("fun (kotlin.Boolean)") // - .hasCauseInstanceOf(IllegalArgumentException::class.java) + assertThatThrownBy { + KotlinClassGeneratingEntityInstantiator().createInstance( + entity, + provider + ) + } // + .isInstanceOf(MappingInstantiationException::class.java) // + .hasMessageContaining("fun (kotlin.Boolean)") // + .hasCauseInstanceOf(IllegalArgumentException::class.java) } @Test // DATACMNS-1200 fun `should apply primitive defaulting for absent parameters`() { - val entity = mockk>() - val constructor = PreferredConstructorDiscoverer.discover(WithPrimitiveDefaulting::class.java) + val entity = + mockk>() + val constructor = + PreferredConstructorDiscoverer.discover( + WithPrimitiveDefaulting::class.java + ) every { provider.getParameterValue(any()) } returns null every { provider.getParameterValue(any()) } returns null @@ -132,14 +150,18 @@ class KotlinClassGeneratingEntityInstantiatorUnitTests { fun `should create instance using @PersistenceConstructor`() { val entity = mockk>() - val constructor = PreferredConstructorDiscoverer.discover(CustomUser::class.java) + val constructor = + PreferredConstructorDiscoverer.discover( + CustomUser::class.java + ) every { provider.getParameterValue(any()) } returns "Walter" every { entity.persistenceConstructor } returns constructor every { entity.type } returns constructor.constructor.declaringClass every { entity.typeInformation } returns mockk() - val instance: CustomUser = KotlinClassGeneratingEntityInstantiator().createInstance(entity, provider) + val instance: CustomUser = + KotlinClassGeneratingEntityInstantiator().createInstance(entity, provider) assertThat(instance.id).isEqualTo("Walter") } diff --git a/src/test/kotlin/org/springframework/data/convert/ReflectionEntityInstantiatorDataClassUnitTests.kt b/src/test/kotlin/org/springframework/data/mapping/model/ReflectionEntityInstantiatorDataClassUnitTests.kt similarity index 71% rename from src/test/kotlin/org/springframework/data/convert/ReflectionEntityInstantiatorDataClassUnitTests.kt rename to src/test/kotlin/org/springframework/data/mapping/model/ReflectionEntityInstantiatorDataClassUnitTests.kt index 824c32ae0..b33897852 100644 --- a/src/test/kotlin/org/springframework/data/convert/ReflectionEntityInstantiatorDataClassUnitTests.kt +++ b/src/test/kotlin/org/springframework/data/mapping/model/ReflectionEntityInstantiatorDataClassUnitTests.kt @@ -1,5 +1,5 @@ /* - * Copyright 2017-2021 the original author or authors. + * Copyright 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. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.data.convert +package org.springframework.data.mapping.model import io.mockk.every import io.mockk.mockk @@ -21,8 +21,6 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test 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. @@ -39,12 +37,16 @@ class ReflectionEntityInstantiatorDataClassUnitTests { fun `should create instance`() { val entity = mockk>() - val constructor = PreferredConstructorDiscoverer.discover(Contact::class.java) + val constructor = + PreferredConstructorDiscoverer.discover( + Contact::class.java + ) every { provider.getParameterValue(any()) }.returnsMany("Walter", "White") every { entity.persistenceConstructor } returns constructor - val instance: Contact = ReflectionEntityInstantiator.INSTANCE.createInstance(entity, provider) + val instance: Contact = + ReflectionEntityInstantiator.INSTANCE.createInstance(entity, provider) assertThat(instance.firstname).isEqualTo("Walter") assertThat(instance.lastname).isEqualTo("White") @@ -53,13 +55,18 @@ class ReflectionEntityInstantiatorDataClassUnitTests { @Test // DATACMNS-1126 fun `should create instance and fill in defaults`() { - val entity = mockk>() - val constructor = PreferredConstructorDiscoverer.discover(ContactWithDefaulting::class.java) + val entity = + mockk>() + val constructor = + PreferredConstructorDiscoverer.discover( + ContactWithDefaulting::class.java + ) every { provider.getParameterValue(any()) }.returnsMany("Walter", null) every { entity.persistenceConstructor } returns constructor - val instance: ContactWithDefaulting = ReflectionEntityInstantiator.INSTANCE.createInstance(entity, provider) + val instance: ContactWithDefaulting = + ReflectionEntityInstantiator.INSTANCE.createInstance(entity, provider) assertThat(instance.firstname).isEqualTo("Walter") assertThat(instance.lastname).isEqualTo("White")