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);
}
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");
}
}
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);
}
});
});
}
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");
}
}
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();
});
}
Aggregations