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.http.nio.util;
29  
30  import java.io.ByteArrayOutputStream;
31  import java.io.IOException;
32  import java.nio.ByteBuffer;
33  import java.nio.channels.Channels;
34  import java.nio.channels.ReadableByteChannel;
35  import java.nio.channels.WritableByteChannel;
36  
37  import org.apache.http.Consts;
38  import org.apache.http.ReadableByteChannelMock;
39  import org.apache.http.impl.io.HttpTransportMetricsImpl;
40  import org.apache.http.impl.nio.reactor.SessionOutputBufferImpl;
41  import org.apache.http.nio.ContentDecoder;
42  import org.apache.http.nio.ContentEncoder;
43  import org.apache.http.nio.reactor.SessionOutputBuffer;
44  import org.apache.http.util.EncodingUtils;
45  import org.junit.Assert;
46  import org.junit.Test;
47  
48  /**
49   * Buffer tests.
50   */
51  public class TestBuffers {
52  
53      @Test
54      public void testInputBufferOperations() throws IOException {
55          final ReadableByteChannel channel = new ReadableByteChannelMock(
56                  new String[] {"stuff;", "more stuff"}, Consts.ASCII);
57  
58          final ContentDecoder decoder = new ContentDecoderMock(channel);
59  
60          final SimpleInputBuffer buffer = new SimpleInputBuffer(4, DirectByteBufferAllocator.INSTANCE);
61          final int count = buffer.consumeContent(decoder);
62          Assert.assertEquals(16, count);
63          Assert.assertTrue(decoder.isCompleted());
64  
65          final byte[] b1 = new byte[5];
66  
67          int len = buffer.read(b1);
68          Assert.assertEquals("stuff", EncodingUtils.getAsciiString(b1, 0, len));
69  
70          final int c = buffer.read();
71          Assert.assertEquals(';', c);
72  
73          final byte[] b2 = new byte[1024];
74  
75          len = buffer.read(b2);
76          Assert.assertEquals("more stuff", EncodingUtils.getAsciiString(b2, 0, len));
77  
78          Assert.assertEquals(-1, buffer.read());
79          Assert.assertEquals(-1, buffer.read(b2));
80          Assert.assertEquals(-1, buffer.read(b2, 0, b2.length));
81          Assert.assertTrue(buffer.isEndOfStream());
82  
83          buffer.reset();
84          Assert.assertFalse(buffer.isEndOfStream());
85      }
86  
87      @Test
88      public void testOutputBufferOperations() throws IOException {
89          final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
90          final WritableByteChannel channel = Channels.newChannel(outstream);
91          final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, Consts.ASCII);
92          final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
93  
94          final ContentEncoder encoder = new ContentEncoderMock(channel, outbuf, metrics);
95  
96          final SimpleOutputBuffer buffer = new SimpleOutputBuffer(4, DirectByteBufferAllocator.INSTANCE);
97  
98          buffer.write(EncodingUtils.getAsciiBytes("stuff"));
99          buffer.write(';');
100         buffer.produceContent(encoder);
101 
102         buffer.write(EncodingUtils.getAsciiBytes("more "));
103         buffer.write(EncodingUtils.getAsciiBytes("stuff"));
104         buffer.produceContent(encoder);
105 
106         final byte[] content = outstream.toByteArray();
107         Assert.assertEquals("stuff;more stuff", EncodingUtils.getAsciiString(content));
108     }
109 
110     @Test
111     public void testBufferInfo() throws Exception {
112         final SimpleOutputBuffer buffer = new SimpleOutputBuffer(8, DirectByteBufferAllocator.INSTANCE);
113 
114         Assert.assertEquals(0, buffer.length());
115         Assert.assertEquals(8, buffer.available());
116         buffer.write(new byte[] {'1', '2', '3', '4'});
117         Assert.assertEquals(4, buffer.length());
118         Assert.assertEquals(4, buffer.available());
119         buffer.write(new byte[] {'1', '2', '3', '4', '5', '6', '7', '8'});
120         Assert.assertEquals(12, buffer.length());
121         Assert.assertEquals(0, buffer.available());
122     }
123 
124     @Test
125     public void testInputBufferNullInput() throws IOException {
126         final SimpleInputBuffer buffer = new SimpleInputBuffer(4, DirectByteBufferAllocator.INSTANCE);
127         Assert.assertEquals(0, buffer.read(null));
128         Assert.assertEquals(0, buffer.read(null, 0, 0));
129     }
130 
131     @Test
132     public void testOutputBufferNullInput() throws IOException {
133         final SimpleOutputBuffer buffer = new SimpleOutputBuffer(4, DirectByteBufferAllocator.INSTANCE);
134         buffer.write(null);
135         buffer.write(null, 0, 10);
136         Assert.assertFalse(buffer.hasData());
137     }
138 
139     @Test
140     public void testDirectByteBufferAllocator() {
141         final DirectByteBufferAllocator allocator = new DirectByteBufferAllocator();
142         ByteBuffer buffer = allocator.allocate(1);
143         Assert.assertNotNull(buffer);
144         Assert.assertTrue(buffer.isDirect());
145         Assert.assertEquals(0, buffer.position());
146         Assert.assertEquals(1, buffer.limit());
147         Assert.assertEquals(1, buffer.capacity());
148 
149         buffer = allocator.allocate(2048);
150         Assert.assertTrue(buffer.isDirect());
151         Assert.assertEquals(0, buffer.position());
152         Assert.assertEquals(2048, buffer.limit());
153         Assert.assertEquals(2048, buffer.capacity());
154 
155         buffer = allocator.allocate(0);
156         Assert.assertTrue(buffer.isDirect());
157         Assert.assertEquals(0, buffer.position());
158         Assert.assertEquals(0, buffer.limit());
159         Assert.assertEquals(0, buffer.capacity());
160     }
161 
162     @Test
163     public void testHeapByteBufferAllocator() {
164         final HeapByteBufferAllocator allocator = new HeapByteBufferAllocator();
165         ByteBuffer buffer = allocator.allocate(1);
166         Assert.assertNotNull(buffer);
167         Assert.assertFalse(buffer.isDirect());
168         Assert.assertEquals(0, buffer.position());
169         Assert.assertEquals(1, buffer.limit());
170         Assert.assertEquals(1, buffer.capacity());
171 
172         buffer = allocator.allocate(2048);
173         Assert.assertFalse(buffer.isDirect());
174         Assert.assertEquals(0, buffer.position());
175         Assert.assertEquals(2048, buffer.limit());
176         Assert.assertEquals(2048, buffer.capacity());
177 
178         buffer = allocator.allocate(0);
179         Assert.assertFalse(buffer.isDirect());
180         Assert.assertEquals(0, buffer.position());
181         Assert.assertEquals(0, buffer.limit());
182         Assert.assertEquals(0, buffer.capacity());
183     }
184 
185 }