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/TestIdentityOutputStream.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.IdentityOutputStream;
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 TestIdentityOutputStream extends TestCase {
045:
046: public TestIdentityOutputStream(String testName) {
047: super (testName);
048: }
049:
050: // ------------------------------------------------------- TestCase Methods
051:
052: public static Test suite() {
053: return new TestSuite(TestIdentityOutputStream.class);
054: }
055:
056: // ------------------------------------------------------------------- Main
057: public static void main(String args[]) {
058: String[] testCaseName = { TestIdentityOutputStream.class
059: .getName() };
060: junit.textui.TestRunner.main(testCaseName);
061: }
062:
063: public void testConstructors() throws Exception {
064: new IdentityOutputStream(new SessionOutputBufferMockup());
065: try {
066: new IdentityOutputStream(null);
067: fail("IllegalArgumentException should have been thrown");
068: } catch (IllegalArgumentException ex) {
069: // expected
070: }
071: }
072:
073: public void testBasics() throws Exception {
074: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
075: SessionOutputBufferMockup datatransmitter = new SessionOutputBufferMockup(
076: buffer);
077: OutputStream out = new IdentityOutputStream(datatransmitter);
078:
079: byte[] tmp = new byte[10];
080: out.write(tmp, 0, 10);
081: out.write(tmp);
082: out.write(1);
083: out.flush();
084: out.close();
085: byte[] data = datatransmitter.getData();
086: assertEquals(21, data.length);
087: }
088:
089: public void testClose() throws Exception {
090: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
091: SessionOutputBufferMockup datatransmitter = new SessionOutputBufferMockup(
092: buffer);
093: OutputStream out = new IdentityOutputStream(datatransmitter);
094: out.close();
095: out.close();
096: byte[] tmp = new byte[10];
097: try {
098: out.write(tmp);
099: fail("IOException should have been thrown");
100: } catch (IOException ex) {
101: // expected
102: }
103: try {
104: out.write(1);
105: fail("IOException should have been thrown");
106: } catch (IOException ex) {
107: // expected
108: }
109: }
110:
111: public void testConstructor() throws Exception {
112: SessionOutputBufferMockup transmitter = new SessionOutputBufferMockup();
113: new IdentityOutputStream(transmitter);
114: try {
115: new IdentityOutputStream(null);
116: fail("IllegalArgumentException should have been thrown");
117: } catch (IllegalArgumentException ex) {
118: //expected
119: }
120: }
121:
122: public void testBasicWrite() throws Exception {
123: SessionOutputBufferMockup transmitter = new SessionOutputBufferMockup();
124: IdentityOutputStream outstream = new IdentityOutputStream(
125: transmitter);
126: outstream.write(new byte[] { 'a', 'b' }, 0, 2);
127: outstream.write('c');
128: outstream.flush();
129:
130: byte[] input = transmitter.getData();
131:
132: assertNotNull(input);
133: byte[] expected = new byte[] { 'a', 'b', 'c' };
134: assertEquals(expected.length, input.length);
135: for (int i = 0; i < expected.length; i++) {
136: assertEquals(expected[i], input[i]);
137: }
138: }
139:
140: public void testClosedCondition() throws Exception {
141: SessionOutputBufferMockup transmitter = new SessionOutputBufferMockup();
142: IdentityOutputStream outstream = new IdentityOutputStream(
143: transmitter);
144: outstream.close();
145: outstream.close();
146:
147: try {
148: byte[] tmp = new byte[2];
149: outstream.write(tmp, 0, tmp.length);
150: fail("IOException should have been thrown");
151: } catch (IOException e) {
152: //expected
153: }
154: try {
155: outstream.write('a');
156: fail("IOException should have been thrown");
157: } catch (IOException e) {
158: //expected
159: }
160: }
161:
162: }
|