Browse Source

Properly emit domain events from calls to saveAll(…).

We now treat CrudRepository.saveAll(…) properly by unwrapping the given *Iterable*. This previously already worked for collections handed into the method but not for types only implementing Iterable directly (like Page or Window).

Fixes #3153.
Related tickets #2931, #2927.
3.0.x
Oliver Drotbohm 2 years ago
parent
commit
880b141b51
  1. 33
      src/main/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessor.java
  2. 15
      src/test/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessorUnitTests.java

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

@ -54,7 +54,15 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr @@ -54,7 +54,15 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr
private final ApplicationEventPublisher publisher;
/**
* Creates a new {@link EventPublishingRepositoryProxyPostProcessor} for the given {@link ApplicationEventPublisher}.
*
* @param publisher must not be {@literal null}.
*/
public EventPublishingRepositoryProxyPostProcessor(ApplicationEventPublisher publisher) {
Assert.notNull(publisher, "Object must not be null");
this.publisher = publisher;
}
@ -103,9 +111,9 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr @@ -103,9 +111,9 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr
return result;
}
Object[] arguments = invocation.getArguments();
Iterable<?> arguments = asCollection(invocation.getArguments()[0], invocation.getMethod());
eventMethod.publishEventsFrom(arguments[0], publisher);
eventMethod.publishEventsFrom(arguments, publisher);
return result;
}
@ -177,22 +185,18 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr @@ -177,22 +185,18 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr
/**
* Publishes all events in the given aggregate root using the given {@link ApplicationEventPublisher}.
*
* @param object can be {@literal null}.
* @param aggregates can be {@literal null}.
* @param publisher must not be {@literal null}.
*/
public void publishEventsFrom(@Nullable Object object, ApplicationEventPublisher publisher) {
if (object == null) {
return;
}
public void publishEventsFrom(Iterable<?> aggregates, ApplicationEventPublisher publisher) {
for (Object aggregateRoot : asCollection(object)) {
for (Object aggregateRoot : aggregates) {
if (!type.isInstance(aggregateRoot)) {
continue;
}
for (Object event : asCollection(ReflectionUtils.invokeMethod(publishingMethod, aggregateRoot))) {
for (Object event : asCollection(ReflectionUtils.invokeMethod(publishingMethod, aggregateRoot), null)) {
publisher.publishEvent(event);
}
@ -261,6 +265,8 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr @@ -261,6 +265,8 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr
return method;
}
}
/**
* Returns the given source object as collection, i.e. collections are returned as is, objects are turned into a
* one-element collection, {@literal null} will become an empty collection.
@ -269,12 +275,16 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr @@ -269,12 +275,16 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr
* @return
*/
@SuppressWarnings("unchecked")
private static Collection<Object> asCollection(@Nullable Object source) {
private static Iterable<Object> asCollection(@Nullable Object source, @Nullable Method method) {
if (source == null) {
return Collections.emptyList();
}
if (method != null && method.getName().startsWith("saveAll")) {
return (Iterable<Object>) source;
}
if (Collection.class.isInstance(source)) {
return (Collection<Object>) source;
}
@ -282,4 +292,3 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr @@ -282,4 +292,3 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr
return Collections.singletonList(source);
}
}
}

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

@ -69,11 +69,6 @@ class EventPublishingRepositoryProxyPostProcessorUnitTests { @@ -69,11 +69,6 @@ class EventPublishingRepositoryProxyPostProcessorUnitTests {
assertThatIllegalArgumentException().isThrownBy(() -> EventPublishingMethod.of(null));
}
@Test // DATACMNS-928
void publishingEventsForNullIsNoOp() {
EventPublishingMethod.of(OneEvent.class).publishEventsFrom(null, publisher);
}
@Test // DATACMNS-928
void exposesEventsExposedByEntityToPublisher() {
@ -81,7 +76,7 @@ class EventPublishingRepositoryProxyPostProcessorUnitTests { @@ -81,7 +76,7 @@ class EventPublishingRepositoryProxyPostProcessorUnitTests {
var second = new SomeEvent();
var entity = MultipleEvents.of(Arrays.asList(first, second));
EventPublishingMethod.of(MultipleEvents.class).publishEventsFrom(entity, publisher);
EventPublishingMethod.of(MultipleEvents.class).publishEventsFrom(List.of(entity), publisher);
verify(publisher).publishEvent(eq(first));
verify(publisher).publishEvent(eq(second));
@ -93,7 +88,7 @@ class EventPublishingRepositoryProxyPostProcessorUnitTests { @@ -93,7 +88,7 @@ class EventPublishingRepositoryProxyPostProcessorUnitTests {
var event = new SomeEvent();
var entity = OneEvent.of(event);
EventPublishingMethod.of(OneEvent.class).publishEventsFrom(entity, publisher);
EventPublishingMethod.of(OneEvent.class).publishEventsFrom(List.of(entity), publisher);
verify(publisher, times(1)).publishEvent(event);
}
@ -103,7 +98,7 @@ class EventPublishingRepositoryProxyPostProcessorUnitTests { @@ -103,7 +98,7 @@ class EventPublishingRepositoryProxyPostProcessorUnitTests {
var entity = OneEvent.of(null);
EventPublishingMethod.of(OneEvent.class).publishEventsFrom(entity, publisher);
EventPublishingMethod.of(OneEvent.class).publishEventsFrom(List.of(entity), publisher);
verify(publisher, times(0)).publishEvent(any());
}
@ -279,7 +274,7 @@ class EventPublishingRepositoryProxyPostProcessorUnitTests { @@ -279,7 +274,7 @@ class EventPublishingRepositoryProxyPostProcessorUnitTests {
var entity = spy(EventsWithClearing.of(Collections.emptyList()));
EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entity, publisher);
EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(List.of(entity), publisher);
verify(entity, times(1)).clearDomainEvents();
}
@ -289,7 +284,7 @@ class EventPublishingRepositoryProxyPostProcessorUnitTests { @@ -289,7 +284,7 @@ class EventPublishingRepositoryProxyPostProcessorUnitTests {
var entity = spy(EventsWithClearing.of(Collections.singletonList(new SomeEvent())));
EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entity, publisher);
EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(List.of(entity), publisher);
verify(entity, times(1)).clearDomainEvents();
}

Loading…
Cancel
Save