-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to verify AuditingEntityListener works with embeddables.
See #2365.
- Loading branch information
Showing
7 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...ata-jpa/src/test/java/org/springframework/data/jpa/domain/sample/AuditableEmbeddable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...ng-data-jpa/src/test/java/org/springframework/data/jpa/domain/sample/AuditableEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...rg/springframework/data/jpa/domain/support/AuditingEntityWithEmbeddableListenerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...c/test/java/org/springframework/data/jpa/repository/sample/AuditableEntityRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
spring-data-jpa/src/test/resources/auditing/auditing-entity-with-embeddable-listener.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |