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/TestByteArrayBuffer.java $
    3    * $Revision: 376458 $
    4    * $Date: 2006-02-09 23:22:06 +0100 (Thu, 09 Feb 2006) $
    5    * 
    6    * ====================================================================
    7    *
    8    *  Copyright 1999-2006 The Apache Software Foundation
    9    *
   10    *  Licensed under the Apache License, Version 2.0 (the "License");
   11    *  you may not use this file except in compliance with the License.
   12    *  You may obtain a copy of the License at
   13    *
   14    *      http://www.apache.org/licenses/LICENSE-2.0
   15    *
   16    *  Unless required by applicable law or agreed to in writing, software
   17    *  distributed under the License is distributed on an "AS IS" BASIS,
   18    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   19    *  See the License for the specific language governing permissions and
   20    *  limitations under the License.
   21    * ====================================================================
   22    *
   23    * This software consists of voluntary contributions made by many
   24    * individuals on behalf of the Apache Software Foundation.  For more
   25    * information on the Apache Software Foundation, please see
   26    * <http://www.apache.org/>.
   27    *
   28    */
   29   
   30   package org.apache.http.io;
   31   
   32   import org.apache.http.io.ByteArrayBuffer;
   33   
   34   import junit.framework.Test;
   35   import junit.framework.TestCase;
   36   import junit.framework.TestSuite;
   37   
   38   /**
   39    * Unit tests for {@link ByteArrayBuffer}.
   40    *
   41    * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
   42    */
   43   public class TestByteArrayBuffer extends TestCase {
   44   
   45       public TestByteArrayBuffer(String testName) {
   46           super(testName);
   47       }
   48   
   49       public static void main(String args[]) {
   50           String[] testCaseName = { TestByteArrayBuffer.class.getName() };
   51           junit.textui.TestRunner.main(testCaseName);
   52       }
   53   
   54       public static Test suite() {
   55           return new TestSuite(TestByteArrayBuffer.class);
   56       }
   57   
   58       public void testConstructor() throws Exception {
   59       	ByteArrayBuffer buffer = new ByteArrayBuffer(16);
   60       	assertEquals(16, buffer.capacity()); 
   61       	assertEquals(0, buffer.length());
   62           assertNotNull(buffer.buffer());
   63           assertEquals(16, buffer.buffer().length);
   64       	try {
   65       		new ByteArrayBuffer(-1);
   66       		fail("IllegalArgumentException should have been thrown");
   67       	} catch (IllegalArgumentException ex) {
   68       		// expected
   69       	}
   70       }
   71       
   72       public void testSimpleAppend() throws Exception {
   73       	ByteArrayBuffer buffer = new ByteArrayBuffer(16);
   74       	assertEquals(16, buffer.capacity()); 
   75       	assertEquals(0, buffer.length());
   76       	byte[] b1 = buffer.toByteArray();
   77       	assertNotNull(b1);
   78       	assertEquals(0, b1.length);
   79       	assertTrue(buffer.isEmpty());
   80           assertFalse(buffer.isFull());
   81       	
   82       	byte[] tmp = new byte[] { 1, 2, 3, 4};
   83       	buffer.append(tmp, 0, tmp.length);
   84       	assertEquals(16, buffer.capacity()); 
   85       	assertEquals(4, buffer.length());
   86       	assertFalse(buffer.isEmpty());
   87           assertFalse(buffer.isFull());
   88       	
   89       	byte[] b2 = buffer.toByteArray();
   90       	assertNotNull(b2);
   91       	assertEquals(4, b2.length);
   92       	for (int i = 0; i < tmp.length; i++) {
   93           	assertEquals(tmp[i], b2[i]);
   94           	assertEquals(tmp[i], buffer.byteAt(i));
   95       	}
   96       	buffer.clear();
   97       	assertEquals(16, buffer.capacity()); 
   98       	assertEquals(0, buffer.length());
   99       	assertTrue(buffer.isEmpty());
  100           assertFalse(buffer.isFull());
  101       }
  102       
  103       public void testExpandAppend() throws Exception {
  104       	ByteArrayBuffer buffer = new ByteArrayBuffer(4);
  105       	assertEquals(4, buffer.capacity()); 
  106       	
  107       	byte[] tmp = new byte[] { 1, 2, 3, 4};
  108       	buffer.append(tmp, 0, 2);
  109       	buffer.append(tmp, 0, 4);
  110       	buffer.append(tmp, 0, 0);
  111   
  112       	assertEquals(8, buffer.capacity()); 
  113       	assertEquals(6, buffer.length());
  114       	
  115       	buffer.append(tmp, 0, 4);
  116       	
  117       	assertEquals(16, buffer.capacity()); 
  118       	assertEquals(10, buffer.length());
  119       }
  120       
  121       public void testInvalidAppend() throws Exception {
  122       	ByteArrayBuffer buffer = new ByteArrayBuffer(4);
  123       	buffer.append((byte[])null, 0, 0);
  124   
  125       	byte[] tmp = new byte[] { 1, 2, 3, 4};
  126       	try {
  127           	buffer.append(tmp, -1, 0);
  128       		fail("IndexOutOfBoundsException should have been thrown");
  129       	} catch (IndexOutOfBoundsException ex) {
  130       		// expected
  131       	}
  132       	try {
  133           	buffer.append(tmp, 0, -1);
  134       		fail("IndexOutOfBoundsException should have been thrown");
  135       	} catch (IndexOutOfBoundsException ex) {
  136       		// expected
  137       	}
  138       	try {
  139           	buffer.append(tmp, 0, 8);
  140       		fail("IndexOutOfBoundsException should have been thrown");
  141       	} catch (IndexOutOfBoundsException ex) {
  142       		// expected
  143       	}
  144       	try {
  145           	buffer.append(tmp, 10, Integer.MAX_VALUE);
  146       		fail("IndexOutOfBoundsException should have been thrown");
  147       	} catch (IndexOutOfBoundsException ex) {
  148       		// expected
  149       	}
  150       	try {
  151           	buffer.append(tmp, 2, 4);
  152       		fail("IndexOutOfBoundsException should have been thrown");
  153       	} catch (IndexOutOfBoundsException ex) {
  154       		// expected
  155       	}
  156       }
  157   
  158       public void testAppendOneByte() throws Exception {
  159           ByteArrayBuffer buffer = new ByteArrayBuffer(4);
  160           assertEquals(4, buffer.capacity()); 
  161           
  162           byte[] tmp = new byte[] { 1, 127, -1, -128, 1, -2};
  163           for (int i = 0; i < tmp.length; i++) {
  164               buffer.append(tmp[i]);
  165           }
  166           assertEquals(8, buffer.capacity()); 
  167           assertEquals(6, buffer.length());
  168           
  169           for (int i = 0; i < tmp.length; i++) {
  170               assertEquals(tmp[i], buffer.byteAt(i));
  171           }
  172       }
  173       
  174       public void testSetLength() throws Exception {
  175       	ByteArrayBuffer buffer = new ByteArrayBuffer(4);
  176       	buffer.setLength(2);
  177       	assertEquals(2, buffer.length());
  178       }
  179       
  180       public void testSetInvalidLength() throws Exception {
  181       	ByteArrayBuffer buffer = new ByteArrayBuffer(4);
  182       	try {
  183           	buffer.setLength(-2);
  184       		fail("IndexOutOfBoundsException should have been thrown");
  185       	} catch (IndexOutOfBoundsException ex) {
  186       		// expected
  187       	}
  188       	try {
  189           	buffer.setLength(200);
  190       		fail("IndexOutOfBoundsException should have been thrown");
  191       	} catch (IndexOutOfBoundsException ex) {
  192       		// expected
  193       	}
  194       }
  195       
  196       public void testAppendCharArrayAsAscii() throws Exception {
  197           String s1 = "stuff";
  198           String s2 = " and more stuff";
  199           char[] b1 = s1.toCharArray();
  200           char[] b2 = s2.toCharArray();
  201           
  202           ByteArrayBuffer buffer = new ByteArrayBuffer(8);
  203           buffer.append(b1, 0, b1.length);
  204           buffer.append(b2, 0, b2.length);
  205            
  206           assertEquals(s1 + s2, new String(buffer.toByteArray(), "US-ASCII"));
  207       }
  208       
  209       public void testAppendNullCharArray() throws Exception {
  210           ByteArrayBuffer buffer = new ByteArrayBuffer(8);
  211           buffer.append((char[])null, 0, 0);
  212           assertEquals(0, buffer.length());
  213       }
  214   
  215       public void testAppendEmptyCharArray() throws Exception {
  216           ByteArrayBuffer buffer = new ByteArrayBuffer(8);
  217           buffer.append(new char[] {}, 0, 0);
  218           assertEquals(0, buffer.length());
  219       }
  220   
  221       public void testAppendNullCharArrayBuffer() throws Exception {
  222           ByteArrayBuffer buffer = new ByteArrayBuffer(8);
  223           buffer.append((CharArrayBuffer)null, 0, 0);
  224           assertEquals(0, buffer.length());
  225       }
  226   
  227       public void testInvalidAppendCharArrayAsAscii() throws Exception {
  228           ByteArrayBuffer buffer = new ByteArrayBuffer(4);
  229           buffer.append((char[])null, 0, 0);
  230   
  231           char[] tmp = new char[] { '1', '2', '3', '4'};
  232           try {
  233               buffer.append(tmp, -1, 0);
  234               fail("IndexOutOfBoundsException should have been thrown");
  235           } catch (IndexOutOfBoundsException ex) {
  236               // expected
  237           }
  238           try {
  239               buffer.append(tmp, 0, -1);
  240               fail("IndexOutOfBoundsException should have been thrown");
  241           } catch (IndexOutOfBoundsException ex) {
  242               // expected
  243           }
  244           try {
  245               buffer.append(tmp, 0, 8);
  246               fail("IndexOutOfBoundsException should have been thrown");
  247           } catch (IndexOutOfBoundsException ex) {
  248               // expected
  249           }
  250           try {
  251               buffer.append(tmp, 10, Integer.MAX_VALUE);
  252               fail("IndexOutOfBoundsException should have been thrown");
  253           } catch (IndexOutOfBoundsException ex) {
  254               // expected
  255           }
  256           try {
  257               buffer.append(tmp, 2, 4);
  258               fail("IndexOutOfBoundsException should have been thrown");
  259           } catch (IndexOutOfBoundsException ex) {
  260               // expected
  261           }
  262       }
  263       
  264   }

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