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.CharBuffer;
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.Assert;
41 import org.junit.Test;
42
43 public class TestAbstractCharAsyncEntityProducer {
44
45 static private class ChunkCharAsyncEntityProducer extends AbstractCharAsyncEntityProducer {
46
47 private final String[] content;
48 private int count = 0;
49
50 public ChunkCharAsyncEntityProducer(
51 final int bufferSize,
52 final int fragmentSizeHint,
53 final ContentType contentType,
54 final String... content) {
55 super(bufferSize, fragmentSizeHint, contentType);
56 this.content = content;
57 }
58
59 @Override
60 public boolean isRepeatable() {
61 return false;
62 }
63
64 @Override
65 protected int availableData() {
66 return Integer.MAX_VALUE;
67 }
68
69 @Override
70 protected void produceData(final StreamChannel<CharBuffer> channel) throws IOException {
71 if (count < content.length) {
72 channel.write(CharBuffer.wrap(content[count]));
73 }
74 count++;
75 if (count >= content.length) {
76 channel.endStream();
77 }
78 }
79
80 @Override
81 public void failed(final Exception cause) {
82 }
83
84 }
85
86 @Test
87 public void testProduceDataNoBuffering() throws Exception {
88
89 final AsyncEntityProducer producer = new ChunkCharAsyncEntityProducer(
90 256, 0, ContentType.TEXT_PLAIN, "this", "this and that");
91
92 Assert.assertEquals(-1, producer.getContentLength());
93 Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
94 Assert.assertEquals(null, producer.getContentEncoding());
95
96 final WritableByteChannelMocknelMock.html#WritableByteChannelMock">WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
97 final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
98
99 producer.produce(streamChannel);
100
101 Assert.assertTrue(byteChannel.isOpen());
102 Assert.assertEquals("this", byteChannel.dump(StandardCharsets.US_ASCII));
103
104 producer.produce(streamChannel);
105
106 Assert.assertFalse(byteChannel.isOpen());
107 Assert.assertEquals("this and that", byteChannel.dump(StandardCharsets.US_ASCII));
108 }
109
110 @Test
111 public void testProduceDataWithBuffering() throws Exception {
112
113 final AsyncEntityProducer producer = new ChunkCharAsyncEntityProducer(
114 256, 5, ContentType.TEXT_PLAIN, "this", " and that", "all", " sorts of stuff");
115
116 final WritableByteChannelMocknelMock.html#WritableByteChannelMock">WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
117 final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
118
119 producer.produce(streamChannel);
120 Assert.assertTrue(byteChannel.isOpen());
121 Assert.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
122
123 producer.produce(streamChannel);
124 Assert.assertTrue(byteChannel.isOpen());
125 Assert.assertEquals("this and that", byteChannel.dump(StandardCharsets.US_ASCII));
126
127 producer.produce(streamChannel);
128 Assert.assertTrue(byteChannel.isOpen());
129 Assert.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
130
131 producer.produce(streamChannel);
132 Assert.assertFalse(byteChannel.isOpen());
133 Assert.assertEquals("all sorts of stuff", byteChannel.dump(StandardCharsets.US_ASCII));
134 }
135
136 }