Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/io/TestContentLengthOutputStream.java


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