Add tests to verify AuditingEntityListener works with embeddables.

See #2365.
This commit is contained in:
Greg L. Turnquist
2022-03-18 16:24:15 -05:00
parent 15e8e3bfe8
commit ef13a0d251
7 changed files with 267 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2008-2022 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
*
* https://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.domain.sample;
import jakarta.persistence.Embeddable;
import java.time.Instant;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
/**
* JPA {@link Embeddable} to test out {@link org.springframework.data.jpa.domain.support.AuditingEntityListener}.
*
* @author Greg Turnquist
*/
@Embeddable
public class AuditableEmbeddable {
@CreatedDate //
private Instant dateCreated;
@LastModifiedDate //
private Instant dateUpdated;
public Instant getDateCreated() {
return dateCreated;
}
public void setDateCreated(Instant dateCreated) {
this.dateCreated = dateCreated;
}
public Instant getDateUpdated() {
return dateUpdated;
}
public void setDateUpdated(Instant dateUpdated) {
this.dateUpdated = dateUpdated;
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2008-2022 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
*
* https://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.domain.sample;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
/**
* JPA entity with an {@link Embedded} set of auditable data.
*
* @author Greg Turnquist
*/
@Entity
@EntityListeners(AuditingEntityListener.class)
public class AuditableEntity {
@Id
@GeneratedValue //
private Long id;
private String data;
@Embedded //
private AuditableEmbeddable auditDetails;
public AuditableEntity() {
this(null, null, null);
}
public AuditableEntity(Long id, String data, AuditableEmbeddable auditDetails) {
this.id = id;
this.data = data;
this.auditDetails = auditDetails;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public AuditableEmbeddable getAuditDetails() {
return auditDetails;
}
public void setAuditDetails(AuditableEmbeddable auditDetails) {
this.auditDetails = auditDetails;
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2008-2022 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
*
* https://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.domain.support;
import static org.assertj.core.api.Assertions.*;
import java.time.Instant;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.sample.AuditableEmbeddable;
import org.springframework.data.jpa.domain.sample.AuditableEntity;
import org.springframework.data.jpa.repository.sample.AuditableEntityRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* Integration test for {@link AuditingEntityListener}.
*
* @author Greg Turnquist
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:auditing/auditing-entity-with-embeddable-listener.xml")
public class AuditingEntityWithEmbeddableListenerTests {
@Autowired AuditableEntityRepository repository;
private AuditableEntity entity;
private AuditableEmbeddable auditDetails;
@BeforeEach
void setUp() {
entity = new AuditableEntity();
entity.setId(1L);
entity.setData("original value");
auditDetails = new AuditableEmbeddable();
entity.setAuditDetails(auditDetails);
}
@Test
void auditsEmbeddedCorrectly() {
// when
repository.saveAndFlush(entity);
Optional<AuditableEntity> optionalEntity = repository.findById(1L);
// then
assertThat(optionalEntity).isNotEmpty();
AuditableEntity auditableEntity = optionalEntity.get();
assertThat(auditableEntity.getData()).isEqualTo("original value");
assertThat(auditableEntity.getAuditDetails().getDateCreated()).isNotNull();
assertThat(auditableEntity.getAuditDetails().getDateUpdated()).isNotNull();
Instant originalCreationDate = auditableEntity.getAuditDetails().getDateCreated();
Instant originalDateUpdated = auditableEntity.getAuditDetails().getDateUpdated();
auditableEntity.setData("updated value");
repository.saveAndFlush(auditableEntity);
Optional<AuditableEntity> optionalRevisedEntity = repository.findById(1L);
assertThat(optionalRevisedEntity).isNotEmpty();
AuditableEntity revisedEntity = optionalRevisedEntity.get();
assertThat(revisedEntity.getData()).isEqualTo("updated value");
assertThat(revisedEntity.getAuditDetails().getDateCreated()).isEqualTo(originalCreationDate);
assertThat(revisedEntity.getAuditDetails().getDateUpdated()).isAfter(originalDateUpdated);
}
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2008-2022 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
*
* https://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.sample;
import org.springframework.data.jpa.domain.sample.AuditableEntity;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* {@link JpaRepository} to test {@link org.springframework.data.jpa.domain.support.AuditingEntityListener}.
*/
public interface AuditableEntityRepository extends JpaRepository<AuditableEntity, Long> {}

View File

@@ -10,6 +10,8 @@
<class>org.springframework.data.jpa.domain.sample.AnnotatedAuditableUser</class>
<class>org.springframework.data.jpa.domain.sample.AuditableRole</class>
<class>org.springframework.data.jpa.domain.sample.AuditableUser</class>
<class>org.springframework.data.jpa.domain.sample.AuditableEntity</class>
<class>org.springframework.data.jpa.domain.sample.AuditableEmbeddable</class>
<class>org.springframework.data.jpa.domain.sample.Category</class>
<class>org.springframework.data.jpa.domain.sample.Child</class>
<class>org.springframework.data.jpa.domain.sample.ConcreteType1</class>

View File

@@ -6,6 +6,8 @@
<class>org.springframework.data.jpa.domain.sample.AnnotatedAuditableUser</class>
<class>org.springframework.data.jpa.domain.sample.AuditableRole</class>
<class>org.springframework.data.jpa.domain.sample.AuditableUser</class>
<class>org.springframework.data.jpa.domain.sample.AuditableEntity</class>
<class>org.springframework.data.jpa.domain.sample.AuditableEmbeddable</class>
<class>org.springframework.data.jpa.domain.sample.Category</class>
<class>org.springframework.data.jpa.domain.sample.CustomAbstractPersistable</class>
<class>org.springframework.data.jpa.domain.sample.EntityWithAssignedId</class>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa https://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<import resource="../infrastructure.xml"/>
<jpa:auditing/>
<jpa:repositories base-package="org.springframework.data.jpa.repository.sample"/>
</beans>