001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/entity/TestHttpEntityWrapper.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.ByteArrayOutputStream;
035:
036: import junit.framework.Test;
037: import junit.framework.TestCase;
038: import junit.framework.TestSuite;
039:
040: import org.apache.http.protocol.HTTP;
041:
042: /**
043: * Unit tests for {@link WrappedEntity}.
044: *
045: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
046: */
047: public class TestHttpEntityWrapper extends TestCase {
048:
049: public TestHttpEntityWrapper(String testName) {
050: super (testName);
051: }
052:
053: public static void main(String args[]) {
054: String[] testCaseName = { TestHttpEntityWrapper.class.getName() };
055: junit.textui.TestRunner.main(testCaseName);
056: }
057:
058: public static Test suite() {
059: return new TestSuite(TestHttpEntityWrapper.class);
060: }
061:
062: public void testBasics() throws Exception {
063: String s = "Message content";
064: StringEntity httpentity = new StringEntity(s, HTTP.ISO_8859_1);
065: httpentity.setContentType(HTTP.PLAIN_TEXT_TYPE);
066: httpentity.setContentEncoding(HTTP.IDENTITY_CODING);
067: HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);
068:
069: assertEquals(httpentity.getContentLength(), wrapped
070: .getContentLength());
071: assertEquals(httpentity.getContentType(), wrapped
072: .getContentType());
073: assertEquals(httpentity.getContentEncoding(), wrapped
074: .getContentEncoding());
075: assertEquals(httpentity.isChunked(), wrapped.isChunked());
076: assertEquals(httpentity.isRepeatable(), wrapped.isRepeatable());
077: assertEquals(httpentity.isStreaming(), wrapped.isStreaming());
078: assertNotNull(wrapped.getContent());
079: }
080:
081: public void testIllegalConstructor() throws Exception {
082: try {
083: new HttpEntityWrapper(null);
084: fail("IllegalArgumentException should have been thrown");
085: } catch (IllegalArgumentException ex) {
086: // expected
087: }
088: }
089:
090: public void testWriteTo() throws Exception {
091: String s = "Message content";
092: byte[] bytes = s.getBytes(HTTP.ISO_8859_1);
093: StringEntity httpentity = new StringEntity(s);
094: HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);
095:
096: ByteArrayOutputStream out = new ByteArrayOutputStream();
097: wrapped.writeTo(out);
098: byte[] bytes2 = out.toByteArray();
099: assertNotNull(bytes2);
100: assertEquals(bytes.length, bytes2.length);
101: for (int i = 0; i < bytes.length; i++) {
102: assertEquals(bytes[i], bytes2[i]);
103: }
104:
105: out = new ByteArrayOutputStream();
106: wrapped.writeTo(out);
107: bytes2 = out.toByteArray();
108: assertNotNull(bytes2);
109: assertEquals(bytes.length, bytes2.length);
110: for (int i = 0; i < bytes.length; i++) {
111: assertEquals(bytes[i], bytes2[i]);
112: }
113:
114: try {
115: wrapped.writeTo(null);
116: fail("IllegalArgumentException should have been thrown");
117: } catch (IllegalArgumentException ex) {
118: // expected
119: }
120: }
121:
122: public void testConsumeContent() throws Exception {
123: String s = "Message content";
124: StringEntity httpentity = new StringEntity(s);
125: HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);
126: wrapped.consumeContent();
127: wrapped.consumeContent();
128: }
129:
130: }
|