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.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.jupiter.api.Assertions;
41  import org.junit.jupiter.api.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          Assertions.assertEquals(-1, producer.getContentLength());
93          Assertions.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
94          Assertions.assertNull(producer.getContentEncoding());
95  
96          final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
97          final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
98  
99          producer.produce(streamChannel);
100 
101         Assertions.assertTrue(byteChannel.isOpen());
102         Assertions.assertEquals("this", byteChannel.dump(StandardCharsets.US_ASCII));
103 
104         producer.produce(streamChannel);
105 
106         Assertions.assertFalse(byteChannel.isOpen());
107         Assertions.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 WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
117         final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
118 
119         producer.produce(streamChannel);
120         Assertions.assertTrue(byteChannel.isOpen());
121         Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
122 
123         producer.produce(streamChannel);
124         Assertions.assertTrue(byteChannel.isOpen());
125         Assertions.assertEquals("this and that", byteChannel.dump(StandardCharsets.US_ASCII));
126 
127         producer.produce(streamChannel);
128         Assertions.assertTrue(byteChannel.isOpen());
129         Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
130 
131         producer.produce(streamChannel);
132         Assertions.assertFalse(byteChannel.isOpen());
133         Assertions.assertEquals("all sorts of stuff", byteChannel.dump(StandardCharsets.US_ASCII));
134     }
135 
136 }