|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.modulith.events.support; |
| 17 | + |
| 18 | +import static org.mockito.ArgumentMatchers.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.context.ApplicationEvent; |
| 25 | +import org.springframework.context.ApplicationEventPublisher; |
| 26 | +import org.springframework.context.annotation.Bean; |
| 27 | +import org.springframework.context.annotation.Configuration; |
| 28 | +import org.springframework.modulith.events.EventPublication; |
| 29 | +import org.springframework.modulith.events.EventPublicationRepository; |
| 30 | +import org.springframework.modulith.events.config.EnablePersistentDomainEvents; |
| 31 | +import org.springframework.stereotype.Component; |
| 32 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 33 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 34 | +import org.springframework.transaction.event.TransactionalEventListener; |
| 35 | + |
| 36 | +/** |
| 37 | + * Integration test for {@link PersistentApplicationEventMulticaster}. |
| 38 | + * |
| 39 | + * @author Oliver Drotbohm |
| 40 | + */ |
| 41 | +@ExtendWith(SpringExtension.class) |
| 42 | +class PersistentApplicationEventMulticasterIntegrationTests { |
| 43 | + |
| 44 | + @Configuration |
| 45 | + @EnableTransactionManagement |
| 46 | + @EnablePersistentDomainEvents |
| 47 | + static class TestConfiguration { |
| 48 | + |
| 49 | + @Bean |
| 50 | + EventPublicationRepository repository() { |
| 51 | + return mock(EventPublicationRepository.class); |
| 52 | + } |
| 53 | + |
| 54 | + @Bean |
| 55 | + SampleEventListener listener() { |
| 56 | + return new SampleEventListener(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Autowired ApplicationEventPublisher publisher; |
| 61 | + @Autowired EventPublicationRepository repository; |
| 62 | + |
| 63 | + @Test // GH-186 |
| 64 | + void doesNotPublishGenericEventsToListeners() throws Exception { |
| 65 | + |
| 66 | + publisher.publishEvent(new SomeGenericEvent<>()); |
| 67 | + verify(repository, never()).create(any(EventPublication.class)); |
| 68 | + |
| 69 | + publisher.publishEvent(new SomeOtherEvent()); |
| 70 | + verify(repository).create(any(EventPublication.class)); |
| 71 | + } |
| 72 | + |
| 73 | + @Component |
| 74 | + static class SampleEventListener { |
| 75 | + |
| 76 | + @TransactionalEventListener |
| 77 | + void listener(SomeOtherEvent event) {} |
| 78 | + } |
| 79 | + |
| 80 | + static class SomeGenericEvent<T> extends ApplicationEvent { |
| 81 | + |
| 82 | + private static final long serialVersionUID = -4054955298417761460L; |
| 83 | + |
| 84 | + public SomeGenericEvent() { |
| 85 | + super(new Object()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + static class SomeOtherEvent {} |
| 90 | +} |
0 commit comments