DATAJPA-58 - JpaPersistableEntityInformation now uses Persistable.isNew(…).

Added unit tests to verify behavior and updated changelog accordingly.
This commit is contained in:
Oliver Gierke
2011-05-10 12:20:29 +02:00
parent 1cfa3acb22
commit 536f8ccd91
3 changed files with 111 additions and 0 deletions

View File

@@ -45,6 +45,20 @@ public class JpaPersistableEntityInformation<T extends Persistable<ID>, ID exten
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.support.AbstractEntityInformation
* #isNew(java.lang.Object)
*/
@Override
public boolean isNew(T entity) {
return entity.isNew();
}
/*
* (non-Javadoc)
*

View File

@@ -4,6 +4,7 @@ Spring Data JPA Changelog
Changes in version 1.0.0.RC1
----------------------------------------
* Improved performance of query execution by uhsing method.getAnnotation(…) instead of AnnotationUtils.getAnnotation(…)
* JpaPersistableEntityInformation now uses Persistable.isNew(…) (DATAJPA-58)
Changes in version 1.0.0.M2 (2011-03-24) - https://jira.springsource.org/browse/DATAJPA/fixforversion/11800
----------------------------------------

View File

@@ -0,0 +1,96 @@
/*
* Copyright 2011 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.jpa.repository.support;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.metamodel.Type;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.domain.Persistable;
import org.springframework.data.repository.support.EntityInformation;
/**
* Unit tests for {@link JpaPersistableEntityInformation}.
*
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class JpaPersistableEntityInformationUnitTests {
@Mock
Metamodel metamodel;
@Mock
EntityType<Foo> type;
@Mock
@SuppressWarnings("rawtypes")
Type idType;
@Before
@SuppressWarnings("unchecked")
public void setUp() {
when(metamodel.entity(Foo.class)).thenReturn(type);
when(type.getIdType()).thenReturn(idType);
}
@Test
public void usesPersistableMethodsForIsNewAndGetId() {
EntityInformation<Foo, Long> entityInformation =
new JpaPersistableEntityInformation<Foo, Long>(Foo.class,
metamodel);
Foo foo = new Foo();
assertThat(entityInformation.isNew(foo), is(false));
assertThat(entityInformation.getId(foo), is(nullValue()));
foo.id = 1L;
assertThat(entityInformation.isNew(foo), is(true));
assertThat(entityInformation.getId(foo), is(1L));
}
@SuppressWarnings("serial")
class Foo implements Persistable<Long> {
Long id;
public Long getId() {
return id;
}
public boolean isNew() {
return id != null;
}
}
}