|
6 | 6 | import io.cucumber.plugin.EventListener;
|
7 | 7 | import io.cucumber.plugin.StrictAware;
|
8 | 8 | import io.cucumber.plugin.event.Event;
|
| 9 | +import io.cucumber.plugin.event.EventHandler; |
9 | 10 | import io.cucumber.plugin.event.EventPublisher;
|
10 | 11 | import org.junit.jupiter.api.Test;
|
11 |
| -import org.junit.jupiter.api.extension.ExtendWith; |
12 |
| -import org.mockito.ArgumentCaptor; |
13 |
| -import org.mockito.ArgumentMatchers; |
14 |
| -import org.mockito.Captor; |
15 |
| -import org.mockito.Mock; |
16 |
| -import org.mockito.junit.jupiter.MockitoExtension; |
17 |
| - |
18 |
| -import static org.hamcrest.MatcherAssert.assertThat; |
19 |
| -import static org.hamcrest.core.Is.is; |
20 |
| -import static org.hamcrest.core.IsEqual.equalTo; |
21 |
| -import static org.mockito.ArgumentMatchers.eq; |
22 |
| -import static org.mockito.Mockito.mock; |
23 |
| -import static org.mockito.Mockito.times; |
24 |
| -import static org.mockito.Mockito.verify; |
25 |
| - |
26 |
| -@ExtendWith({ MockitoExtension.class }) |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertIterableEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 24 | + |
27 | 25 | class PluginsTest {
|
28 | 26 |
|
29 | 27 | private final PluginFactory pluginFactory = new PluginFactory();
|
30 |
| - @Mock |
31 |
| - private EventPublisher rootEventPublisher; |
32 |
| - @Captor |
33 |
| - private ArgumentCaptor<EventPublisher> eventPublisher; |
34 | 28 |
|
35 | 29 | @Test
|
36 | 30 | void shouldSetStrictOnPlugin() {
|
37 | 31 | RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
|
38 | 32 | Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
|
39 |
| - StrictAware plugin = mock(StrictAware.class); |
| 33 | + MockStrictAware plugin = new MockStrictAware(); |
40 | 34 | plugins.addPlugin(plugin);
|
41 |
| - verify(plugin).setStrict(true); |
| 35 | + assertTrue(plugin.strict); |
42 | 36 | }
|
43 | 37 |
|
44 | 38 | @Test
|
45 | 39 | void shouldSetMonochromeOnPlugin() {
|
46 | 40 | RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
|
47 | 41 | Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
|
48 |
| - ColorAware plugin = mock(ColorAware.class); |
| 42 | + MockColorAware plugin = new MockColorAware(); |
49 | 43 | plugins.addPlugin(plugin);
|
50 |
| - verify(plugin).setMonochrome(false); |
| 44 | + assertFalse(plugin.monochrome); |
51 | 45 | }
|
52 | 46 |
|
53 | 47 | @Test
|
54 | 48 | void shouldSetConcurrentEventListener() {
|
55 | 49 | RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
|
56 | 50 | Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
|
57 |
| - ConcurrentEventListener plugin = mock(ConcurrentEventListener.class); |
| 51 | + MockConcurrentEventListener plugin = new MockConcurrentEventListener(); |
| 52 | + EventPublisher rootEventPublisher = new MockEventPublisher(); |
58 | 53 | plugins.addPlugin(plugin);
|
59 | 54 | plugins.setEventBusOnEventListenerPlugins(rootEventPublisher);
|
60 |
| - verify(plugin, times(1)).setEventPublisher(rootEventPublisher); |
| 55 | + |
| 56 | + assertIterableEquals(List.of(rootEventPublisher), plugin.eventPublishers); |
61 | 57 | }
|
62 | 58 |
|
63 | 59 | @Test
|
64 | 60 | void shouldSetNonConcurrentEventListener() {
|
65 | 61 | RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
|
66 | 62 | Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
|
67 |
| - EventListener plugin = mock(EventListener.class); |
| 63 | + MockEventListener plugin = new MockEventListener(); |
| 64 | + EventPublisher rootEventPublisher = new MockEventPublisher(); |
68 | 65 | plugins.addPlugin(plugin);
|
69 | 66 | plugins.setSerialEventBusOnEventListenerPlugins(rootEventPublisher);
|
70 |
| - verify(plugin, times(1)).setEventPublisher(eventPublisher.capture()); |
71 |
| - assertThat(eventPublisher.getValue().getClass(), is(equalTo(CanonicalOrderEventPublisher.class))); |
| 67 | + |
| 68 | + assertEquals(1, plugin.eventPublishers.size()); |
| 69 | + assertInstanceOf(CanonicalOrderEventPublisher.class, plugin.eventPublishers.get(0)); |
72 | 70 | }
|
73 | 71 |
|
74 | 72 | @Test
|
75 | 73 | void shouldRegisterCanonicalOrderEventPublisherWithRootEventPublisher() {
|
76 | 74 | RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
|
77 | 75 | Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
|
78 |
| - EventListener plugin = mock(EventListener.class); |
| 76 | + MockEventListener plugin = new MockEventListener(); |
| 77 | + MockEventPublisher rootEventPublisher = new MockEventPublisher(); |
79 | 78 | plugins.addPlugin(plugin);
|
80 | 79 | plugins.setSerialEventBusOnEventListenerPlugins(rootEventPublisher);
|
81 |
| - verify(rootEventPublisher, times(1)).registerHandlerFor(eq(Event.class), ArgumentMatchers.any()); |
| 80 | + |
| 81 | + List<EventHandler<?>> eventHandlers = rootEventPublisher.handlers.get(Event.class); |
| 82 | + assertNotNull(eventHandlers); |
| 83 | + assertEquals(1, eventHandlers.size()); |
| 84 | + } |
| 85 | + |
| 86 | + @SuppressWarnings("deprecation") |
| 87 | + private static class MockStrictAware implements StrictAware { |
| 88 | + Boolean strict; |
| 89 | + @Override |
| 90 | + public void setStrict(boolean strict) { |
| 91 | + this.strict = strict; |
| 92 | + } |
82 | 93 | }
|
83 | 94 |
|
| 95 | + private static class MockColorAware implements ColorAware { |
| 96 | + Boolean monochrome; |
| 97 | + @Override |
| 98 | + public void setMonochrome(boolean monochrome) { |
| 99 | + this.monochrome = monochrome; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static class MockConcurrentEventListener implements ConcurrentEventListener { |
| 104 | + final List<EventPublisher> eventPublishers = new ArrayList<>(); |
| 105 | + @Override |
| 106 | + public void setEventPublisher(EventPublisher publisher) { |
| 107 | + eventPublishers.add(publisher); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private static class MockEventListener implements EventListener { |
| 112 | + final List<EventPublisher> eventPublishers = new ArrayList<>(); |
| 113 | + @Override |
| 114 | + public void setEventPublisher(EventPublisher publisher) { |
| 115 | + eventPublishers.add(publisher); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private static class MockEventPublisher implements EventPublisher { |
| 120 | + final Map<Class<?>, List<EventHandler<?>>> handlers = new HashMap<>(); |
| 121 | + @Override |
| 122 | + public <T> void registerHandlerFor(Class<T> eventType, EventHandler<T> handler) { |
| 123 | + List<EventHandler<?>> eventHandlers = handlers.computeIfAbsent(eventType, key -> new ArrayList<>()); |
| 124 | + eventHandlers.add(handler); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public <T> void removeHandlerFor(Class<T> eventType, EventHandler<T> handler) { |
| 129 | + |
| 130 | + } |
| 131 | + } |
84 | 132 | }
|
0 commit comments