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.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.nio.charset.StandardCharsets;
33  
34  import org.junit.Assert;
35  import org.junit.Test;
36  
37  /**
38   * Unit tests for {@link BufferedHttpEntity}.
39   *
40   */
41  public class TestBufferedHttpEntity {
42  
43      @Test
44      public void testBufferingEntity() throws Exception {
45          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
46          final BufferedHttpEntity entity = new BufferedHttpEntity(
47                  new InputStreamEntity(new ByteArrayInputStream(bytes), -1, null));
48          Assert.assertEquals(bytes.length, entity.getContentLength());
49          Assert.assertTrue(entity.isRepeatable());
50          Assert.assertFalse(entity.isChunked());
51          Assert.assertFalse(entity.isStreaming());
52  
53          // test if we can obtain contain multiple times
54          Assert.assertNotNull(entity.getContent ());
55          Assert.assertNotNull(entity.getContent ());
56      }
57  
58      @Test
59      public void testWrappingEntity() throws Exception {
60          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
61          final ByteArrayEntity httpentity = new ByteArrayEntity(bytes, null, true);
62          final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
63          Assert.assertEquals(bytes.length, bufentity.getContentLength());
64          Assert.assertTrue(bufentity.isRepeatable());
65          Assert.assertTrue(bufentity.isChunked());
66          Assert.assertFalse(bufentity.isStreaming());
67  
68          // test if we can obtain contain multiple times
69          Assert.assertNotNull(bufentity.getContent ());
70          Assert.assertNotNull(bufentity.getContent ());
71      }
72  
73      @Test
74      public void testIllegalConstructor() throws Exception {
75          try {
76              new BufferedHttpEntity(null);
77              Assert.fail("NullPointerException should have been thrown");
78          } catch (final NullPointerException ex) {
79              // expected
80          }
81      }
82  
83      @Test
84      public void testWriteToBuffered() throws Exception {
85          final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
86          final InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes), -1, null);
87          final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
88  
89          ByteArrayOutputStream out = new ByteArrayOutputStream();
90          bufentity.writeTo(out);
91          byte[] bytes2 = out.toByteArray();
92          Assert.assertNotNull(bytes2);
93          Assert.assertEquals(bytes.length, bytes2.length);
94          for (int i = 0; i < bytes.length; i++) {
95              Assert.assertEquals(bytes[i], bytes2[i]);
96          }
97  
98          out = new ByteArrayOutputStream();
99          bufentity.writeTo(out);
100         bytes2 = out.toByteArray();
101         Assert.assertNotNull(bytes2);
102         Assert.assertEquals(bytes.length, bytes2.length);
103         for (int i = 0; i < bytes.length; i++) {
104             Assert.assertEquals(bytes[i], bytes2[i]);
105         }
106 
107         try {
108             bufentity.writeTo(null);
109             Assert.fail("NullPointerException should have been thrown");
110         } catch (final NullPointerException ex) {
111             // expected
112         }
113     }
114 
115     @Test
116     public void testWriteToWrapped() throws Exception {
117         final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
118         final ByteArrayEntity httpentity = new ByteArrayEntity(bytes, null);
119         final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
120 
121         ByteArrayOutputStream out = new ByteArrayOutputStream();
122         bufentity.writeTo(out);
123         byte[] bytes2 = out.toByteArray();
124         Assert.assertNotNull(bytes2);
125         Assert.assertEquals(bytes.length, bytes2.length);
126         for (int i = 0; i < bytes.length; i++) {
127             Assert.assertEquals(bytes[i], bytes2[i]);
128         }
129 
130         out = new ByteArrayOutputStream();
131         bufentity.writeTo(out);
132         bytes2 = out.toByteArray();
133         Assert.assertNotNull(bytes2);
134         Assert.assertEquals(bytes.length, bytes2.length);
135         for (int i = 0; i < bytes.length; i++) {
136             Assert.assertEquals(bytes[i], bytes2[i]);
137         }
138 
139         try {
140             bufentity.writeTo(null);
141             Assert.fail("NullPointerException should have been thrown");
142         } catch (final NullPointerException ex) {
143             // expected
144         }
145     }
146 
147 }