001: /*
002: * $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/TestBuffers.java $
003: * $Revision:503277 $
004: * $Date:2007-02-03 18:22:45 +0000 (Sat, 03 Feb 2007) $
005: * ====================================================================
006: * Licensed to the Apache Software Foundation (ASF) under one
007: * or more contributor license agreements. See the NOTICE file
008: * distributed with this work for additional information
009: * regarding copyright ownership. The ASF licenses this file
010: * to you under the Apache License, Version 2.0 (the
011: * "License"); you may not use this file except in compliance
012: * with the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing,
017: * software distributed under the License is distributed on an
018: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
019: * KIND, either express or implied. See the License for the
020: * specific language governing permissions and limitations
021: * under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.http.nio.util;
032:
033: import java.io.ByteArrayOutputStream;
034: import java.io.IOException;
035: import java.nio.ByteBuffer;
036: import java.nio.channels.Channels;
037: import java.nio.channels.ReadableByteChannel;
038: import java.nio.channels.WritableByteChannel;
039:
040: import junit.framework.Test;
041: import junit.framework.TestCase;
042: import junit.framework.TestSuite;
043:
044: import org.apache.http.impl.io.HttpTransportMetricsImpl;
045: import org.apache.http.impl.nio.reactor.SessionOutputBufferImpl;
046: import org.apache.http.mockup.MockupDecoder;
047: import org.apache.http.mockup.MockupEncoder;
048: import org.apache.http.mockup.ReadableByteChannelMockup;
049: import org.apache.http.nio.ContentDecoder;
050: import org.apache.http.nio.ContentEncoder;
051: import org.apache.http.nio.reactor.SessionOutputBuffer;
052: import org.apache.http.params.BasicHttpParams;
053: import org.apache.http.params.HttpParams;
054: import org.apache.http.util.EncodingUtils;
055:
056: /**
057: * Buffer tests.
058: *
059: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
060: *
061: * @version $Id:TestBuffers.java 503277 2007-02-03 18:22:45 +0000 (Sat, 03 Feb 2007) olegk $
062: */
063: public class TestBuffers extends TestCase {
064:
065: // ------------------------------------------------------------ Constructor
066: public TestBuffers(String testName) {
067: super (testName);
068: }
069:
070: // ------------------------------------------------------------------- Main
071: public static void main(String args[]) {
072: String[] testCaseName = { TestBuffers.class.getName() };
073: junit.textui.TestRunner.main(testCaseName);
074: }
075:
076: // ------------------------------------------------------- TestCase Methods
077:
078: public static Test suite() {
079: return new TestSuite(TestBuffers.class);
080: }
081:
082: public void testInputBufferOperations() throws IOException {
083: ReadableByteChannel channel = new ReadableByteChannelMockup(
084: new String[] { "stuff;", "more stuff" }, "US-ASCII");
085:
086: ContentDecoder decoder = new MockupDecoder(channel);
087:
088: SimpleInputBuffer buffer = new SimpleInputBuffer(4,
089: new DirectByteBufferAllocator());
090: int count = buffer.consumeContent(decoder);
091: assertEquals(16, count);
092: assertTrue(decoder.isCompleted());
093:
094: byte[] b1 = new byte[5];
095:
096: int len = buffer.read(b1);
097: assertEquals("stuff", EncodingUtils.getAsciiString(b1, 0, len));
098:
099: int c = buffer.read();
100: assertEquals(';', c);
101:
102: byte[] b2 = new byte[1024];
103:
104: len = buffer.read(b2);
105: assertEquals("more stuff", EncodingUtils.getAsciiString(b2, 0,
106: len));
107:
108: assertEquals(-1, buffer.read());
109: assertEquals(-1, buffer.read(b2));
110: assertEquals(-1, buffer.read(b2, 0, b2.length));
111: assertTrue(buffer.isEndOfStream());
112:
113: buffer.reset();
114: assertFalse(buffer.isEndOfStream());
115: }
116:
117: public void testOutputBufferOperations() throws IOException {
118: ByteArrayOutputStream outstream = new ByteArrayOutputStream();
119: WritableByteChannel channel = Channels.newChannel(outstream);
120: HttpParams params = new BasicHttpParams();
121: SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024,
122: 128, params);
123: HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
124:
125: ContentEncoder encoder = new MockupEncoder(channel, outbuf,
126: metrics);
127:
128: SimpleOutputBuffer buffer = new SimpleOutputBuffer(4,
129: new DirectByteBufferAllocator());
130:
131: buffer.write(EncodingUtils.getAsciiBytes("stuff"));
132: buffer.write(';');
133: buffer.produceContent(encoder);
134:
135: buffer.write(EncodingUtils.getAsciiBytes("more "));
136: buffer.write(EncodingUtils.getAsciiBytes("stuff"));
137: buffer.produceContent(encoder);
138:
139: byte[] content = outstream.toByteArray();
140: assertEquals("stuff;more stuff", EncodingUtils
141: .getAsciiString(content));
142: }
143:
144: public void testInputBufferNullInput() throws IOException {
145: SimpleInputBuffer buffer = new SimpleInputBuffer(4,
146: new DirectByteBufferAllocator());
147: assertEquals(0, buffer.read(null));
148: assertEquals(0, buffer.read(null, 0, 0));
149: }
150:
151: public void testOutputBufferNullInput() throws IOException {
152: SimpleOutputBuffer buffer = new SimpleOutputBuffer(4,
153: new DirectByteBufferAllocator());
154: buffer.write(null);
155: buffer.write(null, 0, 10);
156: assertFalse(buffer.hasData());
157: }
158:
159: public void testDirectByteBufferAllocator() {
160: DirectByteBufferAllocator allocator = new DirectByteBufferAllocator();
161: ByteBuffer buffer = allocator.allocate(1);
162: assertNotNull(buffer);
163: assertTrue(buffer.isDirect());
164: assertEquals(0, buffer.position());
165: assertEquals(1, buffer.limit());
166: assertEquals(1, buffer.capacity());
167:
168: buffer = allocator.allocate(2048);
169: assertTrue(buffer.isDirect());
170: assertEquals(0, buffer.position());
171: assertEquals(2048, buffer.limit());
172: assertEquals(2048, buffer.capacity());
173:
174: buffer = allocator.allocate(0);
175: assertTrue(buffer.isDirect());
176: assertEquals(0, buffer.position());
177: assertEquals(0, buffer.limit());
178: assertEquals(0, buffer.capacity());
179: }
180:
181: public void testHeapByteBufferAllocator() {
182: HeapByteBufferAllocator allocator = new HeapByteBufferAllocator();
183: ByteBuffer buffer = allocator.allocate(1);
184: assertNotNull(buffer);
185: assertFalse(buffer.isDirect());
186: assertEquals(0, buffer.position());
187: assertEquals(1, buffer.limit());
188: assertEquals(1, buffer.capacity());
189:
190: buffer = allocator.allocate(2048);
191: assertFalse(buffer.isDirect());
192: assertEquals(0, buffer.position());
193: assertEquals(2048, buffer.limit());
194: assertEquals(2048, buffer.capacity());
195:
196: buffer = allocator.allocate(0);
197: assertFalse(buffer.isDirect());
198: assertEquals(0, buffer.position());
199: assertEquals(0, buffer.limit());
200: assertEquals(0, buffer.capacity());
201: }
202:
203: }
|