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.Assert;
33  import org.junit.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          Assert.assertNotNull(byteBuffer);
45          Assert.assertEquals(0, byteBuffer.capacity());
46  
47          final byte[] bytes = buffer.toByteArray();
48          Assert.assertNotNull(bytes);
49          Assert.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          Assert.assertNotNull(bytes);
61          Assert.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          try {
71              buffer.append(tmp, -1, 0);
72              Assert.fail("IndexOutOfBoundsException should have been thrown");
73          } catch (final IndexOutOfBoundsException ex) {
74              // expected
75          }
76          try {
77              buffer.append(tmp, 0, -1);
78              Assert.fail("IndexOutOfBoundsException should have been thrown");
79          } catch (final IndexOutOfBoundsException ex) {
80              // expected
81          }
82          try {
83              buffer.append(tmp, 0, 8);
84              Assert.fail("IndexOutOfBoundsException should have been thrown");
85          } catch (final IndexOutOfBoundsException ex) {
86              // expected
87          }
88          try {
89              buffer.append(tmp, 10, Integer.MAX_VALUE);
90              Assert.fail("IndexOutOfBoundsException should have been thrown");
91          } catch (final IndexOutOfBoundsException ex) {
92              // expected
93          }
94          try {
95              buffer.append(tmp, 2, 4);
96              Assert.fail("IndexOutOfBoundsException should have been thrown");
97          } catch (final IndexOutOfBoundsException ex) {
98              // expected
99          }
100     }
101 
102     @Test
103     public void testEnsureCapacity() throws Exception {
104         final ByteArrayBuilder buffer = new ByteArrayBuilder();
105         buffer.ensureFreeCapacity(10);
106         Assert.assertEquals(10, buffer.capacity());
107         buffer.ensureFreeCapacity(5);
108         Assert.assertEquals(10, buffer.capacity());
109         buffer.append(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
110         buffer.ensureFreeCapacity(5);
111         Assert.assertEquals(13, buffer.capacity());
112         buffer.ensureFreeCapacity(15);
113         Assert.assertEquals(23, buffer.capacity());
114     }
115 
116     @Test
117     public void testAppendText() throws Exception {
118         final ByteArrayBuilder buffer = new ByteArrayBuilder();
119         buffer.append(new char[]{'1', '2', '3', '4', '5'});
120         buffer.append(new char[]{'3', '4', '5', '6', '7', '8', '9', 'a', 'b'}, 3, 5);
121         buffer.append("bcd");
122         buffer.append("e");
123         buffer.append("f");
124         buffer.append((String) null);
125         buffer.append((char[]) null);
126 
127         final byte[] bytes = buffer.toByteArray();
128         Assert.assertNotNull(bytes);
129         Assert.assertEquals("123456789abcdef", new String(bytes, StandardCharsets.US_ASCII));
130     }
131 
132     @Test
133     public void testInvalidAppendChars() throws Exception {
134         final ByteArrayBuilder buffer = new ByteArrayBuilder();
135         buffer.append((char[])null, 0, 0);
136 
137         final char[] tmp = new char[] { 1, 2, 3, 4};
138         try {
139             buffer.append(tmp, -1, 0);
140             Assert.fail("IndexOutOfBoundsException should have been thrown");
141         } catch (final IndexOutOfBoundsException ex) {
142             // expected
143         }
144         try {
145             buffer.append(tmp, 0, -1);
146             Assert.fail("IndexOutOfBoundsException should have been thrown");
147         } catch (final IndexOutOfBoundsException ex) {
148             // expected
149         }
150         try {
151             buffer.append(tmp, 0, 8);
152             Assert.fail("IndexOutOfBoundsException should have been thrown");
153         } catch (final IndexOutOfBoundsException ex) {
154             // expected
155         }
156         try {
157             buffer.append(tmp, 10, Integer.MAX_VALUE);
158             Assert.fail("IndexOutOfBoundsException should have been thrown");
159         } catch (final IndexOutOfBoundsException ex) {
160             // expected
161         }
162         try {
163             buffer.append(tmp, 2, 4);
164             Assert.fail("IndexOutOfBoundsException should have been thrown");
165         } catch (final IndexOutOfBoundsException ex) {
166             // expected
167         }
168     }
169 
170     @Test
171     public void testReset() throws Exception {
172         final ByteArrayBuilder buffer = new ByteArrayBuilder();
173         buffer.append("abcd");
174         buffer.append("e");
175         buffer.append("f");
176 
177         final byte[] bytes1 = buffer.toByteArray();
178         Assert.assertNotNull(bytes1);
179         Assert.assertEquals("abcdef", new String(bytes1, StandardCharsets.US_ASCII));
180 
181         buffer.reset();
182 
183         final byte[] bytes2 = buffer.toByteArray();
184         Assert.assertNotNull(bytes2);
185         Assert.assertEquals("", new String(bytes2, StandardCharsets.US_ASCII));
186     }
187 
188     @Test
189     public void testNonAsciiCharset() throws Exception {
190         final int[] germanChars = { 0xE4, 0x2D, 0xF6, 0x2D, 0xFc };
191         final StringBuilder tmp = new StringBuilder();
192         for (final int germanChar : germanChars) {
193             tmp.append((char) germanChar);
194         }
195         final String umlauts = tmp.toString();
196 
197 
198         final ByteArrayBuilder buffer = new ByteArrayBuilder();
199         buffer.append(umlauts);
200 
201         final byte[] bytes1 = buffer.toByteArray();
202         Assert.assertNotNull(bytes1);
203         Assert.assertEquals("?-?-?", new String(bytes1, StandardCharsets.US_ASCII));
204 
205         buffer.reset();
206         buffer.charset(StandardCharsets.UTF_8);
207         buffer.append(umlauts);
208 
209         final byte[] bytes2 = buffer.toByteArray();
210         Assert.assertNotNull(bytes2);
211         Assert.assertEquals(umlauts, new String(bytes2, StandardCharsets.UTF_8));
212     }
213 
214 }