Project Report: fawkez

Packagesummary org.jcoderz.commons.connector.http.transport

org.jcoderz.commons.connector.http.transport.HttpClientConnectionTest

LineHitsNoteSource
1  /*
2   * $Id: HttpClientConnectionTest.java 1380 2009-03-29 17:43:09Z 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.transport;
34  
35  import java.io.ByteArrayInputStream;
36  
37  import junit.framework.Test;
38  import junit.framework.TestSuite;
39  
40  import org.jcoderz.commons.ArgumentMalformedException;
41  import org.jcoderz.commons.TestCase;
42  import org.jcoderz.commons.util.Constants;
43  import org.jcoderz.commons.util.StringUtil;
44  
45  
46  
47  /**
48   * This class test the implementation of the HttpConnectionInterface
49   * provided via the Jakarta commons-httpclient project.
50   * @author anonymous
51   */
52100 public class HttpClientConnectionTest
53        extends TestCase
54  {
55     private static final int CONNECT_TIMEOUT = 5000;
56     private static final int READ_TIMEOUT = 5000;
57100    private static final String FILE_SEPARATOR
58           = System.getProperty("file.separator");
59     private static final String
60           DEFAULT_URL = "http://localhost:"
61               + HttpClientConnectionTestSetup.DEFAULT_SERVER_PORT;
62     private static final byte[]
63100          SIMPLE_BODY = "This is a simple POST request".getBytes();
64     private static final String
65           ECHO_SIMPLE_BODY = "Echo:This is a simple POST request";
66     private static final String
67           CONTENT_TYPE_PARAMETER = "Content-Type";
68     private static final String
69           CONTENT_TYPE_PARAMETER_VALUE = "text/xml; charset=ISO-8859-1";
70     private static final String
71           CONNECTION_PARAMETER = "Connection";
72     private static final String
73           CONNECTION_PARAMETER_VALUE_CLOSE = "Close";
74     private static final String
75           CONNECTION_PARAMETER_VALUE_KEEPALIVE = "Keep-Alive";
76     private static final String
77           HTTPCONNECTIONEXCEPTION = "HttpConnectionException: ";
78     private static final String
79           ILLEGALSTATEEXCEPTION = "IllegalStateException: ";
80     private static final String
81           NO_MESSAGE_BODY_IN_RESPONSE = "No message body in response";
82     private static final String
83           CONNECTION_CLOSE_EXPECTED = "Connection close expected";
84     private static final String
85           RESPONSE_BODY_NOT_LIKE_EXPECTED
86              = "Response body not like expected";
87     private static final String
88           ATTRIBUTE_CONNECTION_MISSING
89              = "No attribute \'Connection\'";
90     private static final String
91           CONNECTION_MUST_BE_ESTABLISHED_OR_RELEASED
92              = "Connection must be established before or released";
93     private static final String
94           ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED
95              = "Illegal State message not like expected";
96     private static final String
97           ILLEGALSTATEEXCEPTION_EXPECTED
98              = "IllegalStateException expected";
99  
100     /** The interface implementation in use. */
101     private HttpClientConnection mHttpConnection;
102  
103  
104     /**
105      * Main.
106      *
107      * @param args main arguments
108      */
109     public static void main (
110           String[] args)
111     {
1120       junit.textui.TestRunner.run(suite());
1130    }
114  
115     /**
116      * Creates the testsuite.
117      * @return Test
118      *          the testsuite
119      */
120     public static Test suite ()
121     {
122100       TestSuite suite = null;
123100       if (hasTestCases())
124        {
1250          suite = getSuite(HttpClientConnectionTest.class);
126        }
127        else
128        {
12975          suite = new TestSuite(HttpClientConnectionTest.class);
130        }
131  
132100       final Test setup = new HttpClientConnectionTestSetup(suite);
133100(1)(2)      return setup;
134     }
135  
136     /**
137      * Setup creates a new interface implementation object.
138      * @see TestCase#setUp()
139      */
140     protected void setUp ()
141           throws Exception
142     {
143100       super.setUp();
144100       mHttpConnection = new HttpClientConnectionImpl();
145100    }
146  
147     /**
148      * TearDown of the TestCase.
149      * @see TestCase#tearDown()
150      */
151 (3)   protected void tearDown ()
152           throws Exception
153     {
154100       super.tearDown();
155100    }
156  
157     /**
158      * Performs establishing a connection, creating a request, sending that
159      * request, receiving the response and closing the connection.
160      * @throws Exception for any unexpected error
161      */
162     public void testSendSimplePostRequest ()
163           throws Exception
164     {
165        // connect
166100       mHttpConnection.establishConnection(
167              DEFAULT_URL,
168              CONNECT_TIMEOUT, READ_TIMEOUT);
169100       mHttpConnection.setRequestBody(
170              new ByteArrayInputStream(SIMPLE_BODY));
171100       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
172100       header.addRequestHeader(
173              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
174100       header.addRequestHeader(
175              CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
176100       mHttpConnection.setRequestResponseHeader(header);
177  
178        // execute
179100       mHttpConnection.execute();
180100       final String response = getResponseBodyAsString();
181100       final String connectionValue
182              = mHttpConnection.getResponseHeader(CONNECTION_PARAMETER);
183  
184        // close
185100       mHttpConnection.closeConnection();
186  
187100       assertNotNull(NO_MESSAGE_BODY_IN_RESPONSE, response);
188100       assertEquals(RESPONSE_BODY_NOT_LIKE_EXPECTED, ECHO_SIMPLE_BODY, response);
189100       assertNotNull(ATTRIBUTE_CONNECTION_MISSING, connectionValue);
190100       assertEquals(CONNECTION_CLOSE_EXPECTED,
191              "close",
192              connectionValue.toLowerCase(Constants.SYSTEM_LOCALE));
193100    }
194  
195     /**
196      * Sends post request without a waiting server.
197      * @throws Exception if the test case fails with an exception.
198      */
199     public void testSendSimplePostRequestToNotExistingTarget ()
200         throws Exception
201     {
202        // connect
2030       mHttpConnection.establishConnection(
204              "http://localhost:23",
205              CONNECT_TIMEOUT, READ_TIMEOUT);
2060       mHttpConnection.setRequestBody(
207              new ByteArrayInputStream(SIMPLE_BODY));
2080       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
2090       header.addRequestHeader(
210              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
2110       header.addRequestHeader(
212              CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
2130       mHttpConnection.setRequestResponseHeader(header);
214  
215        // execute
216        try
217        {
2180          mHttpConnection.execute();
2190          fail("IOException expected");
220        }
221100       catch (HttpConnectConnectionException expected)
222        {
223          // expected
2240       }
225        // close
226100       mHttpConnection.closeConnection();
227100    }
228  
229     /**
230      * Tests if a response header key value pair set for the connection
231      * has been evaluated correctly.
232      *
233      * @throws Exception in case of any unexpected error
234      */
235     public void testSendSimplePostRequestWithExpectedResponseHeaderSuccess ()
236           throws Exception
237     {
238        // connect
239100       mHttpConnection.establishConnection(
240              DEFAULT_URL,
241              CONNECT_TIMEOUT, READ_TIMEOUT);
242100       mHttpConnection.setRequestBody(
243              new ByteArrayInputStream(SIMPLE_BODY));
244100       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
245100       header.addRequestHeader(
246              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
247100       header.addRequestHeader(
248              "ECHO_ResponseHeader1", "expected1");
249100       header.addResponseHeader(
250              "ResponseHeader1", "expected1");
251100       mHttpConnection.setRequestResponseHeader(header);
252  
253        // execute
254100       mHttpConnection.execute();
255100       final String response = getResponseBodyAsString();
256100       final String connectionValue
257              = mHttpConnection.getResponseHeader(CONNECTION_PARAMETER);
258100       final String expectedValue
259              = mHttpConnection.getResponseHeader("ResponseHeader1");
260  
261        // close
262100       mHttpConnection.closeConnection();
263  
264100       assertNotNull(NO_MESSAGE_BODY_IN_RESPONSE, response);
265100       assertEquals(RESPONSE_BODY_NOT_LIKE_EXPECTED, ECHO_SIMPLE_BODY, response);
266100       assertNotNull(ATTRIBUTE_CONNECTION_MISSING, connectionValue);
267100       assertNotNull("Expected response header missing", expectedValue);
268100       assertEquals("Value of expected response header not like expected - ",
269              "expected1",
270              expectedValue.toLowerCase(Constants.SYSTEM_LOCALE));
271100    }
272  
273     /**
274      * Tests if the correct exception is thrown if evaluating a response header
275      * key value pair set for the connection has been failed.
276      *
277      * @throws Exception in case of any unexpected error
278      */
279     public void testSendSimplePostRequestWithExpectedResponseHeaderFailed ()
280           throws Exception
281     {
282        // connect
2830       mHttpConnection.establishConnection(
284              DEFAULT_URL,
285              CONNECT_TIMEOUT, READ_TIMEOUT);
2860       mHttpConnection.setRequestBody(
287              new ByteArrayInputStream(SIMPLE_BODY));
2880       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
2890       header.addRequestHeader(
290              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
2910       header.addRequestHeader(
292              "ECHO_ResponseHeader2", "expected2");
2930       header.addResponseHeader(
294              "ResponseHeader1", "expected2");
2950       header.addResponseHeader(
296              "ResponseHeader2", "wrong value");
2970       mHttpConnection.setRequestResponseHeader(header);
298  
299        // execute
300        try
301        {
3020          mHttpConnection.execute();
3030          fail("Exception expected regarding response header");
304        }
305100       catch (HttpInvalidResponseHeaderException expected)
306        {
307           // expected
3080       }
309100    }
310  
311     /**
312      * Tests if the correct exception is thrown if the http response
313      * body received is empty.
314      *
315      * @throws Exception in case of any unexpected error
316      */
317     public void testSendSimplePostRequestWithEmptyResponseBody ()
318           throws Exception
319     {
320        // connect
3210       mHttpConnection.establishConnection(
322              DEFAULT_URL,
323              CONNECT_TIMEOUT, READ_TIMEOUT);
3240       mHttpConnection.setRequestBody(
325              new ByteArrayInputStream(SIMPLE_BODY));
3260       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
3270       header.addRequestHeader(
328              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
3290       header.addRequestHeader(
330              "UseEmptyResponse", "true");
3310       mHttpConnection.setRequestResponseHeader(header);
332  
333        // execute
334        try
335        {
3360          mHttpConnection.execute();
3370          getResponseBodyAsString();
3380          fail("'HttpEmptyResponseException' expected");
339        }
340100       catch (HttpEmptyResponseException expected)
341        {
342           // expected
3430       }
344  
345        // close
346100       mHttpConnection.closeConnection();
347100    }
348  
349     /**
350      * Sends a request and expects no response.
351      * @throws Exception for any unexpected error
352      */
353     public void testSendSimplePostRequestWithoutResponse ()
354           throws Exception
355     {
356        // connect
3570       mHttpConnection.establishConnection(
358              DEFAULT_URL,
359              CONNECT_TIMEOUT, READ_TIMEOUT);
3600       mHttpConnection.setRequestBody(new ByteArrayInputStream(SIMPLE_BODY));
3610       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
3620       header.addRequestHeader(
363              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
3640       header.addRequestHeader(
365              CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_KEEPALIVE);
3660       header.addRequestHeader(
367              "DoNotRespond", "True");
3680       mHttpConnection.setRequestResponseHeader(header);
369  
370        // execute
3710       byte[] response = null;
372        try
373        {
3740          mHttpConnection.execute();
3750(4)         response = mHttpConnection.getResponseBody();
3760          fail("'HttpTimeoutException' expected.");
377        }
378100       catch (HttpTimeoutConnectionException he)
379        {
380100(5)         assertNull("No response message expected", response);
3810       }
382        // close
383100       mHttpConnection.closeConnection();
384100    }
385  
386     /**
387      * Sends a request and expects a immediate close on server side.
388      * @throws Exception for any unexpected errors
389      */
390     public void testSendSimplePostRequestWithImmediateClose ()
391           throws Exception
392     {
393        // connect
3940       mHttpConnection.establishConnection(
395              DEFAULT_URL,
396              CONNECT_TIMEOUT, READ_TIMEOUT);
3970       mHttpConnection.setRequestBody(new ByteArrayInputStream(SIMPLE_BODY));
3980       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
3990       header.addRequestHeader(
400              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
4010       header.addRequestHeader(
402              CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_KEEPALIVE);
4030       header.addRequestHeader(
404              "DoImmediateClose", "True");
4050       mHttpConnection.setRequestResponseHeader(header);
406  
407        // execute
4080       byte[] response = null;
409        try
410        {
4110          mHttpConnection.execute();
4120(6)         response = mHttpConnection.getResponseBody();
4130          fail("HttpConnectionException expected");
414        }
415100       catch (HttpConnectionException he)
416        {
417100(7)         assertNull("No response message expected", response);
4180       }
419  
420        // close
421100       mHttpConnection.closeConnection();
422100    }
423  
424     /**
425      * Sends a request with Connection=close and receives the
426      * response.
427      * @throws Exception for any unexpected error
428      */
429     public void testSendMultiplePostRequestAfterServerClose ()
430           throws Exception
431     {
432        // connect
433100       mHttpConnection.establishConnection(
434              DEFAULT_URL,
435              CONNECT_TIMEOUT, READ_TIMEOUT);
436  
437        // set message 1
438100       mHttpConnection.setRequestBody(new ByteArrayInputStream(SIMPLE_BODY));
439100       HttpRequestResponseHeader header = new HttpRequestResponseHeader();
440100       header.addRequestHeader(
441              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
442100       header.addRequestHeader(
443              CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
444100       mHttpConnection.setRequestResponseHeader(header);
445  
446100       String response = null;
447100       String connectionValue = null;
448  
449        // execute message 1
450100       mHttpConnection.execute();
451100       response = getResponseBodyAsString();
452100       connectionValue = mHttpConnection.getResponseHeader(CONNECTION_PARAMETER);
453  
454        // check reponse 1
455100       assertNotNull(NO_MESSAGE_BODY_IN_RESPONSE, response);
456100       assertEquals(RESPONSE_BODY_NOT_LIKE_EXPECTED, ECHO_SIMPLE_BODY, response);
457100       assertNotNull(ATTRIBUTE_CONNECTION_MISSING, connectionValue);
458100       assertEquals(CONNECTION_CLOSE_EXPECTED,
459              "close",
460              connectionValue.toLowerCase(Constants.SYSTEM_LOCALE));
461  
462        // release connection
463100       mHttpConnection.releaseConnection();
464  
465        // set message 2
466100       mHttpConnection.setRequestBody(
467              new ByteArrayInputStream(
468                    "This is the second POST request".getBytes()));
469100       header = new HttpRequestResponseHeader();
470100       header.addRequestHeader(
471              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
472100       header.addRequestHeader(
473              CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_KEEPALIVE);
474100       mHttpConnection.setRequestResponseHeader(header);
475  
476        // execute message 2
477100       mHttpConnection.execute();
478100       response = getResponseBodyAsString();
479  
480        // release connection
481100       mHttpConnection.releaseConnection();
482  
483        // set message 3
484100       mHttpConnection.setRequestBody(
485              new ByteArrayInputStream(
486                 "This is the third POST request".getBytes()));
487100       header = new HttpRequestResponseHeader();
488100       header.addRequestHeader(
489              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
490100       header.addRequestHeader(
491              CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_KEEPALIVE);
492100       mHttpConnection.setRequestResponseHeader(header);
493  
494        // execute message 3
495100       mHttpConnection.execute();
496100(8)      response = getResponseBodyAsString();
497100       mHttpConnection.closeConnection();
498100    }
499  
500     /**
501      * Tests establishing a connection. Checks one simple good case and several
502      * cases that leads to an ArgumentMalformedException caused by an
503      * unsufficient URL parameter.
504      */
505     public void testEstablishConnection ()
506     {
507        // simple connect success
5080       mHttpConnection.establishConnection(
509              "http://subwayca/php3/index.php3",
510              CONNECT_TIMEOUT, READ_TIMEOUT);
5110       mHttpConnection.closeConnection();
512        //
513  
514        // "invalid" argument leads to ArgumentMalformedException
5150       final String invalidUrl = "malformed url";
516        try
517        {
5180          mHttpConnection.establishConnection(
519                 invalidUrl, CONNECT_TIMEOUT, READ_TIMEOUT);
5200          fail("ArgumentMalformedException expected");
521        }
522100       catch (ArgumentMalformedException expected)
523        {
524           //expected
5250       }
526        //
527  
528        // "null" argument leads to ArgumentMalformedException
529        try
530        {
5310          mHttpConnection.establishConnection(
532                 null, CONNECT_TIMEOUT, READ_TIMEOUT);
5330          fail("ArgumentMalformedException expected");
534        }
535100       catch (ArgumentMalformedException expected)
536        {
537           // expected
5380       }
539        //
540  
541        // useless timeout values
542100       mHttpConnection.establishConnection(
543              "http://www.heise.de", -1, -1);
544  
545        // close
546100       mHttpConnection.closeConnection();
547100    }
548  
549     /**
550      * Releases a connection multiple times.
551      */
552     public void testReleaseConnectionSuccessMultipleUse ()
553     {
554        // connect
555100       mHttpConnection.establishConnection(
556              DEFAULT_URL,
557              CONNECT_TIMEOUT, READ_TIMEOUT);
558         // multiple release
559100       mHttpConnection.releaseConnection();
560100       mHttpConnection.releaseConnection();
561  
562        // close
563100       mHttpConnection.closeConnection();
564100    }
565  
566     /**
567      * Tests the release of connection after sending a message and a sending anew
568      * after releasing.
569      * @throws Exception if the test case fails with an exception.
570      */
571     public void testReleaseConnectionSuccessWithTwoSendMessages ()
572         throws Exception
573     {
574        // connect
575100       mHttpConnection.establishConnection(
576              DEFAULT_URL,
577              CONNECT_TIMEOUT, READ_TIMEOUT);
578  
579        // set message 1
580100       HttpRequestResponseHeader header = new HttpRequestResponseHeader();
581100       header.addRequestHeader(
582              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
583100       header.addRequestHeader(
584              "Message1", "Nachricht 1");
585100       mHttpConnection.setRequestResponseHeader(header);
586  
587100       mHttpConnection.setRequestBody(
588              new ByteArrayInputStream("This is message one".getBytes()));
589  
590        // send message 1
591100       mHttpConnection.execute();
592  
593        // release connection
594100       mHttpConnection.releaseConnection();
595  
596        // set message 2
597100       header = new HttpRequestResponseHeader();
598100       header.addRequestHeader(
599              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
600100       header.addRequestHeader(
601              CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
602100       header.addRequestHeader(
603              "Message2", "Nachricht 2");
604100       mHttpConnection.setRequestResponseHeader(header);
605  
606        // send message 2
607100       mHttpConnection.execute();
608  
609        // close
610100       mHttpConnection.closeConnection();
611100    }
612  
613     /**
614      * Tests releasing the connection and sending a second message without
615      * sending the first message.
616      * @throws Exception if the test case fails with an exception.
617      */
618     public void testReleaseConnectionSuccessWithOneSendMessage ()
619         throws Exception
620     {
621        // connect
622100       mHttpConnection.establishConnection(
623              DEFAULT_URL,
624              CONNECT_TIMEOUT, READ_TIMEOUT);
625  
626        // set message 1
627100       HttpRequestResponseHeader header = new HttpRequestResponseHeader();
628100       header.addRequestHeader(
629              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
630100       header.addRequestHeader(
631              "Message1", "Nachricht 1");
632100       mHttpConnection.setRequestResponseHeader(header);
633  
634100       mHttpConnection.setRequestBody(
635              new ByteArrayInputStream("This is message one".getBytes()));
636  
637        // release connection
638100       mHttpConnection.releaseConnection();
639  
640        // set message 2
641100       header = new HttpRequestResponseHeader();
642100       header.addRequestHeader(
643              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
644100       header.addRequestHeader(
645              CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
646100       header.addRequestHeader(
647              "Message2", "Nachricht 2");
648100       mHttpConnection.setRequestResponseHeader(header);
649  
650        // send message 2
651100       mHttpConnection.execute();
652100    }
653  
654     /**
655      * Tests error case with a missing "releaseConnection" call before reusing
656      * an already opened connection.
657      * @throws Exception if the test case fails with an exception.
658      */
659     public void testReleaseConnectionFailed ()
660         throws Exception
661     {
662        // connect
6630       mHttpConnection.establishConnection(
664              DEFAULT_URL,
665              CONNECT_TIMEOUT, READ_TIMEOUT);
666  
667        // set message 1
6680       HttpRequestResponseHeader header = new HttpRequestResponseHeader();
6690       header.addRequestHeader(
670              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
6710       header.addRequestHeader(
672              "Message1", "Nachricht 1");
6730       mHttpConnection.setRequestResponseHeader(header);
674  
6750       mHttpConnection.setRequestBody(
676              new ByteArrayInputStream("This is message one".getBytes()));
677  
678        // send message 1
6790       mHttpConnection.execute();
680  
681        // set message 2
6820       header = new HttpRequestResponseHeader();
6830       header.addRequestHeader(
684              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
6850       header.addRequestHeader(
686              "Message2", "Nachricht 2");
6870       mHttpConnection.setRequestResponseHeader(header);
688  
689        // send message 2
690        try
691        {
6920          mHttpConnection.execute();
6930          fail(ILLEGALSTATEEXCEPTION_EXPECTED);
694        }
695100       catch (IllegalStateException ise)
696        {
697100          assertEquals(ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED,
698                 CONNECTION_MUST_BE_ESTABLISHED_OR_RELEASED,
699                 ise.getMessage());
7000       }
701        // close
702100       mHttpConnection.closeConnection();
703100    }
704  
705     /**
706      * Release a not established connection.
707      */
708     public void testReleaseConnectionWithInvalidState ()
709     {
710        // invalid state
711        try
712        {
7130          mHttpConnection.releaseConnection();
7140          fail("IllegalStatException expected");
715        }
716100       catch (IllegalStateException ise)
717        {
718100          assertEquals(ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED,
719                 "Connection must be established before",
720                 ise.getMessage());
7210       }
722100    }
723      /**
724      * Closes the connection after successful sending.
725      * @throws Exception if the test case fails with an exception.
726      */
727     public void testCloseConnectionSuccessAfterSending ()
728         throws Exception
729     {
730100       mHttpConnection.establishConnection(
731              DEFAULT_URL,
732              CONNECT_TIMEOUT, READ_TIMEOUT);
733  
734        // set message
735100       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
736100       header.addRequestHeader(
737              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
738100       header.addRequestHeader(
739              CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
740100       mHttpConnection.setRequestResponseHeader(header);
741  
742100       mHttpConnection.setRequestBody(
743              new ByteArrayInputStream("This is the message".getBytes()));
744  
745100       mHttpConnection.execute();
746100       mHttpConnection.closeConnection();
747100    }
748  
749     /**
750      * Multiple closing fails.
751      */
752     public void testCloseConnectionFailed ()
753     {
754        // connection not established
755        try
756        {
7570          mHttpConnection.closeConnection();
7580          fail(ILLEGALSTATEEXCEPTION_EXPECTED);
759        }
760100       catch (IllegalStateException ise)
761        {
762100          assertEquals(ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED,
763                 "Connection must be established before",
764                 ise.getMessage());
7650       }
766  
767        // closed twice
7680       mHttpConnection.establishConnection(
769              "http://subwayca:80",
770              CONNECT_TIMEOUT, READ_TIMEOUT);
771        try
772        {
7730          mHttpConnection.closeConnection();
7740          mHttpConnection.closeConnection();
7750          fail(ILLEGALSTATEEXCEPTION_EXPECTED);
776        }
777100       catch (IllegalStateException ise)
778        {
779100          assertEquals(ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED,
780                 "Connection must be established before",
781                 ise.getMessage());
7820       }
783100    }
784  
785     /**
786      * Executes connection without establishing before.
787      * @throws Exception if the test case fails with an exception.
788      */
789     public void testExecuteFailed ()
790         throws Exception
791     {
792        // not connected
793        try
794        {
7950          mHttpConnection.execute();
7960          fail(ILLEGALSTATEEXCEPTION_EXPECTED);
797        }
798100       catch (IllegalStateException ise)
799        {
800100          assertEquals(ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED,
801                 CONNECTION_MUST_BE_ESTABLISHED_OR_RELEASED,
802                 ise.getMessage());
8030       }
804  
805        // executed twice
8060       mHttpConnection.establishConnection(
807              DEFAULT_URL,
808              CONNECT_TIMEOUT, READ_TIMEOUT);
809  
8100       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
8110       header.addRequestHeader(
812              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
8130       mHttpConnection.setRequestResponseHeader(header);
814  
8150       mHttpConnection.setRequestBody(
816              new ByteArrayInputStream("This is the message".getBytes()));
817  
818        try
819        {
8200          mHttpConnection.execute();
8210          mHttpConnection.execute();
8220          fail(ILLEGALSTATEEXCEPTION_EXPECTED);
823        }
824100       catch (IllegalStateException ise)
825        {
826100          assertEquals(ILLEGAL_STATE_MESSAGE_NOT_LIKE_EXPECTED,
827                 CONNECTION_MUST_BE_ESTABLISHED_OR_RELEASED,
828                 ise.getMessage());
8290       }
830100       mHttpConnection.closeConnection();
831100    }
832  
833     /**
834      * Performs establishing a connection (to SubwayCA) , creating a request,
835      * sending that request, receiving the response and closing the connection
836      * USING SSL.
837      * @throws Exception
838    * in case of missing keystore
839      */
840     public void testSendSimplePostRequestWithSSL ()
841           throws Exception
842     {
843100       final String keyStoreFilename = getBaseDir()
844              + FILE_SEPARATOR + "test" + FILE_SEPARATOR + "data"
845              + FILE_SEPARATOR + "ssl_store.jks";
846100       final String trustStoreFilename = getBaseDir()
847              + FILE_SEPARATOR + "test" + FILE_SEPARATOR + "data"
848              + FILE_SEPARATOR + "fwk_trusted.jks";
849100       System.setProperty("javax.net.ssl.keyStore", keyStoreFilename);
850100       System.setProperty("javax.net.ssl.trustStore", trustStoreFilename);
851100       System.setProperty("javax.net.ssl.trustStorePassword", "fawkez42");
852100       System.setProperty("javax.net.ssl.keyStorePassword", "fawkez42");
853100       mHttpConnection.initSsl("ssl", "sslssl");
854  
855        // connect
856100       mHttpConnection.establishConnection(DEFAULT_URL,
857              CONNECT_TIMEOUT, READ_TIMEOUT);
858100       mHttpConnection.setRequestBody(new ByteArrayInputStream(SIMPLE_BODY));
859  
860100       final HttpRequestResponseHeader header = new HttpRequestResponseHeader();
861100       header.addRequestHeader(
862              CONTENT_TYPE_PARAMETER, CONTENT_TYPE_PARAMETER_VALUE);
863100       header.addRequestHeader(
864              CONNECTION_PARAMETER, CONNECTION_PARAMETER_VALUE_CLOSE);
865100       mHttpConnection.setRequestResponseHeader(header);
866  
867        // execute
868100       mHttpConnection.execute();
869100       final String response = getResponseBodyAsString();
870100       final String connectionValue
871              = mHttpConnection.getResponseHeader(CONNECTION_PARAMETER);
872  
873        // close
874100       mHttpConnection.closeConnection();
875  
876100       assertNotNull(NO_MESSAGE_BODY_IN_RESPONSE, response);
877100       assertNotNull(ATTRIBUTE_CONNECTION_MISSING, connectionValue);
878100       assertEquals(CONNECTION_CLOSE_EXPECTED,
879              "close",
880              connectionValue.toLowerCase(Constants.SYSTEM_LOCALE));
881100    }
882  
883     /**
884      * Gets the response body as String.
885      * @return String
886      *          the response body as string
887      */
888     private String getResponseBodyAsString ()
889           throws HttpEmptyResponseException
890     {
891100       return StringUtil.toString(mHttpConnection.getResponseBody());
892     }
893  }

Findings in this File

i (9) HttpClientConnectionTest.mHttpConnection not initialized in constructor (test code)
i (1) 133 : 0 method org.jcoderz.commons.connector.http.transport.HttpClientConnectionTest.suite() stores return result in local before immediately returning it (test code) Decreased severity from 'warning' for testcode.
c (2) 133 : 7 Consider simply returning the value vs storing it in local variable 'setup'
d (3) 151 : 14 Overriding method merely calls super
i (4) 375 : 0 Dead store to response in org.jcoderz.commons.connector.http.transport.HttpClientConnectionTest.testSendSimplePostRequestWithoutResponse() (test code) Decreased severity from 'warning' for testcode.
i (5) 380 : 0 Load of known null value in org.jcoderz.commons.connector.http.transport.HttpClientConnectionTest.testSendSimplePostRequestWithoutResponse() (test code)
i (6) 412 : 0 Dead store to response in org.jcoderz.commons.connector.http.transport.HttpClientConnectionTest.testSendSimplePostRequestWithImmediateClose() (test code) Decreased severity from 'warning' for testcode.
i (7) 417 : 0 Load of known null value in org.jcoderz.commons.connector.http.transport.HttpClientConnectionTest.testSendSimplePostRequestWithImmediateClose() (test code)
i (8) 496 : 0 Dead store to response in org.jcoderz.commons.connector.http.transport.HttpClientConnectionTest.testSendMultiplePostRequestAfterServerClose() (test code)