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/TestContentLengthInputStream.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.InputStream;
036:
037: import org.apache.http.impl.io.ContentLengthInputStream;
038: import org.apache.http.mockup.SessionInputBufferMockup;
039: import org.apache.http.util.EncodingUtils;
040:
041: import junit.framework.Test;
042: import junit.framework.TestCase;
043: import junit.framework.TestSuite;
044:
045: public class TestContentLengthInputStream extends TestCase {
046:
047: public TestContentLengthInputStream(String testName) {
048: super (testName);
049: }
050:
051: // ------------------------------------------------------- TestCase Methods
052:
053: public static Test suite() {
054: return new TestSuite(TestContentLengthInputStream.class);
055: }
056:
057: // ------------------------------------------------------------------- Main
058: public static void main(String args[]) {
059: String[] testCaseName = { TestContentLengthInputStream.class
060: .getName() };
061: junit.textui.TestRunner.main(testCaseName);
062: }
063:
064: private static final String CONTENT_CHARSET = "ISO-8859-1";
065:
066: public void testConstructors() throws Exception {
067: new ContentLengthInputStream(new SessionInputBufferMockup(
068: new byte[] {}), 10);
069: try {
070: new ContentLengthInputStream(null, 10);
071: fail("IllegalArgumentException should have been thrown");
072: } catch (IllegalArgumentException ex) {
073: // expected
074: }
075: try {
076: new ContentLengthInputStream(new SessionInputBufferMockup(
077: new byte[] {}), -10);
078: fail("IllegalArgumentException should have been thrown");
079: } catch (IllegalArgumentException ex) {
080: // expected
081: }
082: }
083:
084: public void testBasics() throws IOException {
085: String correct = "1234567890123456";
086: InputStream in = new ContentLengthInputStream(
087: new SessionInputBufferMockup(EncodingUtils.getBytes(
088: correct, CONTENT_CHARSET)), 10L);
089: ByteArrayOutputStream out = new ByteArrayOutputStream();
090:
091: byte[] buffer = new byte[50];
092: int len = in.read(buffer, 0, 2);
093: out.write(buffer, 0, len);
094: len = in.read(buffer);
095: out.write(buffer, 0, len);
096:
097: String result = EncodingUtils.getString(out.toByteArray(),
098: CONTENT_CHARSET);
099: assertEquals(result, "1234567890");
100: }
101:
102: public void testSkip() throws IOException {
103: InputStream in = new ContentLengthInputStream(
104: new SessionInputBufferMockup(new byte[20]), 10L);
105: assertEquals(10, in.skip(10));
106: assertTrue(in.read() == -1);
107:
108: in = new ContentLengthInputStream(new SessionInputBufferMockup(
109: new byte[20]), 10L);
110: in.read();
111: assertEquals(9, in.skip(10));
112: assertTrue(in.read() == -1);
113:
114: in = new ContentLengthInputStream(new SessionInputBufferMockup(
115: new byte[20]), 2L);
116: in.read();
117: in.read();
118: assertTrue(in.skip(10) <= 0);
119: assertTrue(in.skip(-1) == 0);
120: assertTrue(in.read() == -1);
121:
122: in = new ContentLengthInputStream(new SessionInputBufferMockup(
123: new byte[2]), 4L);
124: in.read();
125: assertTrue(in.skip(2) == 1);
126: }
127:
128: public void testClose() throws IOException {
129: String correct = "1234567890123456";
130: InputStream in = new ContentLengthInputStream(
131: new SessionInputBufferMockup(EncodingUtils.getBytes(
132: correct, CONTENT_CHARSET)), 10L);
133: in.close();
134: in.close();
135: try {
136: in.read();
137: fail("IOException should have been thrown");
138: } catch (IOException ex) {
139: // expected
140: }
141: byte[] tmp = new byte[10];
142: try {
143: in.read(tmp);
144: fail("IOException should have been thrown");
145: } catch (IOException ex) {
146: // expected
147: }
148: try {
149: in.read(tmp, 0, tmp.length);
150: fail("IOException should have been thrown");
151: } catch (IOException ex) {
152: // expected
153: }
154: }
155:
156: }
|