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.http.io.entity;
29  
30  import java.io.ByteArrayOutputStream;
31  import java.nio.charset.StandardCharsets;
32  
33  import org.junit.jupiter.api.Assertions;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Unit tests for {@link ByteArrayEntity}.
38   *
39   */
40  public class TestByteArrayEntity {
41  
42      @Test
43      public void testBasics() throws Exception {
44          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
45          final ByteArrayEntity entity = new ByteArrayEntity(bytes, null);
46  
47          Assertions.assertEquals(bytes.length, entity.getContentLength());
48          Assertions.assertNotNull(entity.getContent());
49          Assertions.assertTrue(entity.isRepeatable());
50          Assertions.assertFalse(entity.isStreaming());
51      }
52  
53      @Test
54      public void testBasicOffLen() throws Exception {
55          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
56          final ByteArrayEntity entity = new ByteArrayEntity(bytes, 8, 7, null);
57  
58          Assertions.assertEquals(7, entity.getContentLength());
59          Assertions.assertNotNull(entity.getContent());
60          Assertions.assertTrue(entity.isRepeatable());
61          Assertions.assertFalse(entity.isStreaming());
62      }
63  
64      @Test
65      public void testIllegalConstructorNullByteArray() throws Exception {
66          Assertions.assertThrows(NullPointerException.class, () ->
67                  new ByteArrayEntity(null, null));
68      }
69  
70      @Test
71      public void testIllegalConstructorBadLen() throws Exception {
72          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
73          Assertions.assertThrows(IllegalArgumentException.class, () ->
74                  new ByteArrayEntity(bytes, 0, bytes.length + 1, null));
75      }
76  
77      @Test
78      public void testIllegalConstructorBadOff1() throws Exception {
79          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
80          Assertions.assertThrows(IllegalArgumentException.class, () ->
81                  new ByteArrayEntity(bytes, -1, bytes.length, null));
82      }
83  
84      @Test
85      public void testIllegalConstructorBadOff2() throws Exception {
86          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
87          Assertions.assertThrows(IllegalArgumentException.class, () ->
88                  new ByteArrayEntity(bytes, bytes.length + 1, bytes.length, null));
89      }
90  
91      @Test
92      public void testWriteTo() throws Exception {
93          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
94          final ByteArrayEntity entity = new ByteArrayEntity(bytes, null);
95  
96          ByteArrayOutputStream out = new ByteArrayOutputStream();
97          entity.writeTo(out);
98          byte[] bytes2 = out.toByteArray();
99          Assertions.assertNotNull(bytes2);
100         Assertions.assertEquals(bytes.length, bytes2.length);
101         for (int i = 0; i < bytes.length; i++) {
102             Assertions.assertEquals(bytes[i], bytes2[i]);
103         }
104 
105         out = new ByteArrayOutputStream();
106         entity.writeTo(out);
107         bytes2 = out.toByteArray();
108         Assertions.assertNotNull(bytes2);
109         Assertions.assertEquals(bytes.length, bytes2.length);
110         for (int i = 0; i < bytes.length; i++) {
111             Assertions.assertEquals(bytes[i], bytes2[i]);
112         }
113 
114         Assertions.assertThrows(NullPointerException.class, () -> entity.writeTo(null));
115     }
116 
117     @Test
118     public void testWriteToOffLen() throws Exception {
119         final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
120         final int off = 8;
121         final int len = 7;
122         final ByteArrayEntity entity = new ByteArrayEntity(bytes, off, len, null);
123 
124         ByteArrayOutputStream out = new ByteArrayOutputStream();
125         entity.writeTo(out);
126         byte[] bytes2 = out.toByteArray();
127         Assertions.assertNotNull(bytes2);
128         Assertions.assertEquals(len, bytes2.length);
129         for (int i = 0; i < len; i++) {
130             Assertions.assertEquals(bytes[i+off], bytes2[i]);
131         }
132 
133         out = new ByteArrayOutputStream();
134         entity.writeTo(out);
135         bytes2 = out.toByteArray();
136         Assertions.assertNotNull(bytes2);
137         Assertions.assertEquals(len, bytes2.length);
138         for (int i = 0; i < len; i++) {
139             Assertions.assertEquals(bytes[i+off], bytes2[i]);
140         }
141 
142         Assertions.assertThrows(NullPointerException.class, () -> entity.writeTo(null));
143     }
144 
145 }