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/TestHttpDataInputStream.java $
    3    * $Revision: 376458 $
    4    * $Date: 2006-02-09 23:22:06 +0100 (Thu, 09 Feb 2006) $
    5    * ====================================================================
    6    *
    7    *  Copyright 1999-2006 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 org.apache.http.mockup.HttpDataReceiverMockup;
   32   
   33   import junit.framework.Test;
   34   import junit.framework.TestCase;
   35   import junit.framework.TestSuite;
   36   
   37   /**
   38    * Simple tests for {@link HttpDataInputStream}.
   39    *
   40    * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
   41    */
   42   public class TestHttpDataInputStream extends TestCase {
   43   
   44       // ------------------------------------------------------------ Constructor
   45       public TestHttpDataInputStream(String testName) {
   46           super(testName);
   47       }
   48   
   49       // ------------------------------------------------------------------- Main
   50       public static void main(String args[]) {
   51           String[] testCaseName = { TestHttpDataInputStream.class.getName() };
   52           junit.textui.TestRunner.main(testCaseName);
   53       }
   54   
   55       // ------------------------------------------------------- TestCase Methods
   56   
   57       public static Test suite() {
   58           return new TestSuite(TestHttpDataInputStream.class);
   59       }
   60   
   61       public void testConstructor() throws Exception {
   62           HttpDataReceiver receiver = new HttpDataReceiverMockup(new byte[] {});
   63           new HttpDataInputStream(receiver);
   64           try {
   65               new HttpDataInputStream(null);
   66               fail("IllegalArgumentException should have been thrown");
   67           } catch (IllegalArgumentException ex) {
   68               //expected
   69           }
   70       }
   71       
   72       public void testBasicRead() throws Exception {
   73           byte[] input = new byte[] {'a', 'b', 'c'};
   74           HttpDataReceiverMockup receiver = new HttpDataReceiverMockup(input);
   75           HttpDataInputStream instream = new HttpDataInputStream(receiver);
   76           byte[] tmp = new byte[2];
   77           assertEquals(2, instream.read(tmp, 0, tmp.length));
   78           assertEquals('a', tmp[0]);
   79           assertEquals('b', tmp[1]);
   80           assertEquals('c', instream.read());
   81           assertEquals(-1, instream.read(tmp, 0, tmp.length));
   82           assertEquals(-1, instream.read());
   83           assertEquals(-1, instream.read(tmp, 0, tmp.length));
   84           assertEquals(-1, instream.read());        
   85       }
   86       
   87       public void testClosedCondition() throws Exception {
   88           byte[] input = new byte[] {'a', 'b', 'c'};
   89           HttpDataReceiverMockup receiver = new HttpDataReceiverMockup(input);
   90           HttpDataInputStream instream = new HttpDataInputStream(receiver);
   91   
   92           instream.close();
   93           instream.close();
   94           
   95           assertTrue(instream.available() == 0);
   96           byte[] tmp = new byte[2];
   97           assertEquals(-1, instream.read(tmp, 0, tmp.length));
   98           assertEquals(-1, instream.read());
   99           assertEquals(-1, instream.read(tmp, 0, tmp.length));
  100           assertEquals(-1, instream.read());        
  101       }
  102   
  103       public void testAvailable() throws Exception {
  104           byte[] input = new byte[] {'a', 'b', 'c'};
  105           HttpDataReceiverMockup receiver = new HttpDataReceiverMockup(input);
  106           HttpDataInputStream instream = new HttpDataInputStream(receiver);
  107           assertTrue(instream.available() > 0);        
  108       }
  109       
  110   }

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