Search in sources :

Example 1 with BasicPersistentEntity

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

the class AbstractMappingContextUnitTests method doesNotAddInvalidEntity.

// DATACMNS-92
@Test(expected = MappingException.class)
public void doesNotAddInvalidEntity() {
    context = new SampleMappingContext() {

        @Override
        @SuppressWarnings("unchecked")
        protected <S> BasicPersistentEntity<Object, SamplePersistentProperty> createPersistentEntity(TypeInformation<S> typeInformation) {
            return new BasicPersistentEntity<Object, SamplePersistentProperty>((TypeInformation<Object>) typeInformation) {

                @Override
                public void verify() {
                    if (Unsupported.class.isAssignableFrom(getType())) {
                        throw new MappingException("Unsupported type!");
                    }
                }
            };
        }
    };
    try {
        context.getPersistentEntity(Unsupported.class);
    } catch (MappingException e) {
    // expected
    }
    context.getPersistentEntity(Unsupported.class);
}
Also used : BasicPersistentEntity(org.springframework.data.mapping.model.BasicPersistentEntity) TypeInformation(org.springframework.data.util.TypeInformation) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) MappingException(org.springframework.data.mapping.MappingException) Test(org.junit.Test)

Example 2 with BasicPersistentEntity

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

the class ClassGeneratingEntityInstantiatorUnitTests method capturesContextOnInstantiationException.

// DATACMNS-283, DATACMNS-578
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void capturesContextOnInstantiationException() throws Exception {
    PersistentEntity<Sample, P> entity = new BasicPersistentEntity<>(from(Sample.class));
    doReturn("FOO").when(provider).getParameterValue(any(Parameter.class));
    Constructor constructor = Sample.class.getConstructor(Long.class, String.class);
    List<Object> parameters = Arrays.asList("FOO", "FOO");
    try {
        this.instance.createInstance(entity, provider);
        fail("Expected MappingInstantiationException!");
    } catch (MappingInstantiationException o_O) {
        assertThat(o_O.getConstructor()).hasValue(constructor);
        assertThat(o_O.getConstructorArguments()).isEqualTo(parameters);
        assertThat(o_O.getEntityType()).hasValue(Sample.class);
        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");
    }
}
Also used : MappingInstantiationException(org.springframework.data.mapping.model.MappingInstantiationException) Constructor(java.lang.reflect.Constructor) PreferredConstructor(org.springframework.data.mapping.PreferredConstructor) BasicPersistentEntity(org.springframework.data.mapping.model.BasicPersistentEntity) Parameter(org.springframework.data.mapping.PreferredConstructor.Parameter) Test(org.junit.Test)

Example 3 with BasicPersistentEntity

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

the class ClassGeneratingEntityInstantiatorUnitTests method createsInnerClassInstanceCorrectly.

// DATACMNS-134, DATACMNS-578
@Test
public void createsInnerClassInstanceCorrectly() {
    BasicPersistentEntity<Inner, P> entity = new BasicPersistentEntity<>(from(Inner.class));
    assertThat(entity.getPersistenceConstructor()).satisfies(constructor -> {
        Parameter<Object, P> parameter = constructor.getParameters().iterator().next();
        Object outer = new Outer();
        doReturn(outer).when(provider).getParameterValue(parameter);
        Inner instance = this.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.ClassGeneratingEntityInstantiatorUnitTests.Outer.Inner) Test(org.junit.Test)

Example 4 with BasicPersistentEntity

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

the class ReflectionEntityInstantiatorUnitTests method capturesContextOnInstantiationException.

// DATACMNS-283
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void capturesContextOnInstantiationException() throws Exception {
    PersistentEntity<Sample, P> entity = new BasicPersistentEntity<>(from(Sample.class));
    doReturn("FOO").when(provider).getParameterValue(any(Parameter.class));
    Constructor constructor = Sample.class.getConstructor(Long.class, String.class);
    List<Object> parameters = Arrays.asList("FOO", "FOO");
    try {
        INSTANCE.createInstance(entity, provider);
        fail("Expected MappingInstantiationException!");
    } catch (MappingInstantiationException o_O) {
        assertThat(o_O.getConstructor()).hasValue(constructor);
        assertThat(o_O.getConstructorArguments()).isEqualTo(parameters);
        assertThat(o_O.getEntityType()).hasValue(Sample.class);
        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");
    }
}
Also used : MappingInstantiationException(org.springframework.data.mapping.model.MappingInstantiationException) Constructor(java.lang.reflect.Constructor) PreferredConstructor(org.springframework.data.mapping.PreferredConstructor) BasicPersistentEntity(org.springframework.data.mapping.model.BasicPersistentEntity) Parameter(org.springframework.data.mapping.PreferredConstructor.Parameter) Test(org.junit.Test)

Example 5 with BasicPersistentEntity

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

the class PreferredConstructorDiscovererUnitTests method skipsSyntheticConstructor.

// DATACMNS-1082, DATACMNS-1126
@Test
public void skipsSyntheticConstructor() {
    PersistentEntity<SyntheticConstructor, P> entity = new BasicPersistentEntity<>(ClassTypeInformation.from(SyntheticConstructor.class));
    assertThat(PreferredConstructorDiscoverer.discover(entity)).satisfies(constructor -> {
        PersistenceConstructor annotation = constructor.getConstructor().getAnnotation(PersistenceConstructor.class);
        assertThat(annotation).isNotNull();
        assertThat(constructor.getConstructor().isSynthetic()).isFalse();
    });
}
Also used : BasicPersistentEntity(org.springframework.data.mapping.model.BasicPersistentEntity) PersistenceConstructor(org.springframework.data.annotation.PersistenceConstructor) 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