1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 }