You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2005/04/02 20:03:29 UTC

svn commit: r159779 - in jakarta/httpclient/trunk/http-common/src: java/org/apache/http/io/ test/org/apache/http/io/

Author: olegk
Date: Sat Apr  2 10:03:27 2005
New Revision: 159779

URL: http://svn.apache.org/viewcvs?view=rev&rev=159779
Log:
Extra test coverage

Added:
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestContentLengthInputStream.java   (with props)
Modified:
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java?view=diff&r1=159778&r2=159779
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java Sat Apr  2 10:03:27 2005
@@ -77,19 +77,6 @@
     private InputStream wrappedStream = null;
 
     /**
-     * @deprecated use {@link #ContentLengthInputStream(InputStream, long)}
-     * 
-     * Creates a new length limited stream
-     *
-     * @param in The stream to wrap
-     * @param contentLength The maximum number of bytes that can be read from
-     * the stream. Subsequent read operations will return -1.
-     */
-    public ContentLengthInputStream(InputStream in, int contentLength) {
-        this(in, (long)contentLength);
-    }
-
-    /**
      * Creates a new length limited stream
      *
      * @param in The stream to wrap
@@ -98,8 +85,14 @@
      * 
      * @since 3.0
      */
-    public ContentLengthInputStream(InputStream in, long contentLength) {
+    public ContentLengthInputStream(final InputStream in, long contentLength) {
         super();
+        if (in == null) {
+            throw new IllegalArgumentException("Input stream may not be null");
+        }
+        if (contentLength < 0) {
+            throw new IllegalArgumentException("Content length may not be negative");
+        }
         this.wrappedStream = in;
         this.contentLength = contentLength;
     }

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java?view=diff&r1=159778&r2=159779
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java Sat Apr  2 10:03:27 2005
@@ -41,6 +41,7 @@
         suite.addTest(TestHttpDataInputStream.suite());
         suite.addTest(TestHttpDataOutputStream.suite());
         suite.addTest(TestChunkCoding.suite());
+        suite.addTest(TestContentLengthInputStream.suite());
         suite.addTest(TestOldIOHttpDataReceiverAndTransmitter.suite());
         return suite;
     }

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java?view=diff&r1=159778&r2=159779
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java Sat Apr  2 10:03:27 2005
@@ -273,35 +273,6 @@
         assertEquals(0, out.size());
     }
     
