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          try (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  
54      @Test
55      public void testBasicOffLen() throws Exception {
56          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
57          try (final ByteArrayEntity entity = new ByteArrayEntity(bytes, 8, 7, null)) {
58  
59              Assertions.assertEquals(7, entity.getContentLength());
60              Assertions.assertNotNull(entity.getContent());
61              Assertions.assertTrue(entity.isRepeatable());
62              Assertions.assertFalse(entity.isStreaming());
63          }
64      }
65  
66      @Test
67      public void testIllegalConstructorNullByteArray() throws Exception {
68          Assertions.assertThrows(NullPointerException.class, () ->
69                  new ByteArrayEntity(null, null));
70      }
71  
72      @Test
73      public void testIllegalConstructorBadLen() throws Exception {
74          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
75          Assertions.assertThrows(IllegalArgumentException.class, () ->
76                  new ByteArrayEntity(bytes, 0, bytes.length + 1, null));
77      }
78  
79      @Test
80      public void testIllegalConstructorBadOff1() throws Exception {
81          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
82          Assertions.assertThrows(IllegalArgumentException.class, () ->
83                  new ByteArrayEntity(bytes, -1, bytes.length, null));
84      }
85  
86      @Test
87      public void testIllegalConstructorBadOff2() throws Exception {
88          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
89          Assertions.assertThrows(IllegalArgumentException.class, () ->
90                  new ByteArrayEntity(bytes, bytes.length + 1, bytes.length, null));
91      }
92  
93      @Test
94      public void testWriteTo() throws Exception {
95          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
96          try (final ByteArrayEntity entity = new ByteArrayEntity(bytes, null)) {
97  
98              ByteArrayOutputStream out = new ByteArrayOutputStream();
99              entity.writeTo(out);
100             byte[] bytes2 = out.toByteArray();
101             Assertions.assertNotNull(bytes2);
102             Assertions.assertEquals(bytes.length, bytes2.length);
103             for (int i = 0; i < bytes.length; i++) {
104                 Assertions.assertEquals(bytes[i], bytes2[i]);
105             }
106 
107             out = new ByteArrayOutputStream();
108             entity.writeTo(out);
109             bytes2 = out.toByteArray();
110             Assertions.assertNotNull(bytes2);
111             Assertions.assertEquals(bytes.length, bytes2.length);
112             for (int i = 0; i < bytes.length; i++) {
113                 Assertions.assertEquals(bytes[i], bytes2[i]);
114             }
115 
116             Assertions.assertThrows(NullPointerException.class, () -> entity.writeTo(null));
117         }
118     }
119 
120     @Test
121     public void testWriteToOffLen() throws Exception {
122         final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
123         final int off = 8;
124         final int len = 7;
125         try (final ByteArrayEntity entity = new ByteArrayEntity(bytes, off, len, null)) {
126 
127             ByteArrayOutputStream out = new ByteArrayOutputStream();
128             entity.writeTo(out);
129             byte[] bytes2 = out.toByteArray();
130             Assertions.assertNotNull(bytes2);
131             Assertions.assertEquals(len, bytes2.length);
132             for (int i = 0; i < len; i++) {
133                 Assertions.assertEquals(bytes[i + off], bytes2[i]);
134             }
135 
136             out = new ByteArrayOutputStream();
137             entity.writeTo(out);
138             bytes2 = out.toByteArray();
139             Assertions.assertNotNull(bytes2);
140             Assertions.assertEquals(len, bytes2.length);
141             for (int i = 0; i < len; i++) {
142                 Assertions.assertEquals(bytes[i + off], bytes2[i]);
143             }
144 
145             Assertions.assertThrows(NullPointerException.class, () -> entity.writeTo(null));
146         }
147     }
148 
149 }