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.WritableByteChannelMock;
35  import org.apache.hc.core5.http.ContentType;
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 bufferSize,
52                  final int fragmentSizeHint,
53                  final ContentType contentType,
54                  final byte[]... 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 void produceData(final StreamChannel<ByteBuffer> channel) throws IOException {
66              if (count < content.length) {
67                  channel.write(ByteBuffer.wrap(content[count]));
68              }
69              count++;
70              if (count >= content.length) {
71                  channel.endStream();
72              }
73          }
74  
75          @Override
76          public long getContentLength() {
77              return -1;
78          }
79  
80          @Override
81          public int available() {
82              return Integer.MAX_VALUE;
83          }
84  
85          @Override
86          public void failed(final Exception cause) {
87          }
88  
89          @Override
90          public void releaseResources() {
91          }
92  
93      };
94  
95      @Test
96      public void testProduceDataNoBuffering() throws Exception {
97  
98          final AsyncEntityProducer producer = new ChunkByteAsyncEntityProducer(
99                  256, 0, ContentType.TEXT_PLAIN,
100                 new byte[] { '1', '2', '3' }, new byte[] { '4', '5', '6' });
101 
102         Assert.assertEquals(-1, producer.getContentLength());
103         Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
104         Assert.assertEquals(null, producer.getContentEncoding());
105 
106         final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
107         final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
108 
109         producer.produce(streamChannel);
110 
111         Assert.assertTrue(byteChannel.isOpen());
112         Assert.assertEquals("123", byteChannel.dump(StandardCharsets.US_ASCII));
113 
114         producer.produce(streamChannel);
115 
116         Assert.assertFalse(byteChannel.isOpen());
117         Assert.assertEquals("456", byteChannel.dump(StandardCharsets.US_ASCII));
118     }
119 
120     @Test
121     public void testProduceDataWithBuffering() throws Exception {
122 
123         final AsyncEntityProducer producer = new ChunkByteAsyncEntityProducer(
124                 256, 5, ContentType.TEXT_PLAIN,
125                 new byte[] { '1', '2', '3' },
126                 new byte[] { '4', '5', '6' },
127                 new byte[] { '7', '8' },
128                 new byte[] { '9', '0' });
129 
130         final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
131         final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
132 
133         producer.produce(streamChannel);
134         Assert.assertTrue(byteChannel.isOpen());
135         Assert.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
136 
137         producer.produce(streamChannel);
138         Assert.assertTrue(byteChannel.isOpen());
139         Assert.assertEquals("123456", byteChannel.dump(StandardCharsets.US_ASCII));
140 
141         producer.produce(streamChannel);
142         Assert.assertTrue(byteChannel.isOpen());
143         Assert.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
144 
145         producer.produce(streamChannel);
146         Assert.assertFalse(byteChannel.isOpen());
147         Assert.assertEquals("7890", byteChannel.dump(StandardCharsets.US_ASCII));
148     }
149 
150 }