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.CharBuffer;
33  import java.nio.charset.StandardCharsets;
34  import java.util.concurrent.atomic.AtomicLong;
35  
36  import org.apache.hc.core5.concurrent.FutureCallback;
37  import org.apache.hc.core5.http.ContentType;
38  import org.apache.hc.core5.http.HttpException;
39  import org.apache.hc.core5.http.impl.BasicEntityDetails;
40  import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
41  import org.apache.hc.core5.util.ByteArrayBuffer;
42  import org.junit.jupiter.api.Assertions;
43  import org.junit.jupiter.api.Test;
44  
45  public class TestAbstractCharAsyncEntityConsumer {
46  
47      static private class StringBuilderAsyncEntityConsumer extends AbstractCharAsyncEntityConsumer<String> {
48  
49          private final StringBuilder buffer;
50  
51          public StringBuilderAsyncEntityConsumer() {
52              super();
53              this.buffer = new StringBuilder(1024);
54          }
55  
56          @Override
57          protected void streamStart(final ContentType contentType) throws HttpException, IOException {
58          }
59  
60          @Override
61          protected int capacityIncrement() {
62              return Integer.MAX_VALUE;
63          }
64  
65          @Override
66          protected void data(final CharBuffer src, final boolean endOfStream) throws IOException {
67              buffer.append(src);
68          }
69  
70          @Override
71          protected String generateContent() throws IOException {
72              return buffer.toString();
73          }
74  
75          @Override
76          public void releaseResources() {
77              buffer.setLength(0);
78          }
79  
80      }
81  
82      @Test
83      public void testConsumeData() throws Exception {
84  
85          final AsyncEntityConsumer<String> consumer = new StringBuilderAsyncEntityConsumer();
86  
87          final AtomicLong count = new AtomicLong(0);
88          consumer.streamStart(new BasicEntityDetails(-1, ContentType.TEXT_PLAIN), new FutureCallback<String>() {
89  
90              @Override
91              public void completed(final String result) {
92                  count.incrementAndGet();
93              }
94  
95              @Override
96              public void failed(final Exception ex) {
97                  count.incrementAndGet();
98              }
99  
100             @Override
101             public void cancelled() {
102                 count.incrementAndGet();
103             }
104 
105         });
106 
107         consumer.consume(ByteBuffer.wrap(new byte[]{'1', '2', '3'}));
108         consumer.consume(ByteBuffer.wrap(new byte[]{'4', '5'}));
109         consumer.consume(ByteBuffer.wrap(new byte[]{}));
110 
111         Assertions.assertNull(consumer.getContent());
112         consumer.streamEnd(null);
113 
114         Assertions.assertEquals("12345", consumer.getContent());
115         Assertions.assertEquals(1L, count.longValue());
116     }
117 
118     @Test
119     public void testConsumeIncompleteData() throws Exception {
120 
121         final AsyncEntityConsumer<String> consumer = new StringBuilderAsyncEntityConsumer();
122 
123         final AtomicLong count = new AtomicLong(0);
124         consumer.streamStart(new BasicEntityDetails(-1, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8)), new FutureCallback<String>() {
125 
126             @Override
127             public void completed(final String result) {
128                 count.incrementAndGet();
129             }
130 
131             @Override
132             public void failed(final Exception ex) {
133                 count.incrementAndGet();
134             }
135 
136             @Override
137             public void cancelled() {
138                 count.incrementAndGet();
139             }
140 
141         });
142 
143         final byte[] stuff = "stuff".getBytes(StandardCharsets.UTF_8);
144         final byte[] splitCharacter = "£".getBytes(StandardCharsets.UTF_8);
145 
146         final ByteArrayBuffer b1 = new ByteArrayBuffer(1024);
147         b1.append(stuff, 0, stuff.length);
148         b1.append(splitCharacter, 0, 1);
149         consumer.consume(ByteBuffer.wrap(b1.toByteArray()));
150 
151         final ByteArrayBuffer b2 = new ByteArrayBuffer(1024);
152         b2.append(splitCharacter, 1, 1);
153         b2.append(stuff, 0, stuff.length);
154         b2.append(splitCharacter, 0, 1);
155         consumer.consume(ByteBuffer.wrap(b2.toByteArray()));
156 
157         final ByteArrayBuffer b3 = new ByteArrayBuffer(1024);
158         b3.append(splitCharacter, 1, 1);
159         b3.append(stuff, 0, stuff.length);
160         consumer.consume(ByteBuffer.wrap(b3.toByteArray()));
161 
162         consumer.streamEnd(null);
163 
164         Assertions.assertEquals("stuff£stuff£stuff", consumer.getContent());
165         Assertions.assertEquals(1L, count.longValue());
166     }
167 
168 }