DATACMNS-853 - Polishing.

Simplified test case. Formatting in ClassGeneratingPropertyAccessorFactory.

Related tickets: DATACMNS-809.
Original pull request: #161.
Related pull request: #160.
This commit is contained in:
Oliver Gierke
2016-05-12 15:34:18 +02:00
parent db5259292c
commit f29cbf5752
2 changed files with 29 additions and 48 deletions

View File

@@ -13,12 +13,10 @@
* 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 static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.Serializable;
@@ -27,58 +25,55 @@ 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.EntityInformation;
import org.springframework.data.repository.core.support.PersistentEntityInformation;
/**
* Unit tests for {@link ClassGeneratingPropertyAccessorFactory} covering interface
* and concrete class entity types.
* Unit tests for {@link ClassGeneratingPropertyAccessorFactory} covering interface and concrete class entity types.
*
* @author John Blum
* @see DATACMNS-809
* @author Oliver Gierke
*/
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));
}
SampleMappingContext mappingContext = new SampleMappingContext();
/**
* @see DATACMNS-853
*/
@Test
public void getIdentifierOfInterfaceBasedEntity() {
PersistentEntityInformation<Object, ?> quickSortEntityInfo =
getPersistentEntityInformation(Algorithm.class);
Algorithm quickSort = new QuickSort();
assertThat(String.valueOf(quickSortEntityInfo.getId(quickSort)), is(equalTo(quickSort.getName())));
assertThat(getEntityInformation(Algorithm.class).getId(quickSort), is((Object) quickSort.getName()));
}
/**
* @see DATACMNS-853
*/
@Test
public void getIdentifierOfClassBasedEntity() {
Person jonDoe = Person.newPerson("JonDoe");
PersistentEntityInformation<Object, ?> jonDoeEntityInfo = getPersistentEntityInformation(jonDoe);
assertThat(String.valueOf(jonDoeEntityInfo.getId(jonDoe)), is(equalTo(jonDoe.getName())));
Person jonDoe = new Person("JonDoe");
assertThat(getEntityInformation(Person.class).getId(jonDoe), is((Object) jonDoe.name));
}
private EntityInformation<Object, ?> getEntityInformation(Class<?> type) {
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(type);
return new PersistentEntityInformation<Object, Serializable>(entity);
}
interface Algorithm {
@Id String getName();
@Id
String getName();
}
class QuickSort implements Algorithm {
@Override
public String getName() {
return getClass().toString();
@@ -87,24 +82,10 @@ public class ClassGeneratingPropertyAccessorFactoryEntityTypeTests {
static class Person {
@Id
private final String name;
static Person newPerson(String name) {
return new Person(name);
}
@Id String name;
Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return getName();
}
}
}