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