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.http.entity;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.InputStream;
33  
34  import org.apache.http.Consts;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  /**
39   * Unit tests for {@link InputStreamEntity}.
40   *
41   */
42  public class TestInputStreamEntity {
43  
44      @Test
45      public void testBasics() throws Exception {
46          final byte[] bytes = "Message content".getBytes(Consts.ISO_8859_1);
47          final InputStream inStream = new ByteArrayInputStream(bytes);
48          final InputStreamEntity httpentity = new InputStreamEntity(inStream, bytes.length);
49  
50          Assert.assertEquals(bytes.length, httpentity.getContentLength());
51          Assert.assertEquals(inStream, httpentity.getContent());
52          Assert.assertNotNull(httpentity.getContent());
53          Assert.assertFalse(httpentity.isRepeatable());
54          Assert.assertTrue(httpentity.isStreaming());
55      }
56  
57      @Test(expected = IllegalArgumentException.class)
58      public void testIllegalConstructor() throws Exception {
59          new InputStreamEntity(null, 0);
60      }
61  
62      @Test
63      public void testUnknownLengthConstructor() throws Exception {
64          final InputStream inStream = new ByteArrayInputStream(new byte[0]);
65          final InputStreamEntity httpentity = new InputStreamEntity(inStream);
66          Assert.assertEquals(-1, httpentity.getContentLength());
67      }
68  
69      @Test
70      public void testWriteTo() throws Exception {
71          final String message = "Message content";
72          final byte[] bytes = message.getBytes(Consts.ISO_8859_1);
73          final InputStream inStream = new ByteArrayInputStream(bytes);
74          final InputStreamEntity httpentity = new InputStreamEntity(inStream, bytes.length);
75  
76          final ByteArrayOutputStream out = new ByteArrayOutputStream();
77          httpentity.writeTo(out);
78          final byte[] writtenBytes = out.toByteArray();
79          Assert.assertNotNull(writtenBytes);
80          Assert.assertEquals(bytes.length, writtenBytes.length);
81  
82          final String s = new String(writtenBytes, Consts.ISO_8859_1.name());
83          Assert.assertEquals(message, s);
84      }
85  
86      @Test
87      public void testWriteToPartialContent() throws Exception {
88          final String message = "Message content";
89          final byte[] bytes = message.getBytes(Consts.ISO_8859_1);
90          final InputStream inStream = new ByteArrayInputStream(bytes);
91          final int contentLength = 7;
92          final InputStreamEntity httpentity = new InputStreamEntity(inStream, contentLength);
93  
94          final ByteArrayOutputStream out = new ByteArrayOutputStream();
95          httpentity.writeTo(out);
96          final byte[] writtenBytes = out.toByteArray();
97          Assert.assertNotNull(writtenBytes);
98          Assert.assertEquals(contentLength, writtenBytes.length);
99  
100         final String s = new String(writtenBytes, Consts.ISO_8859_1.name());
101         Assert.assertEquals(message.substring(0, contentLength), s);
102     }
103 
104     @Test
105     public void testWriteToUnknownLength() throws Exception {
106         final String message = "Message content";
107         final byte[] bytes = message.getBytes(Consts.ISO_8859_1);
108         final InputStream inStream = new ByteArrayInputStream(bytes);
109         final InputStreamEntity httpentity = new InputStreamEntity(inStream);
110 
111         final ByteArrayOutputStream out = new ByteArrayOutputStream();
112         httpentity.writeTo(out);
113         final byte[] writtenBytes = out.toByteArray();
114         Assert.assertNotNull(writtenBytes);
115         Assert.assertEquals(bytes.length, writtenBytes.length);
116 
117         final String s = new String(writtenBytes, Consts.ISO_8859_1.name());
118         Assert.assertEquals(message, s);
119     }
120 
121     @Test(expected = IllegalArgumentException.class)
122     public void testWriteToNull() throws Exception {
123         final InputStream inStream = new ByteArrayInputStream(new byte[0]);
124         final InputStreamEntity httpentity = new InputStreamEntity(inStream, 0);
125         httpentity.writeTo(null);
126     }
127 }