|
| 1 | +package com.codingapi.springboot.framework.event; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.AfterEach; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 12 | + |
| 13 | +/** |
| 14 | + * Handler 注册表的订阅类型匹配与注销能力。 |
| 15 | + */ |
| 16 | +class ApplicationHandlerUtilsTest { |
| 17 | + |
| 18 | + static class SubscribedEvent implements ISyncEvent { |
| 19 | + } |
| 20 | + |
| 21 | + static class OtherEvent implements ISyncEvent { |
| 22 | + } |
| 23 | + |
| 24 | + private final List<IHandler> registeredHandlers = new ArrayList<>(); |
| 25 | + |
| 26 | + @AfterEach |
| 27 | + void cleanup() { |
| 28 | + ApplicationHandlerUtils.getInstance().removeHandlers(registeredHandlers); |
| 29 | + registeredHandlers.clear(); |
| 30 | + } |
| 31 | + |
| 32 | + private void register(IHandler handler) { |
| 33 | + ApplicationHandlerUtils.getInstance().addHandler(handler); |
| 34 | + registeredHandlers.add(handler); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void should_only_dispatch_event_to_handler_subscribed_it() { |
| 39 | + List<IEvent> received = new ArrayList<>(); |
| 40 | + register(new IHandler<SubscribedEvent>() { |
| 41 | + @Override |
| 42 | + public void handler(SubscribedEvent event) { |
| 43 | + received.add(event); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + ApplicationHandlerUtils.getInstance().handler(new SubscribedEvent()); |
| 48 | + ApplicationHandlerUtils.getInstance().handler(new OtherEvent()); |
| 49 | + |
| 50 | + assertEquals(1, received.size()); |
| 51 | + assertTrue(received.get(0) instanceof SubscribedEvent); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void should_skip_handler_whose_subscribed_type_cannot_be_resolved() { |
| 56 | + List<IEvent> received = new ArrayList<>(); |
| 57 | + IHandler<SubscribedEvent> lambdaHandler = event -> received.add(event); |
| 58 | + register(lambdaHandler); |
| 59 | + |
| 60 | + ApplicationHandlerUtils.getInstance().handler(new OtherEvent()); |
| 61 | + |
| 62 | + assertTrue(received.isEmpty(), "无法解析订阅类型的 Handler 不应接收任何事件"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void should_not_dispatch_event_after_handler_removed() { |
| 67 | + List<IEvent> received = new ArrayList<>(); |
| 68 | + IHandler<SubscribedEvent> handler = new IHandler<SubscribedEvent>() { |
| 69 | + @Override |
| 70 | + public void handler(SubscribedEvent event) { |
| 71 | + received.add(event); |
| 72 | + } |
| 73 | + }; |
| 74 | + register(handler); |
| 75 | + |
| 76 | + ApplicationHandlerUtils.getInstance().handler(new SubscribedEvent()); |
| 77 | + assertEquals(1, received.size()); |
| 78 | + |
| 79 | + ApplicationHandlerUtils.getInstance().removeHandlers(Collections.<IHandler>singletonList(handler)); |
| 80 | + |
| 81 | + ApplicationHandlerUtils.getInstance().handler(new SubscribedEvent()); |
| 82 | + assertEquals(1, received.size(), "已注销的 Handler 不应再接收事件"); |
| 83 | + } |
| 84 | +} |
0 commit comments