From c14a3c1116a074c834c93c7ccf766491c6b1fd6e Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 14 May 2015 18:18:18 +0200 Subject: [PATCH] #31 - Polishing. Tweaked implementation to not only lookup revision but also the entity in the given revision. Additional formatting and metadata polishing. --- .../support/EnversRevisionRepository.java | 14 ++++-- .../support/EnversRevisionRepositoryImpl.java | 39 +++++++++------- .../support/RepositoryIntegrationTest.java | 45 ++++++++++++------- 3 files changed, 65 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepository.java b/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepository.java index 965da4a..9c4b2fa 100644 --- a/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepository.java +++ b/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2012-2015 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. @@ -27,10 +27,18 @@ import org.springframework.data.repository.history.RevisionRepository; * go. * * @author Oliver Gierke + * @author Michael Igler */ @NoRepositoryBean -public interface EnversRevisionRepository> - extends RevisionRepository, JpaRepository { +public interface EnversRevisionRepository> extends + RevisionRepository, JpaRepository { + /** + * Returns the entity with the given ID in the given revision number. + * + * @param id must not be {@literal null}. + * @param revisionNumber must not be {@literal null}. + * @return + */ Revision findRevision(ID id, N revisionNumber); } diff --git a/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepositoryImpl.java b/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepositoryImpl.java index 707f95b..4971fea 100755 --- a/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepositoryImpl.java +++ b/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepositoryImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2012-2015 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. @@ -16,7 +16,12 @@ package org.springframework.data.envers.repository.support; import java.io.Serializable; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; import java.util.Map.Entry; import javax.persistence.EntityManager; @@ -36,7 +41,6 @@ import org.springframework.data.history.Revisions; import org.springframework.data.jpa.repository.support.JpaEntityInformation; import org.springframework.data.jpa.repository.support.SimpleJpaRepository; import org.springframework.data.repository.core.EntityInformation; -import org.springframework.data.repository.history.RevisionRepository; import org.springframework.data.repository.history.support.RevisionEntityInformation; import org.springframework.util.Assert; @@ -47,7 +51,8 @@ import org.springframework.util.Assert; * @author Philipp Huegelmeyer * @author Michael Igler */ -public class EnversRevisionRepositoryImpl> extends SimpleJpaRepository implements RevisionRepository { +public class EnversRevisionRepositoryImpl> extends + SimpleJpaRepository implements EnversRevisionRepository { private final EntityInformation entityInformation; private final RevisionEntityInformation revisionEntityInformation; @@ -62,7 +67,7 @@ public class EnversRevisionRepositoryImpl entityInformation, - RevisionEntityInformation revisionEntityInformation, EntityManager entityManager) { + RevisionEntityInformation revisionEntityInformation, EntityManager entityManager) { super(entityInformation, entityManager); @@ -98,12 +103,17 @@ public class EnversRevisionRepositoryImpl(metadata, reader.find(type, id, latestRevision)); } - @SuppressWarnings("unchecked") + /* + * (non-Javadoc) + * @see org.springframework.data.envers.repository.support.EnversRevisionRepository#findRevision(java.io.Serializable, java.lang.Number) + */ + @Override public Revision findRevision(ID id, N revisionNumber) { - AuditReader reader = AuditReaderFactory.get(entityManager); + Assert.notNull(id, "Identifier must not be null!"); + Assert.notNull(revisionNumber, "Revision number must not be null!"); - return getEntityForRevision(revisionNumber, id, reader); + return getEntityForRevision(revisionNumber, id, AuditReaderFactory.get(entityManager)); } /* @@ -133,7 +143,7 @@ public class EnversRevisionRepositoryImpl revisionNumbers = reader.getRevisions(type, id); if (pageable.getOffset() > revisionNumbers.size()) { - return new PageImpl>(Collections.>emptyList(), pageable, 0); + return new PageImpl>(Collections.> emptyList(), pageable, 0); } int upperBound = pageable.getOffset() + pageable.getPageSize(); @@ -145,7 +155,6 @@ public class EnversRevisionRepositoryImpl>(revisions.getContent(), pageable, revisionNumbers.size()); } - /** * Returns the entities in the given revisions for the entitiy with the given id. * @@ -179,15 +188,15 @@ public class EnversRevisionRepositoryImpl getEntityForRevision(N revisionNumber, ID id, AuditReader reader) { - Class revisionEntityClass = revisionEntityInformation.getRevisionEntityClass(); + Class type = revisionEntityInformation.getRevisionEntityClass(); - T revision = (T) reader.findRevision(revisionEntityClass, revisionNumber); + T revision = (T) reader.findRevision(type, revisionNumber); + Object entity = reader.find(entityInformation.getJavaType(), id, revisionNumber); - RevisionMetadata metadata = (RevisionMetadata) getRevisionMetadata(revision); - - return new Revision(metadata, revision); + return new Revision((RevisionMetadata) getRevisionMetadata(revision), (T) entity); } @SuppressWarnings("unchecked") diff --git a/src/test/java/org/springframework/data/envers/repository/support/RepositoryIntegrationTest.java b/src/test/java/org/springframework/data/envers/repository/support/RepositoryIntegrationTest.java index befd08a..128c544 100755 --- a/src/test/java/org/springframework/data/envers/repository/support/RepositoryIntegrationTest.java +++ b/src/test/java/org/springframework/data/envers/repository/support/RepositoryIntegrationTest.java @@ -20,8 +20,9 @@ import static org.junit.Assert.*; import java.util.Arrays; import java.util.HashSet; -import java.util.List; +import java.util.Iterator; +import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -50,8 +51,6 @@ public class RepositoryIntegrationTest { @Autowired LicenseRepository licenseRepository; @Autowired CountryRepository countryRepository; - @Autowired EnversRevisionRepository enversRevisionRepository; - @Before public void setUp() { licenseRepository.deleteAll(); @@ -93,22 +92,38 @@ public class RepositoryIntegrationTest { Page> revisions = licenseRepository.findRevisions(license.id, new PageRequest(0, 10)); Revisions wrapper = new Revisions(revisions.getContent()); assertThat(wrapper.getLatestRevision(), is(revision)); - - - List> revisionsDe = countryRepository.findRevisions(de.id).getContent(); - for (Revision revisionDe: revisionsDe) { - System.out.println("revisionDe.getRevisionNumber(): " + revisionDe.getRevisionNumber()); - System.out.println("revisionDe.getEntity().name: " + revisionDe.getEntity().name); - } - - Revision originalCountryRevision = enversRevisionRepository.findRevision(de.id, 2); - - Country originalCountry = originalCountryRevision.getEntity(); - assertThat(originalCountry.name, is("Deutschland")); } @Test public void returnsEmptyRevisionsForUnrevisionedEntity() { assertThat(countryRepository.findRevisions(100L).getContent(), is(hasSize(0))); } + + /** + * @see #31 + */ + @Test + public void returnsParticularRevisionForAnEntity() { + + Country de = new Country(); + de.code = "de"; + de.name = "Deutschland"; + + countryRepository.save(de); + + de.name = "Germany"; + + countryRepository.save(de); + + Revisions revisions = countryRepository.findRevisions(de.id); + + assertThat(revisions, is(Matchers.> iterableWithSize(2))); + + Iterator> iterator = revisions.iterator(); + Revision first = iterator.next(); + Revision second = iterator.next(); + + assertThat(countryRepository.findRevision(de.id, first.getRevisionNumber()).getEntity().name, is("Deutschland")); + assertThat(countryRepository.findRevision(de.id, second.getRevisionNumber()).getEntity().name, is("Germany")); + } }