Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/entity/TestBasicHttpEntity.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/test/org/apache/http/entity/TestBasicHttpEntity.java $
3    * $Revision: 385310 $
4    * $Date: 2006-03-12 17:28:43 +0100 (Sun, 12 Mar 2006) $
5    * 
6    * ====================================================================
7    *
8    *  Copyright 1999-2006 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.http.entity;
31  
32  import java.io.ByteArrayInputStream;
33  import java.io.ByteArrayOutputStream;
34  import java.io.InputStream;
35  
36  import org.apache.http.protocol.HTTP;
37  
38  import junit.framework.Test;
39  import junit.framework.TestCase;
40  import junit.framework.TestSuite;
41  
42  /**
43   * Unit tests for {@link BasicHttpEntity}.
44   *
45   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
46   */
47  public class TestBasicHttpEntity extends TestCase {
48  
49      public TestBasicHttpEntity(String testName) {
50          super(testName);
51      }
52  
53      public static void main(String args[]) {
54          String[] testCaseName = { TestBasicHttpEntity.class.getName() };
55          junit.textui.TestRunner.main(testCaseName);
56      }
57  
58      public static Test suite() {
59          return new TestSuite(TestBasicHttpEntity.class);
60      }
61  
62      public void testBasics() throws Exception {
63        
64        byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
65        InputStream content = new ByteArrayInputStream(bytes);
66        BasicHttpEntity httpentity = new BasicHttpEntity();
67        httpentity.setContent(content);
68        httpentity.setContentLength(bytes.length);
69        
70        assertEquals(bytes.length, httpentity.getContentLength());
71        assertFalse(httpentity.isRepeatable());
72          assertTrue(httpentity.isStreaming());
73      }
74      
75      public void testContent() throws Exception {
76          byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
77          InputStream content = new ByteArrayInputStream(bytes);
78          BasicHttpEntity httpentity = new BasicHttpEntity();
79          try {
80              httpentity.getContent();
81              fail("IllegalStateException should have been thrown");
82          } catch (IllegalStateException ex) {
83              // expected
84          }
85  
86          httpentity.setContent(null);
87          try {
88              httpentity.getContent();
89              fail("IllegalStateException should have been thrown");
90          } catch (IllegalStateException ex) {
91              // expected
92          }
93  
94          httpentity.setContent(content);
95          httpentity.getContent();
96          try {
97              httpentity.getContent();
98              fail("IllegalStateException should have been thrown");
99          } catch (IllegalStateException ex) {
100             // expected
101         }
102 
103     }
104     
105     public void testWriteTo() throws Exception {
106       byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
107       InputStream content = new ByteArrayInputStream(bytes);
108       BasicHttpEntity httpentity = new BasicHttpEntity();
109       httpentity.setContent(content);
110       
111       ByteArrayOutputStream out = new ByteArrayOutputStream();
112       httpentity.writeTo(out);
113       byte[] bytes2 = out.toByteArray();
114       assertNotNull(bytes2);
115       assertEquals(bytes.length, bytes2.length);
116       for (int i = 0; i < bytes.length; i++) {
117         assertEquals(bytes[i], bytes2[i]);
118       }
119         out = new ByteArrayOutputStream();
120         try {
121             httpentity.writeTo(out);
122             fail("IllegalStateException should have been thrown");
123         } catch (IllegalStateException ex) {
124             // expected
125         }
126 
127       httpentity.setContent(null);
128         out = new ByteArrayOutputStream();
129         try {
130             httpentity.writeTo(out);
131             fail("IllegalStateException should have been thrown");
132         } catch (IllegalStateException ex) {
133             // expected
134         }
135 
136       try {
137         httpentity.writeTo(null);
138         fail("IllegalArgumentException should have been thrown");
139       } catch (IllegalArgumentException ex) {
140         // expected
141       }
142     }
143 
144     public void testConsumeContent() throws Exception {
145         byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
146         InputStream content = new ByteArrayInputStream(bytes);
147         BasicHttpEntity httpentity = new BasicHttpEntity();
148         httpentity.setContent(null);
149         httpentity.consumeContent();
150         httpentity.setContent(content);
151         httpentity.consumeContent();
152     }
153     
154 }