You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by se...@apache.org on 2014/03/15 15:23:08 UTC

svn commit: r1577865 - /jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java

Author: sebb
Date: Sat Mar 15 14:23:08 2014
New Revision: 1577865

URL: http://svn.apache.org/r1577865
Log:
Close streams; trim trailing spaces

Modified:
    jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java

Modified: jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java?rev=1577865&r1=1577864&r2=1577865&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java (original)
+++ jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java Sat Mar 15 14:23:08 2014
@@ -13,7 +13,7 @@
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- * 
+ *
  */
 
 package org.apache.jmeter.protocol.http.control;
@@ -52,14 +52,14 @@ public class TestHTTPMirrorThread extend
     // We need to use a suite in order to preserve the server across test cases
     // With JUnit4 we could use before/after class annotations
     public static Test suite(){
-        TestSetup setup = new TestSetup(new TestSuite(TestHTTPMirrorThread.class)){         
+        TestSetup setup = new TestSetup(new TestSuite(TestHTTPMirrorThread.class)){
             private HttpMirrorServer httpServer;
-            
+
             @Override
             protected void setUp() throws Exception {
                 httpServer = startHttpMirror(HTTP_SERVER_PORT);
             }
-            
+
             @Override
             protected void tearDown() throws Exception {
                 // Shutdown the http server
@@ -92,19 +92,19 @@ public class TestHTTPMirrorThread extend
                 break; // succeeded
             }
         }
-        
+
         if (!server.isAlive()){
             throw new Exception("Could not start mirror server on port: "+port);
         }
         return server;
     }
 
-    public void testGetRequest() throws Exception {        
+    public void testGetRequest() throws Exception {
         // Connect to the http server, and do a simple http get
         Socket clientSocket = new Socket("localhost", HTTP_SERVER_PORT);
         OutputStream outputStream = clientSocket.getOutputStream();
         InputStream inputStream = clientSocket.getInputStream();
-        
+
         // Write to the socket
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         // Headers
@@ -115,19 +115,21 @@ public class TestHTTPMirrorThread extend
         bos.write(CRLF);
         bos.close();
         outputStream.write(bos.toByteArray());
-        
+
         // Read the response
-        ByteArrayOutputStream response = new ByteArrayOutputStream();        
+        ByteArrayOutputStream response = new ByteArrayOutputStream();
         byte[] buffer = new byte[1024];
         int length = 0;
         while(( length = inputStream.read(buffer)) != -1) {
             response.write(buffer, 0, length);
         }
         response.close();
-        byte[] mirroredResponse = getMirroredResponse(response.toByteArray());          
+        byte[] mirroredResponse = getMirroredResponse(response.toByteArray());
         // Check that the request and response matches
         checkArraysHaveSameContent(bos.toByteArray(), mirroredResponse);
         // Close the connection
+        outputStream.close();
+        inputStream.close();
         clientSocket.close();
 
         // Connect to the http server, and do a simple http get, with
@@ -135,7 +137,7 @@ public class TestHTTPMirrorThread extend
         clientSocket = new Socket("localhost", HTTP_SERVER_PORT);
         outputStream = clientSocket.getOutputStream();
         inputStream = clientSocket.getInputStream();
-        
+
         // Write to the socket
         bos = new ByteArrayOutputStream();
         // Headers
@@ -144,7 +146,7 @@ public class TestHTTPMirrorThread extend
         // Write the start of the headers, and then sleep, so that the mirror
         // thread will have to block to wait for more data to appear
         bos.close();
-        byte[] firstChunk = bos.toByteArray(); 
+        byte[] firstChunk = bos.toByteArray();
         outputStream.write(firstChunk);
         Thread.sleep(300);
         // Write the rest of the headers
@@ -153,10 +155,10 @@ public class TestHTTPMirrorThread extend
         bos.write(CRLF);
         bos.write(CRLF);
         bos.close();
-        byte[] secondChunk = bos.toByteArray(); 
+        byte[] secondChunk = bos.toByteArray();
         outputStream.write(secondChunk);
         // Read the response
-        response = new ByteArrayOutputStream();        
+        response = new ByteArrayOutputStream();
         buffer = new byte[1024];
         length = 0;
         while((length = inputStream.read(buffer)) != -1) {
@@ -168,11 +170,13 @@ public class TestHTTPMirrorThread extend
         bos = new ByteArrayOutputStream();
         bos.write(firstChunk);
         bos.write(secondChunk);
-        bos.close();        
+        bos.close();
         // Check that the request and response matches
         checkArraysHaveSameContent(bos.toByteArray(), mirroredResponse);
         // Close the connection
-        clientSocket.close();        
+        outputStream.close();
+        inputStream.close();
+        clientSocket.close();
     }
 
     public void testPostRequest() throws Exception {
@@ -186,7 +190,7 @@ public class TestHTTPMirrorThread extend
             postBodyBuffer.append("abc");
         }
         byte[] postBody = postBodyBuffer.toString().getBytes(ISO_8859_1);
-        
+
         // Write to the socket
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         // Headers
@@ -204,17 +208,19 @@ public class TestHTTPMirrorThread extend
         // Write the headers and body
         outputStream.write(bos.toByteArray());
         // Read the response
-        ByteArrayOutputStream response = new ByteArrayOutputStream();        
+        ByteArrayOutputStream response = new ByteArrayOutputStream();
         byte[] buffer = new byte[1024];
         int length = 0;
         while((length = inputStream.read(buffer)) != -1) {
             response.write(buffer, 0, length);
         }
         response.close();
-        byte[] mirroredResponse = getMirroredResponse(response.toByteArray());          
+        byte[] mirroredResponse = getMirroredResponse(response.toByteArray());
         // Check that the request and response matches
         checkArraysHaveSameContent(bos.toByteArray(), mirroredResponse);
         // Close the connection
+        outputStream.close();
+        inputStream.close();
         clientSocket.close();
 
         // Connect to the http server, and do a simple http post, with
@@ -222,7 +228,7 @@ public class TestHTTPMirrorThread extend
         clientSocket = new Socket("localhost", HTTP_SERVER_PORT);
         outputStream = clientSocket.getOutputStream();
         inputStream = clientSocket.getInputStream();
-        
+
         // Write to the socket
         bos = new ByteArrayOutputStream();
         // Headers
@@ -238,15 +244,15 @@ public class TestHTTPMirrorThread extend
         bos.close();
         // Write the headers, and then sleep
         bos.close();
-        byte[] firstChunk = bos.toByteArray(); 
+        byte[] firstChunk = bos.toByteArray();
         outputStream.write(firstChunk);
         Thread.sleep(300);
-        
+
         // Write the body
-        byte[] secondChunk = postBody;         
+        byte[] secondChunk = postBody;
         outputStream.write(secondChunk);
         // Read the response
-        response = new ByteArrayOutputStream();        
+        response = new ByteArrayOutputStream();
         buffer = new byte[1024];
         length = 0;
         while((length = inputStream.read(buffer)) != -1) {
@@ -258,12 +264,14 @@ public class TestHTTPMirrorThread extend
         bos = new ByteArrayOutputStream();
         bos.write(firstChunk);
         bos.write(secondChunk);
-        bos.close();        
+        bos.close();
         // Check that the request and response matches
         checkArraysHaveSameContent(bos.toByteArray(), mirroredResponse);
         // Close the connection
+        outputStream.close();
+        inputStream.close();
         clientSocket.close();
-        
+
         // Connect to the http server, and do a simple http post with utf-8
         // encoding of the body, which caused problems when reader/writer
         // classes were used in the HttpMirrorThread
@@ -276,7 +284,7 @@ public class TestHTTPMirrorThread extend
             postBodyBuffer.append("\u0364\u00c5\u2052");
         }
         postBody = postBodyBuffer.toString().getBytes(UTF_8);
-        
+
         // Write to the socket
         bos = new ByteArrayOutputStream();
         // Headers
@@ -292,15 +300,15 @@ public class TestHTTPMirrorThread extend
         bos.close();
         // Write the headers, and then sleep
         bos.close();
-        firstChunk = bos.toByteArray(); 
+        firstChunk = bos.toByteArray();
         outputStream.write(firstChunk);
         Thread.sleep(300);
-        
+
         // Write the body
-        secondChunk = postBody;         
+        secondChunk = postBody;
         outputStream.write(secondChunk);
         // Read the response
-        response = new ByteArrayOutputStream();        
+        response = new ByteArrayOutputStream();
         buffer = new byte[1024];
         length = 0;
         while((length = inputStream.read(buffer)) != -1) {
@@ -313,16 +321,19 @@ public class TestHTTPMirrorThread extend
         bos.write(firstChunk);
         bos.write(secondChunk);
         bos.close();
-        // Check that the request and response matches       
+        // Check that the request and response matches
         checkArraysHaveSameContent(bos.toByteArray(), mirroredResponse);
         // Close the connection
+        outputStream.close();
+        inputStream.close();
         clientSocket.close();
     }
+
 /*
     public void testPostRequestChunked() throws Exception {
         // TODO - implement testing of chunked post request
     }
-*/    
+*/
 
     public void testStatus() throws Exception {
         URL url = new URL("http", "localhost", HTTP_SERVER_PORT, "/");
@@ -377,10 +388,10 @@ public class TestHTTPMirrorThread extend
 
     /**
      * Check that the the two byte arrays have identical content
-     * 
+     *
      * @param expected
      * @param actual
-     * @throws UnsupportedEncodingException 
+     * @throws UnsupportedEncodingException
      */
     private void checkArraysHaveSameContent(byte[] expected, byte[] actual) throws UnsupportedEncodingException {
         if(expected != null && actual != null) {
@@ -410,7 +421,7 @@ public class TestHTTPMirrorThread extend
                             System.out.print(actual[j] + " ");
                         }
                         System.out.println();
-*/                      
+*/
                         fail("byte at position " + i + " is different, expected is " + expected[i] + ", actual is " + actual[i]);
                     }
                 }
@@ -420,7 +431,7 @@ public class TestHTTPMirrorThread extend
             fail("expected or actual byte arrays were null");
         }
     }
-    
+
     private byte[] getMirroredResponse(byte[] allResponse) {
         // The response includes the headers from the mirror server,
         // we want to skip those, to only keep the content mirrored.
@@ -433,8 +444,8 @@ public class TestHTTPMirrorThread extend
                 break;
             }
         }
-        byte[] mirrorResponse = new byte[allResponse.length - startOfMirrorResponse]; 
+        byte[] mirrorResponse = new byte[allResponse.length - startOfMirrorResponse];
         System.arraycopy(allResponse, startOfMirrorResponse, mirrorResponse, 0, mirrorResponse.length);
-        return mirrorResponse;      
+        return mirrorResponse;
     }
 }