001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/entity/TestBufferedHttpEntity.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:
037: import junit.framework.Test;
038: import junit.framework.TestCase;
039: import junit.framework.TestSuite;
040:
041: import org.apache.http.protocol.HTTP;
042:
043: /**
044: * Unit tests for {@link WrappedEntity}.
045: *
046: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
047: */
048: public class TestBufferedHttpEntity extends TestCase {
049:
050: public TestBufferedHttpEntity(String testName) {
051: super (testName);
052: }
053:
054: public static void main(String args[]) {
055: String[] testCaseName = { TestBufferedHttpEntity.class
056: .getName() };
057: junit.textui.TestRunner.main(testCaseName);
058: }
059:
060: public static Test suite() {
061: return new TestSuite(TestBufferedHttpEntity.class);
062: }
063:
064: public void testBufferingEntity() throws Exception {
065: byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
066: InputStreamEntity httpentity = new InputStreamEntity(
067: new ByteArrayInputStream(bytes), -1);
068: BufferedHttpEntity bufentity = new BufferedHttpEntity(
069: httpentity);
070: assertEquals(bytes.length, bufentity.getContentLength());
071: assertTrue(bufentity.isRepeatable());
072: assertFalse(bufentity.isChunked());
073: assertFalse(bufentity.isStreaming());
074:
075: // test if we can obtain contain multiple times
076: assertNotNull(bufentity.getContent());
077: assertNotNull(bufentity.getContent());
078: }
079:
080: public void testWrappingEntity() throws Exception {
081: byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
082: ByteArrayEntity httpentity = new ByteArrayEntity(bytes);
083: httpentity.setChunked(true);
084: BufferedHttpEntity bufentity = new BufferedHttpEntity(
085: httpentity);
086: assertEquals(bytes.length, bufentity.getContentLength());
087: assertTrue(bufentity.isRepeatable());
088: assertTrue(bufentity.isChunked());
089: assertFalse(bufentity.isStreaming());
090:
091: // test if we can obtain contain multiple times
092: assertNotNull(bufentity.getContent());
093: assertNotNull(bufentity.getContent());
094: }
095:
096: public void testIllegalConstructor() throws Exception {
097: try {
098: new BufferedHttpEntity(null);
099: fail("IllegalArgumentException should have been thrown");
100: } catch (IllegalArgumentException ex) {
101: // expected
102: }
103: }
104:
105: public void testWriteToBuffered() throws Exception {
106: byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
107: InputStreamEntity httpentity = new InputStreamEntity(
108: new ByteArrayInputStream(bytes), -1);
109: BufferedHttpEntity bufentity = new BufferedHttpEntity(
110: httpentity);
111:
112: ByteArrayOutputStream out = new ByteArrayOutputStream();
113: bufentity.writeTo(out);
114: byte[] bytes2 = out.toByteArray();
115: assertNotNull(bytes2);
116: assertEquals(bytes.length, bytes2.length);
117: for (int i = 0; i < bytes.length; i++) {
118: assertEquals(bytes[i], bytes2[i]);
119: }
120:
121: out = new ByteArrayOutputStream();
122: bufentity.writeTo(out);
123: bytes2 = out.toByteArray();
124: assertNotNull(bytes2);
125: assertEquals(bytes.length, bytes2.length);
126: for (int i = 0; i < bytes.length; i++) {
127: assertEquals(bytes[i], bytes2[i]);
128: }
129:
130: try {
131: bufentity.writeTo(null);
132: fail("IllegalArgumentException should have been thrown");
133: } catch (IllegalArgumentException ex) {
134: // expected
135: }
136: }
137:
138: public void testWriteToWrapped() throws Exception {
139: byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
140: ByteArrayEntity httpentity = new ByteArrayEntity(bytes);
141: BufferedHttpEntity bufentity = new BufferedHttpEntity(
142: httpentity);
143:
144: ByteArrayOutputStream out = new ByteArrayOutputStream();
145: bufentity.writeTo(out);
146: byte[] bytes2 = out.toByteArray();
147: assertNotNull(bytes2);
148: assertEquals(bytes.length, bytes2.length);
149: for (int i = 0; i < bytes.length; i++) {
150: assertEquals(bytes[i], bytes2[i]);
151: }
152:
153: out = new ByteArrayOutputStream();
154: bufentity.writeTo(out);
155: bytes2 = out.toByteArray();
156: assertNotNull(bytes2);
157: assertEquals(bytes.length, bytes2.length);
158: for (int i = 0; i < bytes.length; i++) {
159: assertEquals(bytes[i], bytes2[i]);
160: }
161:
162: try {
163: bufentity.writeTo(null);
164: fail("IllegalArgumentException should have been thrown");
165: } catch (IllegalArgumentException ex) {
166: // expected
167: }
168: }
169:
170: }
|