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