1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.engine.process;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Matchers.contains;
29 import static org.mockito.Mockito.doThrow;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.verifyNoMoreInteractions;
34 import net.grinder.common.ThreadLifeCycleListener;
35 import net.grinder.engine.common.EngineException;
36 import net.grinder.plugininterface.GrinderPlugin;
37 import net.grinder.plugininterface.PluginException;
38 import net.grinder.plugininterface.PluginRegistry;
39 import net.grinder.script.Grinder.ScriptContext;
40 import net.grinder.statistics.StatisticsServicesImplementation;
41 import net.grinder.util.TimeAuthority;
42
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.ArgumentCaptor;
46 import org.mockito.Captor;
47 import org.mockito.Mock;
48 import org.mockito.MockitoAnnotations;
49 import org.slf4j.Logger;
50
51
52
53
54
55
56
57 public class TestPluginRegistryImplementation {
58
59 @Mock private Logger m_logger;
60 @Mock private ScriptContext m_scriptContext;
61 @Mock private TimeAuthority m_timeAuthority;
62 @Mock private GrinderPlugin m_grinderPlugin;
63 @Captor private ArgumentCaptor<RegisteredPlugin> m_pluginCaptor;
64
65 private final ThreadContextLocator m_threadContextLocator =
66 new StubThreadContextLocator();
67
68 @Before public void setUp() {
69 MockitoAnnotations.initMocks(this);
70 }
71
72 @Test public void testConstructorAndSingleton() throws Exception {
73 final PluginRegistry pluginRegistry =
74 new PluginRegistryImplementation(
75 m_logger, m_scriptContext, m_threadContextLocator,
76 StatisticsServicesImplementation.getInstance(), m_timeAuthority);
77
78 assertSame(pluginRegistry, PluginRegistry.getInstance());
79 }
80
81 @Test public void testRegister() throws Exception {
82 final PluginRegistry pluginRegistry =
83 new PluginRegistryImplementation(
84 m_logger, m_scriptContext, m_threadContextLocator,
85 StatisticsServicesImplementation.getInstance(), m_timeAuthority);
86
87 pluginRegistry.register(m_grinderPlugin);
88
89 verify(m_grinderPlugin).initialize(m_pluginCaptor.capture());
90
91 final RegisteredPlugin registeredPlugin = m_pluginCaptor.getValue();
92 assertSame(m_scriptContext, registeredPlugin.getScriptContext());
93 assertSame(m_timeAuthority, registeredPlugin.getTimeAuthority());
94
95 verify(m_logger).info(contains("registered plug-in"),
96 contains("GrinderPlugin"));
97
98 pluginRegistry.register(m_grinderPlugin);
99
100 verifyNoMoreInteractions(m_grinderPlugin, m_logger);
101 }
102
103 @Test public void testRegisterWithBadPlugin() throws Exception {
104 final PluginRegistry pluginRegistry =
105 new PluginRegistryImplementation(
106 m_logger, m_scriptContext, m_threadContextLocator,
107 StatisticsServicesImplementation.getInstance(), m_timeAuthority);
108
109 final PluginException initialiseException = new PluginException("barf");
110
111 doThrow(initialiseException)
112 .when(m_grinderPlugin).initialize(m_pluginCaptor.capture());
113
114 try {
115 pluginRegistry.register(m_grinderPlugin);
116 fail("Expected EngineException");
117 }
118 catch (EngineException e) {
119 assertSame(initialiseException, e.getCause());
120 }
121 }
122
123 @Test public void testListeners() throws Exception {
124 final PluginRegistryImplementation pluginRegistry =
125 new PluginRegistryImplementation(
126 m_logger, m_scriptContext, m_threadContextLocator,
127 StatisticsServicesImplementation.getInstance(), m_timeAuthority);
128
129 final ThreadContext threadContext = mock(ThreadContext.class);
130
131 pluginRegistry.threadCreated(threadContext);
132
133 final ArgumentCaptor<ThreadLifeCycleListener> listenerCaptor =
134 ArgumentCaptor.forClass(ThreadLifeCycleListener.class);
135
136 verify(threadContext)
137 .registerThreadLifeCycleListener(listenerCaptor.capture());
138
139 final ThreadLifeCycleListener threadListener = listenerCaptor.getValue();
140
141 assertNotNull(threadListener);
142
143 threadListener.beginThread();
144 threadListener.beginRun();
145 threadListener.endRun();
146 threadListener.endThread();
147
148 pluginRegistry.register(m_grinderPlugin);
149
150 verify(m_grinderPlugin).initialize(m_pluginCaptor.capture());
151
152 threadListener.beginThread();
153
154 verify(m_grinderPlugin).createThreadListener(threadContext);
155
156 verify(threadContext, times(2))
157 .registerThreadLifeCycleListener(any(ThreadLifeCycleListener.class));
158
159 threadListener.beginRun();
160 threadListener.endRun();
161 threadListener.endThread();
162 threadListener.beginShutdown();
163
164
165 pluginRegistry.threadStarted(threadContext);
166
167 verifyNoMoreInteractions(threadContext);
168 }
169 }