Search in sources :

Example 1 with PersistentEntity

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

the class BasicPersistentEntityUnitTests method returnsBeanWrapperForPropertyAccessor.

// DATACMNS-596
@Test
public void returnsBeanWrapperForPropertyAccessor() {
    assumeThat(System.getProperty("java.version"), CoreMatchers.startsWith("1.6"));
    SampleMappingContext context = new SampleMappingContext();
    PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Entity.class);
    Entity value = new Entity();
    PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value);
    assertThat(accessor).isInstanceOf(BeanWrapper.class);
    assertThat(accessor.getBean()).isEqualTo(value);
}
Also used : PersistentEntity(org.springframework.data.mapping.PersistentEntity) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) SamplePersistentProperty(org.springframework.data.mapping.context.SamplePersistentProperty) SampleMappingContext(org.springframework.data.mapping.context.SampleMappingContext) Test(org.junit.Test)

Example 2 with PersistentEntity

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

the class ClassGeneratingPropertyAccessorFactory method hasUniquePropertyHashCodes.

private boolean hasUniquePropertyHashCodes(PersistentEntity<?, ?> entity) {
    Set<Integer> hashCodes = new HashSet<>();
    AtomicInteger propertyCount = new AtomicInteger();
    entity.doWithProperties((SimplePropertyHandler) property -> {
        hashCodes.add(property.getName().hashCode());
        propertyCount.incrementAndGet();
    });
    entity.doWithAssociations((SimpleAssociationHandler) association -> {
        if (association.getInverse() != null) {
            hashCodes.add(association.getInverse().getName().hashCode());
            propertyCount.incrementAndGet();
        }
    });
    return hashCodes.size() == propertyCount.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClassWriter(org.springframework.asm.ClassWriter) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Label(org.springframework.asm.Label) HashMap(java.util.HashMap) TypeInformation(org.springframework.data.util.TypeInformation) Constructor(java.lang.reflect.Constructor) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Optionals(org.springframework.data.util.Optionals) Map(java.util.Map) Opcodes(org.springframework.asm.Opcodes) Nullable(org.springframework.lang.Nullable) Method(java.lang.reflect.Method) PersistentProperty(org.springframework.data.mapping.PersistentProperty) NonNull(lombok.NonNull) ReflectUtils(org.springframework.cglib.core.ReflectUtils) Member(java.lang.reflect.Member) SimpleAssociationHandler(org.springframework.data.mapping.SimpleAssociationHandler) Set(java.util.Set) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) PersistentEntity(org.springframework.data.mapping.PersistentEntity) List(java.util.List) ReflectionUtils(org.springframework.util.ReflectionUtils) Modifier(java.lang.reflect.Modifier) ProtectionDomain(java.security.ProtectionDomain) Optional(java.util.Optional) MethodVisitor(org.springframework.asm.MethodVisitor) Collections(java.util.Collections) SimplePropertyHandler(org.springframework.data.mapping.SimplePropertyHandler) Type(org.springframework.asm.Type) Assert(org.springframework.util.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashSet(java.util.HashSet)

Example 3 with PersistentEntity

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

the class BasicPersistentEntityUnitTests method returnsGeneratedPropertyAccessorForPropertyAccessor.

// DATACMNS-809
@Test
public void returnsGeneratedPropertyAccessorForPropertyAccessor() {
    SampleMappingContext context = new SampleMappingContext();
    PersistentEntity<Object, SamplePersistentProperty> entity = context.getRequiredPersistentEntity(Entity.class);
    Entity value = new Entity();
    PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value);
    assertThat(accessor).isNotInstanceOf(BeanWrapper.class);
    assertThat(accessor.getClass().getName()).contains("_Accessor_");
    assertThat(accessor.getBean()).isEqualTo(value);
}
Also used : PersistentEntity(org.springframework.data.mapping.PersistentEntity) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) SamplePersistentProperty(org.springframework.data.mapping.context.SamplePersistentProperty) SampleMappingContext(org.springframework.data.mapping.context.SampleMappingContext) Test(org.junit.Test)

Example 4 with PersistentEntity

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

the class MappingContextTypeInformationMapperUnitTests method detectsTypeForUnknownEntity.

@Test
public void detectsTypeForUnknownEntity() {
    SampleMappingContext mappingContext = new SampleMappingContext();
    mappingContext.initialize();
    mapper = new MappingContextTypeInformationMapper(mappingContext);
    assertThat(mapper.resolveTypeFrom(Alias.of("foo"))).isNull();
    PersistentEntity<?, SamplePersistentProperty> entity = mappingContext.getRequiredPersistentEntity(Entity.class);
    assertThat(entity).isNotNull();
    assertThat(mapper.resolveTypeFrom(Alias.of("foo"))).isEqualTo(from(Entity.class));
}
Also used : PersistentEntity(org.springframework.data.mapping.PersistentEntity) SamplePersistentProperty(org.springframework.data.mapping.context.SamplePersistentProperty) SampleMappingContext(org.springframework.data.mapping.context.SampleMappingContext) Test(org.junit.Test)

Aggregations

PersistentEntity (org.springframework.data.mapping.PersistentEntity)4 Test (org.junit.Test)3 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)3 SampleMappingContext (org.springframework.data.mapping.context.SampleMappingContext)3 SamplePersistentProperty (org.springframework.data.mapping.context.SamplePersistentProperty)3 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 Member (java.lang.reflect.Member)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 ProtectionDomain (java.security.ProtectionDomain)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1