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/TestIdentitynputStream.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 org.apache.http.impl.io.IdentityInputStream;
034: import org.apache.http.io.SessionInputBuffer;
035: import org.apache.http.mockup.SessionInputBufferMockup;
036:
037: import junit.framework.Test;
038: import junit.framework.TestCase;
039: import junit.framework.TestSuite;
040:
041: /**
042: * Simple tests for {@link IdentityInputStream}.
043: *
044: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
045: */
046: public class TestIdentitynputStream extends TestCase {
047:
048: // ------------------------------------------------------------ Constructor
049: public TestIdentitynputStream(String testName) {
050: super (testName);
051: }
052:
053: // ------------------------------------------------------------------- Main
054: public static void main(String args[]) {
055: String[] testCaseName = { TestIdentitynputStream.class
056: .getName() };
057: junit.textui.TestRunner.main(testCaseName);
058: }
059:
060: // ------------------------------------------------------- TestCase Methods
061:
062: public static Test suite() {
063: return new TestSuite(TestIdentitynputStream.class);
064: }
065:
066: public void testConstructor() throws Exception {
067: SessionInputBuffer receiver = new SessionInputBufferMockup(
068: new byte[] {});
069: new IdentityInputStream(receiver);
070: try {
071: new IdentityInputStream(null);
072: fail("IllegalArgumentException should have been thrown");
073: } catch (IllegalArgumentException ex) {
074: //expected
075: }
076: }
077:
078: public void testBasicRead() throws Exception {
079: byte[] input = new byte[] { 'a', 'b', 'c' };
080: SessionInputBufferMockup receiver = new SessionInputBufferMockup(
081: input);
082: IdentityInputStream instream = new IdentityInputStream(receiver);
083: byte[] tmp = new byte[2];
084: assertEquals(2, instream.read(tmp, 0, tmp.length));
085: assertEquals('a', tmp[0]);
086: assertEquals('b', tmp[1]);
087: assertEquals('c', instream.read());
088: assertEquals(-1, instream.read(tmp, 0, tmp.length));
089: assertEquals(-1, instream.read());
090: assertEquals(-1, instream.read(tmp, 0, tmp.length));
091: assertEquals(-1, instream.read());
092: }
093:
094: public void testClosedCondition() throws Exception {
095: byte[] input = new byte[] { 'a', 'b', 'c' };
096: SessionInputBufferMockup receiver = new SessionInputBufferMockup(
097: input);
098: IdentityInputStream instream = new IdentityInputStream(receiver);
099:
100: instream.close();
101: instream.close();
102:
103: assertTrue(instream.available() == 0);
104: byte[] tmp = new byte[2];
105: assertEquals(-1, instream.read(tmp, 0, tmp.length));
106: assertEquals(-1, instream.read());
107: assertEquals(-1, instream.read(tmp, 0, tmp.length));
108: assertEquals(-1, instream.read());
109: }
110:
111: public void testAvailable() throws Exception {
112: byte[] input = new byte[] { 'a', 'b', 'c' };
113: SessionInputBufferMockup receiver = new SessionInputBufferMockup(
114: input);
115: IdentityInputStream instream = new IdentityInputStream(receiver);
116: assertTrue(instream.available() > 0);
117: }
118:
119: }
|