001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/entity/TestInputStreamEntity.java $
003: * $Revision: 496069 $
004: * $Date: 2007-01-14 13:03:05 +0100 (Sun, 14 Jan 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.entity;
033:
034: import java.io.ByteArrayInputStream;
035: import java.io.ByteArrayOutputStream;
036: import java.io.InputStream;
037:
038: import org.apache.http.protocol.HTTP;
039:
040: import junit.framework.Test;
041: import junit.framework.TestCase;
042: import junit.framework.TestSuite;
043:
044: /**
045: * Unit tests for {@link InputStreamEntity}.
046: *
047: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
048: */
049: public class TestInputStreamEntity extends TestCase {
050:
051: public TestInputStreamEntity(String testName) {
052: super (testName);
053: }
054:
055: public static void main(String args[]) {
056: String[] testCaseName = { TestInputStreamEntity.class.getName() };
057: junit.textui.TestRunner.main(testCaseName);
058: }
059:
060: public static Test suite() {
061: return new TestSuite(TestInputStreamEntity.class);
062: }
063:
064: public void testBasics() throws Exception {
065: byte[] bytes = "Message content".getBytes(HTTP.ISO_8859_1);
066: InputStream instream = new ByteArrayInputStream(bytes);
067: InputStreamEntity httpentity = new InputStreamEntity(instream,
068: bytes.length);
069:
070: assertEquals(bytes.length, httpentity.getContentLength());
071: assertEquals(instream, httpentity.getContent());
072: assertNotNull(httpentity.getContent());
073: assertFalse(httpentity.isRepeatable());
074: assertTrue(httpentity.isStreaming());
075: }
076:
077: public void testIllegalConstructor() throws Exception {
078: try {
079: new InputStreamEntity(null, 0);
080: fail("IllegalArgumentException should have been thrown");
081: } catch (IllegalArgumentException ex) {
082: // expected
083: }
084: }
085:
086: public void testWriteTo() throws Exception {
087: byte[] bytes = "Message content".getBytes(HTTP.ISO_8859_1);
088: InputStream instream = new ByteArrayInputStream(bytes);
089: InputStreamEntity httpentity = new InputStreamEntity(instream,
090: 7);
091:
092: ByteArrayOutputStream out = new ByteArrayOutputStream();
093: httpentity.writeTo(out);
094: byte[] bytes2 = out.toByteArray();
095: assertNotNull(bytes2);
096: assertEquals(7, bytes2.length);
097: String s = new String(bytes2, HTTP.ISO_8859_1);
098: assertEquals("Message", s);
099:
100: instream = new ByteArrayInputStream(bytes);
101: httpentity = new InputStreamEntity(instream, 20);
102: out = new ByteArrayOutputStream();
103: httpentity.writeTo(out);
104: bytes2 = out.toByteArray();
105: assertNotNull(bytes2);
106: assertEquals(bytes.length, bytes2.length);
107: assertFalse(httpentity.isStreaming());
108:
109: instream = new ByteArrayInputStream(bytes);
110: httpentity = new InputStreamEntity(instream, -1);
111: out = new ByteArrayOutputStream();
112: httpentity.writeTo(out);
113: bytes2 = out.toByteArray();
114: assertNotNull(bytes2);
115: assertEquals(bytes.length, bytes2.length);
116: assertFalse(httpentity.isStreaming());
117:
118: try {
119: httpentity.writeTo(null);
120: fail("IllegalArgumentException should have been thrown");
121: } catch (IllegalArgumentException ex) {
122: // expected
123: }
124: }
125:
126: public void testConsume() throws Exception {
127: byte[] bytes = "Message content".getBytes(HTTP.ISO_8859_1);
128: InputStream instream = new ByteArrayInputStream(bytes);
129: InputStreamEntity httpentity = new InputStreamEntity(instream,
130: -1);
131: assertTrue(httpentity.isStreaming());
132: httpentity.consumeContent();
133: assertFalse(httpentity.isStreaming());
134: httpentity.consumeContent();
135: assertFalse(httpentity.isStreaming());
136: }
137:
138: }
|