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.entity;
29  
30  import java.io.ByteArrayOutputStream;
31  import java.io.File;
32  import java.io.FileOutputStream;
33  import java.io.InputStream;
34  
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  /**
39   * Unit tests for {@link FileEntity}.
40   *
41   */
42  public class TestFileEntity {
43  
44      @Test
45      public void testBasics() throws Exception {
46          final File tmpfile = File.createTempFile("testfile", ".txt");
47          tmpfile.deleteOnExit();
48          final FileEntity httpentity = new FileEntity(tmpfile, ContentType.TEXT_PLAIN);
49  
50          Assert.assertEquals(tmpfile.length(), httpentity.getContentLength());
51          final InputStream content = httpentity.getContent();
52          Assert.assertNotNull(content);
53          content.close();
54          Assert.assertTrue(httpentity.isRepeatable());
55          Assert.assertFalse(httpentity.isStreaming());
56          if (!tmpfile.delete()){
57              Assert.fail("Failed to delete: "+tmpfile);
58          }
59      }
60  
61      @Test
62      public void testIllegalConstructor() throws Exception {
63          try {
64              new FileEntity(null, ContentType.TEXT_PLAIN);
65              Assert.fail("IllegalArgumentException should have been thrown");
66          } catch (final IllegalArgumentException ex) {
67              // expected
68          }
69      }
70  
71      @Test
72      public void testWriteTo() throws Exception {
73          final File tmpfile = File.createTempFile("testfile", ".txt");
74          tmpfile.deleteOnExit();
75  
76          final FileOutputStream outstream = new FileOutputStream(tmpfile);
77          outstream.write(0);
78          outstream.write(1);
79          outstream.write(2);
80          outstream.write(3);
81          outstream.close();
82  
83          final FileEntity httpentity = new FileEntity(tmpfile, ContentType.TEXT_PLAIN);
84  
85          final ByteArrayOutputStream out = new ByteArrayOutputStream();
86          httpentity.writeTo(out);
87          final byte[] bytes = out.toByteArray();
88          Assert.assertNotNull(bytes);
89          Assert.assertEquals(tmpfile.length(), bytes.length);
90          for (int i = 0; i < 4; i++) {
91              Assert.assertEquals(i, bytes[i]);
92          }
93          if (!tmpfile.delete()){
94              Assert.fail("Failed to delete: "+tmpfile);
95          }
96  
97          try {
98              httpentity.writeTo(null);
99              Assert.fail("IllegalArgumentException should have been thrown");
100         } catch (final IllegalArgumentException ex) {
101             // expected
102         }
103     }
104 
105 }