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

Quick Search    Search Deep

Source code: org/apache/http/entity/TestFileEntity.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/test/org/apache/http/entity/TestFileEntity.java $
3    * $Revision: 385310 $
4    * $Date: 2006-03-12 17:28:43 +0100 (Sun, 12 Mar 2006) $
5    * 
6    * ====================================================================
7    *
8    *  Copyright 1999-2006 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  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, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.http.entity;
31  
32  import java.io.ByteArrayOutputStream;
33  import java.io.File;
34  import java.io.FileOutputStream;
35  
36  import junit.framework.Test;
37  import junit.framework.TestCase;
38  import junit.framework.TestSuite;
39  
40  import org.apache.http.protocol.HTTP;
41  
42  /**
43   * Unit tests for {@link FileEntity}.
44   *
45   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
46   */
47  public class TestFileEntity extends TestCase {
48  
49      public TestFileEntity(String testName) {
50          super(testName);
51      }
52  
53      public static void main(String args[]) {
54          String[] testCaseName = { TestFileEntity.class.getName() };
55          junit.textui.TestRunner.main(testCaseName);
56      }
57  
58      public static Test suite() {
59          return new TestSuite(TestFileEntity.class);
60      }
61  
62      public void testBasics() throws Exception {
63          File tmpfile = File.createTempFile("testfile", ".txt");
64          tmpfile.deleteOnExit();
65          FileEntity httpentity = new FileEntity(tmpfile, HTTP.ISO_8859_1);
66          
67          assertEquals(tmpfile.length(), httpentity.getContentLength());
68          assertNotNull(httpentity.getContent());
69          assertTrue(httpentity.isRepeatable());
70          assertFalse(httpentity.isStreaming());        
71          tmpfile.delete();
72      }
73  
74      public void testIllegalConstructor() throws Exception {
75          try {
76              new FileEntity(null, null);
77              fail("IllegalArgumentException should have been thrown");
78          } catch (IllegalArgumentException ex) {
79              // expected
80          }
81      }
82  
83      public void testWriteTo() throws Exception {
84          File tmpfile = File.createTempFile("testfile", ".txt");
85          tmpfile.deleteOnExit();
86          
87          FileOutputStream outstream = new FileOutputStream(tmpfile);
88          outstream.write(0);
89          outstream.write(1);
90          outstream.write(2);
91          outstream.write(3);
92          outstream.close();
93          
94          FileEntity httpentity = new FileEntity(tmpfile, HTTP.ISO_8859_1);
95          
96          ByteArrayOutputStream out = new ByteArrayOutputStream();
97          httpentity.writeTo(out);
98          byte[] bytes = out.toByteArray();
99          assertNotNull(bytes);
100         assertEquals(tmpfile.length(), bytes.length);
101         for (int i = 0; i < 4; i++) {
102             assertEquals(i, bytes[i]);
103         }
104         tmpfile.delete();
105 
106         try {
107             httpentity.writeTo(null);
108             fail("IllegalArgumentException should have been thrown");
109         } catch (IllegalArgumentException ex) {
110             // expected
111         }
112     }
113         
114 }