Source Code Cross Referenced for TestBuffers.java in  » Net » httpcomponents-core-4.0-beta1 » org » apache » http » nio » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » httpcomponents core 4.0 beta1 » org.apache.http.nio.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.