Search in sources :

Example 6 with BasicPersistentEntity

use of org.springframework.data.mapping.model.BasicPersistentEntity in project spring-data-commons by spring-projects.

the class ReflectionEntityInstantiatorUnitTests method createsInnerClassInstanceCorrectly.

// DATACMNS-134
@Test
public void createsInnerClassInstanceCorrectly() {
    BasicPersistentEntity<Inner, P> entity = new BasicPersistentEntity<>(from(Inner.class));
    assertThat(entity.getPersistenceConstructor()).satisfies(it -> {
        Parameter<Object, P> parameter = it.getParameters().iterator().next();
        Object outer = new Outer();
        when(provider.getParameterValue(parameter)).thenReturn(outer);
        Inner instance = INSTANCE.createInstance(entity, provider);
        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)).isEqualTo(outer);
            }
        });
    });
}
Also used : BasicPersistentEntity(org.springframework.data.mapping.model.BasicPersistentEntity) Inner(org.springframework.data.convert.ReflectionEntityInstantiatorUnitTests.Outer.Inner) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 BasicPersistentEntity (org.springframework.data.mapping.model.BasicPersistentEntity)6 Constructor (java.lang.reflect.Constructor)2 PreferredConstructor (org.springframework.data.mapping.PreferredConstructor)2 Parameter (org.springframework.data.mapping.PreferredConstructor.Parameter)2 MappingInstantiationException (org.springframework.data.mapping.model.MappingInstantiationException)2 PersistenceConstructor (org.springframework.data.annotation.PersistenceConstructor)1 Inner (org.springframework.data.convert.ClassGeneratingEntityInstantiatorUnitTests.Outer.Inner)1 Inner (org.springframework.data.convert.ReflectionEntityInstantiatorUnitTests.Outer.Inner)1 MappingException (org.springframework.data.mapping.MappingException)1 ClassTypeInformation (org.springframework.data.util.ClassTypeInformation)1 TypeInformation (org.springframework.data.util.TypeInformation)1