|
22 | 22 | import static org.mockito.Mockito.*;
|
23 | 23 |
|
24 | 24 | import lombok.Getter;
|
| 25 | +import lombok.RequiredArgsConstructor; |
25 | 26 | import lombok.Value;
|
26 | 27 |
|
27 | 28 | import java.io.Serializable;
|
|
39 | 40 | import org.mockito.runners.MockitoJUnitRunner;
|
40 | 41 | import org.springframework.aop.framework.ProxyFactory;
|
41 | 42 | import org.springframework.context.ApplicationEventPublisher;
|
| 43 | +import org.springframework.data.domain.AfterDomainEventPublication; |
42 | 44 | import org.springframework.data.domain.DomainEvents;
|
43 | 45 | import org.springframework.data.repository.CrudRepository;
|
44 | 46 | import org.springframework.data.repository.core.RepositoryInformation;
|
|
49 | 51 | * Unit tests for {@link EventPublishingRepositoryProxyPostProcessor} and contained classes.
|
50 | 52 | *
|
51 | 53 | * @author Oliver Gierke
|
| 54 | + * @author Mark Paluch |
| 55 | + * @author Yuki Yoshida |
52 | 56 | * @soundtrack Henrik Freischlader Trio - Nobody Else To Blame (Openness)
|
53 | 57 | */
|
54 | 58 | @RunWith(MockitoJUnitRunner.class)
|
@@ -101,6 +105,40 @@ public void doesNotExposeNullEvent() {
|
101 | 105 | verify(publisher, times(0)).publishEvent(any());
|
102 | 106 | }
|
103 | 107 |
|
| 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 | + |
104 | 142 | @Test // DATACMNS-928
|
105 | 143 | public void doesNotCreatePublishingMethodIfNoAnnotationDetected() {
|
106 | 144 | assertThat(EventPublishingMethod.of(Object.class), is(nullValue()));
|
@@ -215,6 +253,14 @@ static class MultipleEvents {
|
215 | 253 | @Getter(onMethod = @__(@DomainEvents)) Collection<? extends Object> events;
|
216 | 254 | }
|
217 | 255 |
|
| 256 | + @RequiredArgsConstructor(staticName = "of") |
| 257 | + static class EventsWithClearing { |
| 258 | + @Getter(onMethod = @__(@DomainEvents)) final Collection<? extends Object> events; |
| 259 | + |
| 260 | + @AfterDomainEventPublication |
| 261 | + void clearDomainEvents() {} |
| 262 | + } |
| 263 | + |
218 | 264 | @Value(staticConstructor = "of")
|
219 | 265 | static class OneEvent {
|
220 | 266 | @Getter(onMethod = @__(@DomainEvents)) Object event;
|
|
0 commit comments