~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore/src/test/java/org/apache/http/impl/io/TestContentLengthOutputStream.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/test/java/org/apache/http/impl/io/TestContentLengthOutputStream.java $
 
3
 * $Revision: 618017 $
 
4
 * $Date: 2008-02-03 17:42:22 +0100 (Sun, 03 Feb 2008) $
 
5
 * ====================================================================
 
6
 * Licensed to the Apache Software Foundation (ASF) under one
 
7
 * or more contributor license agreements.  See the NOTICE file
 
8
 * distributed with this work for additional information
 
9
 * regarding copyright ownership.  The ASF licenses this file
 
10
 * to you under the Apache License, Version 2.0 (the
 
11
 * "License"); you may not use this file except in compliance
 
12
 * with the License.  You may obtain a copy of the License at
 
13
 *
 
14
 *   http://www.apache.org/licenses/LICENSE-2.0
 
15
 *
 
16
 * Unless required by applicable law or agreed to in writing,
 
17
 * software distributed under the License is distributed on an
 
18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
19
 * KIND, either express or implied.  See the License for the
 
20
 * specific language governing permissions and limitations
 
21
 * under the License.
 
22
 * ====================================================================
 
23
 *
 
24
 * This software consists of voluntary contributions made by many
 
25
 * individuals on behalf of the Apache Software Foundation.  For more
 
26
 * information on the Apache Software Foundation, please see
 
27
 * <http://www.apache.org/>.
 
28
 *
 
29
 */
 
30
 
 
31
package org.apache.http.impl.io;
 
32
 
 
33
import java.io.ByteArrayOutputStream;
 
34
import java.io.IOException;
 
35
import java.io.OutputStream;
 
36
 
 
37
import org.apache.http.impl.io.ContentLengthOutputStream;
 
38
import org.apache.http.mockup.SessionOutputBufferMockup;
 
39
 
 
40
import junit.framework.Test;
 
41
import junit.framework.TestCase;
 
42
import junit.framework.TestSuite;
 
43
 
 
44
public class TestContentLengthOutputStream extends TestCase {
 
45
 
 
46
    public TestContentLengthOutputStream(String testName) {
 
47
        super(testName);
 
48
    }
 
49
 
 
50
    // ------------------------------------------------------- TestCase Methods
 
51
 
 
52
    public static Test suite() {
 
53
        return new TestSuite(TestContentLengthOutputStream.class);
 
54
    }
 
55
 
 
56
    // ------------------------------------------------------------------- Main
 
57
    public static void main(String args[]) {
 
58
        String[] testCaseName = { TestContentLengthOutputStream.class.getName() };
 
59
        junit.textui.TestRunner.main(testCaseName);
 
60
    }
 
61
 
 
62
    public void testConstructors() throws Exception {
 
63
        new ContentLengthOutputStream(new SessionOutputBufferMockup(), 10L);
 
64
        try {
 
65
            new ContentLengthOutputStream(null, 10L);
 
66
            fail("IllegalArgumentException should have been thrown");
 
67
        } catch (IllegalArgumentException ex) {
 
68
            // expected
 
69
        }
 
70
        try {
 
71
            new ContentLengthOutputStream(new SessionOutputBufferMockup(), -10);
 
72
            fail("IllegalArgumentException should have been thrown");
 
73
        } catch (IllegalArgumentException ex) {
 
74
            // expected
 
75
        }
 
76
    }
 
77
 
 
78
    public void testBasics() throws Exception {
 
79
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 
80
        SessionOutputBufferMockup datatransmitter = new SessionOutputBufferMockup(buffer);
 
81
        OutputStream out = new ContentLengthOutputStream(datatransmitter, 15L);
 
82
 
 
83
        byte[] tmp = new byte[10];
 
84
        out.write(tmp, 0, 10);
 
85
        out.write(1);
 
86
        out.write(tmp, 0, 10);
 
87
        out.write(tmp, 0, 10);
 
88
        out.write(tmp);
 
89
        out.write(1);
 
90
        out.write(2);
 
91
        out.flush();
 
92
        out.close();
 
93
        byte[] data = datatransmitter.getData();
 
94
        assertEquals(15, data.length);
 
95
    }
 
96
 
 
97
    public void testClose() throws Exception {
 
98
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 
99
        SessionOutputBufferMockup datatransmitter = new SessionOutputBufferMockup(buffer);
 
100
        OutputStream out = new ContentLengthOutputStream(datatransmitter, 15L);
 
101
        out.close();
 
102
        out.close();
 
103
        byte[] tmp = new byte[10];
 
104
        try {
 
105
            out.write(tmp);
 
106
            fail("IOException should have been thrown");
 
107
        } catch (IOException ex) {
 
108
            // expected
 
109
        }
 
110
        try {
 
111
            out.write(1);
 
112
            fail("IOException should have been thrown");
 
113
        } catch (IOException ex) {
 
114
            // expected
 
115
        }
 
116
    }
 
117
    
 
118
}
 
119