KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > upload > MultipartBoundaryInputStreamTest


1 /*
2  * \$Header\$
3  * \$Rev: 54929 $
4  * \$Date\$
5  *
6  * Copyright 2004 The Apache Software Foundation.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20 package org.apache.struts.upload;
21
22 import junit.framework.TestCase;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Arrays JavaDoc;
28
29 /**
30  * This is a unit test for the MultipartBoundaryInputStream class.
31  */

32 public class MultipartBoundaryInputStreamTest extends TestCase
33 {
34     public static final String JavaDoc DISPOSITION_FORM_DATA = "form-data";
35
36     public static final String JavaDoc CONTENT_TYPE_TEXT_PLAIN = "text/plain";
37
38     /**
39      * The resource with a single multipart element with a single final boundary.
40      */

41     public static final String JavaDoc RESOURCE_SINGLE_TEXT_ELEMENT = "/org/apache/struts/upload/resources/multipart-single-text-element.txt";
42
43     /**
44      * The resource with two multipart elements with two boundaries.
45      */

46     public static final String JavaDoc RESOURCE_TWO_TEXT_ELEMENT = "/org/apache/struts/upload/resources/multipart-two-text-element.txt";
47
48     /**
49      * The resource with three multipart elements, two text one file.
50      */

51     public static final String JavaDoc RESOURCE_THREE_MIXED_ELEMENT = "/org/apache/struts/upload/resources/multipart-three-mixed-element.txt";
52
53     /**
54      * The resource with three multipart elements, the first with alot of newlines.
55      */

56     public static final String JavaDoc RESOURCE_THREE_MIXED_NEWLINES = "/org/apache/struts/upload/resources/multipart-three-mixed-newline.txt";
57
58     /**
59      * The resource with a problem.
60      */

61     public static final String JavaDoc RESOURCE_PROBLEM1 = "/org/apache/struts/upload/resources/multipart-problem1.txt";
62
63     public static final String JavaDoc RESOURCE_PROBLEM2 = "/org/apache/struts/upload/resources/multipart-problem2.txt";
64
65     public static final String JavaDoc RESOURCE_PROBLEM3 = "/org/apache/struts/upload/resources/multipart-problem3.txt";
66
67
68
69     /**
70      * The resource with file data of 5400 bytes.
71      */

72     public static final String JavaDoc RESOURCE_5400B = "/org/apache/struts/upload/resources/multipart-file-element-5400b.txt";
73
74     /**
75      * The resource to compare the newline data with.
76      */

77     public static final String JavaDoc RESOURCE_NEWLINES = "/org/apache/struts/upload/resources/multipart-file-element-newlines.txt";
78
79     /**
80      * The resource that is the value of problem1.
81      */

82     public static final String JavaDoc RESOURCE_DESIRED_PROBLEM1 = "/org/apache/struts/upload/resources/multipart-file-element-problem1.txt";
83
84     public static final String JavaDoc RESOURCE_DESIRED_PROBLEM2 = "/org/apache/struts/upload/resources/multipart-file-element-problem2.txt";
85
86     public static final String JavaDoc RESOURCE_DESIRED_PROBLEM3 = "/org/apache/struts/upload/resources/multipart-file-element-problem3.txt";
87
88     /**
89      * The boundary for the single multipart element resource.
90      */

91     public static final String JavaDoc BOUNDARY_SINGLE_TEXT_ELEMENT = "x9F38FJJ";
92
93     /**
94      * The boundary for the two multipart element resource.
95      */

96     public static final String JavaDoc BOUNDARY_TWO_TEXT_ELEMENT = "----------x9F38FJJ339502";
97
98     /**
99      * The boundary for problem1.
100      */

101     public static final String JavaDoc BOUNDARY_PROBLEM1 = "---------------------------63341846741";
102
103     public static final String JavaDoc BOUNDARY_PROBLEM2 = "---------------------------7d21fed3c0352";
104
105     public static final String JavaDoc BOUNDARY_PROBLEM3 = "---------------------------7d21cb2b102cc";
106
107     /**
108      * The boundary for the three multipart element resource.
109      */

110     public static final String JavaDoc BOUNDARY_THREE_TEXT_ELEMENT = "----------g320nnjJJF32AA666FF";
111
112     public static final String JavaDoc NAME_SINGLE_TEXT_ELEMENT = "testData";
113
114     public static final String JavaDoc NAME_TWO_TEXT_ELEMENT_1 = "testData1";
115
116     public static final String JavaDoc NAME_TWO_TEXT_ELEMENT_2 = "testData2";
117
118     public static final String JavaDoc NAME_THREE_MIXED_ELEMENT_1 = "testFile1";
119
120     public static final String JavaDoc NAME_THREE_MIXED_ELEMENT_2 = "testData1";
121
122     public static final String JavaDoc NAME_THREE_MIXED_ELEMENT_3 = "testData2";
123
124     public static final String JavaDoc NAME_PROBLEM1_1 = "theText";
125
126     public static final String JavaDoc NAME_PROBLEM1_2 = "theFile";
127
128     public static final String JavaDoc NAME_PROBLEM1_3 = "writeFile";
129
130     public static final String JavaDoc NAME_PROBLEM1_4 = "filePath";
131
132     public static final String JavaDoc NAME_PROBLEM1_5 = "submit";
133
134     /**
135      * The expected value of the element.
136      */

137     public static final String JavaDoc DESIRED_SINGLE_TEXT_ELEMENT = "this is test data";
138
139     /**
140      * The expected value of the element.
141      */

142     public static final String JavaDoc DESIRED_TEST_DATA_1 = "this is test data 1";
143
144     /**
145      * The expected value of the element.
146      */

147     public static final String JavaDoc DESIRED_TEST_DATA_2 = "this is test data 2";
148
149     /**
150      * The InputStream to single test data.
151      */

152     protected InputStream JavaDoc singleTextElementDataStream;
153
154     /**
155      * The InputStream to test two element data.
156      */

157     protected InputStream JavaDoc twoTextElementDataStream;
158
159     /**
160      * The InputStream to test three element mixed data.
161      */

162     protected InputStream JavaDoc threeMixedElementDataStream;
163
164     /**
165      * The InputStream to the multipart request with all the newlines.
166      */

167     protected InputStream JavaDoc newlinesDataStream;
168
169     /**
170      * The InputStream to the 5400 byte test file.
171      */

172     protected InputStream JavaDoc testFile5400bDataStream;
173
174     /**
175      * The InputStream to the problem1 test file.
176      */

177     protected InputStream JavaDoc testFileProblem1;
178
179     protected InputStream JavaDoc testFileProblem2;
180
181     protected InputStream JavaDoc testFileProblem3;
182
183     /**
184      * The InputStream to the file with alot of newlines.
185      */

186     protected InputStream JavaDoc testFileNewlines;
187
188     /**
189      * The InputStream to the file with a problem.
190      */

191     protected InputStream JavaDoc testProblem1;
192
193     protected InputStream JavaDoc testProblem2;
194
195     protected InputStream JavaDoc testProblem3;
196
197     /**
198      *
199      * // \\
200      * (( ''
201      * \\__,
202      * /6 (%)\,
203      * (__/:";,;\--____----_
204      * ;; :';,:';`;,';,;';`,`_
205      * ;:,;;';';,;':,';';,-Y\
206      * ;,;,;';';,;':;';'; Z/
207      * / ;,';';,;';,;';;'
208      * / / |';/~~~~~\';;'
209      * ( K | | || |
210      * \_\ | | || |
211      * \Z | | || |
212      * L_| LL_|
213      * LW/ LLW/
214      */

215     public MultipartBoundaryInputStreamTest(String JavaDoc name)
216     {
217         super(name);
218     }
219
220     protected void setUp() throws Exception JavaDoc
221     {
222         super.setUp();
223
224         this.singleTextElementDataStream = this.getClass().getResourceAsStream(RESOURCE_SINGLE_TEXT_ELEMENT);
225         if (this.singleTextElementDataStream == null)
226         {
227             fail("Couldn't obtain resource " + RESOURCE_SINGLE_TEXT_ELEMENT);
228         }
229         this.twoTextElementDataStream = this.getClass().getResourceAsStream(RESOURCE_TWO_TEXT_ELEMENT);
230         if (this.twoTextElementDataStream == null)
231         {
232             fail("Couldn't obtain resource " + RESOURCE_TWO_TEXT_ELEMENT);
233         }
234         this.threeMixedElementDataStream = this.getClass().getResourceAsStream(RESOURCE_THREE_MIXED_ELEMENT);
235         if (this.threeMixedElementDataStream == null)
236         {
237             fail("Couldn't obtain resource " + RESOURCE_THREE_MIXED_ELEMENT);
238         }
239         this.newlinesDataStream = this.getClass().getResourceAsStream(RESOURCE_THREE_MIXED_NEWLINES);
240         if (this.newlinesDataStream == null)
241         {
242             fail("Couldn't obtain resource " + RESOURCE_THREE_MIXED_NEWLINES);
243         }
244         this.testFile5400bDataStream = this.getClass().getResourceAsStream(RESOURCE_5400B);
245         if (this.testFile5400bDataStream == null)
246         {
247             fail("Couldn't obtain resource " + RESOURCE_5400B);
248         }
249         this.testFileNewlines = this.getClass().getResourceAsStream(RESOURCE_NEWLINES);
250         if (this.testFileNewlines == null)
251         {
252             fail("Couldn't obtain resource " + RESOURCE_NEWLINES);
253         }
254         this.testFileProblem1 = this.getClass().getResourceAsStream(RESOURCE_DESIRED_PROBLEM1);
255         if (this.testFileProblem1 == null)
256         {
257             fail("Couldn't obtain resource " + RESOURCE_DESIRED_PROBLEM1);
258         }
259         this.testFileProblem2 = this.getClass().getResourceAsStream(RESOURCE_DESIRED_PROBLEM2);
260         if (this.testFileProblem2 == null)
261         {
262             fail("Couldn't obtain resource " + RESOURCE_DESIRED_PROBLEM2);
263         }
264         this.testFileProblem3 = this.getClass().getResourceAsStream(RESOURCE_DESIRED_PROBLEM3);
265         if (this.testFileProblem3 == null)
266         {
267             fail("Couldn't obtain resource " + RESOURCE_DESIRED_PROBLEM3);
268         }
269         this.testProblem1 = this.getClass().getResourceAsStream(RESOURCE_PROBLEM1);
270         if (this.testProblem1 == null)
271         {
272             fail("Couldn't obtain resource " + RESOURCE_PROBLEM1);
273         }
274         this.testProblem2 = this.getClass().getResourceAsStream(RESOURCE_PROBLEM2);
275         if (this.testProblem2 == null)
276         {
277             fail("Couldn't obtain resource " + RESOURCE_PROBLEM2);
278         }
279         this.testProblem3 = this.getClass().getResourceAsStream(RESOURCE_PROBLEM3);
280         if (this.testProblem3 == null)
281         {
282             fail("Couldn't obtain resource " + RESOURCE_PROBLEM3);
283         }
284     }
285
286     protected void tearDown() throws Exception JavaDoc
287     {
288         super.tearDown();
289
290         this.singleTextElementDataStream = null;
291         this.twoTextElementDataStream = null;
292         this.threeMixedElementDataStream = null;
293         this.testFile5400bDataStream = null;
294         this.testFileNewlines = null;
295         this.testFileProblem1 = null;
296         this.testProblem1 = null;
297         this.testFileProblem2 = null;
298         this.testProblem2 = null;
299     }
300
301     public void testSingleTextElementData() throws Exception JavaDoc
302     {
303         MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream();
304         inputStream.setBoundary(BOUNDARY_SINGLE_TEXT_ELEMENT);
305         inputStream.setInputStream(this.singleTextElementDataStream);
306         //get only element
307
String JavaDoc resultData = streamToString(inputStream);
308         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
309         assertEquals(NAME_SINGLE_TEXT_ELEMENT, inputStream.getElementName());
310         assertTrue(!inputStream.isElementFile());
311         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
312         assertEquals(DESIRED_SINGLE_TEXT_ELEMENT, resultData);
313         assertTrue(inputStream.isBoundaryEncountered());
314         assertTrue(inputStream.isFinalBoundaryEncountered());
315         assertTrue(inputStream.isEndOfStream());
316     }
317
318     public void testTwoTextElementData() throws Exception JavaDoc
319     {
320         MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream();
321         inputStream.setBoundary(BOUNDARY_TWO_TEXT_ELEMENT);
322         inputStream.setInputStream(this.twoTextElementDataStream);
323
324         //get first element
325
String JavaDoc resultData = streamToString(inputStream);
326         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
327         assertEquals(NAME_TWO_TEXT_ELEMENT_1, inputStream.getElementName());
328         assertTrue(!inputStream.isElementFile());
329         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
330         assertTrue(inputStream.isBoundaryEncountered());
331         assertEquals(DESIRED_TEST_DATA_1, resultData);
332
333         //get second element
334
inputStream.resetForNextBoundary();
335         resultData = streamToString(inputStream);
336         assertEquals(DESIRED_TEST_DATA_2, resultData);
337         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
338         assertEquals(NAME_TWO_TEXT_ELEMENT_2, inputStream.getElementName());
339         assertEquals("utf-8", inputStream.getElementCharset());
340         assertTrue(!inputStream.isElementFile());
341         assertEquals("text/html", inputStream.getElementContentType());
342         assertTrue(inputStream.isBoundaryEncountered());
343         assertTrue(inputStream.isFinalBoundaryEncountered());
344         assertTrue(inputStream.isEndOfStream());
345     }
346
347     public void testThreeMixedElementData() throws Exception JavaDoc
348     {
349         MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream();
350         inputStream.setBoundary(BOUNDARY_THREE_TEXT_ELEMENT);
351         inputStream.setInputStream(this.threeMixedElementDataStream);
352
353         //get first element
354
assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
355         assertEquals(NAME_THREE_MIXED_ELEMENT_1, inputStream.getElementName());
356         assertTrue(inputStream.isElementFile());
357         assertEquals("text/html", inputStream.getElementContentType());
358         assertTrue(!inputStream.isBoundaryEncountered());
359         //read the element
360
String JavaDoc resultData = streamToString(inputStream);
361         assertTrue(inputStream.isBoundaryEncountered());
362         String JavaDoc desiredData = streamToString(this.testFile5400bDataStream);
363         assertEquals(desiredData, resultData);
364
365         //get second element
366
inputStream.resetForNextBoundary();
367         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
368         assertEquals(NAME_THREE_MIXED_ELEMENT_2, inputStream.getElementName());
369         assertTrue(!inputStream.isElementFile());
370         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
371         //read the element
372
resultData = streamToString(inputStream);
373         assertTrue(inputStream.isBoundaryEncountered());
374         assertEquals(DESIRED_TEST_DATA_1, resultData);
375
376         //get third element
377
inputStream.resetForNextBoundary();
378         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
379         assertEquals(NAME_THREE_MIXED_ELEMENT_3, inputStream.getElementName());
380         assertTrue(!inputStream.isElementFile());
381         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
382         //read the element
383
resultData = streamToString(inputStream);
384         assertTrue(inputStream.isBoundaryEncountered());
385         assertEquals(DESIRED_TEST_DATA_2, resultData);
386         //make sure the stream knows it's done
387
assertTrue(inputStream.isFinalBoundaryEncountered());
388         assertTrue(inputStream.isEndOfStream());
389     }
390
391     /**
392      * Tests a file filled with many, many newlines. A veritable plethora of newlines.
393      */

394     public void testManyNewlines() throws Exception JavaDoc
395     {
396         MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream();
397         inputStream.setBoundary(BOUNDARY_THREE_TEXT_ELEMENT);
398         inputStream.setInputStream(this.newlinesDataStream);
399
400         //get first element
401
assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
402         assertEquals(NAME_THREE_MIXED_ELEMENT_1, inputStream.getElementName());
403         assertTrue(inputStream.isElementFile());
404         assertEquals("text/html", inputStream.getElementContentType());
405         //read the element
406
String JavaDoc resultData = streamToString(inputStream);
407         assertTrue(inputStream.isBoundaryEncountered());
408         String JavaDoc desiredData = streamToString(this.testFileNewlines);
409         assertEquals(desiredData, resultData);
410
411         //get second element
412
inputStream.resetForNextBoundary();
413         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
414         assertEquals(NAME_THREE_MIXED_ELEMENT_2, inputStream.getElementName());
415         assertTrue(!inputStream.isElementFile());
416         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
417         //read the element
418
resultData = streamToString(inputStream);
419         assertTrue(inputStream.isBoundaryEncountered());
420         assertEquals(DESIRED_TEST_DATA_1, resultData);
421
422         //get third element
423
inputStream.resetForNextBoundary();
424         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
425         assertEquals(NAME_THREE_MIXED_ELEMENT_3, inputStream.getElementName());
426         assertTrue(!inputStream.isElementFile());
427         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
428         //read the element
429
resultData = streamToString(inputStream);
430         assertTrue(inputStream.isBoundaryEncountered());
431         assertEquals(DESIRED_TEST_DATA_2, resultData);
432         //make sure the stream knows it's done
433
assertTrue(inputStream.isFinalBoundaryEncountered());
434         assertTrue(inputStream.isEndOfStream());
435     }
436
437     /**
438      * This file caused a problem in the past. This test is here to make sure it doesn't happen again.
439      */

440     public void testProblemFile1() throws Exception JavaDoc
441     {
442         MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream();
443         inputStream.setBoundary(BOUNDARY_PROBLEM1);
444         inputStream.setInputStream(this.testProblem1);
445
446         //get the first element
447
assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
448         assertEquals(NAME_PROBLEM1_1, inputStream.getElementName());
449         assertTrue(!inputStream.isElementFile());
450         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
451         //read the element
452
String JavaDoc resultData = streamToString(inputStream);
453         assertTrue(inputStream.isBoundaryEncountered());
454         assertEquals("test", resultData);
455
456         //get second element
457
inputStream.resetForNextBoundary();
458         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
459         assertEquals(NAME_PROBLEM1_2, inputStream.getElementName());
460         assertTrue(inputStream.isElementFile());
461         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
462         //read the element
463
resultData = streamToString(inputStream);
464         String JavaDoc desiredData = streamToString(this.testFileProblem1);
465         assertTrue(inputStream.isBoundaryEncountered());
466         assertEquals(desiredData, resultData);
467
468         //get third element
469
inputStream.resetForNextBoundary();
470         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
471         assertEquals(NAME_PROBLEM1_3, inputStream.getElementName());
472         assertTrue(!inputStream.isElementFile());
473         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
474         //read the element
475
resultData = streamToString(inputStream);
476         assertTrue(inputStream.isBoundaryEncountered());
477         assertEquals("on", resultData);
478
479         //get fourth element
480
inputStream.resetForNextBoundary();
481         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
482         assertEquals(NAME_PROBLEM1_4, inputStream.getElementName());
483         assertTrue(!inputStream.isElementFile());
484         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
485         //read the element
486
resultData = streamToString(inputStream);
487         assertTrue(inputStream.isBoundaryEncountered());
488         assertEquals("c:\\multipart-test.txt", resultData);
489
490         //get last element
491
inputStream.resetForNextBoundary();
492         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
493         assertEquals(NAME_PROBLEM1_5, inputStream.getElementName());
494         assertTrue(!inputStream.isElementFile());
495         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
496         //read the element
497
resultData = streamToString(inputStream);
498         assertTrue(inputStream.isBoundaryEncountered());
499         assertEquals("Submit", resultData);
500         assertTrue(inputStream.isFinalBoundaryEncountered());
501         assertTrue(inputStream.isEndOfStream());
502     }
503
504     public void testProblemFile2() throws Exception JavaDoc
505     {
506         MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream();
507         inputStream.setBoundary(BOUNDARY_PROBLEM2);
508         inputStream.setInputStream(this.testProblem2);
509
510         //get the first element
511
assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
512         assertEquals(NAME_PROBLEM1_1, inputStream.getElementName());
513         assertTrue(!inputStream.isElementFile());
514         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
515         //read the element
516
String JavaDoc resultData = streamToString(inputStream);
517         assertTrue(inputStream.isBoundaryEncountered());
518         assertEquals("test", resultData);
519
520         //get second element
521
inputStream.resetForNextBoundary();
522         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
523         assertEquals(NAME_PROBLEM1_2, inputStream.getElementName());
524         assertEquals("hist202.doc", inputStream.getElementFileName());
525         assertTrue(inputStream.isElementFile());
526         assertEquals("application/msword", inputStream.getElementContentType());
527         //read the element
528
assertEquals(this.testFileProblem2, inputStream);
529         assertTrue(inputStream.isBoundaryEncountered());
530
531         //get third element
532
inputStream.resetForNextBoundary();
533         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
534         assertEquals(NAME_PROBLEM1_3, inputStream.getElementName());
535         assertTrue(!inputStream.isElementFile());
536         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
537         //read the element
538
resultData = streamToString(inputStream);
539         assertTrue(inputStream.isBoundaryEncountered());
540         assertEquals("on", resultData);
541
542         //get fourth element
543
inputStream.resetForNextBoundary();
544         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
545         assertEquals(NAME_PROBLEM1_4, inputStream.getElementName());
546         assertTrue(!inputStream.isElementFile());
547         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
548         //read the element
549
resultData = streamToString(inputStream);
550         assertTrue(inputStream.isBoundaryEncountered());
551         assertEquals("c:\\multipart-test.txt", resultData);
552
553         //get last element
554
inputStream.resetForNextBoundary();
555         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
556         assertEquals(NAME_PROBLEM1_5, inputStream.getElementName());
557         assertTrue(!inputStream.isElementFile());
558         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
559         //read the element
560
resultData = streamToString(inputStream);
561         assertTrue(inputStream.isBoundaryEncountered());
562         assertEquals("Submit", resultData);
563         assertTrue(inputStream.isFinalBoundaryEncountered());
564         assertTrue(inputStream.isEndOfStream());
565     }
566
567     public void testProblemFile3() throws Exception JavaDoc
568     {
569         MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream();
570         inputStream.setBoundary(BOUNDARY_PROBLEM3);
571         inputStream.setInputStream(this.testProblem3);
572
573         //get the first element
574
assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
575         assertEquals(NAME_PROBLEM1_1, inputStream.getElementName());
576         assertTrue(!inputStream.isElementFile());
577         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
578         //read the element
579
String JavaDoc resultData = streamToString(inputStream);
580         assertTrue(inputStream.isBoundaryEncountered());
581         assertEquals("test", resultData);
582
583         //get second element
584
inputStream.resetForNextBoundary();
585         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
586         assertEquals(NAME_PROBLEM1_2, inputStream.getElementName());
587         assertEquals("testmp3.mp3", inputStream.getElementFileName());
588         assertTrue(inputStream.isElementFile());
589         assertEquals("audio/mpeg", inputStream.getElementContentType());
590         //read the element
591
assertEquals(this.testFileProblem3, inputStream);
592         assertTrue(inputStream.isBoundaryEncountered());
593
594         //get third element
595
inputStream.resetForNextBoundary();
596         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
597         assertEquals(NAME_PROBLEM1_3, inputStream.getElementName());
598         assertTrue(!inputStream.isElementFile());
599         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
600         //read the element
601
resultData = streamToString(inputStream);
602         assertTrue(inputStream.isBoundaryEncountered());
603         assertEquals("on", resultData);
604
605         //get fourth element
606
inputStream.resetForNextBoundary();
607         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
608         assertEquals(NAME_PROBLEM1_4, inputStream.getElementName());
609         assertTrue(!inputStream.isElementFile());
610         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
611         //read the element
612
resultData = streamToString(inputStream);
613         assertTrue(inputStream.isBoundaryEncountered());
614         assertEquals("c:\\multipart-test7.mp3", resultData);
615
616         //get last element
617
inputStream.resetForNextBoundary();
618         assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
619         assertEquals(NAME_PROBLEM1_5, inputStream.getElementName());
620         assertTrue(!inputStream.isElementFile());
621         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
622         //read the element
623
resultData = streamToString(inputStream);
624         assertTrue(inputStream.isBoundaryEncountered());
625         assertEquals("Submit", resultData);
626         assertTrue(inputStream.isFinalBoundaryEncountered());
627         assertTrue(inputStream.isEndOfStream());
628     }
629
630     public void testMaxLengthExceeded() throws Exception JavaDoc
631     {
632         MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream();
633         inputStream.setMaxLength(1500);
634         inputStream.setBoundary(BOUNDARY_PROBLEM3);
635         inputStream.setInputStream(this.testProblem3);
636
637         //get the first element
638
assertEquals(DISPOSITION_FORM_DATA, inputStream.getElementContentDisposition());
639         assertEquals(NAME_PROBLEM1_1, inputStream.getElementName());
640         assertTrue(!inputStream.isElementFile());
641         assertEquals(CONTENT_TYPE_TEXT_PLAIN, inputStream.getElementContentType());
642         //read the element
643
String JavaDoc resultData = streamToString(inputStream);
644         assertTrue(inputStream.isBoundaryEncountered());
645         assertEquals("test", resultData);
646
647         //get second element
648
inputStream.resetForNextBoundary();
649         byte[] result = toByteArray(inputStream);
650         assertTrue(inputStream.isMaxLengthMet());
651         assertTrue(inputStream.getBytesRead() == 1500);
652         assertTrue(result.length < 1500);
653     }
654
655     public static void assertEquals(InputStream JavaDoc desired, InputStream JavaDoc actual) throws Exception JavaDoc
656     {
657         byte[] desiredBytes = toByteArray(desired);
658         byte[] actualBytes = toByteArray(actual);
659         if (!Arrays.equals(desiredBytes, actualBytes))
660         {
661             fail("Actual bytes don't match the desired bytes");
662         }
663     }
664
665     public static byte[] toByteArray(InputStream JavaDoc stream) throws IOException JavaDoc
666     {
667         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
668         byte[] buffer = new byte[4096];
669         int read = 0;
670         while ((read = stream.read(buffer, 0, buffer.length)) != -1)
671         {
672             baos.write(buffer, 0, read);
673         }
674         return baos.toByteArray();
675     }
676
677     private final String JavaDoc streamToString(InputStream JavaDoc inputStream) throws IOException JavaDoc
678     {
679         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
680         byte[] buffer = new byte[4096];
681         int read = 0;
682         int length = 0;
683         while ((read = inputStream.read(buffer, 0, buffer.length)) != -1)
684         {
685             baos.write(buffer, 0, read);
686             length += read;
687         }
688         return baos.toString();
689     }
690
691 }
692
Popular Tags