Skip to content

Commit f558a6c

Browse files
committed
DATACMNS-1067 - Polishing.
Revert change to only invoke cleanup method if events have been exposed. We now again invoke the cleanup method for every aggregate. Changed the publication of events from the aggregate instances that were handed into the method to the ones the save method returns as the save call might return different object instances. Cleanups in the unit tests. Moved newly introduced methods to the bottom of the test case class. Extracted method to set up mock method invocation. Original pull request: #216.
1 parent dcb1ee6 commit f558a6c

File tree

2 files changed

+51
-52
lines changed

2 files changed

+51
-52
lines changed

src/main/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessor.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
9595
return result;
9696
}
9797

98-
for (Object argument : invocation.getArguments()) {
99-
eventMethod.publishEventsFrom(argument, publisher);
100-
}
98+
eventMethod.publishEventsFrom(result, publisher);
10199

102100
return result;
103101
}
@@ -166,12 +164,12 @@ public void publishEventsFrom(Object object, ApplicationEventPublisher publisher
166164
}
167165

168166
for (Object aggregateRoot : asCollection(object)) {
169-
Collection<Object> events = asCollection(ReflectionUtils.invokeMethod(publishingMethod, aggregateRoot));
170-
for (Object event : events) {
167+
168+
for (Object event : asCollection(ReflectionUtils.invokeMethod(publishingMethod, aggregateRoot))) {
171169
publisher.publishEvent(event);
172170
}
173171

174-
if (clearingMethod != null && !events.isEmpty()) {
172+
if (clearingMethod != null) {
175173
ReflectionUtils.invokeMethod(clearingMethod, aggregateRoot);
176174
}
177175
}

src/test/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessorUnitTests.java

+47-46
Original file line numberDiff line numberDiff line change
@@ -105,40 +105,6 @@ public void doesNotExposeNullEvent() {
105105
verify(publisher, times(0)).publishEvent(any());
106106
}
107107

108-
@Test // DATACMNS-1067
109-
public void clearEventsDoesNotExposedByEntity() {
110-
111-
EventsWithClearing entity = spy(EventsWithClearing.of(Collections.emptyList()));
112-
113-
EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entity, publisher);
114-
115-
verify(entity, times(0)).clearDomainEvents();
116-
}
117-
118-
@Test // DATACMNS-1067
119-
public void clearEventsExposedByEntity() {
120-
121-
EventsWithClearing entity = spy(EventsWithClearing.of(Collections.singletonList(new SomeEvent())));
122-
123-
EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entity, publisher);
124-
125-
verify(entity, times(1)).clearDomainEvents();
126-
}
127-
128-
@Test // DATACMNS-1067
129-
public void clearEventsExposedByEntities() {
130-
131-
EventsWithClearing firstEntity = spy(EventsWithClearing.of(Collections.emptyList()));
132-
EventsWithClearing secondEntity = spy(EventsWithClearing.of(Collections.singletonList(new SomeEvent())));
133-
134-
Collection<EventsWithClearing> entities = Arrays.asList(firstEntity, secondEntity);
135-
136-
EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entities, publisher);
137-
138-
verify(firstEntity, times(0)).clearDomainEvents();
139-
verify(secondEntity, times(1)).clearDomainEvents();
140-
}
141-
142108
@Test // DATACMNS-928
143109
public void doesNotCreatePublishingMethodIfNoAnnotationDetected() {
144110
assertThat(EventPublishingMethod.of(Object.class), is(nullValue()));
@@ -147,11 +113,9 @@ public void doesNotCreatePublishingMethodIfNoAnnotationDetected() {
147113
@Test // DATACMNS-928
148114
public void interceptsSaveMethod() throws Throwable {
149115

150-
doReturn(SampleRepository.class.getMethod("save", Object.class)).when(invocation).getMethod();
151-
152116
SomeEvent event = new SomeEvent();
153-
MultipleEvents sample = MultipleEvents.of(Arrays.asList(event));
154-
doReturn(new Object[] { sample }).when(invocation).getArguments();
117+
MultipleEvents sample = MultipleEvents.of(Collections.singletonList(event));
118+
mockInvocation(invocation, SampleRepository.class.getMethod("save", Object.class), sample);
155119

156120
EventPublishingMethodInterceptor//
157121
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
@@ -202,10 +166,8 @@ public void doesNotAddAdviceIfDomainTypeDoesNotExposeEvents() {
202166
public void publishesEventsForCallToSaveWithIterable() throws Throwable {
203167

204168
SomeEvent event = new SomeEvent();
205-
MultipleEvents sample = MultipleEvents.of(Arrays.asList(event));
206-
doReturn(new Object[] { Arrays.asList(sample) }).when(invocation).getArguments();
207-
208-
doReturn(SampleRepository.class.getMethod("save", Iterable.class)).when(invocation).getMethod();
169+
MultipleEvents sample = MultipleEvents.of(Collections.singletonList(event));
170+
mockInvocation(invocation, SampleRepository.class.getMethod("save", Iterable.class), sample);
209171

210172
EventPublishingMethodInterceptor//
211173
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
@@ -234,12 +196,9 @@ public void publishesEventsAfterSaveInvocation() throws Throwable {
234196
@Test // DATACMNS-1113
235197
public void invokesEventsForMethodsThatStartsWithSave() throws Throwable {
236198

237-
Method method = SampleRepository.class.getMethod("saveAndFlush", MultipleEvents.class);
238-
doReturn(method).when(invocation).getMethod();
239-
240199
SomeEvent event = new SomeEvent();
241200
MultipleEvents sample = MultipleEvents.of(Collections.singletonList(event));
242-
doReturn(new Object[] { sample }).when(invocation).getArguments();
201+
mockInvocation(invocation, SampleRepository.class.getMethod("saveAndFlush", MultipleEvents.class), sample);
243202

244203
EventPublishingMethodInterceptor//
245204
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
@@ -248,6 +207,48 @@ public void invokesEventsForMethodsThatStartsWithSave() throws Throwable {
248207
verify(publisher).publishEvent(event);
249208
}
250209

210+
@Test // DATACMNS-1067
211+
public void clearsEventsEvenIfNoneWereExposedToPublish() {
212+
213+
EventsWithClearing entity = spy(EventsWithClearing.of(Collections.emptyList()));
214+
215+
EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entity, publisher);
216+
217+
verify(entity, times(1)).clearDomainEvents();
218+
}
219+
220+
@Test // DATACMNS-1067
221+
public void clearsEventsIfThereWereSomeToBePublished() {
222+
223+
EventsWithClearing entity = spy(EventsWithClearing.of(Collections.singletonList(new SomeEvent())));
224+
225+
EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entity, publisher);
226+
227+
verify(entity, times(1)).clearDomainEvents();
228+
}
229+
230+
@Test // DATACMNS-1067
231+
public void clearsEventsForOperationOnMutlipleAggregates() {
232+
233+
EventsWithClearing firstEntity = spy(EventsWithClearing.of(Collections.emptyList()));
234+
EventsWithClearing secondEntity = spy(EventsWithClearing.of(Collections.singletonList(new SomeEvent())));
235+
236+
Collection<EventsWithClearing> entities = Arrays.asList(firstEntity, secondEntity);
237+
238+
EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entities, publisher);
239+
240+
verify(firstEntity, times(1)).clearDomainEvents();
241+
verify(secondEntity, times(1)).clearDomainEvents();
242+
}
243+
244+
private static void mockInvocation(MethodInvocation invocation, Method method, Object parameterAndReturnValue)
245+
throws Throwable {
246+
247+
doReturn(method).when(invocation).getMethod();
248+
doReturn(new Object[] { parameterAndReturnValue }).when(invocation).getArguments();
249+
doReturn(parameterAndReturnValue).when(invocation).proceed();
250+
}
251+
251252
@Value(staticConstructor = "of")
252253
static class MultipleEvents {
253254
@Getter(onMethod = @__(@DomainEvents)) Collection<? extends Object> events;

0 commit comments

Comments
 (0)