Home » HttpComponents-Core-4.0.1 » org.apache.http.io » [javadoc | source]

    1   /*
    2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/test/org/apache/http/io/TestContentLengthInputStream.java $
    3    * $Revision: 325980 $
    4    * $Date: 2005-10-17 22:39:18 +0200 (Mon, 17 Oct 2005) $
    5    * ====================================================================
    6    *
    7    *  Copyright 2002-2004 The Apache Software Foundation
    8    *
    9    *  Licensed under the Apache License, Version 2.0 (the "License");
   10    *  you may not use this file except in compliance with the License.
   11    *  You may obtain a copy of the License at
   12    *
   13    *      http://www.apache.org/licenses/LICENSE-2.0
   14    *
   15    *  Unless required by applicable law or agreed to in writing, software
   16    *  distributed under the License is distributed on an "AS IS" BASIS,
   17    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   18    *  See the License for the specific language governing permissions and
   19    *  limitations under the License.
   20    * ====================================================================
   21    *
   22    * This software consists of voluntary contributions made by many
   23    * individuals on behalf of the Apache Software Foundation.  For more
   24    * information on the Apache Software Foundation, please see
   25    * <http://www.apache.org/>.
   26    *
   27    */
   28   
   29   package org.apache.http.io;
   30   
   31   import java.io.ByteArrayOutputStream;
   32   import java.io.IOException;
   33   import java.io.InputStream;
   34   
   35   import org.apache.http.mockup.HttpDataReceiverMockup;
   36   import org.apache.http.util.EncodingUtils;
   37   
   38   import junit.framework.Test;
   39   import junit.framework.TestCase;
   40   import junit.framework.TestSuite;
   41   
   42   public class TestContentLengthInputStream extends TestCase {
   43   
   44       public TestContentLengthInputStream(String testName) {
   45           super(testName);
   46       }
   47   
   48       // ------------------------------------------------------- TestCase Methods
   49   
   50       public static Test suite() {
   51           return new TestSuite(TestContentLengthInputStream.class);
   52       }
   53   
   54       // ------------------------------------------------------------------- Main
   55       public static void main(String args[]) {
   56           String[] testCaseName = { TestContentLengthInputStream.class.getName() };
   57           junit.textui.TestRunner.main(testCaseName);
   58       }
   59   
   60       private static final String CONTENT_CHARSET = "ISO-8859-1";
   61           
   62       public void testConstructors() throws Exception {
   63           new ContentLengthInputStream(new HttpDataReceiverMockup(new byte[] {}), 10);
   64           try {
   65               new ContentLengthInputStream(null, 10);
   66               fail("IllegalArgumentException should have been thrown");
   67           } catch (IllegalArgumentException ex) {
   68               // expected
   69           }
   70           try {
   71               new ContentLengthInputStream(new HttpDataReceiverMockup(new byte[] {}), -10);
   72               fail("IllegalArgumentException should have been thrown");
   73           } catch (IllegalArgumentException ex) {
   74               // expected
   75           }
   76       }
   77   
   78       public void testBasics() throws IOException {
   79           String correct = "1234567890123456";
   80           InputStream in = new ContentLengthInputStream(new HttpDataReceiverMockup(
   81               EncodingUtils.getBytes(correct, CONTENT_CHARSET)), 10L);
   82           ByteArrayOutputStream out = new ByteArrayOutputStream();
   83   
   84           byte[] buffer = new byte[50];
   85           int len = in.read(buffer, 0, 2);
   86           out.write(buffer, 0, len);
   87           len = in.read(buffer);
   88           out.write(buffer, 0, len);
   89           
   90           String result = EncodingUtils.getString(out.toByteArray(), CONTENT_CHARSET);
   91           assertEquals(result, "1234567890");
   92       }
   93   
   94       public void testSkip() throws IOException {
   95           InputStream in = new ContentLengthInputStream(new HttpDataReceiverMockup(new byte[20]), 10L);
   96           assertEquals(10, in.skip(10));
   97           assertTrue(in.read() == -1);
   98   
   99           in = new ContentLengthInputStream(new HttpDataReceiverMockup(new byte[20]), 10L);
  100           in.read();
  101           assertEquals(9, in.skip(10));
  102           assertTrue(in.read() == -1);
  103   
  104           in = new ContentLengthInputStream(new HttpDataReceiverMockup(new byte[20]), 2L);
  105           in.read();
  106           in.read();
  107           assertTrue(in.skip(10) <= 0);
  108           assertTrue(in.skip(-1) == 0);
  109           assertTrue(in.read() == -1);
  110           
  111           in = new ContentLengthInputStream(new HttpDataReceiverMockup(new byte[2]), 4L);
  112           in.read();
  113           assertTrue(in.skip(2) == 1);
  114       }
  115   
  116       public void testClose() throws IOException {
  117           String correct = "1234567890123456";
  118           InputStream in = new ContentLengthInputStream(new HttpDataReceiverMockup(
  119               EncodingUtils.getBytes(correct, CONTENT_CHARSET)), 10L);
  120           in.close();
  121           in.close();
  122           try {
  123               in.read();
  124               fail("IOException should have been thrown");
  125           } catch (IOException ex) {
  126               // expected
  127           }
  128           byte[] tmp = new byte[10]; 
  129           try {
  130               in.read(tmp);
  131               fail("IOException should have been thrown");
  132           } catch (IOException ex) {
  133               // expected
  134           }
  135           try {
  136               in.read(tmp, 0, tmp.length);
  137               fail("IOException should have been thrown");
  138           } catch (IOException ex) {
  139               // expected
  140           }
  141       }
  142       
  143   }
  144   

Home » HttpComponents-Core-4.0.1 » org.apache.http.io » [javadoc | source]