CAMEL-8941 use try-close for more stable restlet tests

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8b4d5f0f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8b4d5f0f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8b4d5f0f

Branch: refs/heads/master
Commit: 8b4d5f0f98d546b7fed693090700ea5a03d70d7c
Parents: c1753c7
Author: Anton Koscejev <anton.kosce...@zoomint.com>
Authored: Sat Jul 11 18:16:00 2015 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Sun Jul 12 09:31:20 2015 +0200

----------------------------------------------------------------------
 .../component/restlet/RestletSetBodyTest.java   | 60 +++++++-------------
 1 file changed, 21 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/8b4d5f0f/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletSetBodyTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletSetBodyTest.java
 
b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletSetBodyTest.java
index 798e3f9..b5e16dd 100644
--- 
a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletSetBodyTest.java
+++ 
b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletSetBodyTest.java
@@ -37,7 +37,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
 import static org.junit.Assume.assumeThat;
 
 /**
- * @version 
+ * @version
  */
 public class RestletSetBodyTest extends RestletTestSupport {
     protected static int portNum2 =  
AvailablePortFinder.getNextAvailable(4000);
@@ -47,25 +47,19 @@ public class RestletSetBodyTest extends RestletTestSupport {
         String response = template.requestBody("restlet:http://localhost:"; + 
portNum + "/stock/ORCL?restletMethod=get", null, String.class);
         assertEquals("110", response);
     }
-    
+
     @Test
     public void testSetBodyRepresentation() throws Exception {
         HttpGet get = new HttpGet("http://localhost:"; + portNum + 
"/images/123");
-        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
-        InputStream is = null;
-        try {
+        try (CloseableHttpClient httpclient = 
HttpClientBuilder.create().build()) {
             HttpResponse response = httpclient.execute(get);
             assertEquals(200, response.getStatusLine().getStatusCode());
             assertEquals("image/png", 
response.getEntity().getContentType().getValue());
-            is = response.getEntity().getContent();
             assertEquals("Get wrong available size", 256, 
response.getEntity().getContentLength());
-            byte[] buffer = new byte[256];
-            assumeThat("Should read all data", is.read(buffer), equalTo(256));
-            assertThat("Data should match", buffer, equalTo(getAllBytes()));
-        } finally {
-            httpclient.close();
-            if (is != null) {
-                is.close();
+            try (InputStream is = response.getEntity().getContent()) {
+                byte[] buffer = new byte[256];
+                assumeThat("Should read all data", is.read(buffer), 
equalTo(256));
+                assertThat("Data should match", buffer, 
equalTo(getAllBytes()));
             }
         }
     }
@@ -73,21 +67,15 @@ public class RestletSetBodyTest extends RestletTestSupport {
     @Test
     public void consumerShouldReturnByteArray() throws Exception {
         HttpGet get = new HttpGet("http://localhost:"; + portNum + 
"/music/123");
-        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
-        InputStream is = null;
-        try {
+        try (CloseableHttpClient httpclient = 
HttpClientBuilder.create().build()) {
             HttpResponse response = httpclient.execute(get);
             assertEquals(200, response.getStatusLine().getStatusCode());
             assertEquals("audio/mpeg", 
response.getEntity().getContentType().getValue());
-            is = response.getEntity().getContent();
             assertEquals("Content length should match returned data", 256, 
response.getEntity().getContentLength());
-            byte[] buffer = new byte[256];
-            assumeThat("Should read all data", is.read(buffer), equalTo(256));
-            assertThat("Binary content should match", buffer, 
equalTo(getAllBytes()));
-        } finally {
-            httpclient.close();
-            if (is != null) {
-                is.close();
+            try (InputStream is = response.getEntity().getContent()) {
+                byte[] buffer = new byte[256];
+                assumeThat("Should read all data", is.read(buffer), 
equalTo(256));
+                assertThat("Binary content should match", buffer, 
equalTo(getAllBytes()));
             }
         }
     }
@@ -95,32 +83,26 @@ public class RestletSetBodyTest extends RestletTestSupport {
     @Test
     public void consumerShouldReturnInputStream() throws Exception {
         HttpGet get = new HttpGet("http://localhost:"; + portNum + 
"/video/123");
-        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
-        InputStream is = null;
-        try {
+        try (CloseableHttpClient httpclient = 
HttpClientBuilder.create().build()) {
             HttpResponse response = httpclient.execute(get);
             assertEquals(200, response.getStatusLine().getStatusCode());
             assertEquals("video/mp4", 
response.getEntity().getContentType().getValue());
             assertTrue("Content should be streamed", 
response.getEntity().isChunked());
             assertEquals("Content length should be unknown", -1, 
response.getEntity().getContentLength());
-            is = response.getEntity().getContent();
-            byte[] buffer = new byte[256];
-            assumeThat("Should read all data", is.read(buffer), equalTo(256));
-            assertThat("Binary content should match", buffer, 
equalTo(getAllBytes()));
-        } finally {
-            httpclient.close();
-            if (is != null) {
-                is.close();
+            try (InputStream is = response.getEntity().getContent()) {
+                byte[] buffer = new byte[256];
+                assumeThat("Should read all data", is.read(buffer), 
equalTo(256));
+                assertThat("Binary content should match", buffer, 
equalTo(getAllBytes()));
             }
         }
     }
-    
+
     @Test
     public void testGzipEntity() {
         String response = template.requestBody("restlet:http://localhost:"; + 
portNum + "/gzip/data?restletMethod=get", null, String.class);
         assertEquals("Hello World!", response);
     }
-    
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
@@ -130,14 +112,14 @@ public class RestletSetBodyTest extends 
RestletTestSupport {
                     .to("http://localhost:"; + portNum2 + 
"/test?bridgeEndpoint=true")
                     //.removeHeader("Transfer-Encoding")
                     .setBody().constant("110");
-                
+
                 from("jetty:http://localhost:"; + portNum2 + 
"/test").setBody().constant("response is back");
 
                 // create ByteArrayRepresentation for response
                 from("restlet:http://localhost:"; + portNum + 
"/images/{symbol}?restletMethods=get")
                     .setBody().constant(new InputRepresentation(
                         new ByteArrayInputStream(getAllBytes()), 
MediaType.IMAGE_PNG, 256));
-                
+
                 from("restlet:http://localhost:"; + portNum + 
"/music/{symbol}?restletMethods=get")
                     .setHeader(Exchange.CONTENT_TYPE).constant("audio/mpeg")
                     .setBody().constant(getAllBytes());

Reply via email to