2
* $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/TestBuffers.java $
4
* $Date:2007-02-03 18:22:45 +0000 (Sat, 03 Feb 2007) $
5
* ====================================================================
6
* Licensed to the Apache Software Foundation (ASF) under one
7
* or more contributor license agreements. See the NOTICE file
8
* distributed with this work for additional information
9
* regarding copyright ownership. The ASF licenses this file
10
* to you under the Apache License, Version 2.0 (the
11
* "License"); you may not use this file except in compliance
12
* with the License. You may obtain a copy of the License at
14
* http://www.apache.org/licenses/LICENSE-2.0
16
* Unless required by applicable law or agreed to in writing,
17
* software distributed under the License is distributed on an
18
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19
* KIND, either express or implied. See the License for the
20
* specific language governing permissions and limitations
22
* ====================================================================
24
* This software consists of voluntary contributions made by many
25
* individuals on behalf of the Apache Software Foundation. For more
26
* information on the Apache Software Foundation, please see
27
* <http://www.apache.org/>.
31
package org.apache.http.nio.util;
33
import java.io.ByteArrayOutputStream;
34
import java.io.IOException;
35
import java.nio.ByteBuffer;
36
import java.nio.channels.Channels;
37
import java.nio.channels.ReadableByteChannel;
38
import java.nio.channels.WritableByteChannel;
40
import junit.framework.Test;
41
import junit.framework.TestCase;
42
import junit.framework.TestSuite;
44
import org.apache.http.impl.io.HttpTransportMetricsImpl;
45
import org.apache.http.impl.nio.reactor.SessionOutputBufferImpl;
46
import org.apache.http.mockup.MockupDecoder;
47
import org.apache.http.mockup.MockupEncoder;
48
import org.apache.http.mockup.ReadableByteChannelMockup;
49
import org.apache.http.nio.ContentDecoder;
50
import org.apache.http.nio.ContentEncoder;
51
import org.apache.http.nio.reactor.SessionOutputBuffer;
52
import org.apache.http.params.BasicHttpParams;
53
import org.apache.http.params.HttpParams;
54
import org.apache.http.util.EncodingUtils;
60
* @version $Id:TestBuffers.java 503277 2007-02-03 18:22:45 +0000 (Sat, 03 Feb 2007) olegk $
62
public class TestBuffers extends TestCase {
64
// ------------------------------------------------------------ Constructor
65
public TestBuffers(String testName) {
69
// ------------------------------------------------------------------- Main
70
public static void main(String args[]) {
71
String[] testCaseName = { TestBuffers.class.getName() };
72
junit.textui.TestRunner.main(testCaseName);
75
// ------------------------------------------------------- TestCase Methods
77
public static Test suite() {
78
return new TestSuite(TestBuffers.class);
81
public void testInputBufferOperations() throws IOException {
82
ReadableByteChannel channel = new ReadableByteChannelMockup(
83
new String[] {"stuff;", "more stuff"}, "US-ASCII");
85
ContentDecoder decoder = new MockupDecoder(channel);
87
SimpleInputBuffer buffer = new SimpleInputBuffer(4, new DirectByteBufferAllocator());
88
int count = buffer.consumeContent(decoder);
89
assertEquals(16, count);
90
assertTrue(decoder.isCompleted());
92
byte[] b1 = new byte[5];
94
int len = buffer.read(b1);
95
assertEquals("stuff", EncodingUtils.getAsciiString(b1, 0, len));
97
int c = buffer.read();
100
byte[] b2 = new byte[1024];
102
len = buffer.read(b2);
103
assertEquals("more stuff", EncodingUtils.getAsciiString(b2, 0, len));
105
assertEquals(-1, buffer.read());
106
assertEquals(-1, buffer.read(b2));
107
assertEquals(-1, buffer.read(b2, 0, b2.length));
108
assertTrue(buffer.isEndOfStream());
111
assertFalse(buffer.isEndOfStream());
114
public void testOutputBufferOperations() throws IOException {
115
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
116
WritableByteChannel channel = Channels.newChannel(outstream);
117
HttpParams params = new BasicHttpParams();
118
SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
119
HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
121
ContentEncoder encoder = new MockupEncoder(channel, outbuf, metrics);
123
SimpleOutputBuffer buffer = new SimpleOutputBuffer(4, new DirectByteBufferAllocator());
125
buffer.write(EncodingUtils.getAsciiBytes("stuff"));
127
buffer.produceContent(encoder);
129
buffer.write(EncodingUtils.getAsciiBytes("more "));
130
buffer.write(EncodingUtils.getAsciiBytes("stuff"));
131
buffer.produceContent(encoder);
133
byte[] content = outstream.toByteArray();
134
assertEquals("stuff;more stuff", EncodingUtils.getAsciiString(content));
137
public void testBufferInfo() throws Exception {
138
SimpleOutputBuffer buffer = new SimpleOutputBuffer(8, new DirectByteBufferAllocator());
139
BufferInfo bufferinfo = buffer;
141
assertEquals(0, bufferinfo.length());
142
assertEquals(8, bufferinfo.available());
143
buffer.write(new byte[] {'1', '2', '3', '4'});
144
assertEquals(4, bufferinfo.length());
145
assertEquals(4, bufferinfo.available());
146
buffer.write(new byte[] {'1', '2', '3', '4', '5', '6', '7', '8'});
147
assertEquals(12, bufferinfo.length());
148
assertEquals(0, bufferinfo.available());
151
public void testInputBufferNullInput() throws IOException {
152
SimpleInputBuffer buffer = new SimpleInputBuffer(4, new DirectByteBufferAllocator());
153
assertEquals(0, buffer.read(null));
154
assertEquals(0, buffer.read(null, 0, 0));
157
public void testOutputBufferNullInput() throws IOException {
158
SimpleOutputBuffer buffer = new SimpleOutputBuffer(4, new DirectByteBufferAllocator());
160
buffer.write(null, 0, 10);
161
assertFalse(buffer.hasData());
164
public void testDirectByteBufferAllocator() {
165
DirectByteBufferAllocator allocator = new DirectByteBufferAllocator();
166
ByteBuffer buffer = allocator.allocate(1);
167
assertNotNull(buffer);
168
assertTrue(buffer.isDirect());
169
assertEquals(0, buffer.position());
170
assertEquals(1, buffer.limit());
171
assertEquals(1, buffer.capacity());
173
buffer = allocator.allocate(2048);
174
assertTrue(buffer.isDirect());
175
assertEquals(0, buffer.position());
176
assertEquals(2048, buffer.limit());
177
assertEquals(2048, buffer.capacity());
179
buffer = allocator.allocate(0);
180
assertTrue(buffer.isDirect());
181
assertEquals(0, buffer.position());
182
assertEquals(0, buffer.limit());
183
assertEquals(0, buffer.capacity());
186
public void testHeapByteBufferAllocator() {
187
HeapByteBufferAllocator allocator = new HeapByteBufferAllocator();
188
ByteBuffer buffer = allocator.allocate(1);
189
assertNotNull(buffer);
190
assertFalse(buffer.isDirect());
191
assertEquals(0, buffer.position());
192
assertEquals(1, buffer.limit());
193
assertEquals(1, buffer.capacity());
195
buffer = allocator.allocate(2048);
196
assertFalse(buffer.isDirect());
197
assertEquals(0, buffer.position());
198
assertEquals(2048, buffer.limit());
199
assertEquals(2048, buffer.capacity());
201
buffer = allocator.allocate(0);
202
assertFalse(buffer.isDirect());
203
assertEquals(0, buffer.position());
204
assertEquals(0, buffer.limit());
205
assertEquals(0, buffer.capacity());