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