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.impl.nio;
29  
30  import static org.hamcrest.MatcherAssert.assertThat;
31  
32  import org.hamcrest.CoreMatchers;
33  import org.junit.jupiter.api.Test;
34  
35  public class TestExpandableBuffer {
36  
37      @Test
38      public void testBasics() throws Exception {
39          final ExpandableBuffer buffer = new ExpandableBuffer(16);
40          assertThat(buffer.mode(), CoreMatchers.equalTo(ExpandableBuffer.Mode.INPUT));
41          assertThat(buffer.hasData(), CoreMatchers.equalTo(false));
42  
43          buffer.setInputMode();
44          buffer.buffer().put(new byte[] { 0, 1, 2, 3, 4, 5});
45          assertThat(buffer.hasData(), CoreMatchers.equalTo(true));
46          assertThat(buffer.length(), CoreMatchers.equalTo(6));
47          assertThat(buffer.buffer().capacity(), CoreMatchers.equalTo(16));
48          assertThat(buffer.mode(), CoreMatchers.equalTo(ExpandableBuffer.Mode.OUTPUT));
49  
50          buffer.setInputMode();
51          buffer.buffer().put(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
52          assertThat(buffer.length(), CoreMatchers.equalTo(16));
53          assertThat(buffer.buffer().capacity(), CoreMatchers.equalTo(16));
54          assertThat(buffer.mode(), CoreMatchers.equalTo(ExpandableBuffer.Mode.OUTPUT));
55  
56          buffer.setInputMode();
57          buffer.ensureCapacity(22);
58          buffer.buffer().put(new byte[] { 0, 1, 2, 3, 4, 5});
59          assertThat(buffer.length(), CoreMatchers.equalTo(22));
60          assertThat(buffer.buffer().capacity(), CoreMatchers.equalTo(22));
61          assertThat(buffer.mode(), CoreMatchers.equalTo(ExpandableBuffer.Mode.OUTPUT));
62  
63          buffer.clear();
64          assertThat(buffer.mode(), CoreMatchers.equalTo(ExpandableBuffer.Mode.INPUT));
65          assertThat(buffer.hasData(), CoreMatchers.equalTo(false));
66          assertThat(buffer.capacity(), CoreMatchers.equalTo(22));
67      }
68  
69      @Test
70      public void testAdjustCapacity() throws Exception {
71          final ExpandableBuffer buffer = new ExpandableBuffer(16);
72          assertThat(buffer.capacity(), CoreMatchers.equalTo(16));
73  
74          buffer.ensureCapacity(21);
75          assertThat(buffer.capacity(), CoreMatchers.equalTo(21));
76          buffer.ensureAdjustedCapacity(22);
77          assertThat(buffer.capacity(), CoreMatchers.equalTo(1024));
78          buffer.ensureAdjustedCapacity(1024);
79          assertThat(buffer.capacity(), CoreMatchers.equalTo(1024));
80          buffer.ensureAdjustedCapacity(1025);
81          assertThat(buffer.capacity(), CoreMatchers.equalTo(2048));
82      }
83  }