From 536f8ccd9179cb1e3fddb07f459d0cda0436466d Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 10 May 2011 12:20:29 +0200 Subject: [PATCH] =?UTF-8?q?DATAJPA-58=20-=20JpaPersistableEntityInformatio?= =?UTF-8?q?n=20now=20uses=20Persistable.isNew(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added unit tests to verify behavior and updated changelog accordingly. --- .../JpaPersistableEntityInformation.java | 14 +++ src/main/resources/changelog.txt | 1 + ...PersistableEntityInformationUnitTests.java | 96 +++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 src/test/java/org/springframework/data/jpa/repository/support/JpaPersistableEntityInformationUnitTests.java diff --git a/src/main/java/org/springframework/data/jpa/repository/support/JpaPersistableEntityInformation.java b/src/main/java/org/springframework/data/jpa/repository/support/JpaPersistableEntityInformation.java index b9149e02c..d04e05ebe 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/JpaPersistableEntityInformation.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/JpaPersistableEntityInformation.java @@ -45,6 +45,20 @@ public class JpaPersistableEntityInformation, 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) * diff --git a/src/main/resources/changelog.txt b/src/main/resources/changelog.txt index 7aaf75e7c..c50f19e5c 100644 --- a/src/main/resources/changelog.txt +++ b/src/main/resources/changelog.txt @@ -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 ---------------------------------------- diff --git a/src/test/java/org/springframework/data/jpa/repository/support/JpaPersistableEntityInformationUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/support/JpaPersistableEntityInformationUnitTests.java new file mode 100644 index 000000000..d4e9047d7 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/support/JpaPersistableEntityInformationUnitTests.java @@ -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 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 entityInformation = + new JpaPersistableEntityInformation(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 id; + + + public Long getId() { + + return id; + } + + + public boolean isNew() { + + return id != null; + } + } +}