|
20 | 20 | import static org.mockito.Mockito.*;
|
21 | 21 |
|
22 | 22 | import lombok.Getter;
|
| 23 | +import lombok.RequiredArgsConstructor; |
23 | 24 | import lombok.Value;
|
24 | 25 |
|
25 | 26 | import java.lang.reflect.Method;
|
|
36 | 37 | import org.mockito.junit.MockitoJUnitRunner;
|
37 | 38 | import org.springframework.aop.framework.ProxyFactory;
|
38 | 39 | import org.springframework.context.ApplicationEventPublisher;
|
| 40 | +import org.springframework.data.domain.AfterDomainEventPublication; |
39 | 41 | import org.springframework.data.domain.DomainEvents;
|
40 | 42 | import org.springframework.data.repository.CrudRepository;
|
41 | 43 | import org.springframework.data.repository.core.RepositoryInformation;
|
|
47 | 49 | *
|
48 | 50 | * @author Oliver Gierke
|
49 | 51 | * @author Mark Paluch
|
| 52 | + * @author Yuki Yoshida |
50 | 53 | * @soundtrack Henrik Freischlader Trio - Nobody Else To Blame (Openness)
|
51 | 54 | */
|
52 | 55 | @RunWith(MockitoJUnitRunner.class)
|
@@ -99,6 +102,40 @@ public void doesNotExposeNullEvent() {
|
99 | 102 | verify(publisher, times(0)).publishEvent(any());
|
100 | 103 | }
|
101 | 104 |
|
| 105 | + @Test // DATACMNS-1067 |
| 106 | + public void clearEventsDoesNotExposedByEntity() { |
| 107 | + |
| 108 | + EventsWithClearing entity = spy(EventsWithClearing.of(Collections.emptyList())); |
| 109 | + |
| 110 | + EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entity, publisher); |
| 111 | + |
| 112 | + verify(entity, times(0)).clearDomainEvents(); |
| 113 | + } |
| 114 | + |
| 115 | + @Test // DATACMNS-1067 |
| 116 | + public void clearEventsExposedByEntity() { |
| 117 | + |
| 118 | + EventsWithClearing entity = spy(EventsWithClearing.of(Collections.singletonList(new SomeEvent()))); |
| 119 | + |
| 120 | + EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entity, publisher); |
| 121 | + |
| 122 | + verify(entity, times(1)).clearDomainEvents(); |
| 123 | + } |
| 124 | + |
| 125 | + @Test // DATACMNS-1067 |
| 126 | + public void clearEventsExposedByEntities() { |
| 127 | + |
| 128 | + EventsWithClearing firstEntity = spy(EventsWithClearing.of(Collections.emptyList())); |
| 129 | + EventsWithClearing secondEntity = spy(EventsWithClearing.of(Collections.singletonList(new SomeEvent()))); |
| 130 | + |
| 131 | + Collection<EventsWithClearing> entities = Arrays.asList(firstEntity, secondEntity); |
| 132 | + |
| 133 | + EventPublishingMethod.of(EventsWithClearing.class).publishEventsFrom(entities, publisher); |
| 134 | + |
| 135 | + verify(firstEntity, times(0)).clearDomainEvents(); |
| 136 | + verify(secondEntity, times(1)).clearDomainEvents(); |
| 137 | + } |
| 138 | + |
102 | 139 | @Test // DATACMNS-928
|
103 | 140 | public void doesNotCreatePublishingMethodIfNoAnnotationDetected() {
|
104 | 141 | assertThat(EventPublishingMethod.of(Object.class)).isNull();
|
@@ -210,6 +247,14 @@ static class MultipleEvents {
|
210 | 247 | @Getter(onMethod = @__(@DomainEvents)) Collection<? extends Object> events;
|
211 | 248 | }
|
212 | 249 |
|
| 250 | + @RequiredArgsConstructor(staticName = "of") |
| 251 | + static class EventsWithClearing { |
| 252 | + @Getter(onMethod = @__(@DomainEvents)) final Collection<? extends Object> events; |
| 253 | + |
| 254 | + @AfterDomainEventPublication |
| 255 | + void clearDomainEvents() {} |
| 256 | + } |
| 257 | + |
213 | 258 | @Value(staticConstructor = "of")
|
214 | 259 | static class OneEvent {
|
215 | 260 | @Getter(onMethod = @__(@DomainEvents)) Object event;
|
|
0 commit comments