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