Source Code Cross Referenced for TestSessionBuffers.java in  » Net » httpcomponents-core-4.0-beta1 » org » apache » http » impl » 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.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/impl/TestSessionBuffers.java $
003:         * $Revision: 610464 $
004:         * $Date: 2008-01-09 18:10:55 +0100 (Wed, 09 Jan 2008) $
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.impl;
032:
033:        import java.io.ByteArrayInputStream;
034:        import java.io.ByteArrayOutputStream;
035:        import java.io.IOException;
036:        import java.io.InputStream;
037:
038:        import junit.framework.Test;
039:        import junit.framework.TestCase;
040:        import junit.framework.TestSuite;
041:        import org.apache.http.io.HttpTransportMetrics;
042:
043:        import org.apache.http.params.BasicHttpParams;
044:        import org.apache.http.mockup.SessionInputBufferMockup;
045:        import org.apache.http.mockup.SessionOutputBufferMockup;
046:        import org.apache.http.params.CoreConnectionPNames;
047:        import org.apache.http.params.HttpParams;
048:        import org.apache.http.params.HttpProtocolParams;
049:        import org.apache.http.protocol.HTTP;
050:        import org.apache.http.util.CharArrayBuffer;
051:
052:        public class TestSessionBuffers extends TestCase {
053:
054:            public TestSessionBuffers(String testName) {
055:                super (testName);
056:            }
057:
058:            // ------------------------------------------------------- TestCase Methods
059:
060:            public static Test suite() {
061:                return new TestSuite(TestSessionBuffers.class);
062:            }
063:
064:            // ------------------------------------------------------------------- Main
065:            public static void main(String args[]) {
066:                String[] testCaseName = { TestSessionBuffers.class.getName() };
067:                junit.textui.TestRunner.main(testCaseName);
068:            }
069:
070:            public void testInit() throws Exception {
071:                ByteArrayOutputStream out = new ByteArrayOutputStream();
072:                new SessionOutputBufferMockup(out);
073:                try {
074:                    new SessionOutputBufferMockup(null, new BasicHttpParams());
075:                    fail("IllegalArgumentException should have been thrown");
076:                } catch (IllegalArgumentException ex) {
077:                    //expected
078:                }
079:                ByteArrayInputStream in = new ByteArrayInputStream(out
080:                        .toByteArray());
081:                new SessionInputBufferMockup(in, 10);
082:                try {
083:                    new SessionInputBufferMockup(in, -10);
084:                    fail("IllegalArgumentException should have been thrown");
085:                } catch (IllegalArgumentException ex) {
086:                    //expected
087:                }
088:                try {
089:                    new SessionOutputBufferMockup(out, -10);
090:                    fail("IllegalArgumentException should have been thrown");
091:                } catch (IllegalArgumentException ex) {
092:                    //expected
093:                }
094:                try {
095:                    new SessionInputBufferMockup((InputStream) null, 1024);
096:                    fail("IllegalArgumentException should have been thrown");
097:                } catch (IllegalArgumentException ex) {
098:                    //expected
099:                }
100:            }
101:
102:            public void testBasicReadWriteLine() throws Exception {
103:
104:                String[] teststrs = new String[5];
105:                teststrs[0] = "Hello";
106:                teststrs[1] = "This string should be much longer than the size of the output buffer "
107:                        + "which is only 16 bytes for this test";
108:                StringBuffer buffer = new StringBuffer();
109:                for (int i = 0; i < 15; i++) {
110:                    buffer.append("123456789 ");
111:                }
112:                buffer.append("and stuff like that");
113:                teststrs[2] = buffer.toString();
114:                teststrs[3] = "";
115:                teststrs[4] = "And goodbye";
116:
117:                CharArrayBuffer chbuffer = new CharArrayBuffer(16);
118:                SessionOutputBufferMockup outbuffer = new SessionOutputBufferMockup();
119:                for (int i = 0; i < teststrs.length; i++) {
120:                    chbuffer.clear();
121:                    chbuffer.append(teststrs[i]);
122:                    outbuffer.writeLine(chbuffer);
123:                }
124:                //these write operations should have no effect
125:                outbuffer.writeLine((String) null);
126:                outbuffer.writeLine((CharArrayBuffer) null);
127:                outbuffer.flush();
128:
129:                HttpTransportMetrics tmetrics = outbuffer.getMetrics();
130:                long writedBytes = tmetrics.getBytesTransferred();
131:                long expWrited = 0;
132:                for (int i = 0; i < teststrs.length; i++) {
133:                    expWrited += (teststrs[i].length() + 2/*CRLF*/);
134:                }
135:                assertEquals(expWrited, writedBytes);
136:
137:                SessionInputBufferMockup inbuffer = new SessionInputBufferMockup(
138:                        outbuffer.getData());
139:
140:                for (int i = 0; i < teststrs.length; i++) {
141:                    assertEquals(teststrs[i], inbuffer.readLine());
142:                }
143:
144:                assertNull(inbuffer.readLine());
145:                assertNull(inbuffer.readLine());
146:                tmetrics = inbuffer.getMetrics();
147:                long readedBytes = tmetrics.getBytesTransferred();
148:                assertEquals(expWrited, readedBytes);
149:            }
150:
151:            public void testComplexReadWriteLine() throws Exception {
152:                SessionOutputBufferMockup outbuffer = new SessionOutputBufferMockup();
153:                outbuffer.write(new byte[] { 'a', '\n' });
154:                outbuffer.write(new byte[] { '\r', '\n' });
155:                outbuffer.write(new byte[] { '\r', '\r', '\n' });
156:                outbuffer.write(new byte[] { '\n' });
157:                //these write operations should have no effect
158:                outbuffer.write(null);
159:                outbuffer.write(null, 0, 12);
160:
161:                outbuffer.flush();
162:
163:                long writedBytes = outbuffer.getMetrics().getBytesTransferred();
164:                assertEquals(8, writedBytes);
165:
166:                StringBuffer buffer = new StringBuffer();
167:                for (int i = 0; i < 14; i++) {
168:                    buffer.append("a");
169:                }
170:                String s1 = buffer.toString();
171:                buffer.append("\r\n");
172:                outbuffer.write(buffer.toString().getBytes("US-ASCII"));
173:                outbuffer.flush();
174:                writedBytes = outbuffer.getMetrics().getBytesTransferred();
175:                assertEquals(8 + 14 + 2, writedBytes);
176:
177:                buffer.setLength(0);
178:                for (int i = 0; i < 15; i++) {
179:                    buffer.append("a");
180:                }
181:                String s2 = buffer.toString();
182:                buffer.append("\r\n");
183:                outbuffer.write(buffer.toString().getBytes("US-ASCII"));
184:                outbuffer.flush();
185:                writedBytes = outbuffer.getMetrics().getBytesTransferred();
186:                assertEquals(8 + 14 + 2 + 15 + 2, writedBytes);
187:
188:                buffer.setLength(0);
189:                for (int i = 0; i < 16; i++) {
190:                    buffer.append("a");
191:                }
192:                String s3 = buffer.toString();
193:                buffer.append("\r\n");
194:                outbuffer.write(buffer.toString().getBytes("US-ASCII"));
195:                outbuffer.flush();
196:                writedBytes = outbuffer.getMetrics().getBytesTransferred();
197:                assertEquals(8 + 14 + 2 + 15 + 2 + 16 + 2, writedBytes);
198:
199:                outbuffer.write(new byte[] { 'a' });
200:                outbuffer.flush();
201:                writedBytes = outbuffer.getMetrics().getBytesTransferred();
202:                assertEquals(8 + 14 + 2 + 15 + 2 + 16 + 2 + 1, writedBytes);
203:
204:                SessionInputBufferMockup inbuffer = new SessionInputBufferMockup(
205:                        outbuffer.getData());
206:
207:                assertEquals("a", inbuffer.readLine());
208:                assertEquals("", inbuffer.readLine());
209:                assertEquals("\r", inbuffer.readLine());
210:                assertEquals("", inbuffer.readLine());
211:                assertEquals(s1, inbuffer.readLine());
212:                assertEquals(s2, inbuffer.readLine());
213:                assertEquals(s3, inbuffer.readLine());
214:                assertEquals("a", inbuffer.readLine());
215:                assertNull(inbuffer.readLine());
216:                assertNull(inbuffer.readLine());
217:                long received = inbuffer.getMetrics().getBytesTransferred();
218:                assertEquals(writedBytes, received);
219:            }
220:
221:            public void testBasicReadWriteLineLargeBuffer() throws Exception {
222:
223:                String[] teststrs = new String[5];
224:                teststrs[0] = "Hello";
225:                teststrs[1] = "This string should be much longer than the size of the output buffer "
226:                        + "which is only 16 bytes for this test";
227:                StringBuffer buffer = new StringBuffer();
228:                for (int i = 0; i < 15; i++) {
229:                    buffer.append("123456789 ");
230:                }
231:                buffer.append("and stuff like that");
232:                teststrs[2] = buffer.toString();
233:                teststrs[3] = "";
234:                teststrs[4] = "And goodbye";
235:
236:                CharArrayBuffer chbuffer = new CharArrayBuffer(16);
237:                SessionOutputBufferMockup outbuffer = new SessionOutputBufferMockup();
238:                for (int i = 0; i < teststrs.length; i++) {
239:                    chbuffer.clear();
240:                    chbuffer.append(teststrs[i]);
241:                    outbuffer.writeLine(chbuffer);
242:                }
243:                //these write operations should have no effect
244:                outbuffer.writeLine((String) null);
245:                outbuffer.writeLine((CharArrayBuffer) null);
246:                outbuffer.flush();
247:
248:                long writedBytes = outbuffer.getMetrics().getBytesTransferred();
249:                long expWrited = 0;
250:                for (int i = 0; i < teststrs.length; i++) {
251:                    expWrited += (teststrs[i].length() + 2/*CRLF*/);
252:                }
253:                assertEquals(expWrited, writedBytes);
254:
255:                SessionInputBufferMockup inbuffer = new SessionInputBufferMockup(
256:                        outbuffer.getData(), 1024);
257:
258:                for (int i = 0; i < teststrs.length; i++) {
259:                    assertEquals(teststrs[i], inbuffer.readLine());
260:                }
261:                assertNull(inbuffer.readLine());
262:                assertNull(inbuffer.readLine());
263:                long readedBytes = inbuffer.getMetrics().getBytesTransferred();
264:                assertEquals(expWrited, readedBytes);
265:            }
266:
267:            public void testReadWriteBytes() throws Exception {
268:                // make the buffer larger than that of outbuffer
269:                byte[] out = new byte[40];
270:                for (int i = 0; i < out.length; i++) {
271:                    out[i] = (byte) ('0' + i);
272:                }
273:                SessionOutputBufferMockup outbuffer = new SessionOutputBufferMockup();
274:                int off = 0;
275:                int remaining = out.length;
276:                while (remaining > 0) {
277:                    int chunk = 10;
278:                    if (chunk > remaining) {
279:                        chunk = remaining;
280:                    }
281:                    outbuffer.write(out, off, chunk);
282:                    off += chunk;
283:                    remaining -= chunk;
284:                }
285:                outbuffer.flush();
286:                long writedBytes = outbuffer.getMetrics().getBytesTransferred();
287:                assertEquals(out.length, writedBytes);
288:
289:                byte[] tmp = outbuffer.getData();
290:                assertEquals(out.length, tmp.length);
291:                for (int i = 0; i < out.length; i++) {
292:                    assertEquals(out[i], tmp[i]);
293:                }
294:
295:                SessionInputBufferMockup inbuffer = new SessionInputBufferMockup(
296:                        tmp);
297:
298:                // these read operations will have no effect
299:                assertEquals(0, inbuffer.read(null, 0, 10));
300:                assertEquals(0, inbuffer.read(null));
301:                long receivedBytes = inbuffer.getMetrics()
302:                        .getBytesTransferred();
303:                assertEquals(0, receivedBytes);
304:
305:                byte[] in = new byte[40];
306:                off = 0;
307:                remaining = in.length;
308:                while (remaining > 0) {
309:                    int chunk = 10;
310:                    if (chunk > remaining) {
311:                        chunk = remaining;
312:                    }
313:                    int l = inbuffer.read(in, off, chunk);
314:                    if (l == -1) {
315:                        break;
316:                    }
317:                    off += l;
318:                    remaining -= l;
319:                }
320:                for (int i = 0; i < out.length; i++) {
321:                    assertEquals(out[i], in[i]);
322:                }
323:                assertEquals(-1, inbuffer.read(tmp));
324:                assertEquals(-1, inbuffer.read(tmp));
325:                receivedBytes = inbuffer.getMetrics().getBytesTransferred();
326:                assertEquals(out.length, receivedBytes);
327:            }
328:
329:            public void testReadWriteByte() throws Exception {
330:                // make the buffer larger than that of outbuffer
331:                byte[] out = new byte[40];
332:                for (int i = 0; i < out.length; i++) {
333:                    out[i] = (byte) (120 + i);
334:                }
335:                SessionOutputBufferMockup outbuffer = new SessionOutputBufferMockup();
336:                for (int i = 0; i < out.length; i++) {
337:                    outbuffer.write(out[i]);
338:                }
339:                outbuffer.flush();
340:                long writedBytes = outbuffer.getMetrics().getBytesTransferred();
341:                assertEquals(out.length, writedBytes);
342:
343:                byte[] tmp = outbuffer.getData();
344:                assertEquals(out.length, tmp.length);
345:                for (int i = 0; i < out.length; i++) {
346:                    assertEquals(out[i], tmp[i]);
347:                }
348:
349:                SessionInputBufferMockup inbuffer = new SessionInputBufferMockup(
350:                        tmp);
351:                byte[] in = new byte[40];
352:                for (int i = 0; i < in.length; i++) {
353:                    in[i] = (byte) inbuffer.read();
354:                }
355:                for (int i = 0; i < out.length; i++) {
356:                    assertEquals(out[i], in[i]);
357:                }
358:                assertEquals(-1, inbuffer.read());
359:                assertEquals(-1, inbuffer.read());
360:                long readedBytes = inbuffer.getMetrics().getBytesTransferred();
361:                assertEquals(out.length, readedBytes);
362:            }
363:
364:            public void testLineLimit() throws Exception {
365:                HttpParams params = new BasicHttpParams();
366:                String s = "a very looooooooooooooooooooooooooooooooooooooong line\r\n     ";
367:                byte[] tmp = s.getBytes("US-ASCII");
368:                // no limit
369:                params.setIntParameter(CoreConnectionPNames.MAX_LINE_LENGTH, 0);
370:                SessionInputBufferMockup inbuffer1 = new SessionInputBufferMockup(
371:                        tmp, 5, params);
372:                assertNotNull(inbuffer1.readLine());
373:                long readedBytes = inbuffer1.getMetrics().getBytesTransferred();
374:                assertEquals(60, readedBytes);
375:
376:                // 15 char limit
377:                params
378:                        .setIntParameter(CoreConnectionPNames.MAX_LINE_LENGTH,
379:                                15);
380:                SessionInputBufferMockup inbuffer2 = new SessionInputBufferMockup(
381:                        tmp, 5, params);
382:                try {
383:                    inbuffer2.readLine();
384:                    fail("IOException should have been thrown");
385:                } catch (IOException ex) {
386:                    // expected
387:                    readedBytes = inbuffer2.getMetrics().getBytesTransferred();
388:                    assertEquals(20, readedBytes);
389:                }
390:            }
391:
392:            static final int SWISS_GERMAN_HELLO[] = { 0x47, 0x72, 0xFC, 0x65,
393:                    0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 };
394:
395:            static final int RUSSIAN_HELLO[] = { 0x412, 0x441, 0x435, 0x43C,
396:                    0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 };
397:
398:            private static String constructString(int[] unicodeChars) {
399:                StringBuffer buffer = new StringBuffer();
400:                if (unicodeChars != null) {
401:                    for (int i = 0; i < unicodeChars.length; i++) {
402:                        buffer.append((char) unicodeChars[i]);
403:                    }
404:                }
405:                return buffer.toString();
406:            }
407:
408:            public void testMultibyteCodedReadWriteLine() throws Exception {
409:                String s1 = constructString(SWISS_GERMAN_HELLO);
410:                String s2 = constructString(RUSSIAN_HELLO);
411:                String s3 = "Like hello and stuff";
412:
413:                HttpParams params = new BasicHttpParams();
414:                HttpProtocolParams.setHttpElementCharset(params, "UTF-8");
415:
416:                SessionOutputBufferMockup outbuffer = new SessionOutputBufferMockup(
417:                        params);
418:
419:                CharArrayBuffer chbuffer = new CharArrayBuffer(16);
420:                for (int i = 0; i < 10; i++) {
421:                    chbuffer.clear();
422:                    chbuffer.append(s1);
423:                    outbuffer.writeLine(chbuffer);
424:                    chbuffer.clear();
425:                    chbuffer.append(s2);
426:                    outbuffer.writeLine(chbuffer);
427:                    chbuffer.clear();
428:                    chbuffer.append(s3);
429:                    outbuffer.writeLine(chbuffer);
430:                }
431:                outbuffer.flush();
432:                long writedBytes = outbuffer.getMetrics().getBytesTransferred();
433:                long expBytes = ((s1.toString().getBytes("UTF-8").length + 2)
434:                        + (s2.toString().getBytes("UTF-8").length + 2) + (s3
435:                        .toString().getBytes("UTF-8").length + 2)) * 10;
436:                assertEquals(expBytes, writedBytes);
437:
438:                SessionInputBufferMockup inbuffer = new SessionInputBufferMockup(
439:                        outbuffer.getData(), params);
440:
441:                for (int i = 0; i < 10; i++) {
442:                    assertEquals(s1, inbuffer.readLine());
443:                    assertEquals(s2, inbuffer.readLine());
444:                    assertEquals(s3, inbuffer.readLine());
445:                }
446:                assertNull(inbuffer.readLine());
447:                assertNull(inbuffer.readLine());
448:                long readedBytes = inbuffer.getMetrics().getBytesTransferred();
449:                assertEquals(expBytes, readedBytes);
450:            }
451:
452:            public void testNonAsciiReadWriteLine() throws Exception {
453:                String s1 = constructString(SWISS_GERMAN_HELLO);
454:
455:                HttpParams params = new BasicHttpParams();
456:                HttpProtocolParams.setHttpElementCharset(params,
457:                        HTTP.ISO_8859_1);
458:
459:                SessionOutputBufferMockup outbuffer = new SessionOutputBufferMockup(
460:                        params);
461:
462:                CharArrayBuffer chbuffer = new CharArrayBuffer(16);
463:                for (int i = 0; i < 10; i++) {
464:                    chbuffer.clear();
465:                    chbuffer.append(s1);
466:                    outbuffer.writeLine(chbuffer);
467:                }
468:                outbuffer.flush();
469:                long writedBytes = outbuffer.getMetrics().getBytesTransferred();
470:                long expBytes = ((s1.toString().getBytes(HTTP.ISO_8859_1).length + 2)) * 10;
471:                assertEquals(expBytes, writedBytes);
472:
473:                SessionInputBufferMockup inbuffer = new SessionInputBufferMockup(
474:                        outbuffer.getData(), params);
475:                HttpProtocolParams.setHttpElementCharset(params,
476:                        HTTP.ISO_8859_1);
477:
478:                for (int i = 0; i < 10; i++) {
479:                    assertEquals(s1, inbuffer.readLine());
480:                }
481:                assertNull(inbuffer.readLine());
482:                assertNull(inbuffer.readLine());
483:                long readedBytes = inbuffer.getMetrics().getBytesTransferred();
484:                assertEquals(expBytes, readedBytes);
485:            }
486:
487:            public void testInvalidCharArrayBuffer() throws Exception {
488:                SessionInputBufferMockup inbuffer = new SessionInputBufferMockup(
489:                        new byte[] {});
490:                try {
491:                    inbuffer.readLine(null);
492:                    fail("IllegalArgumentException should have been thrown");
493:                } catch (IllegalArgumentException ex) {
494:                    //expected
495:                    long readedBytes = inbuffer.getMetrics()
496:                            .getBytesTransferred();
497:                    assertEquals(0, readedBytes);
498:                }
499:            }
500:
501:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.