Remove deprecated EntityInstantiator in convert package in favor of instantiators in mapping.model.
See #2466
This commit is contained in:
committed by
Jens Schauder
parent
582319a5fe
commit
aecdf5001a
@@ -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 <T, E extends PersistentEntity<? extends T, P>, P extends PersistentProperty<P>> T createInstance(E entity,
|
||||
ParameterValueProvider<P> provider) {
|
||||
return delegate.createInstance(entity, provider);
|
||||
}
|
||||
}
|
||||
@@ -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 {}
|
||||
@@ -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 <T, E extends PersistentEntity<? extends T, P>, P extends PersistentProperty<P>> T createInstance(E entity,
|
||||
ParameterValueProvider<P> provider) {
|
||||
return delegate.createInstance(entity, provider);
|
||||
}
|
||||
}
|
||||
@@ -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<Class<?>, 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<Class<?>, 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<Class<?>, org.springframework.data.mapping.model.EntityInstantiator> adaptFromLegacy(
|
||||
Map<Class<?>, EntityInstantiator> instantiators) {
|
||||
|
||||
return instantiators == null //
|
||||
? null //
|
||||
: instantiators.entrySet().stream() //
|
||||
.collect(Collectors.toMap(Entry::getKey, e -> new EntityInstantiatorAdapter(e.getValue())));
|
||||
}
|
||||
}
|
||||
@@ -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 <T, E extends PersistentEntity<? extends T, P>, P extends PersistentProperty<P>> T createInstance(E entity,
|
||||
ParameterValueProvider<P> provider) {
|
||||
return delegate.createInstance(entity, provider);
|
||||
}
|
||||
}
|
||||
@@ -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 <T, E extends PersistentEntity<? extends T, P>, P extends PersistentProperty<P>> T createInstance(E entity,
|
||||
ParameterValueProvider<P> provider) {
|
||||
|
||||
return InternalEntityInstantiatorFactory.getReflectionEntityInstantiator().createInstance(entity, provider);
|
||||
}
|
||||
}
|
||||
@@ -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 <T, E extends PersistentEntity<? extends T, P>, P extends PersistentProperty<P>> 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")
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
@@ -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<PersistentEntity<Contact, SamplePersistentProperty>>()
|
||||
val constructor = PreferredConstructorDiscoverer.discover<Contact, SamplePersistentProperty>(Contact::class.java)
|
||||
val constructor =
|
||||
PreferredConstructorDiscoverer.discover<Contact, SamplePersistentProperty>(
|
||||
Contact::class.java
|
||||
)
|
||||
|
||||
every { provider.getParameterValue<String>(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<PersistentEntity<ContactWithDefaulting, SamplePersistentProperty>>()
|
||||
val constructor = PreferredConstructorDiscoverer.discover<ContactWithDefaulting, SamplePersistentProperty>(ContactWithDefaulting::class.java)
|
||||
val entity =
|
||||
mockk<PersistentEntity<ContactWithDefaulting, SamplePersistentProperty>>()
|
||||
val constructor =
|
||||
PreferredConstructorDiscoverer.discover<ContactWithDefaulting, SamplePersistentProperty>(
|
||||
ContactWithDefaulting::class.java
|
||||
)
|
||||
|
||||
every { provider.getParameterValue<String>(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<PersistentEntity<WithBoolean, SamplePersistentProperty>>()
|
||||
val constructor = PreferredConstructorDiscoverer.discover<WithBoolean, SamplePersistentProperty>(WithBoolean::class.java)
|
||||
val constructor =
|
||||
PreferredConstructorDiscoverer.discover<WithBoolean, SamplePersistentProperty>(
|
||||
WithBoolean::class.java
|
||||
)
|
||||
|
||||
every { provider.getParameterValue<Boolean>(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 <init>(kotlin.Boolean)") //
|
||||
.hasCauseInstanceOf(IllegalArgumentException::class.java)
|
||||
assertThatThrownBy {
|
||||
KotlinClassGeneratingEntityInstantiator().createInstance(
|
||||
entity,
|
||||
provider
|
||||
)
|
||||
} //
|
||||
.isInstanceOf(MappingInstantiationException::class.java) //
|
||||
.hasMessageContaining("fun <init>(kotlin.Boolean)") //
|
||||
.hasCauseInstanceOf(IllegalArgumentException::class.java)
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1200
|
||||
fun `should apply primitive defaulting for absent parameters`() {
|
||||
|
||||
val entity = mockk<PersistentEntity<WithPrimitiveDefaulting, SamplePersistentProperty>>()
|
||||
val constructor = PreferredConstructorDiscoverer.discover<WithPrimitiveDefaulting, SamplePersistentProperty>(WithPrimitiveDefaulting::class.java)
|
||||
val entity =
|
||||
mockk<PersistentEntity<WithPrimitiveDefaulting, SamplePersistentProperty>>()
|
||||
val constructor =
|
||||
PreferredConstructorDiscoverer.discover<WithPrimitiveDefaulting, SamplePersistentProperty>(
|
||||
WithPrimitiveDefaulting::class.java
|
||||
)
|
||||
|
||||
every { provider.getParameterValue<Byte>(any()) } returns null
|
||||
every { provider.getParameterValue<Short>(any()) } returns null
|
||||
@@ -132,14 +150,18 @@ class KotlinClassGeneratingEntityInstantiatorUnitTests {
|
||||
fun `should create instance using @PersistenceConstructor`() {
|
||||
|
||||
val entity = mockk<PersistentEntity<CustomUser, SamplePersistentProperty>>()
|
||||
val constructor = PreferredConstructorDiscoverer.discover<CustomUser, SamplePersistentProperty>(CustomUser::class.java)
|
||||
val constructor =
|
||||
PreferredConstructorDiscoverer.discover<CustomUser, SamplePersistentProperty>(
|
||||
CustomUser::class.java
|
||||
)
|
||||
|
||||
every { provider.getParameterValue<String>(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")
|
||||
}
|
||||
@@ -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<PersistentEntity<Contact, SamplePersistentProperty>>()
|
||||
val constructor = PreferredConstructorDiscoverer.discover<Contact, SamplePersistentProperty>(Contact::class.java)
|
||||
val constructor =
|
||||
PreferredConstructorDiscoverer.discover<Contact, SamplePersistentProperty>(
|
||||
Contact::class.java
|
||||
)
|
||||
|
||||
every { provider.getParameterValue<String>(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<PersistentEntity<ContactWithDefaulting, SamplePersistentProperty>>()
|
||||
val constructor = PreferredConstructorDiscoverer.discover<ContactWithDefaulting, SamplePersistentProperty>(ContactWithDefaulting::class.java)
|
||||
val entity =
|
||||
mockk<PersistentEntity<ContactWithDefaulting, SamplePersistentProperty>>()
|
||||
val constructor =
|
||||
PreferredConstructorDiscoverer.discover<ContactWithDefaulting, SamplePersistentProperty>(
|
||||
ContactWithDefaulting::class.java
|
||||
)
|
||||
|
||||
every { provider.getParameterValue<String>(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")
|
||||
Reference in New Issue
Block a user