Project Report: fawkez

Packagesummary org.jcoderz.commons.connector.http

org.jcoderz.commons.connector.http.HttpConnectorBeanTest

LineHitsNoteSource
1  /*
2   * $Id: HttpConnectorBeanTest.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.connector.http;
34  
35  import junit.framework.Test;
36  import junit.framework.TestSuite;
37  import org.jcoderz.commons.TestCase;
38  import org.jcoderz.commons.connector.ConnectionTimeoutErrorException;
39  import org.jcoderz.commons.connector.ConnectorException;
40  import org.jcoderz.commons.connector.http.transport.HttpEmptyResponseException;
41 (1)import org.jcoderz.commons.connector.http.transport.HttpInvalidResponseHeaderException;
42  import org.jcoderz.commons.connector.http.transport.HttpRequestResponseHeader;
43  import org.jcoderz.commons.types.Url;
44  
45  
46  
47  /**
48   * JUnit remote (bean) tests for the commons http connector.
49   *
50   */
510(2)public class HttpConnectorBeanTest
52        extends TestCase
53  {
54     private static final String URL_SUBWAYCA
55           = "http://subwayca/php3/index.php3";
560    private static final String URL_SIMPLE_SERVER
57     // = "http://localhost:55555/index.html?Gruss=Hallo&Wort2=Welt";
58           = "http://" + TestCase.getHostName() + ":55555";
59  
600    private HttpConnectorSessionInterface mBean = null;
61  
62     /**
63      * Creates the testsuite.
64      * @return Test
65      *          the testsuite
66      */
67     public static Test suite ()
68     {
690       TestSuite suite = null;
700       if (hasTestCases())
71        {
720          suite = getSuite(HttpConnectorBeanTest.class);
73        }
74        else
75        {
760          suite = new TestSuite(HttpConnectorBeanTest.class);
77        }
78  
790       final Test setup = new HttpConnectorBeanTestSetup(suite);
800(3)(4)      return setup;
81     }
82  
83     /** {@inheritDoc} */
84     protected void setUp ()
85           throws Exception
86     {
870       super.setUp();
880       mBean = HttpConnectorSessionJNDIUtil.getHome().create();
890    }
90  
91     protected void tearDown ()
92           throws Exception
93     {
940       super.tearDown();
950       mBean = null;
960    }
97  
98     /**
99      * Tests a simple connection to the subwayca.
100      * @throws Exception in case of any error
101      */
102     public void testConnectingSubwayCA ()
103           throws Exception
104     {
1050       final Url url = Url.fromString(URL_SUBWAYCA);
1060       final ConnectorResponse response = mBean.sendAndReceive(
107              getName().getBytes(), url, null);
1080       assertNotNull("Response object is null", response);
1090       assertTrue("Response message is empty",
110              response.getResponse().length > 0);
1110       assertTrue("Request time not calculated", response.getRequestTime() > 0);
1120(5)      assertTrue("Retries not expected", response.getTries() == 1);
1130    }
114  
115     /**
116      * Tests a simple connection to the simple server
117      * started whilst setup.
118      * @throws Exception in case of any error
119      */
120     public void testConnectingSimpleServer ()
121           throws Exception
122     {
1230       final Url url = Url.fromString(URL_SIMPLE_SERVER);
1240       final ConnectorResponse response = mBean.sendAndReceive(
125              getName().getBytes(), url, null);
1260       assertNotNull("Response object is null", response);
1270       assertTrue("Response message is empty",
128              response.getResponse().length > 0);
1290       assertTrue("Request time not calculated", response.getRequestTime() > 0);
1300    }
131  
132     /**
133      * Tests a connection to the simple server started whilst setup
134      * including a header to set and an expected header in response.
135      * @throws Exception in case of any error
136      */
137     public void testConnectingSimpleServerWithExpectedResponseHeaderSuccess ()
138           throws Exception
139     {
1400       final Url url = Url.fromString(URL_SIMPLE_SERVER);
1410       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
1420       header.addRequestHeader("ECHO_ExpectedHeader", "value");
1430       header.addResponseHeader("ExpectedHeader", "value");
1440       final ConnectorResponse response = mBean.sendAndReceive(
145              getName().getBytes(), url, header);
1460       assertNotNull("Response object is null", response);
1470       assertTrue("Response message is empty",
148              response.getResponse().length > 0);
1490       assertTrue("Request time not calculated", response.getRequestTime() > 0);
1500    }
151  
152     /**
153      * Tests a connection to the simple server started whilst setup
154      * including a header to validate a parameter in response.
155      * The parameter to validate failed.
156      * @throws Exception in case of any error
157      */
158     public void testConnectingSimpleServerWithExpectedResponseHeaderFailed ()
159           throws Exception
160     {
1610       final Url url = Url.fromString(URL_SIMPLE_SERVER);
1620       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
1630       header.addResponseHeader("Content-Type", "text/plain");
164        ConnectorResponse response;
165        try
166        {
1670(6)         response = mBean.sendAndReceive(
168                 getName().getBytes(), url, header);
1690          fail("Expected 'HttpInvalidResponseHeaderException' "
170                 + " with invalid value for 'Content-Type' in response");
171        }
1720       catch (ConnectorException ce)
173        {
1740          if (ce.getCause() instanceof HttpInvalidResponseHeaderException)
175           {
176              // expected
1770             final HttpInvalidResponseHeaderException expected
178                    = (HttpInvalidResponseHeaderException) ce.getCause();
1790             assertTrue("Content-Type not invalid in response",
180                    expected.getInvalidParameter().containsKey("Content-Type"));
1810          }
182           else
183           {
1840             fail("Not the expected exception: " + ce.getMessage());
185           }
1860       }
1870    }
188  
189     /**
190      * Tests a connection to the simple server started whilst setup
191      * with an empty message body received in response.
192      *
193      * @throws Exception in case of any error
194      */
195     public void testConnectingSimpleServerWithEmptyResponseBody ()
196           throws Exception
197     {
1980       final Url url = Url.fromString(URL_SIMPLE_SERVER);
1990       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
2000       header.addRequestHeader("UseEmptyResponse", "true");
201        ConnectorResponse response;
202        try
203        {
2040(7)         response = mBean.sendAndReceive(
205                 getName().getBytes(), url, header);
2060          fail("Expected 'HttpEmptyResponseException'");
207        }
2080       catch (ConnectorException ce)
209        {
2100          if (!(ce.getCause() instanceof HttpEmptyResponseException))
211           {
2120             fail("Not the expected exception: " + ce.getCause().getMessage());
213           }
2140       }
2150    }
216  
217     /**
218      * Tests a timeout whilst waiting for a response from the simple server.
219      * @throws Exception in case of any error
220      */
221     public void testConnectingSimpleServerWithTimeout ()
222           throws Exception
223     {
2240       final Url url = Url.fromString(URL_SIMPLE_SERVER);
2250       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
2260       header.addRequestHeader("DoNotRespond", "True");
227        ConnectorResponse response;
228        try
229        {
2300(8)         response = mBean.sendAndReceive(
231                 getName().getBytes(), url, header);
2320          fail("Expected 'TimeoutException'");
233        }
2340       catch (ConnectionTimeoutErrorException expected)
235        {
236           // expected
237        }
2380       catch (ConnectorException ce)
239        {
2400          fail("Not the expected exception: " + ce.getMessage());
2410       }
2420    }
243  
244  }
245  

Findings in this File

c (1) 41 : 0 Line is longer than 80 characters.
c (2) 51 : 0 Type Javadoc comment is missing an @author tag.
i (3) 80 : 0 method org.jcoderz.commons.connector.http.HttpConnectorBeanTest.suite() stores return result in local before immediately returning it (test code) Decreased severity from 'warning' for testcode.
c (4) 80 : 7 Consider simply returning the value vs storing it in local variable 'setup'
c (5) 112 : 7 Use assertSame(x, y) instead of assertTrue(x==y), or assertNotSame(x,y) vs assertFalse(x==y)
i (6) 167 : 0 Dead store to response in org.jcoderz.commons.connector.http.HttpConnectorBeanTest.testConnectingSimpleServerWithExpectedResponseHeaderFailed() (test code) Decreased severity from 'warning' for testcode.
i (7) 204 : 0 Dead store to response in org.jcoderz.commons.connector.http.HttpConnectorBeanTest.testConnectingSimpleServerWithEmptyResponseBody() (test code) Decreased severity from 'warning' for testcode.
i (8) 230 : 0 Dead store to response in org.jcoderz.commons.connector.http.HttpConnectorBeanTest.testConnectingSimpleServerWithTimeout() (test code) Decreased severity from 'warning' for testcode.