Skip to content

Treat Page and Window as Collection for event publishing #2931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -28,6 +28,8 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.AfterDomainEventPublication;
import org.springframework.data.domain.DomainEvents;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Window;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.util.AnnotationDetectionMethodCallback;
@@ -47,6 +49,7 @@
* @author Christoph Strobl
* @author Yuki Yoshida
* @author Réda Housni Alaoui
* @author Yanming Zhou
* @since 1.13
* @soundtrack Henrik Freischlader Trio - Master Plan (Openness)
*/
@@ -268,15 +271,23 @@ private static Method getClearingMethod(AnnotationDetectionMethodCallback<?> cle
* @param source can be {@literal null}.
* @return
*/
@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "rawtypes"})
private static Collection<Object> asCollection(@Nullable Object source) {

if (source == null) {
return Collections.emptyList();
}

if (Collection.class.isInstance(source)) {
return (Collection<Object>) source;
if (source instanceof Collection collection) {
return collection;
}

if (source instanceof Window window) {
return window.toList();
}

if (source instanceof Page page) {
return page.toList();
}

return Collections.singletonList(source);
Original file line number Diff line number Diff line change
@@ -38,6 +38,12 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.AfterDomainEventPublication;
import org.springframework.data.domain.DomainEvents;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Window;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.support.EventPublishingRepositoryProxyPostProcessor.EventPublishingMethod;
@@ -50,6 +56,7 @@
* @author Mark Paluch
* @author Yuki Yoshida
* @author Réda Housni Alaoui
* @author Yanming Zhou
* @soundtrack Henrik Freischlader Trio - Nobody Else To Blame (Openness)
*/
@ExtendWith(MockitoExtension.class)
@@ -199,6 +206,36 @@ void publishesEventsForCallToSaveWithIterable() throws Throwable {
verify(publisher).publishEvent(any(SomeEvent.class));
}

@Test
void publishesEventsForCallToSaveWithIterableAndWindowAsParameter() throws Throwable {

var event = new SomeEvent();
var sample = MultipleEvents.of(Collections.singletonList(event));
Window<MultipleEvents> window = Window.from(List.of(sample), ScrollPosition::offset);
mockInvocation(invocation, SampleRepository.class.getMethod("saveAll", Iterable.class), window);

EventPublishingMethodInterceptor//
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
.invoke(invocation);

verify(publisher).publishEvent(any(SomeEvent.class));
}

@Test
void publishesEventsForCallToSaveWithIterableAndPageAsParameter() throws Throwable {

var event = new SomeEvent();
var sample = MultipleEvents.of(Collections.singletonList(event));
Page<MultipleEvents> page = new PageImpl<>(List.of(sample), Pageable.ofSize(10), 1);
mockInvocation(invocation, SampleRepository.class.getMethod("saveAll", Iterable.class), page);

EventPublishingMethodInterceptor//
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
.invoke(invocation);

verify(publisher).publishEvent(any(SomeEvent.class));
}

@Test // DATACMNS-1663
void publishesEventsForCallToDeleteWithIterable() throws Throwable {