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.IOException;
31 import java.nio.ByteBuffer;
32 import java.nio.charset.StandardCharsets;
33
34 import org.apache.hc.core5.http.ContentType;
35 import org.apache.hc.core5.http.WritableByteChannelMock;
36 import org.apache.hc.core5.http.nio.AsyncEntityProducer;
37 import org.apache.hc.core5.http.nio.BasicDataStreamChannel;
38 import org.apache.hc.core5.http.nio.DataStreamChannel;
39 import org.apache.hc.core5.http.nio.StreamChannel;
40 import org.junit.jupiter.api.Assertions;
41 import org.junit.jupiter.api.Test;
42
43 public class TestAbstractBinAsyncEntityProducer {
44
45 static private class ChunkByteAsyncEntityProducer extends AbstractBinAsyncEntityProducer {
46
47 private final byte[][] content;
48 private int count = 0;
49
50 public ChunkByteAsyncEntityProducer(
51 final int fragmentSizeHint,
52 final ContentType contentType,
53 final byte[]... content) {
54 super(fragmentSizeHint, contentType);
55 this.content = content;
56 }
57
58 @Override
59 public boolean isRepeatable() {
60 return false;
61 }
62
63 @Override
64 protected int availableData() {
65 return Integer.MAX_VALUE;
66 }
67
68 @Override
69 protected void produceData(final StreamChannel<ByteBuffer> channel) throws IOException {
70 if (count < content.length) {
71 channel.write(ByteBuffer.wrap(content[count]));
72 }
73 count++;
74 if (count >= content.length) {
75 channel.endStream();
76 }
77 }
78
79 @Override
80 public void failed(final Exception cause) {
81 }
82
83 }
84
85 @Test
86 public void testProduceDataNoBuffering() throws Exception {
87
88 final AsyncEntityProducer producer = new ChunkByteAsyncEntityProducer(
89 0, ContentType.TEXT_PLAIN,
90 new byte[] { '1', '2', '3' },
91 new byte[] { '4', '5', '6' });
92
93 Assertions.assertEquals(-1, producer.getContentLength());
94 Assertions.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
95 Assertions.assertNull(producer.getContentEncoding());
96
97 final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
98 final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
99
100 producer.produce(streamChannel);
101
102 Assertions.assertTrue(byteChannel.isOpen());
103 Assertions.assertEquals("123", byteChannel.dump(StandardCharsets.US_ASCII));
104
105 producer.produce(streamChannel);
106
107 Assertions.assertFalse(byteChannel.isOpen());
108 Assertions.assertEquals("456", byteChannel.dump(StandardCharsets.US_ASCII));
109 }
110
111 @Test
112 public void testProduceDataWithBuffering1() throws Exception {
113
114 final AsyncEntityProducer producer = new ChunkByteAsyncEntityProducer(
115 5, ContentType.TEXT_PLAIN,
116 new byte[] { '1', '2', '3' },
117 new byte[] { '4', '5', '6' },
118 new byte[] { '7', '8' },
119 new byte[] { '9', '0' });
120
121 final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
122 final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
123
124 producer.produce(streamChannel);
125 Assertions.assertTrue(byteChannel.isOpen());
126 Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
127
128 producer.produce(streamChannel);
129 Assertions.assertTrue(byteChannel.isOpen());
130 Assertions.assertEquals("123", byteChannel.dump(StandardCharsets.US_ASCII));
131
132 producer.produce(streamChannel);
133 Assertions.assertTrue(byteChannel.isOpen());
134 Assertions.assertEquals("45678", byteChannel.dump(StandardCharsets.US_ASCII));
135
136 producer.produce(streamChannel);
137 Assertions.assertFalse(byteChannel.isOpen());
138 Assertions.assertEquals("90", byteChannel.dump(StandardCharsets.US_ASCII));
139 }
140
141 @Test
142 public void testProduceDataWithBuffering2() throws Exception {
143
144 final AsyncEntityProducer producer = new ChunkByteAsyncEntityProducer(
145 5, ContentType.TEXT_PLAIN,
146 new byte[] { '1' },
147 new byte[] { '2' },
148 new byte[] { '3' },
149 new byte[] { '4', '5' },
150 new byte[] { '6' },
151 new byte[] { '7', '8' },
152 new byte[] { '9', '0' });
153
154 final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
155 final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
156
157 producer.produce(streamChannel);
158 Assertions.assertTrue(byteChannel.isOpen());
159 Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
160
161 producer.produce(streamChannel);
162 Assertions.assertTrue(byteChannel.isOpen());
163 Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
164
165 producer.produce(streamChannel);
166 Assertions.assertTrue(byteChannel.isOpen());
167 Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
168
169 producer.produce(streamChannel);
170 Assertions.assertTrue(byteChannel.isOpen());
171 Assertions.assertEquals("12345", byteChannel.dump(StandardCharsets.US_ASCII));
172
173 producer.produce(streamChannel);
174 Assertions.assertTrue(byteChannel.isOpen());
175 Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
176
177 producer.produce(streamChannel);
178 Assertions.assertTrue(byteChannel.isOpen());
179 Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
180
181 producer.produce(streamChannel);
182 Assertions.assertFalse(byteChannel.isOpen());
183 Assertions.assertEquals("67890", byteChannel.dump(StandardCharsets.US_ASCII));
184
185 }
186
187 }