View Javadoc

1   // Copyright (C) 2008 - 2012 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.plugin.http;
23  
24  import static java.util.Collections.singleton;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertSame;
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.fail;
29  import static org.mockito.Matchers.isA;
30  import static org.mockito.Mockito.doThrow;
31  import static org.mockito.Mockito.mock;
32  import static org.mockito.Mockito.when;
33  
34  import java.net.URLClassLoader;
35  import java.util.Collections;
36  
37  import net.grinder.common.GrinderException;
38  import net.grinder.plugininterface.GrinderPlugin;
39  import net.grinder.plugininterface.PluginException;
40  import net.grinder.plugininterface.PluginProcessContext;
41  import net.grinder.plugininterface.PluginRegistry;
42  import net.grinder.plugininterface.PluginThreadContext;
43  import net.grinder.plugininterface.PluginThreadListener;
44  import net.grinder.script.Grinder.ScriptContext;
45  import net.grinder.script.Statistics;
46  import net.grinder.util.BlockingClassLoader;
47  
48  import org.junit.Before;
49  import org.junit.Test;
50  import org.mockito.Mock;
51  import org.mockito.MockitoAnnotations;
52  
53  
54  /**
55   * Unit tests for {@link HTTPPlugin}.
56   *
57   * @author Philip Aston
58   */
59  public class TestHTTPPlugin {
60  
61    @Mock private PluginProcessContext m_pluginProcessContext;
62    @Mock private ScriptContext m_scriptContext;
63    @Mock private Statistics m_statistics;
64  
65    @Before public void setUp() {
66      MockitoAnnotations.initMocks(this);
67  
68      when(m_pluginProcessContext.getScriptContext()).thenReturn(m_scriptContext);
69      when(m_scriptContext.getStatistics()).thenReturn(m_statistics);
70    }
71  
72    @Test public void testInitialiseWithBadHTTPClient() throws Exception {
73  
74      final String pluginName = HTTPPlugin.class.getName();
75  
76      final URLClassLoader blockingLoader =
77        new BlockingClassLoader(singleton("HTTPClient.RetryModule"),
78                                singleton(pluginName),
79                                Collections.<String>emptySet(),
80                                false);
81  
82      new PluginRegistry() {
83        {
84          setInstance(this);
85        }
86  
87        public void register(GrinderPlugin plugin) throws GrinderException {
88          plugin.initialize(m_pluginProcessContext);
89        }
90      };
91  
92      try {
93        Class.forName(pluginName, true, blockingLoader);
94        fail("Expected PluginException");
95      }
96      catch (ExceptionInInitializerError e) {
97        // EIIE ->  PluginException -> ClassNotFoundException
98        assertTrue(e.getCause().getCause() instanceof ClassNotFoundException);
99      }
100   }
101 
102   @Test public void testInitializeWithBadStatistics() throws Exception {
103 
104     final GrinderException grinderException = new GrinderException("Hello") {};
105 
106     doThrow(grinderException).when(m_statistics)
107       .registerDataLogExpression(isA(String.class), isA(String.class));
108 
109     final HTTPPlugin plugin = new HTTPPlugin();
110 
111     try {
112       plugin.initialize(m_pluginProcessContext);
113       fail("Expected PluginException");
114     }
115     catch (PluginException e) {
116       assertSame(grinderException, e.getCause());
117     }
118   }
119 
120   @Test public void testCreateThreadListener() throws Exception {
121     final HTTPPlugin plugin = new HTTPPlugin();
122 
123     plugin.initialize(m_pluginProcessContext);
124 
125     assertSame(m_pluginProcessContext, plugin.getPluginProcessContext());
126 
127     final PluginThreadContext
128       pluginThreadContext = mock(PluginThreadContext.class);
129     final PluginThreadListener threadListener =
130       plugin.createThreadListener(pluginThreadContext);
131 
132     assertNotNull(threadListener);
133   }
134 }