DATACMNS-1067 - Fix not to call @AfterDomainEventPublication method from collection of entities.

We now make sure the event cleanup method is called on the aggregate root, not on the parameter object directly (as the latter might be a collection.

Original pull request: #216.
This commit is contained in:
yukiyoshida
2017-05-15 15:44:27 +09:00
committed by Oliver Gierke
parent 2eb830d68f
commit 417fad340b
2 changed files with 51 additions and 4 deletions

View File

@@ -20,6 +20,7 @@ import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import java.lang.reflect.Method;
@@ -36,6 +37,7 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.AfterDomainEventPublication;
import org.springframework.data.domain.DomainEvents;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.RepositoryInformation;
@@ -47,6 +49,7 @@ import org.springframework.data.repository.core.support.EventPublishingRepositor
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Yuki Yoshida
* @soundtrack Henrik Freischlader Trio - Nobody Else To Blame (Openness)
*/
@RunWith(MockitoJUnitRunner.class)
@@ -99,6 +102,40 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests {
verify(publisher, times(0)).publishEvent(any());
}
@Test // DATACMNS-1067
public void clearEventsDoesNotExposedByEntity() {
EventsWithClearing entity = spy(EventsWithClearing.of(Collections.emptyList()));
EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entity, publisher);
verify(entity, times(0)).clearDomainEvents();
}
@Test // DATACMNS-1067
public void clearEventsExposedByEntity() {
EventsWithClearing entity = spy(EventsWithClearing.of(Collections.singletonList(new SomeEvent())));
EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entity, publisher);
verify(entity, times(1)).clearDomainEvents();
}
@Test // DATACMNS-1067
public void clearEventsExposedByEntities() {
EventsWithClearing firstEntity = spy(EventsWithClearing.of(Collections.emptyList()));
EventsWithClearing secondEntity = spy(EventsWithClearing.of(Collections.singletonList(new SomeEvent())));
Collection<EventsWithClearing> entities = Arrays.asList(firstEntity, secondEntity);
EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entities, publisher);
verify(firstEntity, times(0)).clearDomainEvents();
verify(secondEntity, times(1)).clearDomainEvents();
}
@Test // DATACMNS-928
public void doesNotCreatePublishingMethodIfNoAnnotationDetected() {
assertThat(EventPublishingMethod.of(Object.class)).isNull();
@@ -210,6 +247,14 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests {
@Getter(onMethod = @__(@DomainEvents)) Collection<? extends Object> events;
}
@RequiredArgsConstructor(staticName = "of")
static class EventsWithClearing {
@Getter(onMethod = @__(@DomainEvents)) final Collection<? extends Object> events;
@AfterDomainEventPublication
void clearDomainEvents() {}
}
@Value(staticConstructor = "of")
static class OneEvent {
@Getter(onMethod = @__(@DomainEvents)) Object event;