001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/impl/io/TestContentLengthOutputStream.java $
003: * $Revision: 560358 $
004: * $Date: 2007-07-27 21:30:42 +0200 (Fri, 27 Jul 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.io;
032:
033: import java.io.ByteArrayOutputStream;
034: import java.io.IOException;
035: import java.io.OutputStream;
036:
037: import org.apache.http.impl.io.ContentLengthOutputStream;
038: import org.apache.http.mockup.SessionOutputBufferMockup;
039:
040: import junit.framework.Test;
041: import junit.framework.TestCase;
042: import junit.framework.TestSuite;
043:
044: public class TestContentLengthOutputStream extends TestCase {
045:
046: public TestContentLengthOutputStream(String testName) {
047: super (testName);
048: }
049:
050: // ------------------------------------------------------- TestCase Methods
051:
052: public static Test suite() {
053: return new TestSuite(TestContentLengthOutputStream.class);
054: }
055:
056: // ------------------------------------------------------------------- Main
057: public static void main(String args[]) {
058: String[] testCaseName = { TestContentLengthOutputStream.class
059: .getName() };
060: junit.textui.TestRunner.main(testCaseName);
061: }
062:
063: public void testConstructors() throws Exception {
064: new ContentLengthOutputStream(new SessionOutputBufferMockup(),
065: 10L);
066: try {
067: new ContentLengthOutputStream(null, 10L);
068: fail("IllegalArgumentException should have been thrown");
069: } catch (IllegalArgumentException ex) {
070: // expected
071: }
072: try {
073: new ContentLengthOutputStream(
074: new SessionOutputBufferMockup(), -10);
075: fail("IllegalArgumentException should have been thrown");
076: } catch (IllegalArgumentException ex) {
077: // expected
078: }
079: }
080:
081: public void testBasics() throws Exception {
082: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
083: SessionOutputBufferMockup datatransmitter = new SessionOutputBufferMockup(
084: buffer);
085: OutputStream out = new ContentLengthOutputStream(
086: datatransmitter, 15L);
087:
088: byte[] tmp = new byte[10];
089: out.write(tmp, 0, 10);
090: out.write(1);
091: out.write(tmp, 0, 10);
092: out.write(tmp, 0, 10);
093: out.write(tmp);
094: out.write(1);
095: out.write(2);
096: out.flush();
097: out.close();
098: byte[] data = datatransmitter.getData();
099: assertEquals(15, data.length);
100: }
101:
102: public void testClose() throws Exception {
103: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
104: SessionOutputBufferMockup datatransmitter = new SessionOutputBufferMockup(
105: buffer);
106: OutputStream out = new ContentLengthOutputStream(
107: datatransmitter, 15L);
108: out.close();
109: out.close();
110: byte[] tmp = new byte[10];
111: try {
112: out.write(tmp);
113: fail("IOException should have been thrown");
114: } catch (IOException ex) {
115: // expected
116: }
117: try {
118: out.write(1);
119: fail("IOException should have been thrown");
120: } catch (IOException ex) {
121: // expected
122: }
123: }
124:
125: }
|