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 org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertNotSame;
26  import static org.junit.Assert.assertSame;
27  import static org.mockito.Mockito.RETURNS_MOCKS;
28  import static org.mockito.Mockito.mock;
29  import static org.mockito.Mockito.when;
30  import net.grinder.common.GrinderException;
31  import net.grinder.plugininterface.GrinderPlugin;
32  import net.grinder.plugininterface.PluginProcessContext;
33  import net.grinder.plugininterface.PluginRegistry;
34  import net.grinder.script.Grinder.ScriptContext;
35  import net.grinder.util.InsecureSSLContextFactory;
36  import net.grinder.util.StandardTimeAuthority;
37  
38  import org.junit.Test;
39  
40  
41  /**
42   * Unit tests for {@link HTTPPluginControl}.
43   *
44   * @author Philip Aston
45   */
46  public class TestHTTPPluginControl {
47  
48    @Test public void testHTTPPluginControl() throws Exception {
49      final HTTPPluginThreadState threadState =
50        new HTTPPluginThreadState(null,
51                                  new InsecureSSLContextFactory(),
52                                  null,
53                                  new StandardTimeAuthority());
54  
55      final ScriptContext scriptContext =
56          mock(ScriptContext.class, RETURNS_MOCKS);
57  
58      final PluginProcessContext pluginProcessContext =
59          mock(PluginProcessContext.class);
60  
61      when(pluginProcessContext.getPluginThreadListener())
62        .thenReturn(threadState);
63      when(pluginProcessContext.getScriptContext()).thenReturn(scriptContext);
64  
65      new PluginRegistry() {
66        { setInstance(this); }
67  
68        @Override
69        public void register(final GrinderPlugin plugin) throws GrinderException {
70          plugin.initialize(pluginProcessContext);
71        }
72      };
73  
74      // Sigh, if a previous test has registered a stub PluginProcessContext, we
75      // need to rewire it to make this test valid. Further proof that static
76      // references are evil.
77      final PluginProcessContext existingMock =
78          HTTPPlugin.getPlugin().getPluginProcessContext();
79      if (existingMock != null &&
80          existingMock != pluginProcessContext) {
81  
82        when(existingMock.getPluginThreadListener()).thenReturn(threadState);
83        when(existingMock.getScriptContext()).thenReturn(scriptContext);
84      }
85  
86      final HTTPPluginConnection connectionDefaults =
87        HTTPPluginControl.getConnectionDefaults();
88  
89      assertNotNull(connectionDefaults);
90      assertSame(connectionDefaults, HTTPPluginControl.getConnectionDefaults());
91  
92      final HTTPUtilities utilities = HTTPPluginControl.getHTTPUtilities();
93      assertNotNull(utilities);
94  
95      final Object threadContext = HTTPPluginControl.getThreadHTTPClientContext();
96      assertSame(threadState, threadContext);
97      assertSame(threadState, HTTPPluginControl.getThreadHTTPClientContext());
98  
99      final HTTPPluginConnection connection =
100       HTTPPluginControl.getThreadConnection("http://foo");
101     assertSame(connection,
102                HTTPPluginControl.getThreadConnection("http://foo/bah"));
103     assertNotSame(connection,
104                   HTTPPluginControl.getThreadConnection("http://bah"));
105   }
106 }