Return-Path: X-Original-To: apmail-ace-commits-archive@www.apache.org Delivered-To: apmail-ace-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 60C6310135 for ; Wed, 18 Sep 2013 12:23:39 +0000 (UTC) Received: (qmail 21870 invoked by uid 500); 18 Sep 2013 12:23:39 -0000 Delivered-To: apmail-ace-commits-archive@ace.apache.org Received: (qmail 21844 invoked by uid 500); 18 Sep 2013 12:23:35 -0000 Mailing-List: contact commits-help@ace.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ace.apache.org Delivered-To: mailing list commits@ace.apache.org Received: (qmail 21832 invoked by uid 99); 18 Sep 2013 12:23:32 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 18 Sep 2013 12:23:32 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 18 Sep 2013 12:23:31 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 556A42388A02; Wed, 18 Sep 2013 12:23:11 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1524386 - /ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ContentRangeInputStreamTest.java Date: Wed, 18 Sep 2013 12:23:11 -0000 To: commits@ace.apache.org From: jawi@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20130918122311.556A42388A02@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jawi Date: Wed Sep 18 12:23:10 2013 New Revision: 1524386 URL: http://svn.apache.org/r1524386 Log: Added extra tests for non-HTTP URLConnections. Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ContentRangeInputStreamTest.java Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ContentRangeInputStreamTest.java URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ContentRangeInputStreamTest.java?rev=1524386&r1=1524385&r2=1524386&view=diff ============================================================================== --- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ContentRangeInputStreamTest.java (original) +++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ContentRangeInputStreamTest.java Wed Sep 18 12:23:10 2013 @@ -21,6 +21,8 @@ package org.apache.ace.agent.impl; import static org.testng.Assert.assertEquals; import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; @@ -232,9 +234,9 @@ public class ContentRangeInputStreamTest /** Stub implementation that simply opens all given URLs. */ private static class TestConnectionHandler implements ConnectionHandler { - private final HttpURLConnection m_conn; + private final URLConnection m_conn; - public TestConnectionHandler(HttpURLConnection conn) { + public TestConnectionHandler(URLConnection conn) { m_conn = conn; } @@ -308,6 +310,18 @@ public class ContentRangeInputStreamTest } /** + * Tests that we call {@link InputStream#close()} multiple times. + */ + @Test + public void testDoubleClosedStreamOk() throws Exception { + ConnectionHandler handler = new TestConnectionHandler(new CompleteContentConnection(m_content, true)); + + ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); + is.close(); // simulate an early close... + is.close(); // not a problem... + } + + /** * Tests that the "Range" header is correctly set. */ @Test @@ -409,6 +423,54 @@ public class ContentRangeInputStreamTest * Tests that we can read non-partial content and return the expected contents. */ @Test + public void testReadNonPartialFileContentByteForByteOk() throws Exception { + File file = File.createTempFile("cris", ".tmp"); + file.deleteOnExit(); + + FileOutputStream fos = new FileOutputStream(file); + fos.write(m_content.getBytes()); + fos.close(); + + ConnectionHandler handler = new TestConnectionHandler(file.toURI().toURL().openConnection()); + ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL); + + assertEquals(slurpAsStringByteForByte(is), m_content); + + // try several additional reads, which should all return -1 (= EOF)... + int tries = 5; + while (--tries > 0) { + assertEquals(is.read(), -1); + } + } + + /** + * Tests that we can read non-partial content and return the expected contents. + */ + @Test + public void testReadNonPartialFileContentByteForByteWithOffsetOk() throws Exception { + File file = File.createTempFile("cris", ".tmp"); + file.deleteOnExit(); + + FileOutputStream fos = new FileOutputStream(file); + fos.write(m_content.getBytes()); + fos.close(); + + ConnectionHandler handler = new TestConnectionHandler(file.toURI().toURL().openConnection()); + ContentRangeInputStream is = new ContentRangeInputStream(handler, m_testURL, 48); + + assertEquals(slurpAsStringByteForByte(is), m_content.substring(48)); + + // try several additional reads, which should all return -1 (= EOF)... + int tries = 5; + while (--tries > 0) { + assertEquals(is.read(), -1); + } + } + + /** + * Tests that we can read non-partial content and return the expected contents. + */ + @Test public void testReadNonPartialWithoutContentLengthOk() throws Exception { String content = "";