DATACMNS-19 - Fixed PersistableEntityMetadata.

Added generics to PersistableEntityMetadata and let it being handed the actual domain class to return.
This commit is contained in:
Oliver Gierke
2011-03-02 09:02:18 +01:00
parent 695f4ca9c0
commit cae2961275
4 changed files with 36 additions and 92 deletions

View File

@@ -26,17 +26,17 @@ import org.springframework.data.domain.Persistable;
* @author Oliver Gierke
*/
@SuppressWarnings("rawtypes")
public class PersistableEntityMetadata extends
AbstractEntityMetadata<Persistable> {
public class PersistableEntityMetadata<T extends Persistable> extends
AbstractEntityMetadata<T> {
/**
* Creates a new {@link PersistableEntityMetadata}.
*
* @param domainClass
*/
public PersistableEntityMetadata() {
public PersistableEntityMetadata(Class<T> domainClass) {
super(Persistable.class);
super(domainClass);
}
@@ -48,7 +48,7 @@ public class PersistableEntityMetadata extends
* .Object)
*/
@Override
public boolean isNew(Persistable entity) {
public boolean isNew(T entity) {
return entity.isNew();
}
@@ -61,7 +61,7 @@ public class PersistableEntityMetadata extends
* org.springframework.data.repository.support.IdAware#getId(java.lang.Object
* )
*/
public Object getId(Persistable entity) {
public Object getId(T entity) {
return entity.getId();
}

View File

@@ -1,83 +0,0 @@
/*
* Copyright 2008-2010 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.support;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.data.domain.Persistable;
/**
* Unit test for {@link PersistableEntityMetadata}.
*
* @author Oliver Gierke
*/
public class PersistableEntityInformationTests {
@Test
public void detectsPersistableCorrectly() throws Exception {
PersistableEntityMetadata info = new PersistableEntityMetadata();
assertNewAndNoId(info, new PersistableEntity(null));
assertNotNewAndId(info, new PersistableEntity(1L), 1L);
}
@SuppressWarnings("rawtypes")
private <S extends EntityMetadata<Persistable>> void assertNewAndNoId(
S info, Persistable entity) {
assertThat(info.isNew(entity), is(true));
assertThat(info.getId(entity), is(nullValue()));
}
@SuppressWarnings("rawtypes")
private <S extends EntityMetadata<Persistable>> void assertNotNewAndId(
S info, Persistable entity, Object id) {
assertThat(info.isNew(entity), is(false));
assertThat(info.getId(entity), is(id));
}
static class PersistableEntity implements Persistable<Long> {
private static final long serialVersionUID = -5898780128204716452L;
private final Long id;
public PersistableEntity(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public boolean isNew() {
return id == null;
}
}
}

View File

@@ -34,15 +34,15 @@ import org.springframework.data.domain.Persistable;
@RunWith(MockitoJUnitRunner.class)
public class PersistableEntityMetadataUnitTests {
static final PersistableEntityMetadata metadata =
new PersistableEntityMetadata();
@SuppressWarnings("rawtypes")
static final PersistableEntityMetadata<Persistable> metadata =
new PersistableEntityMetadata<Persistable>(Persistable.class);
@Mock
Persistable<Long> persistable;
@Test
@SuppressWarnings("serial")
public void usesPersistablesGetId() throws Exception {
when(persistable.getId()).thenReturn(2L, 1L, 3L);
@@ -59,4 +59,30 @@ public class PersistableEntityMetadataUnitTests {
assertThat(metadata.isNew(persistable), is(true));
assertThat(metadata.isNew(persistable), is(false));
}
@Test
public void returnsGivenClassAsEntityType() throws Exception {
PersistableEntityMetadata<PersistableEntity> info =
new PersistableEntityMetadata<PersistableEntity>(
PersistableEntity.class);
assertEquals(PersistableEntity.class, info.getJavaType());
}
@SuppressWarnings("serial")
static class PersistableEntity implements Persistable<Long> {
public Long getId() {
return null;
}
public boolean isNew() {
return false;
}
}
}

View File

@@ -9,6 +9,7 @@ Repository
* Added support for 'Distinct' keyword in finder method names (DATACMNS-15)
* Added support for 'In' and 'NotIn' keywords (DATACMNS-16)
* Introduced metamodel for entities and repositories (DATACMNS-17)
* Fixed returning wrong class PersistableEntityMetadata(DATACMNS-19)
Changes in version 1.0.0.M3 (2011-02-09)
----------------------------------------