View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.io;
29  
30  import java.io.ByteArrayOutputStream;
31  import java.io.IOException;
32  import java.io.OutputStream;
33  
34  import org.apache.http.impl.SessionOutputBufferMock;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  public class TestContentLengthOutputStream {
39  
40      @Test
41      public void testConstructors() throws Exception {
42          final ContentLengthOutputStream in = new ContentLengthOutputStream(
43                  new SessionOutputBufferMock(), 10L);
44          in.close();
45          try {
46              new ContentLengthOutputStream(null, 10L);
47              Assert.fail("IllegalArgumentException should have been thrown");
48          } catch (final IllegalArgumentException ex) {
49              // expected
50          }
51          try {
52              new ContentLengthOutputStream(new SessionOutputBufferMock(), -10);
53              Assert.fail("IllegalArgumentException should have been thrown");
54          } catch (final IllegalArgumentException ex) {
55              // expected
56          }
57      }
58  
59      @Test
60      public void testBasics() throws Exception {
61          final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
62          final SessionOutputBufferMock datatransmitter = new SessionOutputBufferMock(buffer);
63          final OutputStream out = new ContentLengthOutputStream(datatransmitter, 15L);
64  
65          final byte[] tmp = new byte[10];
66          out.write(tmp, 0, 10);
67          out.write(1);
68          out.write(tmp, 0, 10);
69          out.write(tmp, 0, 10);
70          out.write(tmp);
71          out.write(1);
72          out.write(2);
73          out.flush();
74          out.close();
75          final byte[] data = datatransmitter.getData();
76          Assert.assertEquals(15, data.length);
77      }
78  
79      @Test
80      public void testClose() throws Exception {
81          final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
82          final SessionOutputBufferMock datatransmitter = new SessionOutputBufferMock(buffer);
83          final OutputStream out = new ContentLengthOutputStream(datatransmitter, 15L);
84          out.close();
85          out.close();
86          final byte[] tmp = new byte[10];
87          try {
88              out.write(tmp);
89              Assert.fail("IOException should have been thrown");
90          } catch (final IOException ex) {
91              // expected
92          }
93          try {
94              out.write(1);
95              Assert.fail("IOException should have been thrown");
96          } catch (final IOException ex) {
97              // expected
98          }
99      }
100 
101 }
102