Skip to content

Commit b0edbf0

Browse files
committed
Add tests to verify AuditingEntityListener works with embeddables.
See #2365.
1 parent 9be2915 commit b0edbf0

File tree

7 files changed

+267
-0
lines changed

7 files changed

+267
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2008-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.domain.sample;
17+
18+
import java.time.Instant;
19+
20+
import javax.persistence.Embeddable;
21+
22+
import org.springframework.data.annotation.CreatedDate;
23+
import org.springframework.data.annotation.LastModifiedDate;
24+
25+
/**
26+
* JPA {@link Embeddable} to test out {@link org.springframework.data.jpa.domain.support.AuditingEntityListener}.
27+
*
28+
* @author Greg Turnquist
29+
*/
30+
@Embeddable
31+
public class AuditableEmbeddable {
32+
33+
@CreatedDate //
34+
private Instant dateCreated;
35+
36+
@LastModifiedDate //
37+
private Instant dateUpdated;
38+
39+
public Instant getDateCreated() {
40+
return dateCreated;
41+
}
42+
43+
public void setDateCreated(Instant dateCreated) {
44+
this.dateCreated = dateCreated;
45+
}
46+
47+
public Instant getDateUpdated() {
48+
return dateUpdated;
49+
}
50+
51+
public void setDateUpdated(Instant dateUpdated) {
52+
this.dateUpdated = dateUpdated;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2008-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.domain.sample;
17+
18+
import javax.persistence.Embedded;
19+
import javax.persistence.Entity;
20+
import javax.persistence.EntityListeners;
21+
import javax.persistence.GeneratedValue;
22+
import javax.persistence.Id;
23+
24+
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
25+
26+
/**
27+
* JPA entity with an {@link Embedded} set of auditable data.
28+
*
29+
* @author Greg Turnquist
30+
*/
31+
@Entity
32+
@EntityListeners(AuditingEntityListener.class)
33+
public class AuditableEntity {
34+
35+
@Id
36+
@GeneratedValue //
37+
private Long id;
38+
39+
private String data;
40+
41+
@Embedded //
42+
private AuditableEmbeddable auditDetails;
43+
44+
public AuditableEntity() {
45+
this(null, null, null);
46+
}
47+
48+
public AuditableEntity(Long id, String data, AuditableEmbeddable auditDetails) {
49+
50+
this.id = id;
51+
this.data = data;
52+
this.auditDetails = auditDetails;
53+
}
54+
55+
public Long getId() {
56+
return id;
57+
}
58+
59+
public void setId(Long id) {
60+
this.id = id;
61+
}
62+
63+
public String getData() {
64+
return data;
65+
}
66+
67+
public void setData(String data) {
68+
this.data = data;
69+
}
70+
71+
public AuditableEmbeddable getAuditDetails() {
72+
return auditDetails;
73+
}
74+
75+
public void setAuditDetails(AuditableEmbeddable auditDetails) {
76+
this.auditDetails = auditDetails;
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2008-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.domain.support;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.time.Instant;
21+
import java.util.Optional;
22+
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.extension.ExtendWith;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.data.jpa.domain.sample.AuditableEmbeddable;
28+
import org.springframework.data.jpa.domain.sample.AuditableEntity;
29+
import org.springframework.data.jpa.repository.sample.AuditableEntityRepository;
30+
import org.springframework.test.context.ContextConfiguration;
31+
import org.springframework.test.context.junit.jupiter.SpringExtension;
32+
33+
/**
34+
* Integration test for {@link AuditingEntityListener}.
35+
*
36+
* @author Greg Turnquist
37+
*/
38+
@ExtendWith(SpringExtension.class)
39+
@ContextConfiguration("classpath:auditing/auditing-entity-with-embeddable-listener.xml")
40+
public class AuditingEntityWithEmbeddableListenerTests {
41+
42+
@Autowired AuditableEntityRepository repository;
43+
44+
private AuditableEntity entity;
45+
private AuditableEmbeddable auditDetails;
46+
47+
@BeforeEach
48+
void setUp() {
49+
50+
entity = new AuditableEntity();
51+
entity.setId(1L);
52+
entity.setData("original value");
53+
54+
auditDetails = new AuditableEmbeddable();
55+
entity.setAuditDetails(auditDetails);
56+
}
57+
58+
@Test
59+
void auditsEmbeddedCorrectly() {
60+
61+
// when
62+
repository.saveAndFlush(entity);
63+
Optional<AuditableEntity> optionalEntity = repository.findById(1L);
64+
65+
// then
66+
assertThat(optionalEntity).isNotEmpty();
67+
68+
AuditableEntity auditableEntity = optionalEntity.get();
69+
assertThat(auditableEntity.getData()).isEqualTo("original value");
70+
71+
assertThat(auditableEntity.getAuditDetails().getDateCreated()).isNotNull();
72+
assertThat(auditableEntity.getAuditDetails().getDateUpdated()).isNotNull();
73+
74+
Instant originalCreationDate = auditableEntity.getAuditDetails().getDateCreated();
75+
Instant originalDateUpdated = auditableEntity.getAuditDetails().getDateUpdated();
76+
77+
auditableEntity.setData("updated value");
78+
79+
repository.saveAndFlush(auditableEntity);
80+
81+
Optional<AuditableEntity> optionalRevisedEntity = repository.findById(1L);
82+
83+
assertThat(optionalRevisedEntity).isNotEmpty();
84+
85+
AuditableEntity revisedEntity = optionalRevisedEntity.get();
86+
assertThat(revisedEntity.getData()).isEqualTo("updated value");
87+
88+
assertThat(revisedEntity.getAuditDetails().getDateCreated()).isEqualTo(originalCreationDate);
89+
assertThat(revisedEntity.getAuditDetails().getDateUpdated()).isAfter(originalDateUpdated);
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2008-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.repository.sample;
17+
18+
import org.springframework.data.jpa.domain.sample.AuditableEntity;
19+
import org.springframework.data.jpa.repository.JpaRepository;
20+
21+
/**
22+
* {@link JpaRepository} to test {@link org.springframework.data.jpa.domain.support.AuditingEntityListener}.
23+
*/
24+
public interface AuditableEntityRepository extends JpaRepository<AuditableEntity, Long> {}

src/test/resources/META-INF/persistence.xml

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<class>org.springframework.data.jpa.domain.sample.AnnotatedAuditableUser</class>
1111
<class>org.springframework.data.jpa.domain.sample.AuditableRole</class>
1212
<class>org.springframework.data.jpa.domain.sample.AuditableUser</class>
13+
<class>org.springframework.data.jpa.domain.sample.AuditableEntity</class>
14+
<class>org.springframework.data.jpa.domain.sample.AuditableEmbeddable</class>
1315
<class>org.springframework.data.jpa.domain.sample.Category</class>
1416
<class>org.springframework.data.jpa.domain.sample.Child</class>
1517
<class>org.springframework.data.jpa.domain.sample.ConcreteType1</class>

src/test/resources/META-INF/persistence2.xml

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<class>org.springframework.data.jpa.domain.sample.AnnotatedAuditableUser</class>
77
<class>org.springframework.data.jpa.domain.sample.AuditableRole</class>
88
<class>org.springframework.data.jpa.domain.sample.AuditableUser</class>
9+
<class>org.springframework.data.jpa.domain.sample.AuditableEntity</class>
10+
<class>org.springframework.data.jpa.domain.sample.AuditableEmbeddable</class>
911
<class>org.springframework.data.jpa.domain.sample.Category</class>
1012
<class>org.springframework.data.jpa.domain.sample.CustomAbstractPersistable</class>
1113
<class>org.springframework.data.jpa.domain.sample.EntityWithAssignedId</class>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
7+
http://www.springframework.org/schema/data/jpa https://www.springframework.org/schema/data/jpa/spring-jpa.xsd
8+
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
9+
10+
<import resource="../infrastructure.xml"/>
11+
12+
<jpa:auditing/>
13+
14+
<jpa:repositories base-package="org.springframework.data.jpa.repository.sample"/>
15+
16+
</beans>

0 commit comments

Comments
 (0)