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  package org.apache.hc.client5.http.utils;
28  
29  import java.nio.ByteBuffer;
30  import java.nio.charset.StandardCharsets;
31  
32  import org.junit.jupiter.api.Assertions;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * {@link ByteArrayBuilder} test cases.
37   */
38  public class TestByteArrayBuilder {
39  
40      @Test
41      public void testEmptyBuffer() throws Exception {
42          final ByteArrayBuilder buffer = new ByteArrayBuilder();
43          final ByteBuffer byteBuffer = buffer.toByteBuffer();
44          Assertions.assertNotNull(byteBuffer);
45          Assertions.assertEquals(0, byteBuffer.capacity());
46  
47          final byte[] bytes = buffer.toByteArray();
48          Assertions.assertNotNull(bytes);
49          Assertions.assertEquals(0, bytes.length);
50      }
51  
52      @Test
53      public void testAppendBytes() throws Exception {
54          final ByteArrayBuilder buffer = new ByteArrayBuilder();
55          buffer.append(new byte[]{1, 2, 3, 4, 5});
56          buffer.append(new byte[]{3, 4, 5, 6, 7, 8, 9, 10, 11}, 3, 5);
57          buffer.append((byte[]) null);
58  
59          final byte[] bytes = buffer.toByteArray();
60          Assertions.assertNotNull(bytes);
61          Assertions.assertArrayEquals(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, bytes);
62      }
63  
64      @Test
65      public void testInvalidAppendBytes() throws Exception {
66          final ByteArrayBuilder buffer = new ByteArrayBuilder();
67          buffer.append((byte[])null, 0, 0);
68  
69          final byte[] tmp = new byte[] { 1, 2, 3, 4};
70          Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, -1, 0));
71          Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, -1));
72          Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, 8));
73          Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 10, Integer.MAX_VALUE));
74          Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 2, 4));
75      }
76  
77      @Test
78      public void testEnsureCapacity() throws Exception {
79          final ByteArrayBuilder buffer = new ByteArrayBuilder();
80          buffer.ensureFreeCapacity(10);
81          Assertions.assertEquals(10, buffer.capacity());
82          buffer.ensureFreeCapacity(5);
83          Assertions.assertEquals(10, buffer.capacity());
84          buffer.append(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
85          buffer.ensureFreeCapacity(5);
86          Assertions.assertEquals(13, buffer.capacity());
87          buffer.ensureFreeCapacity(15);
88          Assertions.assertEquals(23, buffer.capacity());
89      }
90  
91      @Test
92      public void testAppendText() throws Exception {
93          final ByteArrayBuilder buffer = new ByteArrayBuilder();
94          buffer.append(new char[]{'1', '2', '3', '4', '5'});
95          buffer.append(new char[]{'3', '4', '5', '6', '7', '8', '9', 'a', 'b'}, 3, 5);
96          buffer.append("bcd");
97          buffer.append("e");
98          buffer.append("f");
99          buffer.append("");
100         buffer.append((String) null);
101         buffer.append((char[]) null);
102 
103         final byte[] bytes = buffer.toByteArray();
104         Assertions.assertNotNull(bytes);
105         Assertions.assertEquals("123456789abcdef", new String(bytes, StandardCharsets.US_ASCII));
106     }
107 
108     @Test
109     public void testInvalidAppendChars() throws Exception {
110         final ByteArrayBuilder buffer = new ByteArrayBuilder();
111         buffer.append((char[])null, 0, 0);
112 
113         final char[] tmp = new char[] { 1, 2, 3, 4};
114         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, -1, 0));
115         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, -1));
116         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, 8));
117         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 10, Integer.MAX_VALUE));
118         Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 2, 4));
119     }
120 
121     @Test
122     public void testReset() throws Exception {
123         final ByteArrayBuilder buffer = new ByteArrayBuilder();
124         buffer.append("abcd");
125         buffer.append("e");
126         buffer.append("f");
127 
128         final byte[] bytes1 = buffer.toByteArray();
129         Assertions.assertNotNull(bytes1);
130         Assertions.assertEquals("abcdef", new String(bytes1, StandardCharsets.US_ASCII));
131 
132         buffer.reset();
133 
134         final byte[] bytes2 = buffer.toByteArray();
135         Assertions.assertNotNull(bytes2);
136         Assertions.assertEquals("", new String(bytes2, StandardCharsets.US_ASCII));
137     }
138 
139     @Test
140     public void testNonAsciiCharset() throws Exception {
141         final int[] germanChars = { 0xE4, 0x2D, 0xF6, 0x2D, 0xFc };
142         final StringBuilder tmp = new StringBuilder();
143         for (final int germanChar : germanChars) {
144             tmp.append((char) germanChar);
145         }
146         final String umlauts = tmp.toString();
147 
148 
149         final ByteArrayBuilder buffer = new ByteArrayBuilder();
150         buffer.append(umlauts);
151 
152         final byte[] bytes1 = buffer.toByteArray();
153         Assertions.assertNotNull(bytes1);
154         Assertions.assertEquals("?-?-?", new String(bytes1, StandardCharsets.US_ASCII));
155 
156         buffer.reset();
157         buffer.charset(StandardCharsets.UTF_8);
158         buffer.append(umlauts);
159 
160         final byte[] bytes2 = buffer.toByteArray();
161         Assertions.assertNotNull(bytes2);
162         Assertions.assertEquals(umlauts, new String(bytes2, StandardCharsets.UTF_8));
163     }
164 
165 }