~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore-nio/src/test/java/org/apache/http/nio/util/TestBuffers.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/TestBuffers.java $
 
3
 * $Revision:503277 $
 
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
 
13
 *
 
14
 *   http://www.apache.org/licenses/LICENSE-2.0
 
15
 *
 
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
 
21
 * under the License.
 
22
 * ====================================================================
 
23
 *
 
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/>.
 
28
 *
 
29
 */
 
30
 
 
31
package org.apache.http.nio.util;
 
32
 
 
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;
 
39
 
 
40
import junit.framework.Test;
 
41
import junit.framework.TestCase;
 
42
import junit.framework.TestSuite;
 
43
 
 
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;
 
55
 
 
56
/**
 
57
 * Buffer tests.
 
58
 *
 
59
 * 
 
60
 * @version $Id:TestBuffers.java 503277 2007-02-03 18:22:45 +0000 (Sat, 03 Feb 2007) olegk $
 
61
 */
 
62
public class TestBuffers extends TestCase {
 
63
 
 
64
    // ------------------------------------------------------------ Constructor
 
65
    public TestBuffers(String testName) {
 
66
        super(testName);
 
67
    }
 
68
 
 
69
    // ------------------------------------------------------------------- Main
 
70
    public static void main(String args[]) {
 
71
        String[] testCaseName = { TestBuffers.class.getName() };
 
72
        junit.textui.TestRunner.main(testCaseName);
 
73
    }
 
74
 
 
75
    // ------------------------------------------------------- TestCase Methods
 
76
 
 
77
    public static Test suite() {
 
78
        return new TestSuite(TestBuffers.class);
 
79
    }
 
80
 
 
81
    public void testInputBufferOperations() throws IOException {
 
82
        ReadableByteChannel channel = new ReadableByteChannelMockup(
 
83
                new String[] {"stuff;", "more stuff"}, "US-ASCII"); 
 
84
        
 
85
        ContentDecoder decoder = new MockupDecoder(channel); 
 
86
        
 
87
        SimpleInputBuffer buffer = new SimpleInputBuffer(4, new DirectByteBufferAllocator());
 
88
        int count = buffer.consumeContent(decoder);
 
89
        assertEquals(16, count);
 
90
        assertTrue(decoder.isCompleted());
 
91
        
 
92
        byte[] b1 = new byte[5];
 
93
        
 
94
        int len = buffer.read(b1);
 
95
        assertEquals("stuff", EncodingUtils.getAsciiString(b1, 0, len));
 
96
        
 
97
        int c = buffer.read();
 
98
        assertEquals(';', c);
 
99
        
 
100
        byte[] b2 = new byte[1024];
 
101
 
 
102
        len = buffer.read(b2);
 
103
        assertEquals("more stuff", EncodingUtils.getAsciiString(b2, 0, len));
 
104
 
 
105
        assertEquals(-1, buffer.read());
 
106
        assertEquals(-1, buffer.read(b2));
 
107
        assertEquals(-1, buffer.read(b2, 0, b2.length));
 
108
        assertTrue(buffer.isEndOfStream());
 
109
        
 
110
        buffer.reset();
 
111
        assertFalse(buffer.isEndOfStream());
 
112
    }
 
113
 
 
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();
 
120
        
 
121
        ContentEncoder encoder = new MockupEncoder(channel, outbuf, metrics);
 
122
        
 
123
        SimpleOutputBuffer buffer = new SimpleOutputBuffer(4, new DirectByteBufferAllocator()); 
 
124
        
 
125
        buffer.write(EncodingUtils.getAsciiBytes("stuff"));
 
126
        buffer.write(';');
 
127
        buffer.produceContent(encoder);
 
128
 
 
129
        buffer.write(EncodingUtils.getAsciiBytes("more "));
 
130
        buffer.write(EncodingUtils.getAsciiBytes("stuff"));
 
131
        buffer.produceContent(encoder);
 
132
        
 
133
        byte[] content = outstream.toByteArray();
 
134
        assertEquals("stuff;more stuff", EncodingUtils.getAsciiString(content));
 
135
    }
 
136
 
 
137
    public void testBufferInfo() throws Exception {
 
138
        SimpleOutputBuffer buffer = new SimpleOutputBuffer(8, new DirectByteBufferAllocator());
 
139
        BufferInfo bufferinfo = buffer; 
 
140
        
 
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());
 
149
    }
 
150
    
 
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));
 
155
    }
 
156
    
 
157
    public void testOutputBufferNullInput() throws IOException {
 
158
        SimpleOutputBuffer buffer = new SimpleOutputBuffer(4, new DirectByteBufferAllocator());
 
159
        buffer.write(null);
 
160
        buffer.write(null, 0, 10);
 
161
        assertFalse(buffer.hasData());
 
162
    }
 
163
    
 
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());
 
172
        
 
173
        buffer = allocator.allocate(2048);
 
174
        assertTrue(buffer.isDirect());
 
175
        assertEquals(0, buffer.position());
 
176
        assertEquals(2048, buffer.limit());
 
177
        assertEquals(2048, buffer.capacity());
 
178
        
 
179
        buffer = allocator.allocate(0);
 
180
        assertTrue(buffer.isDirect());
 
181
        assertEquals(0, buffer.position());
 
182
        assertEquals(0, buffer.limit());
 
183
        assertEquals(0, buffer.capacity());
 
184
    }
 
185
 
 
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());
 
194
        
 
195
        buffer = allocator.allocate(2048);
 
196
        assertFalse(buffer.isDirect());
 
197
        assertEquals(0, buffer.position());
 
198
        assertEquals(2048, buffer.limit());
 
199
        assertEquals(2048, buffer.capacity());
 
200
        
 
201
        buffer = allocator.allocate(0);
 
202
        assertFalse(buffer.isDirect());
 
203
        assertEquals(0, buffer.position());
 
204
        assertEquals(0, buffer.limit());
 
205
        assertEquals(0, buffer.capacity());
 
206
    }
 
207
    
 
208
}