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.assertSame;
25  import static org.junit.Assert.fail;
26  import static org.mockito.Mockito.*;
27  import net.grinder.common.ThreadLifeCycleListener;
28  import net.grinder.engine.common.EngineException;
29  import net.grinder.plugininterface.GrinderPlugin;
30  import net.grinder.plugininterface.PluginException;
31  import net.grinder.plugininterface.PluginThreadContext;
32  import net.grinder.plugininterface.PluginThreadListener;
33  import net.grinder.script.Grinder.ScriptContext;
34  import net.grinder.statistics.StatisticsServicesImplementation;
35  import net.grinder.testutility.RandomStubFactory;
36  import net.grinder.util.TimeAuthority;
37  
38  import org.junit.Before;
39  import org.junit.Test;
40  import org.mockito.Mock;
41  import org.mockito.MockitoAnnotations;
42  import org.slf4j.Logger;
43  
44  
45  /**
46   * Unit test case for {@code RegisteredPlugin}.
47   *
48   * @author Philip Aston
49   */
50  public class TestRegisteredPlugin {
51  
52    @Mock private Logger m_logger;
53    @Mock private ThreadContext m_threadContext;
54  
55    @Before public void setUp() {
56      MockitoAnnotations.initMocks(this);
57    }
58  
59    @Test public void testConstructorAndSimpleAccessors() throws Exception {
60  
61      final RandomStubFactory<GrinderPlugin> pluginStubFactory =
62        RandomStubFactory.create(GrinderPlugin.class);
63      final GrinderPlugin plugin = pluginStubFactory.getStub();
64  
65      final RandomStubFactory<ScriptContext> scriptContextStubFactory =
66        RandomStubFactory.create(ScriptContext.class);
67      final ScriptContext scriptContext =
68        scriptContextStubFactory.getStub();
69  
70      final RandomStubFactory<TimeAuthority> timeAuthorityStubFactory =
71        RandomStubFactory.create(TimeAuthority.class);
72      final TimeAuthority timeAuthority =
73        timeAuthorityStubFactory.getStub();
74  
75      final ThreadContextLocator threadContextLocator =
76        new StubThreadContextLocator();
77  
78      final RegisteredPlugin registeredPlugin =
79        new RegisteredPlugin(plugin,
80                             scriptContext,
81                             threadContextLocator,
82                             StatisticsServicesImplementation.getInstance(),
83                             timeAuthority,
84                             m_logger);
85  
86      assertSame(scriptContext, registeredPlugin.getScriptContext());
87      assertSame(StatisticsServicesImplementation.getInstance(),
88                 registeredPlugin.getStatisticsServices());
89      assertSame(timeAuthority, registeredPlugin.getTimeAuthority());
90    }
91  
92    @Test public void testGetPluginThreadListener() throws Exception {
93  
94      final GrinderPluginStubFactory grinderPluginStubFactory =
95        new GrinderPluginStubFactory();
96      final GrinderPlugin grinderPlugin =
97        grinderPluginStubFactory.getStub();
98  
99      final RandomStubFactory<ScriptContext> scriptContextStubFactory =
100       RandomStubFactory.create(ScriptContext.class);
101 
102     final RandomStubFactory<TimeAuthority> timeAuthorityStubFactory =
103       RandomStubFactory.create(TimeAuthority.class);
104 
105     final StubThreadContextLocator threadContextLocator =
106       new StubThreadContextLocator();
107 
108     final RegisteredPlugin registeredPlugin =
109       new RegisteredPlugin(grinderPlugin,
110                            scriptContextStubFactory.getStub(),
111                            threadContextLocator,
112                            StatisticsServicesImplementation.getInstance(),
113                            timeAuthorityStubFactory.getStub(),
114                            m_logger);
115 
116     try {
117       registeredPlugin.getPluginThreadListener();
118       fail("Expected EngineException");
119     }
120     catch (EngineException e) {
121     }
122 
123     threadContextLocator.set(m_threadContext);
124 
125     grinderPluginStubFactory.setThrowExceptionFromCreateThreadListener(true);
126 
127     try {
128       registeredPlugin.getPluginThreadListener();
129       fail("Expected EngineException");
130     }
131     catch (EngineException e) {
132     }
133 
134     verify(m_threadContext).getLogMarker();
135 
136     grinderPluginStubFactory.assertException("createThreadListener",
137                                              PluginException.class,
138                                              PluginThreadContext.class);
139 
140     grinderPluginStubFactory.assertNoMoreCalls();
141 
142     grinderPluginStubFactory.setThrowExceptionFromCreateThreadListener(false);
143 
144     final PluginThreadListener pluginThreadListener1 =
145       registeredPlugin.getPluginThreadListener();
146 
147     grinderPluginStubFactory.assertSuccess(
148       "createThreadListener", PluginThreadContext.class);
149 
150     final PluginThreadListener pluginThreadListener2 =
151       registeredPlugin.getPluginThreadListener();
152 
153     verify(m_threadContext)
154       .registerThreadLifeCycleListener(isA(ThreadLifeCycleListener.class));
155 
156     grinderPluginStubFactory.assertNoMoreCalls();
157 
158     assertSame(pluginThreadListener1, pluginThreadListener2);
159 
160     final PluginThreadListener pluginThreadListener3 =
161       registeredPlugin.createPluginThreadListener(m_threadContext);
162 
163     assertSame(pluginThreadListener1, pluginThreadListener3);
164 
165     verifyNoMoreInteractions(m_threadContext);
166   }
167 
168   public static class GrinderPluginStubFactory
169     extends RandomStubFactory<GrinderPlugin> {
170     private boolean m_throwExceptionFromCreateThreadListener;
171     private final GrinderPlugin m_delegateStub;
172 
173     public GrinderPluginStubFactory() {
174       super(GrinderPlugin.class);
175       m_delegateStub = RandomStubFactory.create(GrinderPlugin.class).getStub();
176     }
177 
178     void setThrowExceptionFromCreateThreadListener(boolean b) {
179       m_throwExceptionFromCreateThreadListener = b;
180     }
181 
182     public PluginThreadListener override_createThreadListener(
183       Object proxy, PluginThreadContext pluginThreadContext)
184       throws PluginException {
185 
186       if (m_throwExceptionFromCreateThreadListener) {
187         throw new PluginException("Eat me");
188       }
189 
190       return m_delegateStub.createThreadListener(pluginThreadContext);
191     }
192   }
193 }