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.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 void produceData(final StreamChannel<CharBuffer> channel) throws IOException {
66              if (count < content.length) {
67                  channel.write(CharBuffer.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      };
90  
91      @Test
92      public void testProduceDataNoBuffering() throws Exception {
93  
94          final AsyncEntityProducer producer = new ChunkCharAsyncEntityProducer(
95                  256, 0, ContentType.TEXT_PLAIN, "this", "this and that");
96  
97          Assert.assertEquals(-1, producer.getContentLength());
98          Assert.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
99          Assert.assertEquals(null, producer.getContentEncoding());
100 
101         final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
102         final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
103 
104         producer.produce(streamChannel);
105 
106         Assert.assertTrue(byteChannel.isOpen());
107         Assert.assertEquals("this", byteChannel.dump(StandardCharsets.US_ASCII));
108 
109         producer.produce(streamChannel);
110 
111         Assert.assertFalse(byteChannel.isOpen());
112         Assert.assertEquals("this and that", byteChannel.dump(StandardCharsets.US_ASCII));
113     }
114 
115     @Test
116     public void testProduceDataWithBuffering() throws Exception {
117 
118         final AsyncEntityProducer producer = new ChunkCharAsyncEntityProducer(
119                 256, 5, ContentType.TEXT_PLAIN, "this", " and that", "all", " sorts of stuff");
120 
121         final 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("this and that", byteChannel.dump(StandardCharsets.US_ASCII));
131 
132         producer.produce(streamChannel);
133         Assert.assertTrue(byteChannel.isOpen());
134         Assert.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
135 
136         producer.produce(streamChannel);
137         Assert.assertFalse(byteChannel.isOpen());
138         Assert.assertEquals("all sorts of stuff", byteChannel.dump(StandardCharsets.US_ASCII));
139     }
140 
141 }