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.nio.charset.StandardCharsets;
31
32 import org.apache.hc.core5.http.ContentType;
33 import org.apache.hc.core5.http.WritableByteChannelMock;
34 import org.apache.hc.core5.http.nio.AsyncEntityProducer;
35 import org.apache.hc.core5.http.nio.BasicDataStreamChannel;
36 import org.apache.hc.core5.http.nio.DataStreamChannel;
37 import org.junit.Assert;
38 import org.junit.Test;
39
40 public class TestBasicAsyncEntityProducer {
41
42 @Test
43 public void testBinaryContent() throws Exception {
44
45 final AsyncEntityProducer producer = new BasicAsyncEntityProducer(
46 new byte[] { 'a', 'b', 'c' }, ContentType.DEFAULT_BINARY);
47
48 Assert.assertEquals(3, producer.getContentLength());
49 Assert.assertEquals(ContentType.DEFAULT_BINARY.toString(), producer.getContentType());
50 Assert.assertEquals(null, producer.getContentEncoding());
51
52 final WritableByteChannelMocknelMock.html#WritableByteChannelMock">WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
53 final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
54
55 producer.produce(streamChannel);
56
57 Assert.assertFalse(byteChannel.isOpen());
58 Assert.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
59 }
60
61 @Test
62 public void testTextContent() throws Exception {
63
64 final AsyncEntityProducer producer = new BasicAsyncEntityProducer(
65 "abc", ContentType.TEXT_PLAIN);
66
67 Assert.assertEquals(3, producer.getContentLength());
68 Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
69 Assert.assertEquals(null, producer.getContentEncoding());
70
71 final WritableByteChannelMocknelMock.html#WritableByteChannelMock">WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
72 final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
73
74 producer.produce(streamChannel);
75
76 Assert.assertFalse(byteChannel.isOpen());
77 Assert.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
78 }
79
80 @Test
81 public void testTextContentRepeatable() throws Exception {
82 final AsyncEntityProducer producer = new BasicAsyncEntityProducer(
83 "abc", ContentType.TEXT_PLAIN);
84
85 Assert.assertEquals(3, producer.getContentLength());
86 Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
87 Assert.assertEquals(null, producer.getContentEncoding());
88
89 for (int i = 0; i < 3; i++) {
90 final WritableByteChannelMocknelMock.html#WritableByteChannelMock">WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
91 final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
92
93 producer.produce(streamChannel);
94
95 Assert.assertFalse(byteChannel.isOpen());
96 Assert.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
97
98 producer.releaseResources();
99 }
100 }
101
102 }