Skip to content

Commit 2ecab81

Browse files
committed
Polishing.
Fix author tags. Reorder methods, add since tags. Extend reactive callbacks with configurable order. See #4914 Original pull request: #4968
1 parent b9de7b4 commit 2ecab81

File tree

8 files changed

+136
-38
lines changed

8 files changed

+136
-38
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AuditingEntityCallback.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* {@link EntityCallback} to populate auditing related fields on an entity about to be saved.
2828
*
2929
* @author Mark Paluch
30-
* @author yangchef1
30+
* @author HeeChul Yang
3131
* @since 2.2
3232
*/
3333
public class AuditingEntityCallback implements BeforeConvertCallback<Object>, Ordered {
@@ -47,17 +47,26 @@ public AuditingEntityCallback(ObjectFactory<IsNewAwareAuditingHandler> auditingH
4747
this.auditingHandlerFactory = auditingHandlerFactory;
4848
}
4949

50-
@Override
51-
public Object onBeforeConvert(Object entity, String collection) {
52-
return auditingHandlerFactory.getObject().markAudited(entity);
53-
}
54-
5550
@Override
5651
public int getOrder() {
5752
return this.order;
5853
}
5954

55+
/**
56+
* Specify the order value for this {@link BeforeConvertCallback}.
57+
* <p>
58+
* The default value is {@code 100}.
59+
*
60+
* @see org.springframework.core.Ordered#getOrder()
61+
* @since 5.0
62+
*/
6063
public void setOrder(int order) {
6164
this.order = order;
6265
}
66+
67+
@Override
68+
public Object onBeforeConvert(Object entity, String collection) {
69+
return auditingHandlerFactory.getObject().markAudited(entity);
70+
}
71+
6372
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/ReactiveAuditingEntityCallback.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
public class ReactiveAuditingEntityCallback implements ReactiveBeforeConvertCallback<Object>, Ordered {
3535

3636
private final ObjectFactory<ReactiveIsNewAwareAuditingHandler> auditingHandlerFactory;
37+
private int order = 100;
3738

3839
/**
3940
* Creates a new {@link ReactiveAuditingEntityCallback} using the given {@link MappingContext} and
@@ -48,12 +49,24 @@ public ReactiveAuditingEntityCallback(ObjectFactory<ReactiveIsNewAwareAuditingHa
4849
}
4950

5051
@Override
51-
public Publisher<Object> onBeforeConvert(Object entity, String collection) {
52-
return auditingHandlerFactory.getObject().markAudited(entity);
52+
public int getOrder() {
53+
return this.order;
54+
}
55+
56+
/**
57+
* Specify the order value for this {@link BeforeConvertCallback}.
58+
* <p>
59+
* The default value is {@code 100}.
60+
*
61+
* @see org.springframework.core.Ordered#getOrder()
62+
* @since 5.0
63+
*/
64+
public void setOrder(int order) {
65+
this.order = order;
5366
}
5467

5568
@Override
56-
public int getOrder() {
57-
return 100;
69+
public Publisher<Object> onBeforeConvert(Object entity, String collection) {
70+
return auditingHandlerFactory.getObject().markAudited(entity);
5871
}
5972
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/ReactiveValidatingEntityCallback.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
public class ReactiveValidatingEntityCallback implements ReactiveBeforeSaveCallback<Object>, Ordered {
4040

4141
private final BeanValidationDelegate delegate;
42+
private int order = 100;
4243

4344
/**
4445
* Creates a new {@link ReactiveValidatingEntityCallback} using the given {@link Validator}.
@@ -49,6 +50,23 @@ public ReactiveValidatingEntityCallback(Validator validator) {
4950
this.delegate = new BeanValidationDelegate(validator);
5051
}
5152

53+
@Override
54+
public int getOrder() {
55+
return this.order;
56+
}
57+
58+
/**
59+
* Specify the order value for this {@link ReactiveValidatingEntityCallback}.
60+
* <p>
61+
* The default value is {@code 100}.
62+
*
63+
* @see org.springframework.core.Ordered#getOrder()
64+
* @since 5.0
65+
*/
66+
public void setOrder(int order) {
67+
this.order = order;
68+
}
69+
5270
@Override
5371
public Mono<Object> onBeforeSave(Object entity, Document document, String collection) {
5472

@@ -60,10 +78,4 @@ public Mono<Object> onBeforeSave(Object entity, Document document, String collec
6078

6179
return Mono.just(entity);
6280
}
63-
64-
@Override
65-
public int getOrder() {
66-
return 100;
67-
}
68-
6981
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/ValidatingEntityCallback.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*
3434
* @author Rene Felgenträger
3535
* @author Mark Paluch
36-
* @author yangchef1
36+
* @author HeeChul Yang
3737
* @since 4.5
3838
*/
3939
public class ValidatingEntityCallback implements BeforeSaveCallback<Object>, Ordered {
@@ -50,6 +50,23 @@ public ValidatingEntityCallback(Validator validator) {
5050
this.delegate = new BeanValidationDelegate(validator);
5151
}
5252

53+
@Override
54+
public int getOrder() {
55+
return this.order;
56+
}
57+
58+
/**
59+
* Specify the order value for this {@link BeforeConvertCallback}.
60+
* <p>
61+
* The default value is {@code 100}.
62+
*
63+
* @see org.springframework.core.Ordered#getOrder()
64+
* @since 5.0
65+
*/
66+
public void setOrder(int order) {
67+
this.order = order;
68+
}
69+
5370
@Override
5471
public Object onBeforeSave(Object entity, Document document, String collection) {
5572

@@ -61,13 +78,4 @@ public Object onBeforeSave(Object entity, Document document, String collection)
6178

6279
return entity;
6380
}
64-
65-
@Override
66-
public int getOrder() {
67-
return this.order;
68-
}
69-
70-
public void setOrder(int order) {
71-
this.order = order;
72-
}
7381
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AuditingEntityCallbackUnitTests.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.junit.jupiter.api.Test;
2828
import org.junit.jupiter.api.extension.ExtendWith;
2929
import org.mockito.junit.jupiter.MockitoExtension;
30-
import org.springframework.core.Ordered;
30+
3131
import org.springframework.data.annotation.CreatedDate;
3232
import org.springframework.data.annotation.Id;
3333
import org.springframework.data.annotation.LastModifiedDate;
@@ -40,10 +40,10 @@
4040
* Unit tests for {@link AuditingEntityCallback}.
4141
*
4242
* @author Mark Paluch
43-
* @author yangchef1
43+
* @author HeeChul Yang
4444
*/
4545
@ExtendWith(MockitoExtension.class)
46-
public class AuditingEntityCallbackUnitTests {
46+
class AuditingEntityCallbackUnitTests {
4747

4848
private final MongoMappingContext mappingContext = new MongoMappingContext();
4949

@@ -88,17 +88,12 @@ void triggersModificationMarkForObjectWithSetId() {
8888

8989
@Test // DATAMONGO-2261
9090
void hasExplicitOrder() {
91-
92-
assertThat(callback).isInstanceOf(Ordered.class);
9391
assertThat(callback.getOrder()).isEqualTo(100);
9492
}
9593

9694
@Test // GH-4914
9795
void allowsChangingOrderDynamically() {
9896

99-
assertThat(callback).isInstanceOf(Ordered.class);
100-
assertThat(callback.getOrder()).isEqualTo(100);
101-
10297
callback.setOrder(50);
10398
assertThat(callback.getOrder()).isEqualTo(50);
10499
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2025 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.mongodb.core.mapping.event;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.data.auditing.ReactiveIsNewAwareAuditingHandler;
25+
import org.springframework.data.mapping.context.PersistentEntities;
26+
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
27+
28+
/**
29+
* Unit tests for {@link ReactiveAuditingEntityCallback}.
30+
*
31+
* @author Mark Paluch
32+
*/
33+
class ReactiveAuditingEntityCallbackUnitTests {
34+
35+
private final MongoMappingContext mappingContext = new MongoMappingContext();
36+
37+
private ReactiveIsNewAwareAuditingHandler handler;
38+
private ReactiveAuditingEntityCallback callback;
39+
40+
@BeforeEach
41+
void setUp() {
42+
43+
mappingContext.getPersistentEntity(AuditingEntityCallbackUnitTests.Sample.class);
44+
45+
handler = spy(new ReactiveIsNewAwareAuditingHandler(PersistentEntities.of(mappingContext)));
46+
47+
callback = new ReactiveAuditingEntityCallback(() -> handler);
48+
}
49+
50+
@Test // GH-4914
51+
void allowsChangingOrderDynamically() {
52+
53+
callback.setOrder(50);
54+
assertThat(callback.getOrder()).isEqualTo(50);
55+
}
56+
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ReactiveValidatingEntityCallbackUnitTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.mongodb.core.mapping.event;
1717

18+
import static org.assertj.core.api.Assertions.*;
19+
1820
import jakarta.validation.ConstraintViolationException;
1921
import jakarta.validation.Validation;
2022
import jakarta.validation.ValidatorFactory;
@@ -64,6 +66,13 @@ void validateSuccessful() {
6466
.verifyComplete();
6567
}
6668

69+
@Test // GH-4914
70+
void allowsChangingOrderDynamically() {
71+
72+
callback.setOrder(50);
73+
assertThat(callback.getOrder()).isEqualTo(50);
74+
}
75+
6776
record Coordinates(@NotNull @Min(0) Integer x, @NotNull @Min(0) Integer y) {
6877

6978
Document toDocument() {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/ValidatingEntityCallbackUnitTests.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.bson.Document;
2727
import org.junit.jupiter.api.BeforeEach;
2828
import org.junit.jupiter.api.Test;
29-
import org.springframework.core.Ordered;
3029

3130
/**
3231
* Unit tests for {@link ValidatingEntityCallback}.
@@ -68,9 +67,6 @@ void validateSuccessful() {
6867
@Test // GH-4914
6968
void allowsChangingOrderDynamically() {
7069

71-
assertThat(callback).isInstanceOf(Ordered.class);
72-
assertThat(callback.getOrder()).isEqualTo(100);
73-
7470
callback.setOrder(50);
7571
assertThat(callback.getOrder()).isEqualTo(50);
7672
}

0 commit comments

Comments
 (0)