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 TestIdentityOutputStream {
39  
40      @Test
41      public void testConstructors() throws Exception {
42          new IdentityOutputStream(new SessionOutputBufferMock()).close();
43          try {
44              new IdentityOutputStream(null);
45              Assert.fail("IllegalArgumentException should have been thrown");
46          } catch (final IllegalArgumentException ex) {
47              // expected
48          }
49      }
50  
51      @Test
52      public void testBasics() throws Exception {
53          final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
54          final SessionOutputBufferMockessionOutputBufferMock">SessionOutputBufferMock datatransmitter = new SessionOutputBufferMock(buffer);
55          final OutputStream out = new IdentityOutputStream(datatransmitter);
56  
57          final byte[] tmp = new byte[10];
58          out.write(tmp, 0, 10);
59          out.write(tmp);
60          out.write(1);
61          out.flush();
62          out.close();
63          final byte[] data = datatransmitter.getData();
64          Assert.assertEquals(21, data.length);
65      }
66  
67      @Test
68      public void testClose() throws Exception {
69          final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
70          final SessionOutputBufferMockessionOutputBufferMock">SessionOutputBufferMock datatransmitter = new SessionOutputBufferMock(buffer);
71          final OutputStream out = new IdentityOutputStream(datatransmitter);
72          out.close();
73          out.close();
74          final byte[] tmp = new byte[10];
75          try {
76              out.write(tmp);
77              Assert.fail("IOException should have been thrown");
78          } catch (final IOException ex) {
79              // expected
80          }
81          try {
82              out.write(1);
83              Assert.fail("IOException should have been thrown");
84          } catch (final IOException ex) {
85              // expected
86          }
87      }
88  
89      @Test
90      public void testConstructor() throws Exception {
91          final SessionOutputBufferMockml#SessionOutputBufferMock">SessionOutputBufferMock transmitter = new SessionOutputBufferMock();
92          new IdentityOutputStream(transmitter).close();
93          try {
94              new IdentityOutputStream(null);
95              Assert.fail("IllegalArgumentException should have been thrown");
96          } catch (final IllegalArgumentException ex) {
97              //expected
98          }
99      }
100 
101     @Test
102     public void testBasicWrite() throws Exception {
103         final SessionOutputBufferMockml#SessionOutputBufferMock">SessionOutputBufferMock transmitter = new SessionOutputBufferMock();
104         final IdentityOutputStream outStream = new IdentityOutputStream(transmitter);
105         outStream.write(new byte[] {'a', 'b'}, 0, 2);
106         outStream.write('c');
107         outStream.flush();
108 
109         final byte[] input = transmitter.getData();
110 
111         Assert.assertNotNull(input);
112         final byte[] expected = new byte[] {'a', 'b', 'c'};
113         Assert.assertEquals(expected.length, input.length);
114         for (int i = 0; i < expected.length; i++) {
115             Assert.assertEquals(expected[i], input[i]);
116         }
117         outStream.close();
118     }
119 
120     @Test
121     public void testClosedCondition() throws Exception {
122         final SessionOutputBufferMockml#SessionOutputBufferMock">SessionOutputBufferMock transmitter = new SessionOutputBufferMock();
123         final IdentityOutputStream outStream = new IdentityOutputStream(transmitter);
124         outStream.close();
125         outStream.close();
126 
127         try {
128             final byte[] tmp = new byte[2];
129             outStream.write(tmp, 0, tmp.length);
130             Assert.fail("IOException should have been thrown");
131         } catch (final IOException e) {
132             //expected
133         }
134         try {
135             outStream.write('a');
136             Assert.fail("IOException should have been thrown");
137         } catch (final IOException e) {
138             //expected
139         }
140     }
141 
142 
143 }
144