Skip to content

DATACMNS-809 - Reproduce issue accessing persistent properties from interface-based entities using the ClassGeneratingPropertyAccessorFactory. #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*
* @author Mark Paluch
* @author Oliver Gierke
* @see DATACMNS-809
* @see <a href="https://jira.spring.io/browse/DATACMNS-809">DATACMNS-809</a>
*/
@RunWith(Parameterized.class)
public class ClassGeneratingPropertyAccessorFactoryDatatypeTests {
Expand Down Expand Up @@ -121,7 +121,7 @@ private static List<Object[]> parameters(List<Class<?>> types, String propertyNa
}

/**
* @see DATACMNS-809
* @see <a href="https://jira.spring.io/browse/DATACMNS-809">DATACMNS-809</a>
*/
@Test
public void shouldSetAndGetProperty() throws Exception {
Expand All @@ -134,7 +134,7 @@ public void shouldSetAndGetProperty() throws Exception {
}

/**
* @see DATACMNS-809
* @see <a href="https://jira.spring.io/browse/DATACMNS-809">DATACMNS-809</a>
*/
@Test
public void shouldUseClassPropertyAccessorFactory() throws Exception {
Expand All @@ -158,7 +158,7 @@ private PersistentProperty<?> getProperty(Object bean, String name) {
}

/**
* @see DATACMNS-809
* @see <a href="https://jira.spring.io/browse/DATACMNS-809">DATACMNS-809</a>
*/
@AccessType(Type.FIELD)
public static class FieldAccess {
Expand Down Expand Up @@ -208,7 +208,7 @@ public static class FieldAccess {
}

/**
* @see DATACMNS-809
* @see <a href="https://jira.spring.io/browse/DATACMNS-809">DATACMNS-809</a>
*/
@AccessType(Type.PROPERTY)
@Data
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <a href="https://jira.spring.io/browse/DATACMNS-809">DATACMNS-809</a>
*/
public class ClassGeneratingPropertyAccessorFactoryEntityTypeTests {

private final SampleMappingContext mappingContext = new SampleMappingContext();

protected PersistentEntity<Object, SamplePersistentProperty> getPersistentEntity(Object entity) {
return getPersistentEntity(entity.getClass());
}

protected PersistentEntity<Object, SamplePersistentProperty> getPersistentEntity(Class<?> entityType) {
return mappingContext.getPersistentEntity(entityType);
}

protected PersistentEntityInformation<Object, ?> getPersistentEntityInformation(Object entity) {
return new PersistentEntityInformation<Object, Serializable>(getPersistentEntity(entity));
}

protected PersistentEntityInformation<Object, ?> getPersistentEntityInformation(Class<?> entityType) {
return new PersistentEntityInformation<Object, Serializable>(getPersistentEntity(entityType));
}

@Test
public void getIdentifierOfInterfaceBasedEntity() {
PersistentEntityInformation<Object, ?> 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<Object, ?> 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();
}
}
}