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