DATACMNS-357 - Cleanups in primitive id detection.
Changed implementation in AbstractEntityInformation quite a bit to not need the non-final fields and a protected callback method. Cleaned up test cases to really test the new behavior, not the artificially introduced test helper class. Original pull request: #37.
This commit is contained in:
@@ -20,9 +20,13 @@ import static org.junit.Assert.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.domain.Persistable;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AbstractEntityInformation}.
|
||||
@@ -32,6 +36,8 @@ import org.springframework.data.repository.core.EntityInformation;
|
||||
*/
|
||||
public class AbstractEntityInformationUnitTests {
|
||||
|
||||
@Rule public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullDomainClass() throws Exception {
|
||||
|
||||
@@ -46,78 +52,85 @@ public class AbstractEntityInformationUnitTests {
|
||||
assertThat(metadata.isNew(new Object()), is(false));
|
||||
}
|
||||
|
||||
// See DATACMNS-357
|
||||
/**
|
||||
* @see DATACMNS-357
|
||||
*/
|
||||
@Test
|
||||
public void considersEntityNewIfGetIdReturnsNullAndIdTypeIsPrimitiveNumber() throws Exception {
|
||||
public void detectsNewStateForPrimitiveIds() {
|
||||
|
||||
EntityInformation<LongIdEntity, Long> metadata1 =
|
||||
new DummyEntityAndIdInformation<LongIdEntity, Long>(LongIdEntity.class, long.class);
|
||||
assertThat(metadata1.isNew(null), is(true));
|
||||
assertThat(metadata1.isNew(new LongIdEntity(null)), is(true));
|
||||
assertThat(metadata1.isNew(new LongIdEntity(-1L)), is(true));
|
||||
assertThat(metadata1.isNew(new LongIdEntity(0L)), is(true));
|
||||
assertThat(metadata1.isNew(new LongIdEntity(1L)), is(false));
|
||||
FooEn<PrimitiveIdEntity, Serializable> fooEn = new FooEn<PrimitiveIdEntity, Serializable>(PrimitiveIdEntity.class);
|
||||
|
||||
EntityInformation<IntIdEntity, Integer> metadata2 =
|
||||
new DummyEntityAndIdInformation<IntIdEntity, Integer>(IntIdEntity.class, int.class);
|
||||
assertThat(metadata2.isNew(null), is(true));
|
||||
assertThat(metadata2.isNew(new IntIdEntity(null)), is(true));
|
||||
assertThat(metadata2.isNew(new IntIdEntity(-1)), is(true));
|
||||
assertThat(metadata2.isNew(new IntIdEntity(0)), is(true));
|
||||
assertThat(metadata2.isNew(new IntIdEntity(1)), is(false));
|
||||
PrimitiveIdEntity entity = new PrimitiveIdEntity();
|
||||
assertThat(fooEn.isNew(entity), is(true));
|
||||
|
||||
entity.id = 5L;
|
||||
assertThat(fooEn.isNew(entity), is(false));
|
||||
}
|
||||
|
||||
// See DATACMNS-357
|
||||
/**
|
||||
* @see DATACMNS-357
|
||||
*/
|
||||
@Test
|
||||
public void considersEntityNewIfGetIdReturnsNullAndIdTypeIsPrimitiveWrapperNumber() throws Exception {
|
||||
public void detectsNewStateForPrimitiveWrapperIds() {
|
||||
|
||||
EntityInformation<LongIdEntity, Long> metadata1 =
|
||||
new DummyEntityAndIdInformation<LongIdEntity, Long>(LongIdEntity.class, Long.class);
|
||||
assertThat(metadata1.isNew(null), is(true));
|
||||
assertThat(metadata1.isNew(new LongIdEntity(null)), is(true));
|
||||
assertThat(metadata1.isNew(new LongIdEntity(-1L)), is(false));
|
||||
assertThat(metadata1.isNew(new LongIdEntity(0L)), is(false));
|
||||
assertThat(metadata1.isNew(new LongIdEntity(1L)), is(false));
|
||||
FooEn<PrimitiveWrapperIdEntity, Serializable> fooEn = new FooEn<PrimitiveWrapperIdEntity, Serializable>(
|
||||
PrimitiveWrapperIdEntity.class);
|
||||
|
||||
EntityInformation<IntIdEntity, Integer> metadata2 =
|
||||
new DummyEntityAndIdInformation<IntIdEntity, Integer>(IntIdEntity.class, Integer.class);
|
||||
assertThat(metadata2.isNew(null), is(true));
|
||||
assertThat(metadata2.isNew(new IntIdEntity(null)), is(true));
|
||||
assertThat(metadata2.isNew(new IntIdEntity(-1)), is(false));
|
||||
assertThat(metadata2.isNew(new IntIdEntity(0)), is(false));
|
||||
assertThat(metadata2.isNew(new IntIdEntity(1)), is(false));
|
||||
PrimitiveWrapperIdEntity entity = new PrimitiveWrapperIdEntity();
|
||||
assertThat(fooEn.isNew(entity), is(true));
|
||||
|
||||
entity.id = 5L;
|
||||
assertThat(fooEn.isNew(entity), is(false));
|
||||
}
|
||||
|
||||
private static abstract class NumberIdEntity<T extends Number> implements Persistable<T> {
|
||||
/**
|
||||
* @see DATACMNS-357
|
||||
*/
|
||||
@Test
|
||||
public void rejectsUnsupportedPrimitiveIdType() {
|
||||
|
||||
private final T id;
|
||||
FooEn<UnsupportedPrimitiveIdEntity, ?> information = new FooEn<UnsupportedPrimitiveIdEntity, Boolean>(
|
||||
UnsupportedPrimitiveIdEntity.class);
|
||||
|
||||
public NumberIdEntity(T id) {
|
||||
this.id = id;
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
exception.expectMessage(boolean.class.getName());
|
||||
information.isNew(new UnsupportedPrimitiveIdEntity());
|
||||
}
|
||||
|
||||
static class PrimitiveIdEntity {
|
||||
|
||||
@Id long id;
|
||||
}
|
||||
|
||||
static class PrimitiveWrapperIdEntity {
|
||||
|
||||
@Id Long id;
|
||||
}
|
||||
|
||||
static class UnsupportedPrimitiveIdEntity {
|
||||
|
||||
@Id boolean id;
|
||||
}
|
||||
|
||||
static class FooEn<T, ID extends Serializable> extends AbstractEntityInformation<T, ID> {
|
||||
|
||||
private final Class<T> type;
|
||||
|
||||
private FooEn(Class<T> type) {
|
||||
super(type);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getId() {
|
||||
return this.id;
|
||||
@SuppressWarnings("unchecked")
|
||||
public ID getId(T entity) {
|
||||
return (ID) ReflectionTestUtils.getField(entity, "id");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNew() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class LongIdEntity extends NumberIdEntity<Long> {
|
||||
|
||||
public LongIdEntity(Long id) {
|
||||
super(id);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class IntIdEntity extends NumberIdEntity<Integer> {
|
||||
|
||||
public IntIdEntity(Integer id) {
|
||||
super(id);
|
||||
@SuppressWarnings("unchecked")
|
||||
public Class<ID> getIdType() {
|
||||
return (Class<ID>) ReflectionUtils.findField(type, "id").getType();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011-2013 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.repository.core.support;
|
||||
|
||||
import org.springframework.data.domain.Persistable;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Dummy implementation of {@link AbstractEntityInformation} that also provides ID information.
|
||||
*
|
||||
* @author Nick Williams
|
||||
*/
|
||||
public class DummyEntityAndIdInformation<T extends Persistable<ID>, ID extends Serializable>
|
||||
extends AbstractEntityInformation<T, ID> {
|
||||
|
||||
private final Class<ID> idClass;
|
||||
|
||||
/**
|
||||
* Creates a new {@link DummyEntityInformation} for the given domain class.
|
||||
*
|
||||
* @param domainClass
|
||||
*/
|
||||
public DummyEntityAndIdInformation(Class<T> domainClass, Class<ID> idClass) {
|
||||
super(domainClass);
|
||||
this.idClass = idClass;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.core.EntityInformation#getId(java.lang.Object)
|
||||
*/
|
||||
public ID getId(T entity) {
|
||||
return entity == null ? null : entity.getId();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.core.EntityInformation#getIdType()
|
||||
*/
|
||||
public Class<ID> getIdType() {
|
||||
return this.idClass;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012 the original author or authors.
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may
|
||||
@@ -48,18 +48,38 @@ public class ReflectionEntityInformationUnitTests {
|
||||
getEntityInformation(Unannotated.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-357
|
||||
*/
|
||||
@Test
|
||||
public void detectsNewStateForEntitiesWithPrimitiveIds() {
|
||||
|
||||
PrimitiveId primitiveId = new PrimitiveId();
|
||||
|
||||
EntityInformation<PrimitiveId, Serializable> information = new ReflectionEntityInformation<PrimitiveId, Serializable>(
|
||||
PrimitiveId.class);
|
||||
assertThat(information.isNew(primitiveId), is(true));
|
||||
|
||||
primitiveId.id = 5L;
|
||||
assertThat(information.isNew(primitiveId), is(false));
|
||||
}
|
||||
|
||||
private static <T> EntityInformation<T, Serializable> getEntityInformation(Class<T> type) {
|
||||
return new ReflectionEntityInformation<T, Serializable>(type, Id.class);
|
||||
}
|
||||
|
||||
static class Sample {
|
||||
|
||||
@Id
|
||||
String id;
|
||||
@Id String id;
|
||||
}
|
||||
|
||||
static class Unannotated {
|
||||
|
||||
String id;
|
||||
}
|
||||
|
||||
static class PrimitiveId {
|
||||
|
||||
@Id long id;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user