001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/entity/TestBasicHttpEntity.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 BasicHttpEntity}.
046: *
047: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
048: */
049: public class TestBasicHttpEntity extends TestCase {
050:
051: public TestBasicHttpEntity(String testName) {
052: super (testName);
053: }
054:
055: public static void main(String args[]) {
056: String[] testCaseName = { TestBasicHttpEntity.class.getName() };
057: junit.textui.TestRunner.main(testCaseName);
058: }
059:
060: public static Test suite() {
061: return new TestSuite(TestBasicHttpEntity.class);
062: }
063:
064: public void testBasics() throws Exception {
065:
066: byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
067: InputStream content = new ByteArrayInputStream(bytes);
068: BasicHttpEntity httpentity = new BasicHttpEntity();
069: httpentity.setContent(content);
070: httpentity.setContentLength(bytes.length);
071:
072: assertEquals(bytes.length, httpentity.getContentLength());
073: assertFalse(httpentity.isRepeatable());
074: assertTrue(httpentity.isStreaming());
075: }
076:
077: public void testContent() throws Exception {
078: byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
079: InputStream content = new ByteArrayInputStream(bytes);
080: BasicHttpEntity httpentity = new BasicHttpEntity();
081: try {
082: httpentity.getContent();
083: fail("IllegalStateException should have been thrown");
084: } catch (IllegalStateException ex) {
085: // expected
086: }
087:
088: httpentity.setContent(null);
089: try {
090: httpentity.getContent();
091: fail("IllegalStateException should have been thrown");
092: } catch (IllegalStateException ex) {
093: // expected
094: }
095:
096: httpentity.setContent(content);
097: httpentity.getContent();
098: try {
099: httpentity.getContent();
100: fail("IllegalStateException should have been thrown");
101: } catch (IllegalStateException ex) {
102: // expected
103: }
104:
105: }
106:
107: public void testWriteTo() throws Exception {
108: byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
109: InputStream content = new ByteArrayInputStream(bytes);
110: BasicHttpEntity httpentity = new BasicHttpEntity();
111: httpentity.setContent(content);
112:
113: ByteArrayOutputStream out = new ByteArrayOutputStream();
114: httpentity.writeTo(out);
115: byte[] bytes2 = out.toByteArray();
116: assertNotNull(bytes2);
117: assertEquals(bytes.length, bytes2.length);
118: for (int i = 0; i < bytes.length; i++) {
119: assertEquals(bytes[i], bytes2[i]);
120: }
121: out = new ByteArrayOutputStream();
122: try {
123: httpentity.writeTo(out);
124: fail("IllegalStateException should have been thrown");
125: } catch (IllegalStateException ex) {
126: // expected
127: }
128:
129: httpentity.setContent(null);
130: out = new ByteArrayOutputStream();
131: try {
132: httpentity.writeTo(out);
133: fail("IllegalStateException should have been thrown");
134: } catch (IllegalStateException ex) {
135: // expected
136: }
137:
138: try {
139: httpentity.writeTo(null);
140: fail("IllegalArgumentException should have been thrown");
141: } catch (IllegalArgumentException ex) {
142: // expected
143: }
144: }
145:
146: public void testConsumeContent() throws Exception {
147: byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
148: InputStream content = new ByteArrayInputStream(bytes);
149: BasicHttpEntity httpentity = new BasicHttpEntity();
150: httpentity.setContent(null);
151: httpentity.consumeContent();
152: httpentity.setContent(content);
153: httpentity.consumeContent();
154: }
155:
156: }
|