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.ByteArrayOutputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.OutputStream;
34  
35  import org.apache.http.HttpEntity;
36  import org.apache.http.util.EntityUtils;
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  /**
41   * Unit tests for {@link EntityTemplate}.
42   */
43  public class TestEntityTemplate {
44  
45      @Test
46      public void testBasics() throws Exception {
47  
48          final HttpEntity httpentity = new EntityTemplate(new ContentProducer() {
49  
50              @Override
51              public void writeTo(final OutputStream outstream) throws IOException {
52                  outstream.write('a');
53              }
54  
55          });
56  
57          Assert.assertEquals(-1, httpentity.getContentLength());
58          Assert.assertTrue(httpentity.isRepeatable());
59          Assert.assertFalse(httpentity.isStreaming());
60      }
61  
62      @Test
63      public void testIllegalConstructor() throws Exception {
64          try {
65              new EntityTemplate(null);
66              Assert.fail("IllegalArgumentException should have been thrown");
67          } catch (final IllegalArgumentException ex) {
68              // expected
69          }
70      }
71  
72      @Test
73      public void testWriteTo() throws Exception {
74          final HttpEntity httpentity = new EntityTemplate(new ContentProducer() {
75  
76              @Override
77              public void writeTo(final OutputStream outstream) throws IOException {
78                  outstream.write('a');
79              }
80  
81          });
82  
83          final ByteArrayOutputStream out = new ByteArrayOutputStream();
84          httpentity.writeTo(out);
85          final byte[] bytes2 = out.toByteArray();
86          Assert.assertNotNull(bytes2);
87          Assert.assertEquals(1, bytes2.length);
88  
89          try {
90              httpentity.writeTo(null);
91              Assert.fail("IllegalArgumentException should have been thrown");
92          } catch (final IllegalArgumentException ex) {
93              // expected
94          }
95      }
96  
97      @Test
98      public void testgetContent() throws Exception {
99          final HttpEntity httpentity = new EntityTemplate(new ContentProducer() {
100 
101             @Override
102             public void writeTo(final OutputStream outstream) throws IOException {
103                 outstream.write('a');
104             }
105 
106         });
107         final InputStream instream = httpentity.getContent();
108         Assert.assertNotNull(instream);
109         final String s = EntityUtils.toString(httpentity);
110         Assert.assertEquals("a", s);
111     }
112 
113 }