-    public void testContentLengthInputStream() throws IOException {
-        String correct = "1234567890123456";
-        InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(
-            EncodingUtil.getBytes(correct, CONTENT_CHARSET)), 10L);
-        byte[] buffer = new byte[50];
-        int len = in.read(buffer);
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        out.write(buffer, 0, len);
-        String result = EncodingUtil.getString(out.toByteArray(), CONTENT_CHARSET);
-        assertEquals(result, "1234567890");
-    }
-
-    public void testContentLengthInputStreamSkip() throws IOException {
-        InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 10L);
-        assertEquals(10, in.skip(10));
-        assertTrue(in.read() == -1);
-
-        in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 10L);
-        in.read();
-        assertEquals(9, in.skip(10));
-        assertTrue(in.read() == -1);
-
-        in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 2L);
-        in.read();
-        in.read();
-        assertTrue(in.skip(10) <= 0);
-        assertTrue(in.read() == -1);
-    }
-
     public void testChunkedConsitance() throws IOException {
         String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?\\suweb";
         ByteArrayOutputStream buffer = new ByteArrayOutputStream();

Added: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestContentLengthInputStream.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestContentLengthInputStream.java?view=auto&rev=159779
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestContentLengthInputStream.java (added)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestContentLengthInputStream.java Sat Apr  2 10:03:27 2005
@@ -0,0 +1,139 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ *
+ *  Copyright 2002-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.io;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.http.util.EncodingUtil;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class TestContentLengthInputStream extends TestCase {
+
+    public TestContentLengthInputStream(String testName) {
+        super(testName);
+    }
+
+    // ------------------------------------------------------- TestCase Methods
+
+    public static Test suite() {
+        return new TestSuite(TestContentLengthInputStream.class);
+    }
+
+    // ------------------------------------------------------------------- Main
+    public static void main(String args[]) {
+        String[] testCaseName = { TestContentLengthInputStream.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    private static final String CONTENT_CHARSET = "ISO-8859-1";
+        
+    public void testConstructors() throws Exception {
+        new ContentLengthInputStream(new ByteArrayInputStream(new byte[] {}), 10);
+        try {
+            new ContentLengthInputStream(null, 10);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            new ContentLengthInputStream(new ByteArrayInputStream(new byte[] {}), -10);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
+    public void testBasics() throws IOException {
+        String correct = "1234567890123456";
+        InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(
+            EncodingUtil.getBytes(correct, CONTENT_CHARSET)), 10L);
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+        byte[] buffer = new byte[50];
+        int len = in.read(buffer, 0, 2);
+        out.write(buffer, 0, len);
+        len = in.read(buffer);
+        out.write(buffer, 0, len);
+        
+        String result = EncodingUtil.getString(out.toByteArray(), CONTENT_CHARSET);
+        assertEquals(result, "1234567890");
+    }
+
+    public void testSkip() throws IOException {
+        InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 10L);
+        assertEquals(10, in.skip(10));
+        assertTrue(in.read() == -1);
+
+        in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 10L);
+        in.read();
+        assertEquals(9, in.skip(10));
+        assertTrue(in.read() == -1);
+
+        in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 2L);
+        in.read();
+        in.read();
+        assertTrue(in.skip(10) <= 0);
+        assertTrue(in.read() == -1);
+    }
+
+    public void testClose() throws IOException {
+        String correct = "1234567890123456";
+        InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(
+            EncodingUtil.getBytes(correct, CONTENT_CHARSET)), 10L);
+        in.close();
+        in.close();
+        try {
+            in.read();
+            fail("IOException should have been thrown");
+        } catch (IOException ex) {
+            // expected
+        }
+        byte[] tmp = new byte[10]; 
+        try {
+            in.read(tmp);
+            fail("IOException should have been thrown");
+        } catch (IOException ex) {
+            // expected
+        }
+        try {
+            in.read(tmp, 0, tmp.length);
+            fail("IOException should have been thrown");
+        } catch (IOException ex) {
+            // expected
+        }
+    }
+    
+}
+

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestContentLengthInputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestContentLengthInputStream.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestContentLengthInputStream.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java?view=diff&r1=159778&r2=159779
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestOldIOHttpDataReceiverAndTransmitter.java Sat Apr  2 10:03:27 2005
@@ -80,7 +80,8 @@
     public void testInputStreamHttpDataReceiver() throws IOException {
         String s = "aaaaa";
         InputStream in = new ByteArrayInputStream(EncodingUtil.getAsciiBytes(s));
-        HttpDataReceiver datareceiver = new InputStreamHttpDataReceiver(in);
+        InputStreamHttpDataReceiver datareceiver = new InputStreamHttpDataReceiver(in);
+        assertNotNull(datareceiver.getInputStream());
         assertTrue(datareceiver.isDataAvailable(1));
         assertEquals('a', datareceiver.read());
         byte[] tmp = new byte[2];
@@ -97,13 +98,16 @@
     public void testOutputStreamHttpDataTransmitter() throws IOException {
         String s = "aaaaa\r\n";
         ByteArrayOutputStream out = new ByteArrayOutputStream();
-        HttpDataTransmitter datatransmitter = new OutputStreamHttpDataTransmitter(out);
+        OutputStreamHttpDataTransmitter datatransmitter = new OutputStreamHttpDataTransmitter(out);
         datatransmitter.reset(new DefaultHttpParams(null));
+
+        assertNotNull(datatransmitter.getOutputStream());
         
         datatransmitter.write('a');
         datatransmitter.write(new byte[] {'a'});
         datatransmitter.write(new byte[] {'a'}, 0, 1);
         datatransmitter.writeLine("aa");
+        datatransmitter.writeLine(null);
         datatransmitter.flush();
         
         assertEquals(s, out.toString("US-ASCII"));