From 08e8f158450fcb659f9acc31701e4c07696756d6 Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 5 May 2016 22:58:58 -0700 Subject: [PATCH] DATACMNS-853 - Added test case reproducing the issue. Reproduce issue accessing persistent properties from interface-based entities using the ClassGeneratingPropertyAccessorFactory. Related tickets: DATACMNS-809. Original pull request: #161. Related pull request: #160. --- ...ropertyAccessorFactoryEntityTypeTests.java | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryEntityTypeTests.java diff --git a/src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryEntityTypeTests.java b/src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryEntityTypeTests.java new file mode 100644 index 000000000..0cca4b882 --- /dev/null +++ b/src/test/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactoryEntityTypeTests.java @@ -0,0 +1,110 @@ +/* + * Copyright 2016 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 + * + * http://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; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.io.Serializable; + +import org.junit.Test; +import org.springframework.data.annotation.Id; +import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.mapping.context.SampleMappingContext; +import org.springframework.data.mapping.context.SamplePersistentProperty; +import org.springframework.data.repository.core.support.PersistentEntityInformation; + +/** + * Unit tests for {@link ClassGeneratingPropertyAccessorFactory} covering interface + * and concrete class entity types. + * + * @author John Blum + * @see DATACMNS-809 + */ +public class ClassGeneratingPropertyAccessorFactoryEntityTypeTests { + + private final SampleMappingContext mappingContext = new SampleMappingContext(); + + protected PersistentEntity getPersistentEntity(Object entity) { + return getPersistentEntity(entity.getClass()); + } + + protected PersistentEntity getPersistentEntity(Class entityType) { + return mappingContext.getPersistentEntity(entityType); + } + + protected PersistentEntityInformation getPersistentEntityInformation(Object entity) { + return new PersistentEntityInformation(getPersistentEntity(entity)); + } + + protected PersistentEntityInformation getPersistentEntityInformation(Class entityType) { + return new PersistentEntityInformation(getPersistentEntity(entityType)); + } + + @Test + public void getIdentifierOfInterfaceBasedEntity() { + PersistentEntityInformation quickSortEntityInfo = + getPersistentEntityInformation(Algorithm.class); + + Algorithm quickSort = new QuickSort(); + + assertThat(String.valueOf(quickSortEntityInfo.getId(quickSort)), is(equalTo(quickSort.getName()))); + } + + @Test + public void getIdentifierOfClassBasedEntity() { + Person jonDoe = Person.newPerson("JonDoe"); + PersistentEntityInformation jonDoeEntityInfo = getPersistentEntityInformation(jonDoe); + + assertThat(String.valueOf(jonDoeEntityInfo.getId(jonDoe)), is(equalTo(jonDoe.getName()))); + } + + interface Algorithm { + @Id String getName(); + } + + class QuickSort implements Algorithm { + @Override + public String getName() { + return getClass().toString(); + } + } + + static class Person { + + @Id + private final String name; + + static Person newPerson(String name) { + return new Person(name); + } + + Person(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return getName(); + } + } +}