1 20 package org.apache.struts.upload; 21 22 import junit.framework.TestCase; 23 24 import java.io.InputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.IOException ; 27 import java.util.Arrays ; 28 29 32 public class MultipartBoundaryInputStreamTest extends TestCase 33 { 34 public static final String DISPOSITION_FORM_DATA = "form-data"; 35 36 public static final String CONTENT_TYPE_TEXT_PLAIN = "text/plain"; 37 38 41 public static final String RESOURCE_SINGLE_TEXT_ELEMENT = "/org/apache/struts/upload/resources/multipart-single-text-element.txt"; 42 43 46 public static final String RESOURCE_TWO_TEXT_ELEMENT = "/org/apache/struts/upload/resources/multipart-two-text-element.txt"; 47 48 51 public static final String RESOURCE_THREE_MIXED_ELEMENT = "/org/apache/struts/upload/resources/multipart-three-mixed-element.txt"; 52 53 56 public static final String RESOURCE_THREE_MIXED_NEWLINES = "/org/apache/struts/upload/resources/multipart-three-mixed-newline.txt"; 57 58 61 public static final String RESOURCE_PROBLEM1 = "/org/apache/struts/upload/resources/multipart-problem1.txt"; 62 63 public static final String RESOURCE_PROBLEM2 = "/org/apache/struts/upload/resources/multipart-problem2.txt"; 64 65 public static final String RESOURCE_PROBLEM3 = "/org/apache/struts/upload/resources/multipart-problem3.txt"; 66 67 68 69 72 public static final String RESOURCE_5400B = "/org/apache/struts/upload/resources/multipart-file-element-5400b.txt"; 73 74 77 public static final String RESOURCE_NEWLINES = "/org/apache/struts/upload/resources/multipart-file-element-newlines.txt"; 78 79 82 public static final String RESOURCE_DESIRED_PROBLEM1 = "/org/apache/struts/upload/resources/multipart-file-element-problem1.txt"; 83 84 public static final String RESOURCE_DESIRED_PROBLEM2 = "/org/apache/struts/upload/resources/multipart-file-element-problem2.txt"; 85 86 public static final String RESOURCE_DESIRED_PROBLEM3 = "/org/apache/struts/upload/resources/multipart-file-element-problem3.txt"; 87 88 91 public static final String BOUNDARY_SINGLE_TEXT_ELEMENT = "x9F38FJJ"; 92 93 96 public static final String BOUNDARY_TWO_TEXT_ELEMENT = "----------x9F38FJJ339502"; 97 98 101 public static final String BOUNDARY_PROBLEM1 = "---------------------------63341846741"; 102 103 public static final String BOUNDARY_PROBLEM2 = "---------------------------7d21fed3c0352"; 104 105 public static final String BOUNDARY_PROBLEM3 = "---------------------------7d21cb2b102cc"; 106 107 110 public static final String BOUNDARY_THREE_TEXT_ELEMENT = "----------g320nnjJJF32AA666FF"; 111 112 public static final String NAME_SINGLE_TEXT_ELEMENT = "testData"; 113 114 public static final String NAME_TWO_TEXT_ELEMENT_1 = "testData1"; 115 116 public static final String NAME_TWO_TEXT_ELEMENT_2 = "testData2"; 117 118 public static final String NAME_THREE_MIXED_ELEMENT_1 = "testFile1"; 119 120 public static final String NAME_THREE_MIXED_ELEMENT_2 = "testData1"; 121 122 public static final String NAME_THREE_MIXED_ELEMENT_3 = "testData2"; 123 124 public static final String NAME_PROBLEM1_1 = "theText"; 125 126 public static final String NAME_PROBLEM1_2 = "theFile"; 127 128 public static final String NAME_PROBLEM1_3 = "writeFile"; 129 130 public static final String NAME_PROBLEM1_4 = "filePath"; 131 132 public static final String NAME_PROBLEM1_5 = "submit"; 133 134 137 public static final String DESIRED_SINGLE_TEXT_ELEMENT = "this is test data"; 138 139 142 public static final String DESIRED_TEST_DATA_1 = "this is test data 1"; 143 144 147 public static final String DESIRED_TEST_DATA_2 = "this is test data 2"; 148 149 152 protected InputStream singleTextElementDataStream; 153 154 157 protected InputStream twoTextElementDataStream; 158 159 162 protected InputStream threeMixedElementDataStream; 163 164 167 protected InputStream newlinesDataStream; 168 169 172 protected InputStream testFile5400bDataStream; 173 174 177 protected InputStream testFileProblem1; 178 179 protected InputStream testFileProblem2; 180 181 protected InputStream testFileProblem3; 182 183 186 protected InputStream testFileNewlines; 187 188 191 protected InputStream testProblem1; 192 193 protected InputStream testProblem2; 194 195 protected InputStream testProblem3; 196 197 215 public MultipartBoundaryInputStreamTest(String name) 216 { 217 super(name); 218 } 219 220 protected void setUp() throws Exception 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 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 302 { 303 MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream(); 304 inputStream.setBoundary(BOUNDARY_SINGLE_TEXT_ELEMENT); 305 inputStream.setInputStream(this.singleTextElementDataStream); 306 String 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 319 { 320 MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream(); 321 inputStream.setBoundary(BOUNDARY_TWO_TEXT_ELEMENT); 322 inputStream.setInputStream(this.twoTextElementDataStream); 323 324 String 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 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 348 { 349 MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream(); 350 inputStream.setBoundary(BOUNDARY_THREE_TEXT_ELEMENT); 351 inputStream.setInputStream(this.threeMixedElementDataStream); 352 353 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 String resultData = streamToString(inputStream); 361 assertTrue(inputStream.isBoundaryEncountered()); 362 String desiredData = streamToString(this.testFile5400bDataStream); 363 assertEquals(desiredData, resultData); 364 365 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 resultData = streamToString(inputStream); 373 assertTrue(inputStream.isBoundaryEncountered()); 374 assertEquals(DESIRED_TEST_DATA_1, resultData); 375 376 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 resultData = streamToString(inputStream); 384 assertTrue(inputStream.isBoundaryEncountered()); 385 assertEquals(DESIRED_TEST_DATA_2, resultData); 386 assertTrue(inputStream.isFinalBoundaryEncountered()); 388 assertTrue(inputStream.isEndOfStream()); 389 } 390 391 394 public void testManyNewlines() throws Exception 395 { 396 MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream(); 397 inputStream.setBoundary(BOUNDARY_THREE_TEXT_ELEMENT); 398 inputStream.setInputStream(this.newlinesDataStream); 399 400 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 String resultData = streamToString(inputStream); 407 assertTrue(inputStream.isBoundaryEncountered()); 408 String desiredData = streamToString(this.testFileNewlines); 409 assertEquals(desiredData, resultData); 410 411 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 resultData = streamToString(inputStream); 419 assertTrue(inputStream.isBoundaryEncountered()); 420 assertEquals(DESIRED_TEST_DATA_1, resultData); 421 422 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 resultData = streamToString(inputStream); 430 assertTrue(inputStream.isBoundaryEncountered()); 431 assertEquals(DESIRED_TEST_DATA_2, resultData); 432 assertTrue(inputStream.isFinalBoundaryEncountered()); 434 assertTrue(inputStream.isEndOfStream()); 435 } 436 437 440 public void testProblemFile1() throws Exception 441 { 442 MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream(); 443 inputStream.setBoundary(BOUNDARY_PROBLEM1); 444 inputStream.setInputStream(this.testProblem1); 445 446 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 String resultData = streamToString(inputStream); 453 assertTrue(inputStream.isBoundaryEncountered()); 454 assertEquals("test", resultData); 455 456 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 resultData = streamToString(inputStream); 464 String desiredData = streamToString(this.testFileProblem1); 465 assertTrue(inputStream.isBoundaryEncountered()); 466 assertEquals(desiredData, resultData); 467 468 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 resultData = streamToString(inputStream); 476 assertTrue(inputStream.isBoundaryEncountered()); 477 assertEquals("on", resultData); 478 479 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 resultData = streamToString(inputStream); 487 assertTrue(inputStream.isBoundaryEncountered()); 488 assertEquals("c:\\multipart-test.txt", resultData); 489 490 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 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 505 { 506 MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream(); 507 inputStream.setBoundary(BOUNDARY_PROBLEM2); 508 inputStream.setInputStream(this.testProblem2); 509 510 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 String resultData = streamToString(inputStream); 517 assertTrue(inputStream.isBoundaryEncountered()); 518 assertEquals("test", resultData); 519 520 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 assertEquals(this.testFileProblem2, inputStream); 529 assertTrue(inputStream.isBoundaryEncountered()); 530 531 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 resultData = streamToString(inputStream); 539 assertTrue(inputStream.isBoundaryEncountered()); 540 assertEquals("on", resultData); 541 542 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 resultData = streamToString(inputStream); 550 assertTrue(inputStream.isBoundaryEncountered()); 551 assertEquals("c:\\multipart-test.txt", resultData); 552 553 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 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 568 { 569 MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream(); 570 inputStream.setBoundary(BOUNDARY_PROBLEM3); 571 inputStream.setInputStream(this.testProblem3); 572 573 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 String resultData = streamToString(inputStream); 580 assertTrue(inputStream.isBoundaryEncountered()); 581 assertEquals("test", resultData); 582 583 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 assertEquals(this.testFileProblem3, inputStream); 592 assertTrue(inputStream.isBoundaryEncountered()); 593 594 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 resultData = streamToString(inputStream); 602 assertTrue(inputStream.isBoundaryEncountered()); 603 assertEquals("on", resultData); 604 605 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 resultData = streamToString(inputStream); 613 assertTrue(inputStream.isBoundaryEncountered()); 614 assertEquals("c:\\multipart-test7.mp3", resultData); 615 616 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 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 631 { 632 MultipartBoundaryInputStream inputStream = new MultipartBoundaryInputStream(); 633 inputStream.setMaxLength(1500); 634 inputStream.setBoundary(BOUNDARY_PROBLEM3); 635 inputStream.setInputStream(this.testProblem3); 636 637 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 String resultData = streamToString(inputStream); 644 assertTrue(inputStream.isBoundaryEncountered()); 645 assertEquals("test", resultData); 646 647 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 desired, InputStream actual) throws Exception 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 stream) throws IOException 666 { 667 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 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 streamToString(InputStream inputStream) throws IOException 678 { 679 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 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 |