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.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.Assert;
41  import org.junit.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          Assert.assertEquals(-1, producer.getContentLength());
94          Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
95          Assert.assertEquals(null, producer.getContentEncoding());
96  
97          final WritableByteChannelMocknelMock.html#WritableByteChannelMock">WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
98          final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
99  
100         producer.produce(streamChannel);
101 
102         Assert.assertTrue(byteChannel.isOpen());
103         Assert.assertEquals("123", byteChannel.dump(StandardCharsets.US_ASCII));
104 
105         producer.produce(streamChannel);
106 
107         Assert.assertFalse(byteChannel.isOpen());
108         Assert.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 WritableByteChannelMocknelMock.html#WritableByteChannelMock">WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
122         final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
123 
124         producer.produce(streamChannel);
125         Assert.assertTrue(byteChannel.isOpen());
126         Assert.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
127 
128         producer.produce(streamChannel);
129         Assert.assertTrue(byteChannel.isOpen());
130         Assert.assertEquals("123", byteChannel.dump(StandardCharsets.US_ASCII));
131 
132         producer.produce(streamChannel);
133         Assert.assertTrue(byteChannel.isOpen());
134         Assert.assertEquals("45678", byteChannel.dump(StandardCharsets.US_ASCII));
135 
136         producer.produce(streamChannel);
137         Assert.assertFalse(byteChannel.isOpen());
138         Assert.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 WritableByteChannelMocknelMock.html#WritableByteChannelMock">WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
155         final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
156 
157         producer.produce(streamChannel);
158         Assert.assertTrue(byteChannel.isOpen());
159         Assert.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
160 
161         producer.produce(streamChannel);
162         Assert.assertTrue(byteChannel.isOpen());
163         Assert.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
164 
165         producer.produce(streamChannel);
166         Assert.assertTrue(byteChannel.isOpen());
167         Assert.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
168 
169         producer.produce(streamChannel);
170         Assert.assertTrue(byteChannel.isOpen());
171         Assert.assertEquals("12345", byteChannel.dump(StandardCharsets.US_ASCII));
172 
173         producer.produce(streamChannel);
174         Assert.assertTrue(byteChannel.isOpen());
175         Assert.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
176 
177         producer.produce(streamChannel);
178         Assert.assertTrue(byteChannel.isOpen());
179         Assert.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
180 
181         producer.produce(streamChannel);
182         Assert.assertFalse(byteChannel.isOpen());
183         Assert.assertEquals("67890", byteChannel.dump(StandardCharsets.US_ASCII));
184 
185     }
186 
187 }