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.nio.entity;
29  
30  import java.io.File;
31  import java.io.FileOutputStream;
32  import java.io.OutputStreamWriter;
33  import java.io.Writer;
34  import java.nio.charset.StandardCharsets;
35  
36  import org.apache.hc.core5.http.ContentType;
37  import org.apache.hc.core5.http.WritableByteChannelMock;
38  import org.apache.hc.core5.http.nio.AsyncEntityProducer;
39  import org.apache.hc.core5.http.nio.BasicDataStreamChannel;
40  import org.apache.hc.core5.http.nio.DataStreamChannel;
41  import org.junit.After;
42  import org.junit.Assert;
43  import org.junit.Before;
44  import org.junit.Test;
45  
46  public class TestFileAsyncEntityProducer {
47  
48      private File tempFile;
49  
50      @Before
51      public void setup() throws Exception {
52          tempFile = File.createTempFile("testing", ".txt");
53          try (final Writer writer = new OutputStreamWriter(new FileOutputStream(tempFile), StandardCharsets.US_ASCII)) {
54              writer.append("abcdef");
55              writer.flush();
56          }
57      }
58  
59      @After
60      public void cleanup() {
61          if (tempFile != null) {
62              tempFile.delete();
63              tempFile = null;
64          }
65      }
66      @Test
67      public void testTextContent() throws Exception {
68  
69          final AsyncEntityProducer producer = new FileEntityProducer(tempFile, ContentType.TEXT_PLAIN);
70  
71          Assert.assertEquals(6, producer.getContentLength());
72          Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
73          Assert.assertEquals(null, producer.getContentEncoding());
74  
75          final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
76          final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
77  
78          producer.produce(streamChannel);
79          producer.produce(streamChannel);
80  
81          Assert.assertFalse(byteChannel.isOpen());
82          Assert.assertEquals("abcdef", byteChannel.dump(StandardCharsets.US_ASCII));
83      }
84  
85      @Test
86      public void testTextContentRepeatable() throws Exception {
87          final AsyncEntityProducer producer = new FileEntityProducer(tempFile, ContentType.TEXT_PLAIN);
88  
89          Assert.assertEquals(6, producer.getContentLength());
90          Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
91          Assert.assertEquals(null, producer.getContentEncoding());
92  
93          for (int i = 0; i < 3; i++) {
94              final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
95              final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
96  
97              producer.produce(streamChannel);
98              producer.produce(streamChannel);
99  
100             Assert.assertFalse(byteChannel.isOpen());
101             Assert.assertEquals("abcdef", byteChannel.dump(StandardCharsets.US_ASCII));
102 
103             producer.releaseResources();
104         }
105     }
106 
107 }