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

Quick Search    Search Deep

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


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/test/org/apache/http/entity/TestHttpEntityWrapper.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.ByteArrayOutputStream;
33  
34  import junit.framework.Test;
35  import junit.framework.TestCase;
36  import junit.framework.TestSuite;
37  
38  import org.apache.http.protocol.HTTP;
39  
40  /**
41   * Unit tests for {@link WrappedEntity}.
42   *
43   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
44   */
45  public class TestHttpEntityWrapper extends TestCase {
46  
47      public TestHttpEntityWrapper(String testName) {
48          super(testName);
49      }
50  
51      public static void main(String args[]) {
52          String[] testCaseName = { TestHttpEntityWrapper.class.getName() };
53          junit.textui.TestRunner.main(testCaseName);
54      }
55  
56      public static Test suite() {
57          return new TestSuite(TestHttpEntityWrapper.class);
58      }
59  
60      public void testBasics() throws Exception {
61          String s = "Message content";
62          StringEntity httpentity = new StringEntity(s, HTTP.ISO_8859_1);
63          httpentity.setContentType(HTTP.PLAIN_TEXT_TYPE);
64          httpentity.setContentEncoding(HTTP.IDENTITY_CODING);
65          HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity); 
66          
67          assertEquals(httpentity.getContentLength(), wrapped.getContentLength());
68          assertEquals(httpentity.getContentType(), wrapped.getContentType());
69          assertEquals(httpentity.getContentEncoding(), wrapped.getContentEncoding());
70          assertEquals(httpentity.isChunked(), wrapped.isChunked());
71          assertEquals(httpentity.isRepeatable(), wrapped.isRepeatable());
72          assertEquals(httpentity.isStreaming(), wrapped.isStreaming());
73          assertNotNull(wrapped.getContent());
74      }
75  
76      public void testIllegalConstructor() throws Exception {
77          try {
78              new HttpEntityWrapper(null);
79              fail("IllegalArgumentException should have been thrown");
80          } catch (IllegalArgumentException ex) {
81              // expected
82          }
83      }
84  
85      public void testWriteTo() throws Exception {
86          String s = "Message content";
87          byte[] bytes = s.getBytes(HTTP.ISO_8859_1);
88          StringEntity httpentity = new StringEntity(s);
89          HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity); 
90          
91          ByteArrayOutputStream out = new ByteArrayOutputStream();
92          wrapped.writeTo(out);
93          byte[] bytes2 = out.toByteArray();
94          assertNotNull(bytes2);
95          assertEquals(bytes.length, bytes2.length);
96          for (int i = 0; i < bytes.length; i++) {
97              assertEquals(bytes[i], bytes2[i]);
98          }
99  
100         out = new ByteArrayOutputStream();
101         wrapped.writeTo(out);
102         bytes2 = out.toByteArray();
103         assertNotNull(bytes2);
104         assertEquals(bytes.length, bytes2.length);
105         for (int i = 0; i < bytes.length; i++) {
106             assertEquals(bytes[i], bytes2[i]);
107         }
108         
109         try {
110             wrapped.writeTo(null);
111             fail("IllegalArgumentException should have been thrown");
112         } catch (IllegalArgumentException ex) {
113             // expected
114         }
115     }
116 
117     public void testConsumeContent() throws Exception {
118         String s = "Message content";
119         StringEntity httpentity = new StringEntity(s);
120         HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);
121         wrapped.consumeContent();
122         wrapped.consumeContent();
123     }
124     
125 }