View Javadoc

1   // Copyright (C) 2004 - 2011 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Unit test case for <code>PluginRegistry</code>.
54   *
55   * @author Philip Aston
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     // No-op.
165     pluginRegistry.threadStarted(threadContext);
166 
167     verifyNoMoreInteractions(threadContext);
168   }
169 }