001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/impl/entity/TestEntitySerializer.java $
003: * $Revision: 576073 $
004: * $Date: 2007-09-16 12:53:13 +0200 (Sun, 16 Sep 2007) $
005: * ====================================================================
006: * Licensed to the Apache Software Foundation (ASF) under one
007: * or more contributor license agreements. See the NOTICE file
008: * distributed with this work for additional information
009: * regarding copyright ownership. The ASF licenses this file
010: * to you under the Apache License, Version 2.0 (the
011: * "License"); you may not use this file except in compliance
012: * with the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing,
017: * software distributed under the License is distributed on an
018: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
019: * KIND, either express or implied. See the License for the
020: * specific language governing permissions and limitations
021: * under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.http.impl.entity;
032:
033: import java.io.OutputStream;
034:
035: import junit.framework.Test;
036: import junit.framework.TestCase;
037: import junit.framework.TestSuite;
038:
039: import org.apache.http.HttpMessage;
040: import org.apache.http.HttpVersion;
041: import org.apache.http.ProtocolException;
042: import org.apache.http.entity.ByteArrayEntity;
043: import org.apache.http.impl.io.ChunkedOutputStream;
044: import org.apache.http.impl.io.ContentLengthOutputStream;
045: import org.apache.http.impl.io.IdentityOutputStream;
046: import org.apache.http.io.SessionOutputBuffer;
047: import org.apache.http.mockup.SessionOutputBufferMockup;
048: import org.apache.http.mockup.HttpMessageMockup;
049: import org.apache.http.params.CoreProtocolPNames;
050:
051: public class TestEntitySerializer extends TestCase {
052:
053: public TestEntitySerializer(String testName) {
054: super (testName);
055: }
056:
057: // ------------------------------------------------------- TestCase Methods
058:
059: public static Test suite() {
060: return new TestSuite(TestEntitySerializer.class);
061: }
062:
063: // ------------------------------------------------------------------- Main
064: public static void main(String args[]) {
065: String[] testCaseName = { TestEntitySerializer.class.getName() };
066: junit.textui.TestRunner.main(testCaseName);
067: }
068:
069: public void testIllegalGenerateArg() throws Exception {
070: EntitySerializer entitywriter = new EntitySerializer(
071: new StrictContentLengthStrategy());
072: try {
073: entitywriter.serialize(null, null, null);
074: fail("IllegalArgumentException should have been thrown");
075: } catch (IllegalArgumentException ex) {
076: // expected
077: }
078: try {
079: entitywriter.serialize(new SessionOutputBufferMockup(),
080: null, null);
081: fail("IllegalArgumentException should have been thrown");
082: } catch (IllegalArgumentException ex) {
083: // expected
084: }
085: try {
086: entitywriter.serialize(new SessionOutputBufferMockup(),
087: new HttpMessageMockup(), null);
088: fail("IllegalArgumentException should have been thrown");
089: } catch (IllegalArgumentException ex) {
090: // expected
091: }
092: }
093:
094: public void testEntityWithChunkTransferEncoding() throws Exception {
095: SessionOutputBuffer datatransmitter = new SessionOutputBufferMockup();
096: HttpMessage message = new HttpMessageMockup();
097: message.addHeader("Transfer-Encoding", "Chunked");
098:
099: EntitySerializer entitywriter = new EntitySerializer(
100: new StrictContentLengthStrategy());
101: OutputStream outstream = entitywriter.doSerialize(
102: datatransmitter, message);
103: assertNotNull(outstream);
104: assertTrue(outstream instanceof ChunkedOutputStream);
105: }
106:
107: public void testEntityWithIdentityTransferEncoding()
108: throws Exception {
109: SessionOutputBuffer datatransmitter = new SessionOutputBufferMockup();
110: HttpMessage message = new HttpMessageMockup();
111: message.addHeader("Transfer-Encoding", "Identity");
112:
113: EntitySerializer entitywriter = new EntitySerializer(
114: new StrictContentLengthStrategy());
115: OutputStream outstream = entitywriter.doSerialize(
116: datatransmitter, message);
117: assertNotNull(outstream);
118: assertTrue(outstream instanceof IdentityOutputStream);
119: }
120:
121: public void testEntityWithInvalidTransferEncoding()
122: throws Exception {
123: SessionOutputBuffer datatransmitter = new SessionOutputBufferMockup();
124: HttpMessage message = new HttpMessageMockup();
125: message.addHeader("Transfer-Encoding", "whatever");
126:
127: EntitySerializer entitywriter = new EntitySerializer(
128: new StrictContentLengthStrategy());
129: try {
130: entitywriter.doSerialize(datatransmitter, message);
131: fail("ProtocolException should have been thrown");
132: } catch (ProtocolException ex) {
133: // expected
134: }
135: }
136:
137: public void testEntityWithInvalidChunkEncodingAndHTTP10()
138: throws Exception {
139: SessionOutputBuffer datatransmitter = new SessionOutputBufferMockup();
140: HttpMessage message = new HttpMessageMockup();
141: message.getParams().setParameter(
142: CoreProtocolPNames.PROTOCOL_VERSION,
143: HttpVersion.HTTP_1_0);
144: message.addHeader("Transfer-Encoding", "chunked");
145:
146: EntitySerializer entitywriter = new EntitySerializer(
147: new StrictContentLengthStrategy());
148: try {
149: entitywriter.doSerialize(datatransmitter, message);
150: fail("ProtocolException should have been thrown");
151: } catch (ProtocolException ex) {
152: // expected
153: }
154: }
155:
156: public void testEntityWithContentLength() throws Exception {
157: SessionOutputBuffer datatransmitter = new SessionOutputBufferMockup();
158: HttpMessage message = new HttpMessageMockup();
159: message.addHeader("Content-Length", "100");
160: EntitySerializer entitywriter = new EntitySerializer(
161: new StrictContentLengthStrategy());
162: OutputStream outstream = entitywriter.doSerialize(
163: datatransmitter, message);
164: assertNotNull(outstream);
165: assertTrue(outstream instanceof ContentLengthOutputStream);
166: }
167:
168: public void testEntityWithInvalidContentLength() throws Exception {
169: SessionOutputBuffer datatransmitter = new SessionOutputBufferMockup();
170: HttpMessage message = new HttpMessageMockup();
171: message.addHeader("Content-Length", "whatever");
172:
173: EntitySerializer entitywriter = new EntitySerializer(
174: new StrictContentLengthStrategy());
175: try {
176: entitywriter.doSerialize(datatransmitter, message);
177: fail("ProtocolException should have been thrown");
178: } catch (ProtocolException ex) {
179: // expected
180: }
181: }
182:
183: public void testEntityNoContentDelimiter() throws Exception {
184: SessionOutputBuffer datatransmitter = new SessionOutputBufferMockup();
185: HttpMessage message = new HttpMessageMockup();
186: EntitySerializer entitywriter = new EntitySerializer(
187: new StrictContentLengthStrategy());
188: OutputStream outstream = entitywriter.doSerialize(
189: datatransmitter, message);
190: assertNotNull(outstream);
191: assertTrue(outstream instanceof IdentityOutputStream);
192: }
193:
194: public void testEntitySerialization() throws Exception {
195: byte[] content = new byte[] { 1, 2, 3, 4, 5 };
196: ByteArrayEntity entity = new ByteArrayEntity(content);
197:
198: SessionOutputBufferMockup datatransmitter = new SessionOutputBufferMockup();
199: HttpMessage message = new HttpMessageMockup();
200: message.addHeader("Content-Length", Integer
201: .toString(content.length));
202:
203: EntitySerializer entitywriter = new EntitySerializer(
204: new StrictContentLengthStrategy());
205: entitywriter.serialize(datatransmitter, message, entity);
206:
207: byte[] data = datatransmitter.getData();
208: assertNotNull(data);
209: assertEquals(content.length, data.length);
210: }
211: }
|