View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.util;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  import java.nio.charset.StandardCharsets;
35  
36  import org.junit.jupiter.api.Assertions;
37  import org.junit.jupiter.api.Test;
38  
39  /**
40   * Unit tests for {@link CharArrayBuffer}.
41   *
42   */
43  public class TestCharArrayBuffer {
44  
45      @Test
46      public void testConstructor() throws Exception {
47          final CharArrayBuffer buffer = new CharArrayBuffer(16);
48          Assertions.assertEquals(16, buffer.capacity());
49          Assertions.assertEquals(0, buffer.length());
50          Assertions.assertNotNull(buffer.array());
51          Assertions.assertEquals(16, buffer.array().length);
52          Assertions.assertThrows(IllegalArgumentException.class, () -> new CharArrayBuffer(-1));
53      }
54  
55      @Test
56      public void testSimpleAppend() throws Exception {
57          final CharArrayBuffer buffer = new CharArrayBuffer(16);
58          Assertions.assertEquals(16, buffer.capacity());
59          Assertions.assertEquals(0, buffer.length());
60          final char[] b1 = buffer.toCharArray();
61          Assertions.assertNotNull(b1);
62          Assertions.assertEquals(0, b1.length);
63          Assertions.assertTrue(buffer.isEmpty());
64          Assertions.assertFalse(buffer.isFull());
65  
66          final char[] tmp = new char[] { '1', '2', '3', '4'};
67          buffer.append(tmp, 0, tmp.length);
68          Assertions.assertEquals(16, buffer.capacity());
69          Assertions.assertEquals(4, buffer.length());
70          Assertions.assertFalse(buffer.isEmpty());
71          Assertions.assertFalse(buffer.isFull());
72  
73          final char[] b2 = buffer.toCharArray();
74          Assertions.assertNotNull(b2);
75          Assertions.assertEquals(4, b2.length);
76          for (int i = 0; i < tmp.length; i++) {
77              Assertions.assertEquals(tmp[i], b2[i]);
78              Assertions.assertEquals(tmp[i], buffer.charAt(i));
79          }
80          Assertions.assertEquals("1234", buffer.toString());
81  
82          buffer.clear();
83          Assertions.assertEquals(16, buffer.capacity());
84          Assertions.assertEquals(0, buffer.length());
85          Assertions.assertTrue(buffer.isEmpty());
86          Assertions.assertFalse(buffer.isFull());
87      }
88  
89      @Test
90      public void testExpandAppend() throws Exception {
91          final CharArrayBuffer buffer = new CharArrayBuffer(4);
92          Assertions.assertEquals(4, buffer.capacity());
93  
94          final char[] tmp = new char[] { '1', '2', '3', '4'};
95          buffer.append(tmp, 0, 2);
96          buffer.append(tmp, 0, 4);
97          buffer.append(tmp, 0, 0);
98  
99          Assertions.assertEquals(8, buffer.capacity());
100         Assertions.assertEquals(6, buffer.length());
101 
102         buffer.append(tmp, 0, 4);
103 
104         Assertions.assertEquals(16, buffer.capacity());
105         Assertions.assertEquals(10, buffer.length());
106 
107         Assertions.assertEquals("1212341234", buffer.toString());
108     }
109 
110     @Test
111     public void testAppendString() throws Exception {
112         final CharArrayBuffer buffer = new CharArrayBuffer(8);
113         buffer.append("stuff");
114         buffer.append(" and more stuff");
115         Assertions.assertEquals("stuff and more stuff", buffer.toString());
116     }
117 
118     @Test
119     public void testAppendNullString() throws Exception {
120         final CharArrayBuffer buffer = new CharArrayBuffer(8);
121         buffer.append((String)null);
122         Assertions.assertEquals("null", buffer.toString());
123     }
124 
125     @Test
126     public void testAppendCharArrayBuffer() throws Exception {
127         final CharArrayBuffer buffer1 = new CharArrayBuffer(8);
128         buffer1.append(" and more stuff");
129         final CharArrayBuffer buffer2 = new CharArrayBuffer(8);
130         buffer2.append("stuff");
131         buffer2.append(buffer1);
132         Assertions.assertEquals("stuff and more stuff", buffer2.toString());
133     }
134 
135     @Test
136     public void testAppendNullCharArrayBuffer() throws Exception {
137         final CharArrayBuffer buffer = new CharArrayBuffer(8);
138         buffer.append((CharArrayBuffer)null);
139         buffer.append((CharArrayBuffer)null, 0, 0);
140         Assertions.assertEquals("", buffer.toString());
141     }
142 
143     @Test
144     public void testAppendSingleChar() throws Exception {
145         final CharArrayBuffer buffer = new CharArrayBuffer(4);
146         buffer.append('1');
147         buffer.append('2');
148         buffer.append('3');
149         buffer.append('4');
150         buffer.append('5');
151         buffer.append('6');
152         Assertions.assertEquals("123456", buffer.toString());
153     }
154 
155     @Test
156     public void testInvalidCharArrayAppend() throws Exception {
157         final CharArrayBuffer buffer = new CharArrayBuffer(4);
158         buffer.append((char[])null, 0, 0);
159 
160         final char[] tmp = new char[] { '1', '2', '3', '4'};
161         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, -1, 0));
162         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, -1));
163         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, 8));
164         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 10, Integer.MAX_VALUE));
165         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 2, 4));
166     }
167 
168     @Test
169     public void testSetLength() throws Exception {
170         final CharArrayBuffer buffer = new CharArrayBuffer(4);
171         buffer.setLength(2);
172         Assertions.assertEquals(2, buffer.length());
173     }
174 
175     @Test
176     public void testSetInvalidLength() throws Exception {
177         final CharArrayBuffer buffer = new CharArrayBuffer(4);
178         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.setLength(-2));
179         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.setLength(200));
180     }
181 
182     @Test
183     public void testEnsureCapacity() throws Exception {
184         final CharArrayBuffer buffer = new CharArrayBuffer(4);
185         buffer.ensureCapacity(2);
186         Assertions.assertEquals(4, buffer.capacity());
187         buffer.ensureCapacity(8);
188         Assertions.assertEquals(8, buffer.capacity());
189     }
190 
191     @Test
192     public void testIndexOf() {
193         final CharArrayBuffer buffer = new CharArrayBuffer(16);
194         buffer.append("name: value");
195         Assertions.assertEquals(4, buffer.indexOf(':'));
196         Assertions.assertEquals(-1, buffer.indexOf(','));
197         Assertions.assertEquals(4, buffer.indexOf(':', -1, 11));
198         Assertions.assertEquals(4, buffer.indexOf(':', 0, 1000));
199         Assertions.assertEquals(-1, buffer.indexOf(':', 2, 1));
200     }
201 
202     @Test
203     public void testSubstring() {
204         final CharArrayBuffer buffer = new CharArrayBuffer(16);
205         buffer.append(" name:  value    ");
206         Assertions.assertEquals(5, buffer.indexOf(':'));
207         Assertions.assertEquals(" name", buffer.substring(0, 5));
208         Assertions.assertEquals("  value    ", buffer.substring(6, buffer.length()));
209         Assertions.assertEquals("name", buffer.substringTrimmed(0, 5));
210         Assertions.assertEquals("value", buffer.substringTrimmed(6, buffer.length()));
211         Assertions.assertEquals("", buffer.substringTrimmed(13, buffer.length()));
212     }
213 
214     @Test
215     public void testSubstringIndexOfOutBound() {
216         final CharArrayBuffer buffer = new CharArrayBuffer(16);
217         buffer.append("stuff");
218         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.substring(-2, 10));
219         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.substringTrimmed(-2, 10));
220         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.substring(12, 10));
221         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.substringTrimmed(12, 10));
222         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.substring(2, 1));
223         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.substringTrimmed(2, 1));
224     }
225 
226     @Test
227     public void testAppendAsciiByteArray() throws Exception {
228         final String s1 = "stuff";
229         final String s2 = " and more stuff";
230         final byte[] b1 = s1.getBytes(StandardCharsets.US_ASCII);
231         final byte[] b2 = s2.getBytes(StandardCharsets.US_ASCII);
232 
233         final CharArrayBuffer buffer = new CharArrayBuffer(8);
234         buffer.append(b1, 0, b1.length);
235         buffer.append(b2, 0, b2.length);
236 
237         Assertions.assertEquals("stuff and more stuff", buffer.toString());
238     }
239 
240     @Test
241     public void testAppendISOByteArray() throws Exception {
242         final byte[] b = new byte[] {0x00, 0x20, 0x7F, -0x80, -0x01};
243 
244         final CharArrayBuffer buffer = new CharArrayBuffer(8);
245         buffer.append(b, 0, b.length);
246         final char[] ch = buffer.toCharArray();
247         Assertions.assertNotNull(ch);
248         Assertions.assertEquals(5, ch.length);
249         Assertions.assertEquals(0x00, ch[0]);
250         Assertions.assertEquals(0x20, ch[1]);
251         Assertions.assertEquals(0x7F, ch[2]);
252         Assertions.assertEquals(0x80, ch[3]);
253         Assertions.assertEquals(0xFF, ch[4]);
254     }
255 
256     @Test
257     public void testAppendNullByteArray() throws Exception {
258         final CharArrayBuffer buffer = new CharArrayBuffer(8);
259         buffer.append((byte[])null, 0, 0);
260         Assertions.assertEquals("", buffer.toString());
261     }
262 
263     @Test
264     public void testAppendNullByteArrayBuffer() throws Exception {
265         final CharArrayBuffer buffer = new CharArrayBuffer(8);
266         buffer.append((ByteArrayBuffer)null, 0, 0);
267         Assertions.assertEquals("", buffer.toString());
268     }
269 
270     @Test
271     public void testInvalidAppendAsciiByteArray() throws Exception {
272         final CharArrayBuffer buffer = new CharArrayBuffer(4);
273         buffer.append((byte[])null, 0, 0);
274 
275         final byte[] tmp = new byte[] { '1', '2', '3', '4'};
276         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, -1, 0));
277         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, -1));
278         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, 8));
279         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 10, Integer.MAX_VALUE));
280         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 2, 4));
281     }
282 
283     @Test
284     public void testSerialization() throws Exception {
285         final CharArrayBuffer orig = new CharArrayBuffer(32);
286         orig.append('a');
287         orig.append('b');
288         orig.append('c');
289         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
290         try (final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer)) {
291             outStream.writeObject(orig);
292         }
293         final byte[] raw = outbuffer.toByteArray();
294         final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
295         final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
296         final CharArrayBuffer clone = (CharArrayBuffer) inStream.readObject();
297         Assertions.assertEquals(orig.capacity(), clone.capacity());
298         Assertions.assertEquals(orig.length(), clone.length());
299         final char[] data = clone.toCharArray();
300         Assertions.assertNotNull(data);
301         Assertions.assertEquals(3, data.length);
302         Assertions.assertEquals('a', data[0]);
303         Assertions.assertEquals('b', data[1]);
304         Assertions.assertEquals('c', data[2]);
305     }
306 
307     @Test
308     public void testSubSequence() {
309         final CharArrayBuffer buffer = new CharArrayBuffer(16);
310         buffer.append(" name:  value    ");
311         Assertions.assertEquals(5, buffer.indexOf(':'));
312         Assertions.assertEquals(" name", buffer.subSequence(0, 5).toString());
313         Assertions.assertEquals("  value    ",
314             buffer.subSequence(6, buffer.length()).toString());
315     }
316 
317     @Test
318     public void testSubSequenceIndexOfOutBound() {
319         final CharArrayBuffer buffer = new CharArrayBuffer(16);
320         buffer.append("stuff");
321         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.subSequence(-2, 10));
322         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.subSequence(12, 10));
323         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.subSequence(2, 1));
324     }
325 
326 }