Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 42B651815B for ; Sun, 12 Jul 2015 07:27:31 +0000 (UTC) Received: (qmail 83818 invoked by uid 500); 12 Jul 2015 07:27:31 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 83689 invoked by uid 500); 12 Jul 2015 07:27:31 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 83460 invoked by uid 99); 12 Jul 2015 07:27:31 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 12 Jul 2015 07:27:31 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id B1A8FE35C6; Sun, 12 Jul 2015 07:27:30 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@camel.apache.org Date: Sun, 12 Jul 2015 07:27:35 -0000 Message-Id: <1dc29b5b89b641088f1f014625099c3a@git.apache.org> In-Reply-To: <310c8f99bdd14c7b9aaec0012e72c573@git.apache.org> References: <310c8f99bdd14c7b9aaec0012e72c573@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [6/7] camel git commit: CAMEL-8941 use try-close for more stable restlet tests 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 Authored: Sat Jul 11 18:16:00 2015 +0200 Committer: Claus Ibsen 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());