Skip to content

Commit 08e8f15

Browse files
jxblumodrotbohm
authored andcommitted
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.
1 parent 38a4113 commit 08e8f15

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.mapping.model;
18+
19+
import static org.hamcrest.Matchers.equalTo;
20+
import static org.hamcrest.Matchers.is;
21+
import static org.junit.Assert.assertThat;
22+
23+
import java.io.Serializable;
24+
25+
import org.junit.Test;
26+
import org.springframework.data.annotation.Id;
27+
import org.springframework.data.mapping.PersistentEntity;
28+
import org.springframework.data.mapping.context.SampleMappingContext;
29+
import org.springframework.data.mapping.context.SamplePersistentProperty;
30+
import org.springframework.data.repository.core.support.PersistentEntityInformation;
31+
32+
/**
33+
* Unit tests for {@link ClassGeneratingPropertyAccessorFactory} covering interface
34+
* and concrete class entity types.
35+
*
36+
* @author John Blum
37+
* @see DATACMNS-809
38+
*/
39+
public class ClassGeneratingPropertyAccessorFactoryEntityTypeTests {
40+
41+
private final SampleMappingContext mappingContext = new SampleMappingContext();
42+
43+
protected PersistentEntity<Object, SamplePersistentProperty> getPersistentEntity(Object entity) {
44+
return getPersistentEntity(entity.getClass());
45+
}
46+
47+
protected PersistentEntity<Object, SamplePersistentProperty> getPersistentEntity(Class<?> entityType) {
48+
return mappingContext.getPersistentEntity(entityType);
49+
}
50+
51+
protected PersistentEntityInformation<Object, ?> getPersistentEntityInformation(Object entity) {
52+
return new PersistentEntityInformation<Object, Serializable>(getPersistentEntity(entity));
53+
}
54+
55+
protected PersistentEntityInformation<Object, ?> getPersistentEntityInformation(Class<?> entityType) {
56+
return new PersistentEntityInformation<Object, Serializable>(getPersistentEntity(entityType));
57+
}
58+
59+
@Test
60+
public void getIdentifierOfInterfaceBasedEntity() {
61+
PersistentEntityInformation<Object, ?> quickSortEntityInfo =
62+
getPersistentEntityInformation(Algorithm.class);
63+
64+
Algorithm quickSort = new QuickSort();
65+
66+
assertThat(String.valueOf(quickSortEntityInfo.getId(quickSort)), is(equalTo(quickSort.getName())));
67+
}
68+
69+
@Test
70+
public void getIdentifierOfClassBasedEntity() {
71+
Person jonDoe = Person.newPerson("JonDoe");
72+
PersistentEntityInformation<Object, ?> jonDoeEntityInfo = getPersistentEntityInformation(jonDoe);
73+
74+
assertThat(String.valueOf(jonDoeEntityInfo.getId(jonDoe)), is(equalTo(jonDoe.getName())));
75+
}
76+
77+
interface Algorithm {
78+
@Id String getName();
79+
}
80+
81+
class QuickSort implements Algorithm {
82+
@Override
83+
public String getName() {
84+
return getClass().toString();
85+
}
86+
}
87+
88+
static class Person {
89+
90+
@Id
91+
private final String name;
92+
93+
static Person newPerson(String name) {
94+
return new Person(name);
95+
}
96+
97+
Person(String name) {
98+
this.name = name;
99+
}
100+
101+
public String getName() {
102+
return name;
103+
}
104+
105+
@Override
106+
public String toString() {
107+
return getName();
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)