Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/io/TestCharArrayBuffer.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/test/org/apache/http/io/TestCharArrayBuffer.java $
3    * $Revision: 385120 $
4    * $Date: 2006-03-11 19:33:23 +0100 (Sat, 11 Mar 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.CharArrayBuffer;
33  
34  import junit.framework.Test;
35  import junit.framework.TestCase;
36  import junit.framework.TestSuite;
37  
38  /**
39   * Unit tests for {@link CharArrayBuffer}.
40   *
41   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
42   */
43  public class TestCharArrayBuffer extends TestCase {
44  
45      public TestCharArrayBuffer(String testName) {
46          super(testName);
47      }
48  
49      public static void main(String args[]) {
50          String[] testCaseName = { TestCharArrayBuffer.class.getName() };
51          junit.textui.TestRunner.main(testCaseName);
52      }
53  
54      public static Test suite() {
55          return new TestSuite(TestCharArrayBuffer.class);
56      }
57  
58      public void testConstructor() throws Exception {
59        CharArrayBuffer buffer = new CharArrayBuffer(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 CharArrayBuffer(-1);
66          fail("IllegalArgumentException should have been thrown");
67        } catch (IllegalArgumentException ex) {
68          // expected
69        }
70      }
71      
72      public void testSimpleAppend() throws Exception {
73        CharArrayBuffer buffer = new CharArrayBuffer(16);
74        assertEquals(16, buffer.capacity()); 
75        assertEquals(0, buffer.length());
76        char[] b1 = buffer.toCharArray();
77        assertNotNull(b1);
78        assertEquals(0, b1.length);
79        assertTrue(buffer.isEmpty());
80          assertFalse(buffer.isFull());
81        
82        char[] tmp = new char[] { '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        char[] b2 = buffer.toCharArray();
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.charAt(i));
95        }
96        assertEquals("1234", buffer.toString());
97        
98        buffer.clear();
99        assertEquals(16, buffer.capacity()); 
100       assertEquals(0, buffer.length());
101       assertTrue(buffer.isEmpty());
102         assertFalse(buffer.isFull());
103     }
104     
105     public void testExpandAppend() throws Exception {
106       CharArrayBuffer buffer = new CharArrayBuffer(4);
107       assertEquals(4, buffer.capacity()); 
108       
109       char[] tmp = new char[] { '1', '2', '3', '4'};
110       buffer.append(tmp, 0, 2);
111       buffer.append(tmp, 0, 4);
112       buffer.append(tmp, 0, 0);
113 
114       assertEquals(8, buffer.capacity()); 
115       assertEquals(6, buffer.length());
116       
117       buffer.append(tmp, 0, 4);
118       
119       assertEquals(16, buffer.capacity()); 
120       assertEquals(10, buffer.length());
121       
122       assertEquals("1212341234", buffer.toString());
123     }
124 
125     public void testAppendString() throws Exception {
126       CharArrayBuffer buffer = new CharArrayBuffer(8);
127       buffer.append("stuff");
128       buffer.append(" and more stuff");
129       assertEquals("stuff and more stuff", buffer.toString());
130     }
131     
132     public void testAppendNullString() throws Exception {
133       CharArrayBuffer buffer = new CharArrayBuffer(8);
134       buffer.append((String)null);
135       assertEquals("null", buffer.toString());
136     }
137     
138     public void testAppendCharArrayBuffer() throws Exception {
139         CharArrayBuffer buffer1 = new CharArrayBuffer(8);
140         buffer1.append(" and more stuff");
141         CharArrayBuffer buffer2 = new CharArrayBuffer(8);
142         buffer2.append("stuff");
143         buffer2.append(buffer1);
144         assertEquals("stuff and more stuff", buffer2.toString());
145     }
146     
147     public void testAppendNullCharArrayBuffer() throws Exception {
148         CharArrayBuffer buffer = new CharArrayBuffer(8);
149         buffer.append((CharArrayBuffer)null);
150         buffer.append((CharArrayBuffer)null, 0, 0);
151         assertEquals("", buffer.toString());
152     }
153     
154     public void testAppendSingleChar() throws Exception {
155       CharArrayBuffer buffer = new CharArrayBuffer(4);
156       buffer.append('1');
157       buffer.append('2');
158       buffer.append('3');
159       buffer.append('4');
160       buffer.append('5');
161       buffer.append('6');
162       assertEquals("123456", buffer.toString());
163     }
164     
165     public void testInvalidCharArrayAppend() throws Exception {
166       CharArrayBuffer buffer = new CharArrayBuffer(4);
167       buffer.append((char[])null, 0, 0);
168 
169       char[] tmp = new char[] { '1', '2', '3', '4'};
170       try {
171           buffer.append(tmp, -1, 0);
172         fail("IndexOutOfBoundsException should have been thrown");
173       } catch (IndexOutOfBoundsException ex) {
174         // expected
175       }
176       try {
177           buffer.append(tmp, 0, -1);
178         fail("IndexOutOfBoundsException should have been thrown");
179       } catch (IndexOutOfBoundsException ex) {
180         // expected
181       }
182       try {
183           buffer.append(tmp, 0, 8);
184         fail("IndexOutOfBoundsException should have been thrown");
185       } catch (IndexOutOfBoundsException ex) {
186         // expected
187       }
188       try {
189           buffer.append(tmp, 10, Integer.MAX_VALUE);
190         fail("IndexOutOfBoundsException should have been thrown");
191       } catch (IndexOutOfBoundsException ex) {
192         // expected
193       }
194       try {
195           buffer.append(tmp, 2, 4);
196         fail("IndexOutOfBoundsException should have been thrown");
197       } catch (IndexOutOfBoundsException ex) {
198         // expected
199       }
200     }
201 
202     public void testSetLength() throws Exception {
203       CharArrayBuffer buffer = new CharArrayBuffer(4);
204       buffer.setLength(2);
205       assertEquals(2, buffer.length());
206     }
207     
208     public void testSetInvalidLength() throws Exception {
209       CharArrayBuffer buffer = new CharArrayBuffer(4);
210       try {
211           buffer.setLength(-2);
212         fail("IndexOutOfBoundsException should have been thrown");
213       } catch (IndexOutOfBoundsException ex) {
214         // expected
215       }
216       try {
217           buffer.setLength(200);
218         fail("IndexOutOfBoundsException should have been thrown");
219       } catch (IndexOutOfBoundsException ex) {
220         // expected
221       }
222     }
223 
224     public void testEnsureCapacity() throws Exception {
225         CharArrayBuffer buffer = new CharArrayBuffer(4);
226         buffer.ensureCapacity(2);
227         assertEquals(4, buffer.capacity());
228         buffer.ensureCapacity(8);
229         assertEquals(8, buffer.capacity());
230     }
231 
232     public void testIndexOf() {
233         CharArrayBuffer buffer = new CharArrayBuffer(16);
234         buffer.append("name: value");
235         assertEquals(4, buffer.indexOf(':'));
236         assertEquals(-1, buffer.indexOf(','));
237         assertEquals(4, buffer.indexOf(':', -1, 11));
238         assertEquals(4, buffer.indexOf(':', 0, 1000));
239         assertEquals(-1, buffer.indexOf(':', 2, 1));
240     }
241     
242     public void testSubstring() {
243         CharArrayBuffer buffer = new CharArrayBuffer(16);
244         buffer.append(" name:  value    ");
245         assertEquals(5, buffer.indexOf(':'));
246         assertEquals(" name", buffer.substring(0, 5));
247         assertEquals("  value    ", buffer.substring(6, buffer.length()));
248         assertEquals("name", buffer.substringTrimmed(0, 5));
249         assertEquals("value", buffer.substringTrimmed(6, buffer.length()));
250         assertEquals("", buffer.substringTrimmed(13, buffer.length()));
251     }
252     
253     public void testSubstringIndexOfOutBound() {
254         CharArrayBuffer buffer = new CharArrayBuffer(16);
255         buffer.append("stuff");
256         try {
257             buffer.substring(-2, 10);
258             fail("IndexOutOfBoundsException should have been thrown");
259         } catch (IndexOutOfBoundsException ex) {
260             // expected
261         }
262         try {
263             buffer.substringTrimmed(-2, 10);
264             fail("IndexOutOfBoundsException should have been thrown");
265         } catch (IndexOutOfBoundsException ex) {
266             // expected
267         }
268         try {
269             buffer.substring(12, 10);
270             fail("IndexOutOfBoundsException should have been thrown");
271         } catch (IndexOutOfBoundsException ex) {
272             // expected
273         }
274         try {
275             buffer.substringTrimmed(12, 10);
276             fail("IndexOutOfBoundsException should have been thrown");
277         } catch (IndexOutOfBoundsException ex) {
278             // expected
279         }
280         try {
281             buffer.substring(2, 1);
282             fail("IndexOutOfBoundsException should have been thrown");
283         } catch (IndexOutOfBoundsException ex) {
284             // expected
285         }
286         try {
287             buffer.substringTrimmed(2, 1);
288             fail("IndexOutOfBoundsException should have been thrown");
289         } catch (IndexOutOfBoundsException ex) {
290             // expected
291         }
292     }    
293     
294     public void testAppendAsciiByteArray() throws Exception {
295         String s1 = "stuff";
296         String s2 = " and more stuff";
297         byte[] b1 = s1.getBytes("US-ASCII");
298         byte[] b2 = s2.getBytes("US-ASCII");
299         
300         CharArrayBuffer buffer = new CharArrayBuffer(8);
301         buffer.append(b1, 0, b1.length);
302         buffer.append(b2, 0, b2.length);
303         
304         assertEquals("stuff and more stuff", buffer.toString());
305     }
306     
307     public void testAppendISOByteArray() throws Exception {
308         byte[] b = new byte[] {0x00, 0x20, 0x7F, -0x80, -0x01};
309         
310         CharArrayBuffer buffer = new CharArrayBuffer(8);
311         buffer.append(b, 0, b.length);
312         char[] ch = buffer.toCharArray();
313         assertNotNull(ch);
314         assertEquals(5, ch.length);
315         assertEquals(0x00, ch[0]);
316         assertEquals(0x20, ch[1]);
317         assertEquals(0x7F, ch[2]);
318         assertEquals(0x80, ch[3]);
319         assertEquals(0xFF, ch[4]);
320     }
321     
322     public void testAppendNullByteArray() throws Exception {
323         CharArrayBuffer buffer = new CharArrayBuffer(8);
324         buffer.append((byte[])null, 0, 0);
325         assertEquals("", buffer.toString());
326     }
327 
328     public void testAppendNullByteArrayBuffer() throws Exception {
329         CharArrayBuffer buffer = new CharArrayBuffer(8);
330         buffer.append((ByteArrayBuffer)null, 0, 0);
331         assertEquals("", buffer.toString());
332     }
333 
334     public void testInvalidAppendAsciiByteArray() throws Exception {
335         CharArrayBuffer buffer = new CharArrayBuffer(4);
336         buffer.append((byte[])null, 0, 0);
337 
338         byte[] tmp = new byte[] { '1', '2', '3', '4'};
339         try {
340             buffer.append(tmp, -1, 0);
341             fail("IndexOutOfBoundsException should have been thrown");
342         } catch (IndexOutOfBoundsException ex) {
343             // expected
344         }
345         try {
346             buffer.append(tmp, 0, -1);
347             fail("IndexOutOfBoundsException should have been thrown");
348         } catch (IndexOutOfBoundsException ex) {
349             // expected
350         }
351         try {
352             buffer.append(tmp, 0, 8);
353             fail("IndexOutOfBoundsException should have been thrown");
354         } catch (IndexOutOfBoundsException ex) {
355             // expected
356         }
357         try {
358             buffer.append(tmp, 10, Integer.MAX_VALUE);
359             fail("IndexOutOfBoundsException should have been thrown");
360         } catch (IndexOutOfBoundsException ex) {
361             // expected
362         }
363         try {
364             buffer.append(tmp, 2, 4);
365             fail("IndexOutOfBoundsException should have been thrown");
366         } catch (IndexOutOfBoundsException ex) {
367             // expected
368         }
369     }
370     
371 }