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 BasicHttpEntity}.
40   *
41   */
42  public class TestBasicHttpEntity {
43  
44      @Test
45      public void testBasics() throws Exception {
46  
47          final byte[] bytes = "Message content".getBytes(Consts.ASCII);
48          final InputStream content = new ByteArrayInputStream(bytes);
49          final BasicHttpEntity httpentity = new BasicHttpEntity();
50          httpentity.setContent(content);
51          httpentity.setContentLength(bytes.length);
52  
53          Assert.assertEquals(bytes.length, httpentity.getContentLength());
54          Assert.assertFalse(httpentity.isRepeatable());
55          Assert.assertTrue(httpentity.isStreaming());
56      }
57  
58      @Test
59      public void testContent() throws Exception {
60          final byte[] bytes = "Message content".getBytes(Consts.ASCII);
61          final InputStream content = new ByteArrayInputStream(bytes);
62          final BasicHttpEntity httpentity = new BasicHttpEntity();
63          try {
64              httpentity.getContent();
65              Assert.fail("IllegalStateException should have been thrown");
66          } catch (final IllegalStateException ex) {
67              // expected
68          }
69          httpentity.setContent(content);
70          Assert.assertEquals(content, httpentity.getContent());
71  
72          httpentity.setContent(null);
73          try {
74              httpentity.getContent();
75              Assert.fail("IllegalStateException should have been thrown");
76          } catch (final IllegalStateException ex) {
77              // expected
78          }
79      }
80  
81      @Test
82      public void testWriteTo() throws Exception {
83          final byte[] bytes = "Message content".getBytes(Consts.ASCII);
84          final InputStream content = new ByteArrayInputStream(bytes);
85          final BasicHttpEntity httpentity = new BasicHttpEntity();
86          httpentity.setContent(content);
87  
88          ByteArrayOutputStream out = new ByteArrayOutputStream();
89          httpentity.writeTo(out);
90          final byte[] bytes2 = out.toByteArray();
91          Assert.assertNotNull(bytes2);
92          Assert.assertEquals(bytes.length, bytes2.length);
93          for (int i = 0; i < bytes.length; i++) {
94              Assert.assertEquals(bytes[i], bytes2[i]);
95          }
96          httpentity.setContent(null);
97          out = new ByteArrayOutputStream();
98          try {
99              httpentity.writeTo(out);
100             Assert.fail("IllegalStateException should have been thrown");
101         } catch (final IllegalStateException ex) {
102             // expected
103         }
104 
105         try {
106             httpentity.writeTo(null);
107             Assert.fail("IllegalArgumentException should have been thrown");
108         } catch (final IllegalArgumentException ex) {
109             // expected
110         }
111     }
112 
113 }