Source Code Cross Referenced for TestChunkCoding.java in  » Net » httpcomponents-core-4.0-beta1 » org » apache » http » impl » io » 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.io 
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/io/TestChunkCoding.java $
003:         * $Revision: 560358 $
004:         * $Date: 2007-07-27 21:30:42 +0200 (Fri, 27 Jul 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.impl.io;
032:
033:        import java.io.ByteArrayOutputStream;
034:        import java.io.IOException;
035:        import java.io.InputStream;
036:        import java.io.OutputStream;
037:
038:        import org.apache.http.Header;
039:        import org.apache.http.MalformedChunkCodingException;
040:        import org.apache.http.impl.io.ChunkedInputStream;
041:        import org.apache.http.impl.io.ChunkedOutputStream;
042:        import org.apache.http.io.SessionInputBuffer;
043:        import org.apache.http.mockup.SessionInputBufferMockup;
044:        import org.apache.http.mockup.SessionOutputBufferMockup;
045:        import org.apache.http.util.EncodingUtils;
046:
047:        import junit.framework.Test;
048:        import junit.framework.TestCase;
049:        import junit.framework.TestSuite;
050:
051:        public class TestChunkCoding extends TestCase {
052:
053:            private static final String CONTENT_CHARSET = "ISO-8859-1";
054:
055:            public TestChunkCoding(String testName) {
056:                super (testName);
057:            }
058:
059:            // ------------------------------------------------------- TestCase Methods
060:
061:            public static Test suite() {
062:                return new TestSuite(TestChunkCoding.class);
063:            }
064:
065:            // ------------------------------------------------------------------- Main
066:            public static void main(String args[]) {
067:                String[] testCaseName = { TestChunkCoding.class.getName() };
068:                junit.textui.TestRunner.main(testCaseName);
069:            }
070:
071:            public void testConstructors() throws Exception {
072:                try {
073:                    new ChunkedInputStream((SessionInputBuffer) null);
074:                    fail("IllegalArgumentException should have been thrown");
075:                } catch (IllegalArgumentException ex) {
076:                    // expected
077:                }
078:                new MalformedChunkCodingException();
079:                new MalformedChunkCodingException("");
080:            }
081:
082:            private final static String CHUNKED_INPUT = "10;key=\"value\"\r\n1234567890123456\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
083:
084:            private final static String CHUNKED_RESULT = "123456789012345612345";
085:
086:            // Test for when buffer is larger than chunk size
087:            public void testChunkedInputStreamLargeBuffer() throws IOException {
088:                ChunkedInputStream in = new ChunkedInputStream(
089:                        new SessionInputBufferMockup(EncodingUtils.getBytes(
090:                                CHUNKED_INPUT, CONTENT_CHARSET)));
091:                byte[] buffer = new byte[300];
092:                ByteArrayOutputStream out = new ByteArrayOutputStream();
093:                int len;
094:                while ((len = in.read(buffer)) > 0) {
095:                    out.write(buffer, 0, len);
096:                }
097:                assertEquals(-1, in.read(buffer));
098:                assertEquals(-1, in.read(buffer));
099:
100:                in.close();
101:
102:                String result = EncodingUtils.getString(out.toByteArray(),
103:                        CONTENT_CHARSET);
104:                assertEquals(result, CHUNKED_RESULT);
105:
106:                Header[] footers = in.getFooters();
107:                assertNotNull(footers);
108:                assertEquals(2, footers.length);
109:                assertEquals("Footer1", footers[0].getName());
110:                assertEquals("abcde", footers[0].getValue());
111:                assertEquals("Footer2", footers[1].getName());
112:                assertEquals("fghij", footers[1].getValue());
113:            }
114:
115:            //Test for when buffer is smaller than chunk size.
116:            public void testChunkedInputStreamSmallBuffer() throws IOException {
117:                ChunkedInputStream in = new ChunkedInputStream(
118:                        new SessionInputBufferMockup(EncodingUtils.getBytes(
119:                                CHUNKED_INPUT, CONTENT_CHARSET)));
120:
121:                byte[] buffer = new byte[7];
122:                ByteArrayOutputStream out = new ByteArrayOutputStream();
123:                int len;
124:                while ((len = in.read(buffer)) > 0) {
125:                    out.write(buffer, 0, len);
126:                }
127:                assertEquals(-1, in.read(buffer));
128:                assertEquals(-1, in.read(buffer));
129:
130:                in.close();
131:
132:                EncodingUtils.getString(out.toByteArray(), CONTENT_CHARSET);
133:                Header[] footers = in.getFooters();
134:                assertNotNull(footers);
135:                assertEquals(2, footers.length);
136:                assertEquals("Footer1", footers[0].getName());
137:                assertEquals("abcde", footers[0].getValue());
138:                assertEquals("Footer2", footers[1].getName());
139:                assertEquals("fghij", footers[1].getValue());
140:            }
141:
142:            // One byte read
143:            public void testChunkedInputStreamOneByteRead() throws IOException {
144:                String s = "5\r\n01234\r\n5\r\n56789\r\n0\r\n";
145:                ChunkedInputStream in = new ChunkedInputStream(
146:                        new SessionInputBufferMockup(EncodingUtils.getBytes(s,
147:                                CONTENT_CHARSET)));
148:                int ch;
149:                int i = '0';
150:                while ((ch = in.read()) != -1) {
151:                    assertEquals(i, ch);
152:                    i++;
153:                }
154:                assertEquals(-1, in.read());
155:                assertEquals(-1, in.read());
156:
157:                in.close();
158:            }
159:
160:            public void testChunkedInputStreamClose() throws IOException {
161:                String s = "5\r\n01234\r\n5\r\n56789\r\n0\r\n";
162:                ChunkedInputStream in = new ChunkedInputStream(
163:                        new SessionInputBufferMockup(EncodingUtils.getBytes(s,
164:                                CONTENT_CHARSET)));
165:                in.close();
166:                in.close();
167:                try {
168:                    in.read();
169:                    fail("IOException should have been thrown");
170:                } catch (IOException ex) {
171:                    // expected
172:                }
173:                byte[] tmp = new byte[10];
174:                try {
175:                    in.read(tmp);
176:                    fail("IOException should have been thrown");
177:                } catch (IOException ex) {
178:                    // expected
179:                }
180:                try {
181:                    in.read(tmp, 0, tmp.length);
182:                    fail("IOException should have been thrown");
183:                } catch (IOException ex) {
184:                    // expected
185:                }
186:            }
187:
188:            public void testChunkedOutputStreamClose() throws IOException {
189:                ChunkedOutputStream out = new ChunkedOutputStream(
190:                        new SessionOutputBufferMockup());
191:                out.close();
192:                out.close();
193:                try {
194:                    out.write(new byte[] { 1, 2, 3 });
195:                    fail("IOException should have been thrown");
196:                } catch (IOException ex) {
197:                    // expected
198:                }
199:                try {
200:                    out.write(1);
201:                    fail("IOException should have been thrown");
202:                } catch (IOException ex) {
203:                    // expected
204:                }
205:            }
206:
207:            // Missing \r\n at the end of the first chunk
208:            public void testCorruptChunkedInputStreamMissingCRLF()
209:                    throws IOException {
210:                String s = "5\r\n012345\r\n56789\r\n0\r\n";
211:                InputStream in = new ChunkedInputStream(
212:                        new SessionInputBufferMockup(EncodingUtils.getBytes(s,
213:                                CONTENT_CHARSET)));
214:                byte[] buffer = new byte[300];
215:                ByteArrayOutputStream out = new ByteArrayOutputStream();
216:                int len;
217:                try {
218:                    while ((len = in.read(buffer)) > 0) {
219:                        out.write(buffer, 0, len);
220:                    }
221:                    fail("MalformedChunkCodingException should have been thrown");
222:                } catch (MalformedChunkCodingException e) {
223:                    /* expected exception */
224:                }
225:            }
226:
227:            // Missing LF
228:            public void testCorruptChunkedInputStreamMissingLF()
229:                    throws IOException {
230:                String s = "5\r01234\r\n5\r\n56789\r\n0\r\n";
231:                InputStream in = new ChunkedInputStream(
232:                        new SessionInputBufferMockup(EncodingUtils.getBytes(s,
233:                                CONTENT_CHARSET)));
234:                try {
235:                    in.read();
236:                    fail("MalformedChunkCodingException should have been thrown");
237:                } catch (MalformedChunkCodingException e) {
238:                    /* expected exception */
239:                }
240:            }
241:
242:            // Missing closing chunk
243:            public void testCorruptChunkedInputStreamNoClosingChunk()
244:                    throws IOException {
245:                InputStream in = new ChunkedInputStream(
246:                        new SessionInputBufferMockup(new byte[] {}));
247:                try {
248:                    in.read();
249:                    fail("MalformedChunkCodingException should have been thrown");
250:                } catch (MalformedChunkCodingException e) {
251:                    /* expected exception */
252:                }
253:            }
254:
255:            // Invalid chunk size
256:            public void testCorruptChunkedInputStreamInvalidSize()
257:                    throws IOException {
258:                String s = "whatever\r\n01234\r\n5\r\n56789\r\n0\r\n";
259:                InputStream in = new ChunkedInputStream(
260:                        new SessionInputBufferMockup(EncodingUtils.getBytes(s,
261:                                CONTENT_CHARSET)));
262:                try {
263:                    in.read();
264:                    fail("MalformedChunkCodingException should have been thrown");
265:                } catch (MalformedChunkCodingException e) {
266:                    /* expected exception */
267:                }
268:            }
269:
270:            // Negative chunk size
271:            public void testCorruptChunkedInputStreamNegativeSize()
272:                    throws IOException {
273:                String s = "-5\r\n01234\r\n5\r\n56789\r\n0\r\n";
274:                InputStream in = new ChunkedInputStream(
275:                        new SessionInputBufferMockup(EncodingUtils.getBytes(s,
276:                                CONTENT_CHARSET)));
277:                try {
278:                    in.read();
279:                    fail("MalformedChunkCodingException should have been thrown");
280:                } catch (MalformedChunkCodingException e) {
281:                    /* expected exception */
282:                }
283:            }
284:
285:            // Invalid footer
286:            public void testCorruptChunkedInputStreamInvalidFooter()
287:                    throws IOException {
288:                String s = "1\r\n0\r\n0\r\nstuff\r\n";
289:                InputStream in = new ChunkedInputStream(
290:                        new SessionInputBufferMockup(EncodingUtils.getBytes(s,
291:                                CONTENT_CHARSET)));
292:                try {
293:                    in.read();
294:                    in.read();
295:                    fail("MalformedChunkCodingException should have been thrown");
296:                } catch (MalformedChunkCodingException e) {
297:                    /* expected exception */
298:                }
299:            }
300:
301:            public void testEmptyChunkedInputStream() throws IOException {
302:                String input = "0\r\n";
303:                InputStream in = new ChunkedInputStream(
304:                        new SessionInputBufferMockup(EncodingUtils.getBytes(
305:                                input, CONTENT_CHARSET)));
306:                byte[] buffer = new byte[300];
307:                ByteArrayOutputStream out = new ByteArrayOutputStream();
308:                int len;
309:                while ((len = in.read(buffer)) > 0) {
310:                    out.write(buffer, 0, len);
311:                }
312:                assertEquals(0, out.size());
313:            }
314:
315:            public void testChunkedConsitance() throws IOException {
316:                String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?\\suweb";
317:                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
318:                OutputStream out = new ChunkedOutputStream(
319:                        new SessionOutputBufferMockup(buffer));
320:                out.write(EncodingUtils.getBytes(input, CONTENT_CHARSET));
321:                out.flush();
322:                out.close();
323:                out.close();
324:                buffer.close();
325:                InputStream in = new ChunkedInputStream(
326:                        new SessionInputBufferMockup(buffer.toByteArray()));
327:
328:                byte[] d = new byte[10];
329:                ByteArrayOutputStream result = new ByteArrayOutputStream();
330:                int len = 0;
331:                while ((len = in.read(d)) > 0) {
332:                    result.write(d, 0, len);
333:                }
334:
335:                String output = EncodingUtils.getString(result.toByteArray(),
336:                        CONTENT_CHARSET);
337:                assertEquals(input, output);
338:            }
339:
340:            public void testChunkedOutputStream() throws IOException {
341:                SessionOutputBufferMockup buffer = new SessionOutputBufferMockup();
342:                ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
343:                out.write('1');
344:                out.write('2');
345:                out.write('3');
346:                out.write('4');
347:                out.finish();
348:                out.close();
349:
350:                byte[] rawdata = buffer.getData();
351:
352:                assertEquals(19, rawdata.length);
353:                assertEquals('2', rawdata[0]);
354:                assertEquals('\r', rawdata[1]);
355:                assertEquals('\n', rawdata[2]);
356:                assertEquals('1', rawdata[3]);
357:                assertEquals('2', rawdata[4]);
358:                assertEquals('\r', rawdata[5]);
359:                assertEquals('\n', rawdata[6]);
360:                assertEquals('2', rawdata[7]);
361:                assertEquals('\r', rawdata[8]);
362:                assertEquals('\n', rawdata[9]);
363:                assertEquals('3', rawdata[10]);
364:                assertEquals('4', rawdata[11]);
365:                assertEquals('\r', rawdata[12]);
366:                assertEquals('\n', rawdata[13]);
367:                assertEquals('0', rawdata[14]);
368:                assertEquals('\r', rawdata[15]);
369:                assertEquals('\n', rawdata[16]);
370:                assertEquals('\r', rawdata[17]);
371:                assertEquals('\n', rawdata[18]);
372:            }
373:
374:            public void testChunkedOutputStreamLargeChunk() throws IOException {
375:                SessionOutputBufferMockup buffer = new SessionOutputBufferMockup();
376:                ChunkedOutputStream out = new ChunkedOutputStream(buffer, 2);
377:                out.write(new byte[] { '1', '2', '3', '4' });
378:                out.finish();
379:                out.close();
380:
381:                byte[] rawdata = buffer.getData();
382:
383:                assertEquals(14, rawdata.length);
384:                assertEquals('4', rawdata[0]);
385:                assertEquals('\r', rawdata[1]);
386:                assertEquals('\n', rawdata[2]);
387:                assertEquals('1', rawdata[3]);
388:                assertEquals('2', rawdata[4]);
389:                assertEquals('3', rawdata[5]);
390:                assertEquals('4', rawdata[6]);
391:                assertEquals('\r', rawdata[7]);
392:                assertEquals('\n', rawdata[8]);
393:                assertEquals('0', rawdata[9]);
394:                assertEquals('\r', rawdata[10]);
395:                assertEquals('\n', rawdata[11]);
396:                assertEquals('\r', rawdata[12]);
397:                assertEquals('\n', rawdata[13]);
398:            }
399:
400:            public void testChunkedOutputStreamSmallChunk() throws IOException {
401:                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
402:                ChunkedOutputStream out = new ChunkedOutputStream(
403:                        new SessionOutputBufferMockup(buffer), 2);
404:                out.write('1');
405:                out.finish();
406:                out.close();
407:
408:                byte[] rawdata = buffer.toByteArray();
409:
410:                assertEquals(11, rawdata.length);
411:                assertEquals('1', rawdata[0]);
412:                assertEquals('\r', rawdata[1]);
413:                assertEquals('\n', rawdata[2]);
414:                assertEquals('1', rawdata[3]);
415:                assertEquals('\r', rawdata[4]);
416:                assertEquals('\n', rawdata[5]);
417:                assertEquals('0', rawdata[6]);
418:                assertEquals('\r', rawdata[7]);
419:                assertEquals('\n', rawdata[8]);
420:                assertEquals('\r', rawdata[9]);
421:                assertEquals('\n', rawdata[10]);
422:            }
423:
424:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.