Source code: org/apache/http/entity/TestInputStreamEntity.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/test/org/apache/http/entity/TestInputStreamEntity.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 InputStreamEntity}.
44 *
45 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
46 */
47 public class TestInputStreamEntity extends TestCase {
48
49 public TestInputStreamEntity(String testName) {
50 super(testName);
51 }
52
53 public static void main(String args[]) {
54 String[] testCaseName = { TestInputStreamEntity.class.getName() };
55 junit.textui.TestRunner.main(testCaseName);
56 }
57
58 public static Test suite() {
59 return new TestSuite(TestInputStreamEntity.class);
60 }
61
62 public void testBasics() throws Exception {
63 byte[] bytes = "Message content".getBytes(HTTP.ISO_8859_1);
64 InputStream instream = new ByteArrayInputStream(bytes);
65 InputStreamEntity httpentity = new InputStreamEntity(instream, bytes.length);
66
67 assertEquals(bytes.length, httpentity.getContentLength());
68 assertEquals(instream, httpentity.getContent());
69 assertNotNull(httpentity.getContent());
70 assertFalse(httpentity.isRepeatable());
71 assertTrue(httpentity.isStreaming());
72 }
73
74 public void testIllegalConstructor() throws Exception {
75 try {
76 new InputStreamEntity(null, 0);
77 fail("IllegalArgumentException should have been thrown");
78 } catch (IllegalArgumentException ex) {
79 // expected
80 }
81 }
82
83 public void testWriteTo() throws Exception {
84 byte[] bytes = "Message content".getBytes(HTTP.ISO_8859_1);
85 InputStream instream = new ByteArrayInputStream(bytes);
86 InputStreamEntity httpentity = new InputStreamEntity(instream, 7);
87
88 ByteArrayOutputStream out = new ByteArrayOutputStream();
89 httpentity.writeTo(out);
90 byte[] bytes2 = out.toByteArray();
91 assertNotNull(bytes2);
92 assertEquals(7, bytes2.length);
93 String s = new String(bytes2, HTTP.ISO_8859_1);
94 assertEquals("Message", s);
95
96 instream = new ByteArrayInputStream(bytes);
97 httpentity = new InputStreamEntity(instream, 20);
98 out = new ByteArrayOutputStream();
99 httpentity.writeTo(out);
100 bytes2 = out.toByteArray();
101 assertNotNull(bytes2);
102 assertEquals(bytes.length, bytes2.length);
103 assertFalse(httpentity.isStreaming());
104
105 instream = new ByteArrayInputStream(bytes);
106 httpentity = new InputStreamEntity(instream, -1);
107 out = new ByteArrayOutputStream();
108 httpentity.writeTo(out);
109 bytes2 = out.toByteArray();
110 assertNotNull(bytes2);
111 assertEquals(bytes.length, bytes2.length);
112 assertFalse(httpentity.isStreaming());
113
114 try {
115 httpentity.writeTo(null);
116 fail("IllegalArgumentException should have been thrown");
117 } catch (IllegalArgumentException ex) {
118 // expected
119 }
120 }
121
122 public void testConsume() throws Exception {
123 byte[] bytes = "Message content".getBytes(HTTP.ISO_8859_1);
124 InputStream instream = new ByteArrayInputStream(bytes);
125 InputStreamEntity httpentity = new InputStreamEntity(instream, -1);
126 assertTrue(httpentity.isStreaming());
127 httpentity.consumeContent();
128 assertFalse(httpentity.isStreaming());
129 httpentity.consumeContent();
130 assertFalse(httpentity.isStreaming());
131 }
132
133 }