1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.archive.tests.java.util.zip; 19 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestTargets; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetNew; 24 25import java.io.InputStream; 26import java.io.UnsupportedEncodingException; 27import java.util.zip.Adler32; 28 29import java.util.zip.DataFormatException; 30import java.util.zip.Deflater; 31import java.util.zip.Inflater; 32 33import junit.framework.TestCase; 34import tests.support.resource.Support_Resources; 35 36@TestTargetClass(Deflater.class) 37public class DeflaterTest extends TestCase { 38 39 class MyDeflater extends Deflater { 40 MyDeflater() { 41 super(); 42 } 43 44 MyDeflater(int lvl) { 45 super(lvl); 46 } 47 48 MyDeflater(int lvl, boolean noHeader) { 49 super(lvl, noHeader); 50 } 51 52 void myFinalize() { 53 finalize(); 54 } 55 56 int getDefCompression() { 57 return DEFAULT_COMPRESSION; 58 } 59 60 int getDefStrategy() { 61 return DEFAULT_STRATEGY; 62 } 63 64 int getHuffman() { 65 return HUFFMAN_ONLY; 66 } 67 68 int getFiltered() { 69 return FILTERED; 70 } 71 } 72 73 /** 74 * @tests java.util.zip.Deflater#deflate(byte[]) 75 */ 76 @TestTargetNew( 77 level = TestLevel.COMPLETE, 78 notes = "", 79 method = "deflate", 80 args = {byte[].class} 81 ) 82 public void test_deflate$B() { 83 byte outPutBuf[] = new byte[50]; 84 byte byteArray[] = {1, 3, 4, 7, 8}; 85 byte outPutInf[] = new byte[50]; 86 int x = 0; 87 88 Deflater defl = new Deflater(); 89 defl.setInput(byteArray); 90 defl.finish(); 91 while (!defl.finished()) { 92 x += defl.deflate(outPutBuf); 93 } 94 assertEquals("Deflater at end of stream, should return 0", 0, defl 95 .deflate(outPutBuf)); 96 int totalOut = defl.getTotalOut(); 97 int totalIn = defl.getTotalIn(); 98 assertEquals(x, totalOut); 99 assertEquals(byteArray.length, totalIn); 100 defl.end(); 101 102 Inflater infl = new Inflater(); 103 try { 104 infl.setInput(outPutBuf); 105 while (!infl.finished()) { 106 infl.inflate(outPutInf); 107 } 108 } catch (DataFormatException e) { 109 fail("Invalid input to be decompressed"); 110 } 111 assertEquals(totalIn, infl.getTotalOut()); 112 assertEquals(totalOut, infl.getTotalIn()); 113 for (int i = 0; i < byteArray.length; i++) { 114 assertEquals(byteArray[i], outPutInf[i]); 115 } 116 assertEquals( 117 "Final decompressed data contained more bytes than original", 118 0, outPutInf[byteArray.length]); 119 infl.end(); 120 } 121 122 /** 123 * @tests java.util.zip.Deflater#deflate(byte[], int, int) 124 */ 125 @TestTargetNew( 126 level = TestLevel.COMPLETE, 127 notes = "", 128 method = "deflate", 129 args = {byte[].class, int.class, int.class} 130 ) 131 public void test_deflate$BII() { 132 byte outPutBuf[] = new byte[50]; 133 byte byteArray[] = {5, 2, 3, 7, 8}; 134 byte outPutInf[] = new byte[50]; 135 int offSet = 1; 136 int length = outPutBuf.length - 1; 137 int x = 0; 138 139 Deflater defl = new Deflater(); 140 defl.setInput(byteArray); 141 defl.finish(); 142 while (!defl.finished()) { 143 x += defl.deflate(outPutBuf, offSet, length); 144 } 145 assertEquals("Deflater at end of stream, should return 0", 0, defl 146 .deflate(outPutBuf, offSet, length)); 147 int totalOut = defl.getTotalOut(); 148 int totalIn = defl.getTotalIn(); 149 assertEquals(x, totalOut); 150 assertEquals(byteArray.length, totalIn); 151 defl.end(); 152 153 Inflater infl = new Inflater(); 154 try { 155 infl.setInput(outPutBuf, offSet, length); 156 while (!infl.finished()) { 157 infl.inflate(outPutInf); 158 } 159 } catch (DataFormatException e) { 160 fail("Invalid input to be decompressed"); 161 } 162 assertEquals(totalIn, infl.getTotalOut()); 163 assertEquals(totalOut, infl.getTotalIn()); 164 for (int i = 0; i < byteArray.length; i++) { 165 assertEquals(byteArray[i], outPutInf[i]); 166 } 167 assertEquals( 168 "Final decompressed data contained more bytes than original", 169 0, outPutInf[byteArray.length]); 170 infl.end(); 171 172 // Set of tests testing the boundaries of the offSet/length 173 defl = new Deflater(); 174 outPutBuf = new byte[100]; 175 defl.setInput(byteArray); 176 for (int i = 0; i < 2; i++) { 177 if (i == 0) { 178 offSet = outPutBuf.length + 1; 179 length = outPutBuf.length; 180 } else { 181 offSet = 0; 182 length = outPutBuf.length + 1; 183 } 184 try { 185 defl.deflate(outPutBuf, offSet, length); 186 fail("Test " + i 187 + ": ArrayIndexOutOfBoundsException not thrown"); 188 } catch (ArrayIndexOutOfBoundsException e) { 189 } 190 } 191 defl.end(); 192 } 193 194 /** 195 * @tests java.util.zip.Deflater#end() 196 */ 197 @TestTargetNew( 198 level = TestLevel.COMPLETE, 199 notes = "", 200 method = "end", 201 args = {} 202 ) 203 public void test_end() { 204 byte byteArray[] = {5, 2, 3, 7, 8}; 205 byte outPutBuf[] = new byte[100]; 206 207 Deflater defl = new Deflater(); 208 defl.setInput(byteArray); 209 defl.finish(); 210 while (!defl.finished()) { 211 defl.deflate(outPutBuf); 212 } 213 defl.end(); 214 helper_end_test(defl, "end"); 215 } 216 217 /** 218 * @tests java.util.zip.Deflater#finalize() 219 */ 220 @TestTargetNew( 221 level = TestLevel.COMPLETE, 222 notes = "", 223 method = "finalize", 224 args = {} 225 ) 226 public void test_finalize() { 227 MyDeflater mdefl = new MyDeflater(); 228 mdefl.myFinalize(); 229 System.gc(); 230 helper_end_test(mdefl, "finalize"); 231 } 232 233 /** 234 * @tests java.util.zip.Deflater#finish() 235 */ 236 @TestTargetNew( 237 level = TestLevel.COMPLETE, 238 notes = "", 239 method = "finish", 240 args = {} 241 ) 242 public void test_finish() throws Exception { 243 // This test already here, its the same as test_deflate() 244 byte byteArray[] = {5, 2, 3, 7, 8}; 245 byte outPutBuf[] = new byte[100]; 246 byte outPutInf[] = new byte[100]; 247 int x = 0; 248 Deflater defl = new Deflater(); 249 defl.setInput(byteArray); 250 defl.finish(); 251 252 // needsInput should never return true after finish() is called 253 if (System.getProperty("java.vendor").startsWith("IBM")) { 254 assertFalse( 255 "needsInput() should return false after finish() is called", 256 defl.needsInput()); 257 } 258 259 while (!defl.finished()) { 260 x += defl.deflate(outPutBuf); 261 } 262 int totalOut = defl.getTotalOut(); 263 int totalIn = defl.getTotalIn(); 264 assertEquals(x, totalOut); 265 assertEquals(byteArray.length, totalIn); 266 defl.end(); 267 268 Inflater infl = new Inflater(); 269 infl.setInput(outPutBuf); 270 while (!infl.finished()) { 271 infl.inflate(outPutInf); 272 } 273 assertEquals(totalIn, infl.getTotalOut()); 274 assertEquals(totalOut, infl.getTotalIn()); 275 for (int i = 0; i < byteArray.length; i++) { 276 assertEquals(byteArray[i], outPutInf[i]); 277 } 278 assertEquals( 279 "Final decompressed data contained more bytes than original", 280 0, outPutInf[byteArray.length]); 281 infl.end(); 282 } 283 284 /** 285 * @tests java.util.zip.Deflater#finished() 286 */ 287 @TestTargetNew( 288 level = TestLevel.COMPLETE, 289 notes = "", 290 method = "finished", 291 args = {} 292 ) 293 public void test_finished() { 294 byte byteArray[] = {5, 2, 3, 7, 8}; 295 byte outPutBuf[] = new byte[100]; 296 Deflater defl = new Deflater(); 297 assertTrue("Test 1: Deflater should not be finished.", !defl.finished()); 298 defl.setInput(byteArray); 299 assertTrue("Test 2: Deflater should not be finished.", !defl.finished()); 300 defl.finish(); 301 assertTrue("Test 3: Deflater should not be finished.", !defl.finished()); 302 while (!defl.finished()) { 303 defl.deflate(outPutBuf); 304 } 305 assertTrue("Test 4: Deflater should be finished.", defl.finished()); 306 defl.end(); 307 assertTrue("Test 5: Deflater should be finished.", defl.finished()); 308 } 309 310 /** 311 * @tests java.util.zip.Deflater#getAdler() 312 */ 313 @TestTargetNew( 314 level = TestLevel.COMPLETE, 315 notes = "", 316 method = "getAdler", 317 args = {} 318 ) 319 public void test_getAdler() { 320 byte byteArray[] = {'a', 'b', 'c', 1, 2, 3}; 321 byte outPutBuf[] = new byte[100]; 322 Deflater defl = new Deflater(); 323 324 // getting the checkSum value using the Adler 325 defl.setInput(byteArray); 326 defl.finish(); 327 while (!defl.finished()) { 328 defl.deflate(outPutBuf); 329 } 330 long checkSumD = defl.getAdler(); 331 defl.end(); 332 333 // getting the checkSum value through the Adler32 class 334 Adler32 adl = new Adler32(); 335 adl.update(byteArray); 336 long checkSumR = adl.getValue(); 337 assertEquals( 338 "The checksum value returned by getAdler() is not the same as the checksum returned by creating the adler32 instance", 339 checkSumD, checkSumR); 340 } 341 342 /** 343 * @tests java.util.zip.Deflater#getTotalIn() 344 */ 345 @TestTargetNew( 346 level = TestLevel.COMPLETE, 347 notes = "", 348 method = "getTotalIn", 349 args = {} 350 ) 351 public void test_getTotalIn() { 352 byte outPutBuf[] = new byte[5]; 353 byte byteArray[] = {1, 3, 4, 7, 8}; 354 355 Deflater defl = new Deflater(); 356 defl.setInput(byteArray); 357 defl.finish(); 358 while (!defl.finished()) { 359 defl.deflate(outPutBuf); 360 } 361 assertEquals(byteArray.length, defl.getTotalIn()); 362 defl.end(); 363 364 defl = new Deflater(); 365 int offSet = 2; 366 int length = 3; 367 outPutBuf = new byte[5]; 368 defl.setInput(byteArray, offSet, length); 369 defl.finish(); 370 while (!defl.finished()) { 371 defl.deflate(outPutBuf); 372 } 373 assertEquals(length, defl.getTotalIn()); 374 defl.end(); 375 } 376 377 /** 378 * @tests java.util.zip.Deflater#getTotalOut() 379 */ 380 @TestTargetNew( 381 level = TestLevel.COMPLETE, 382 notes = "", 383 method = "getTotalOut", 384 args = {} 385 ) 386 public void test_getTotalOut() { 387 // the getTotalOut should equal the sum of value returned by deflate() 388 byte outPutBuf[] = new byte[5]; 389 byte byteArray[] = {5, 2, 3, 7, 8}; 390 int x = 0; 391 Deflater defl = new Deflater(); 392 defl.setInput(byteArray); 393 defl.finish(); 394 while (!defl.finished()) { 395 x += defl.deflate(outPutBuf); 396 } 397 assertEquals(x, defl.getTotalOut()); 398 defl.end(); 399 400 x = 0; 401 int offSet = 2; 402 int length = 3; 403 defl = new Deflater(); 404 outPutBuf = new byte[5]; 405 defl.setInput(byteArray, offSet, length); 406 defl.finish(); 407 while (!defl.finished()) { 408 x += defl.deflate(outPutBuf); 409 } 410 assertEquals(x, defl.getTotalOut()); 411 } 412 413 /** 414 * @tests java.util.zip.Deflater#needsInput() 415 */ 416 @TestTargetNew( 417 level = TestLevel.COMPLETE, 418 notes = "", 419 method = "needsInput", 420 args = {} 421 ) 422 public void test_needsInput() { 423 Deflater defl = new Deflater(); 424 assertTrue( 425 "needsInput give the wrong boolean value as a result of no input buffer", 426 defl.needsInput()); 427 byte byteArray[] = {1, 2, 3}; 428 defl.setInput(byteArray); 429 assertFalse( 430 "needsInput give wrong boolean value as a result of a full input buffer", 431 defl.needsInput()); 432 byte[] outPutBuf = new byte[50]; 433 while (!defl.needsInput()) { 434 defl.deflate(outPutBuf); 435 } 436 byte emptyByteArray[] = new byte[0]; 437 defl.setInput(emptyByteArray); 438 assertTrue( 439 "needsInput give wrong boolean value as a result of an empty input buffer", 440 defl.needsInput()); 441 defl.setInput(byteArray); 442 defl.finish(); 443 while (!defl.finished()) { 444 defl.deflate(outPutBuf); 445 } 446 // needsInput should NOT return true after finish() has been 447 // called. 448 if (System.getProperty("java.vendor").startsWith("IBM")) { 449 assertFalse( 450 "needsInput gave wrong boolean value as a result of finish() being called", 451 defl.needsInput()); 452 } 453 defl.end(); 454 } 455 456 /** 457 * @tests java.util.zip.Deflater#reset() 458 */ 459 @TestTargetNew( 460 level = TestLevel.COMPLETE, 461 notes = "", 462 method = "reset", 463 args = {} 464 ) 465 public void test_reset() { 466 byte outPutBuf[] = new byte[100]; 467 byte outPutInf[] = new byte[100]; 468 byte curArray[] = new byte[5]; 469 byte byteArray[] = {1, 3, 4, 7, 8}; 470 byte byteArray2[] = {8, 7, 4, 3, 1}; 471 int x = 0; 472 int orgValue = 0; 473 Deflater defl = new Deflater(); 474 475 for (int i = 0; i < 3; i++) { 476 if (i == 0) { 477 curArray = byteArray; 478 } else if (i == 1) { 479 curArray = byteArray2; 480 } else { 481 defl.reset(); 482 } 483 484 defl.setInput(curArray); 485 defl.finish(); 486 while (!defl.finished()) { 487 x += defl.deflate(outPutBuf); 488 } 489 490 if (i == 0) { 491 assertEquals(x, defl.getTotalOut()); 492 } else if (i == 1) { 493 assertEquals(x, orgValue); 494 } else { 495 assertEquals(x, orgValue * 2); 496 } 497 498 if (i == 0) { 499 orgValue = x; 500 } 501 502 try { 503 Inflater infl = new Inflater(); 504 infl.setInput(outPutBuf); 505 while (!infl.finished()) { 506 infl.inflate(outPutInf); 507 } 508 infl.end(); 509 } catch (DataFormatException e) { 510 fail("Test " + i + ": Invalid input to be decompressed"); 511 } 512 513 if (i == 1) { 514 curArray = byteArray; 515 } 516 517 for (int j = 0; j < curArray.length; j++) { 518 assertEquals(curArray[j], outPutInf[j]); 519 } 520 assertEquals(0, outPutInf[curArray.length]); 521 } 522 } 523 524 /** 525 * @tests java.util.zip.Deflater#setDictionary(byte[]) 526 */ 527 @TestTargetNew( 528 level = TestLevel.COMPLETE, 529 notes = "", 530 method = "setDictionary", 531 args = {byte[].class} 532 ) 533 public void test_setDictionary$B() { 534 // This test is very close to getAdler() 535 byte dictionaryArray[] = {'e', 'r', 't', 'a', 'b', 2, 3}; 536 byte byteArray[] = { 537 4, 5, 3, 2, 'a', 'b', 6, 7, 8, 9, 0, 's', '3', 'w', 'r'}; 538 byte outPutBuf[] = new byte[100]; 539 540 Deflater defl = new Deflater(); 541 long deflAdler = defl.getAdler(); 542 assertEquals( 543 "No dictionary set, no data deflated, getAdler should return 1", 544 1, deflAdler); 545 defl.setDictionary(dictionaryArray); 546 deflAdler = defl.getAdler(); 547 548 // getting the checkSum value through the Adler32 class 549 Adler32 adl = new Adler32(); 550 adl.update(dictionaryArray); 551 long realAdler = adl.getValue(); 552 assertEquals(deflAdler, realAdler); 553 554 defl.setInput(byteArray); 555 defl.finish(); 556 while (!defl.finished()) { 557 defl.deflate(outPutBuf); 558 } 559 deflAdler = defl.getAdler(); 560 adl = new Adler32(); 561 adl.update(byteArray); 562 realAdler = adl.getValue(); 563 // Deflate is finished and there were bytes deflated that did not occur 564 // in the dictionaryArray, therefore a new dictionary was automatically 565 // set. 566 assertEquals(realAdler, deflAdler); 567 defl.end(); 568 } 569 570 /** 571 * @tests java.util.zip.Deflater#setDictionary(byte[], int, int) 572 */ 573 @TestTargetNew( 574 level = TestLevel.COMPLETE, 575 notes = "", 576 method = "setDictionary", 577 args = {byte[].class, int.class, int.class} 578 ) 579 public void test_setDictionary$BII() { 580 // This test is very close to getAdler() 581 byte dictionaryArray[] = {'e', 'r', 't', 'a', 'b', 2, 3, 'o', 't'}; 582 byte byteArray[] = { 583 4, 5, 3, 2, 'a', 'b', 6, 7, 8, 9, 0, 's', '3', 'w', 'r', 't', 584 'u', 'i', 'o', 4, 5, 6, 7}; 585 byte outPutBuf[] = new byte[500]; 586 587 int offSet = 4; 588 int length = 5; 589 590 Deflater defl = new Deflater(); 591 long deflAdler = defl.getAdler(); 592 assertEquals( 593 "No dictionary set, no data deflated, getAdler should return 1", 594 1, deflAdler); 595 defl.setDictionary(dictionaryArray, offSet, length); 596 deflAdler = defl.getAdler(); 597 598 // getting the checkSum value through the Adler32 class 599 Adler32 adl = new Adler32(); 600 adl.update(dictionaryArray, offSet, length); 601 long realAdler = adl.getValue(); 602 assertEquals(deflAdler, realAdler); 603 604 defl.setInput(byteArray); 605 while (!defl.needsInput()) { 606 defl.deflate(outPutBuf); 607 } 608 deflAdler = defl.getAdler(); 609 adl = new Adler32(); 610 adl.update(byteArray); 611 realAdler = adl.getValue(); 612 // Deflate is finished and there were bytes deflated that did not occur 613 // in the dictionaryArray, therefore a new dictionary was automatically 614 // set. 615 assertEquals(realAdler, deflAdler); 616 defl.end(); 617 618 // boundary check 619 defl = new Deflater(); 620 for (int i = 0; i < 2; i++) { 621 if (i == 0) { 622 offSet = 0; 623 length = dictionaryArray.length + 1; 624 } else { 625 offSet = dictionaryArray.length + 1; 626 length = 1; 627 } 628 try { 629 defl.setDictionary(dictionaryArray, offSet, length); 630 fail("Test " 631 + i 632 + ": boundary check for setDictionary failed for offset " 633 + offSet + " and length " + length); 634 } catch (ArrayIndexOutOfBoundsException e) { 635 } 636 } 637 } 638 639 /** 640 * @tests java.util.zip.Deflater#setInput(byte[]) 641 */ 642 @TestTargetNew( 643 level = TestLevel.COMPLETE, 644 notes = "", 645 method = "setInput", 646 args = {byte[].class} 647 ) 648 public void test_setInput$B() { 649 byte[] byteArray = {1, 2, 3}; 650 byte[] outPutBuf = new byte[50]; 651 byte[] outPutInf = new byte[50]; 652 653 Deflater defl = new Deflater(); 654 defl.setInput(byteArray); 655 assertTrue("the array buffer in setInput() is empty", !defl 656 .needsInput()); 657 // The second setInput() should be ignored since needsInput() return 658 // false 659 defl.setInput(byteArray); 660 defl.finish(); 661 while (!defl.finished()) { 662 defl.deflate(outPutBuf); 663 } 664 defl.end(); 665 666 Inflater infl = new Inflater(); 667 try { 668 infl.setInput(outPutBuf); 669 while (!infl.finished()) { 670 infl.inflate(outPutInf); 671 } 672 } catch (DataFormatException e) { 673 fail("Invalid input to be decompressed"); 674 } 675 for (int i = 0; i < byteArray.length; i++) { 676 assertEquals(byteArray[i], outPutInf[i]); 677 } 678 assertEquals(byteArray.length, infl.getTotalOut()); 679 infl.end(); 680 } 681 682 /** 683 * @tests java.util.zip.Deflater#setInput(byte[], int, int) 684 */ 685 @TestTargetNew( 686 level = TestLevel.COMPLETE, 687 notes = "", 688 method = "setInput", 689 args = {byte[].class, int.class, int.class} 690 ) 691 public void test_setInput$BII() throws Exception { 692 byte[] byteArray = {1, 2, 3, 4, 5}; 693 byte[] outPutBuf = new byte[50]; 694 byte[] outPutInf = new byte[50]; 695 int offSet = 1; 696 int length = 3; 697 698 Deflater defl = new Deflater(); 699 defl.setInput(byteArray, offSet, length); 700 assertFalse("the array buffer in setInput() is empty", defl 701 .needsInput()); 702 // The second setInput() should be ignored since needsInput() return 703 // false 704 defl.setInput(byteArray, offSet, length); 705 defl.finish(); 706 while (!defl.finished()) { 707 defl.deflate(outPutBuf); 708 } 709 defl.end(); 710 711 Inflater infl = new Inflater(); 712 infl.setInput(outPutBuf); 713 while (!infl.finished()) { 714 infl.inflate(outPutInf); 715 } 716 for (int i = 0; i < length; i++) { 717 assertEquals(byteArray[i + offSet], outPutInf[i]); 718 } 719 assertEquals(length, infl.getTotalOut()); 720 infl.end(); 721 722 // boundary check 723 defl = new Deflater(); 724 for (int i = 0; i < 2; i++) { 725 if (i == 0) { 726 offSet = 0; 727 length = byteArray.length + 1; 728 } else { 729 offSet = byteArray.length + 1; 730 length = 1; 731 } 732 try { 733 defl.setInput(byteArray, offSet, length); 734 fail("Test " + i 735 + ": boundary check for setInput failed for offset " 736 + offSet + " and length " + length); 737 } catch (ArrayIndexOutOfBoundsException e) { 738 } 739 } 740 } 741 742 /** 743 * @tests java.util.zip.Deflater#setLevel(int) 744 */ 745 @TestTargetNew( 746 level = TestLevel.COMPLETE, 747 notes = "", 748 method = "setLevel", 749 args = {int.class} 750 ) 751 public void test_setLevelI() throws Exception { 752 // Very similar to test_Constructor(int) 753 byte[] byteArray = new byte[100]; 754 InputStream inFile = Support_Resources.getStream("hyts_checkInput.txt"); 755 inFile.read(byteArray); 756 inFile.close(); 757 758 byte[] outPutBuf; 759 int totalOut; 760 for (int i = 0; i < 10; i++) { 761 Deflater defl = new Deflater(); 762 defl.setLevel(i); 763 outPutBuf = new byte[500]; 764 defl.setInput(byteArray); 765 while (!defl.needsInput()) { 766 defl.deflate(outPutBuf); 767 } 768 defl.finish(); 769 while (!defl.finished()) { 770 defl.deflate(outPutBuf); 771 } 772 totalOut = defl.getTotalOut(); 773 defl.end(); 774 775 outPutBuf = new byte[500]; 776 defl = new Deflater(i); 777 defl.setInput(byteArray); 778 while (!defl.needsInput()) { 779 defl.deflate(outPutBuf); 780 } 781 defl.finish(); 782 while (!defl.finished()) { 783 defl.deflate(outPutBuf); 784 } 785 assertEquals(totalOut, defl.getTotalOut()); 786 defl.end(); 787 } 788 789 // testing boundaries 790 try { 791 Deflater boundDefl = new Deflater(); 792 // Level must be between 0-9 793 boundDefl.setLevel(-2); 794 fail("IllegalArgumentException not thrown when setting level to a number < 0."); 795 } catch (IllegalArgumentException e) { 796 } 797 try { 798 Deflater boundDefl = new Deflater(); 799 boundDefl.setLevel(10); 800 fail("IllegalArgumentException not thrown when setting level to a number > 9."); 801 } catch (IllegalArgumentException e) { 802 } 803 } 804 805 /** 806 * @tests java.util.zip.Deflater#setStrategy(int) 807 */ 808 @TestTargetNew( 809 level = TestLevel.COMPLETE, 810 notes = "", 811 method = "setStrategy", 812 args = {int.class} 813 ) 814 public void test_setStrategyI() throws Exception { 815 byte[] byteArray = new byte[100]; 816 InputStream inFile = Support_Resources.getStream("hyts_checkInput.txt"); 817 inFile.read(byteArray); 818 inFile.close(); 819 820 for (int i = 0; i < 3; i++) { 821 byte outPutBuf[] = new byte[500]; 822 MyDeflater mdefl = new MyDeflater(); 823 824 if (i == 0) { 825 mdefl.setStrategy(mdefl.getDefStrategy()); 826 } else if (i == 1) { 827 mdefl.setStrategy(mdefl.getHuffman()); 828 } else { 829 mdefl.setStrategy(mdefl.getFiltered()); 830 } 831 832 mdefl.setInput(byteArray); 833 while (!mdefl.needsInput()) { 834 mdefl.deflate(outPutBuf); 835 } 836 mdefl.finish(); 837 while (!mdefl.finished()) { 838 mdefl.deflate(outPutBuf); 839 } 840 841 if (i == 0) { 842 // System.out.println(mdefl.getTotalOut()); 843 // ran JDK and found that getTotalOut() = 86 for this particular 844 // file 845 assertEquals( 846 "getTotalOut() for the default strategy did not correspond with JDK", 847 86, mdefl.getTotalOut()); 848 } else if (i == 1) { 849 // System.out.println(mdefl.getTotalOut()); 850 // ran JDK and found that getTotalOut() = 100 for this 851 // particular file 852 assertEquals( 853 "getTotalOut() for the Huffman strategy did not correspond with JDK", 854 100, mdefl.getTotalOut()); 855 } else { 856 // System.out.println(mdefl.getTotalOut()); 857 // ran JDK and found that totalOut = 93 for this particular file 858 assertEquals( 859 "Total Out for the Filtered strategy did not correspond with JDK", 860 93, mdefl.getTotalOut()); 861 } 862 mdefl.end(); 863 } 864 865 // Attempting to setStrategy to an invalid value 866 try { 867 Deflater defl = new Deflater(); 868 defl.setStrategy(-412); 869 fail("IllegalArgumentException not thrown when setting strategy to an invalid value."); 870 } catch (IllegalArgumentException e) { 871 } 872 } 873 874 /** 875 * @tests java.util.zip.Deflater#Deflater() 876 */ 877 @TestTargetNew( 878 level = TestLevel.COMPLETE, 879 notes = "", 880 method = "Deflater", 881 args = {} 882 ) 883 public void test_Constructor() throws Exception { 884 byte[] byteArray = new byte[100]; 885 InputStream inFile = Support_Resources.getStream("hyts_checkInput.txt"); 886 inFile.read(byteArray); 887 inFile.close(); 888 889 Deflater defl = new Deflater(); 890 byte[] outPutBuf = new byte[500]; 891 defl.setInput(byteArray); 892 while (!defl.needsInput()) { 893 defl.deflate(outPutBuf); 894 } 895 defl.finish(); 896 while (!defl.finished()) { 897 defl.deflate(outPutBuf); 898 } 899 int totalOut = defl.getTotalOut(); 900 defl.end(); 901 902 // creating a Deflater using the DEFAULT_COMPRESSION as the int 903 MyDeflater mdefl = new MyDeflater(); 904 mdefl = new MyDeflater(mdefl.getDefCompression()); 905 outPutBuf = new byte[500]; 906 mdefl.setInput(byteArray); 907 while (!mdefl.needsInput()) { 908 mdefl.deflate(outPutBuf); 909 } 910 mdefl.finish(); 911 while (!mdefl.finished()) { 912 mdefl.deflate(outPutBuf); 913 } 914 assertEquals(totalOut, mdefl.getTotalOut()); 915 mdefl.end(); 916 } 917 918 /** 919 * @tests java.util.zip.Deflater#Deflater(int, boolean) 920 */ 921 @TestTargetNew( 922 level = TestLevel.COMPLETE, 923 notes = "", 924 method = "Deflater", 925 args = {int.class, boolean.class} 926 ) 927 public void test_ConstructorIZ() throws Exception { 928 byte byteArray[] = { 929 4, 5, 3, 2, 'a', 'b', 6, 7, 8, 9, 0, 's', '3', 'w', 'r'}; 930 931 Deflater defl = new Deflater(); 932 byte outPutBuf[] = new byte[500]; 933 defl.setLevel(2); 934 defl.setInput(byteArray); 935 while (!defl.needsInput()) { 936 defl.deflate(outPutBuf); 937 } 938 defl.finish(); 939 while (!defl.finished()) { 940 defl.deflate(outPutBuf); 941 } 942 int totalOut = defl.getTotalOut(); 943 defl.end(); 944 945 outPutBuf = new byte[500]; 946 defl = new Deflater(2, false); 947 defl.setInput(byteArray); 948 while (!defl.needsInput()) { 949 defl.deflate(outPutBuf); 950 } 951 defl.finish(); 952 while (!defl.finished()) { 953 defl.deflate(outPutBuf); 954 } 955 assertEquals(totalOut, defl.getTotalOut()); 956 defl.end(); 957 958 outPutBuf = new byte[500]; 959 defl = new Deflater(2, true); 960 defl.setInput(byteArray); 961 while (!defl.needsInput()) { 962 defl.deflate(outPutBuf); 963 } 964 defl.finish(); 965 while (!defl.finished()) { 966 defl.deflate(outPutBuf); 967 } 968 assertTrue( 969 "getTotalOut() should not be equal comparing two Deflaters with different header options.", 970 defl.getTotalOut() != totalOut); 971 defl.end(); 972 973 byte outPutInf[] = new byte[500]; 974 Inflater infl = new Inflater(true); 975 while (!infl.finished()) { 976 if (infl.needsInput()) { 977 infl.setInput(outPutBuf); 978 } 979 infl.inflate(outPutInf); 980 } 981 for (int i = 0; i < byteArray.length; i++) { 982 assertEquals(byteArray[i], outPutInf[i]); 983 } 984 assertEquals( 985 "final decompressed data contained more bytes than original - constructorIZ", 986 0, outPutInf[byteArray.length]); 987 infl.end(); 988 989 infl = new Inflater(false); 990 outPutInf = new byte[500]; 991 int r = 0; 992 try { 993 while (!infl.finished()) { 994 if (infl.needsInput()) { 995 infl.setInput(outPutBuf); 996 } 997 infl.inflate(outPutInf); 998 } 999 } catch (DataFormatException e) { 1000 r = 1; 1001 } 1002 assertEquals("header option did not correspond", 1, r); 1003 1004 // testing boundaries 1005 try { 1006 Deflater boundDefl = new Deflater(); 1007 // Level must be between 0-9 1008 boundDefl.setLevel(-2); 1009 fail("IllegalArgumentException not thrown when setting level to a number < 0."); 1010 } catch (IllegalArgumentException e) { 1011 } 1012 try { 1013 Deflater boundDefl = new Deflater(); 1014 boundDefl.setLevel(10); 1015 fail("IllegalArgumentException not thrown when setting level to a number > 9."); 1016 } catch (IllegalArgumentException e) { 1017 } 1018 } 1019 1020 /** 1021 * @tests java.util.zip.Deflater#Deflater(int) 1022 */ 1023 @TestTargetNew( 1024 level = TestLevel.COMPLETE, 1025 notes = "", 1026 method = "Deflater", 1027 args = {int.class} 1028 ) 1029 public void test_ConstructorI() throws Exception { 1030 byte[] byteArray = new byte[100]; 1031 InputStream inFile = Support_Resources.getStream("hyts_checkInput.txt"); 1032 inFile.read(byteArray); 1033 inFile.close(); 1034 1035 byte outPutBuf[] = new byte[500]; 1036 Deflater defl = new Deflater(3); 1037 defl.setInput(byteArray); 1038 while (!defl.needsInput()) { 1039 defl.deflate(outPutBuf); 1040 } 1041 defl.finish(); 1042 while (!defl.finished()) { 1043 defl.deflate(outPutBuf); 1044 } 1045 int totalOut = defl.getTotalOut(); 1046 defl.end(); 1047 1048 // test to see if the compression ratio is the same as setting the level 1049 // on a deflater 1050 outPutBuf = new byte[500]; 1051 defl = new Deflater(); 1052 defl.setLevel(3); 1053 defl.setInput(byteArray); 1054 while (!defl.needsInput()) { 1055 defl.deflate(outPutBuf); 1056 } 1057 defl.finish(); 1058 while (!defl.finished()) { 1059 defl.deflate(outPutBuf); 1060 } 1061 assertEquals(totalOut, defl.getTotalOut()); 1062 defl.end(); 1063 1064 // testing boundaries 1065 try { 1066 Deflater boundDefl = new Deflater(); 1067 // Level must be between 0-9 1068 boundDefl.setLevel(-2); 1069 fail("IllegalArgumentException not thrown when setting level to a number < 0."); 1070 } catch (IllegalArgumentException e) { 1071 } 1072 try { 1073 Deflater boundDefl = new Deflater(); 1074 boundDefl.setLevel(10); 1075 fail("IllegalArgumentException not thrown when setting level to a number > 9."); 1076 } catch (IllegalArgumentException e) { 1077 } 1078 } 1079 1080 private void helper_end_test(Deflater defl, String desc) { 1081 // Help tests for test_end() and test_reset(). 1082 byte byteArray[] = {5, 2, 3, 7, 8}; 1083 1084 // Methods where we expect IllegalStateException or NullPointerException 1085 // to be thrown 1086 try { 1087 defl.getTotalOut(); 1088 fail("defl.getTotalOut() can still be used after " + desc 1089 + " is called in test_" + desc); 1090 } catch (IllegalStateException e) { 1091 } catch (NullPointerException e) { 1092 } 1093 try { 1094 defl.getTotalIn(); 1095 fail("defl.getTotalIn() can still be used after " + desc 1096 + " is called in test_" + desc); 1097 } catch (IllegalStateException e) { 1098 } catch (NullPointerException e) { 1099 } 1100 try { 1101 defl.getAdler(); 1102 fail("defl.getAdler() can still be used after " + desc 1103 + " is called in test_" + desc); 1104 } catch (IllegalStateException e) { 1105 } catch (NullPointerException e) { 1106 } 1107 try { 1108 byte[] dict = {'a', 'b', 'c'}; 1109 defl.setDictionary(dict); 1110 fail("defl.setDictionary() can still be used after " + desc 1111 + " is called in test_" + desc); 1112 } catch (IllegalStateException e) { 1113 } catch (NullPointerException e) { 1114 } 1115 try { 1116 defl.getTotalIn(); 1117 fail("defl.getTotalIn() can still be used after " + desc 1118 + " is called in test_" + desc); 1119 } catch (IllegalStateException e) { 1120 } catch (NullPointerException e) { 1121 } 1122 try { 1123 defl.getTotalIn(); 1124 fail("defl.getTotalIn() can still be used after " + desc 1125 + " is called in test_" + desc); 1126 } catch (IllegalStateException e) { 1127 } catch (NullPointerException e) { 1128 } 1129 try { 1130 defl.deflate(byteArray); 1131 fail("defl.deflate() can still be used after " + desc 1132 + " is called in test_" + desc); 1133 } catch (IllegalStateException e) { 1134 } catch (NullPointerException e) { 1135 } 1136 1137 // Methods where we expect NullPointerException to be thrown 1138 try { 1139 defl.reset(); 1140 fail("defl.reset() can still be used after " + desc 1141 + " is called in test_" + desc); 1142 } catch (NullPointerException e) { 1143 } 1144 1145 // Methods that should be allowed to be called after end() is called 1146 defl.needsInput(); 1147 defl.setStrategy(1); 1148 defl.setLevel(1); 1149 defl.end(); 1150 1151 // Methods where exceptions should be thrown 1152 String vendor = System.getProperty("java.vendor"); 1153 if (vendor.indexOf("IBM") != -1) { 1154 try { 1155 defl.setInput(byteArray); 1156 fail("defl.setInput() can still be used after " + desc 1157 + " is called in test_" + desc); 1158 } catch (IllegalStateException e) { 1159 } 1160 } 1161 } 1162 1163 /** 1164 * @throws DataFormatException 1165 * @throws UnsupportedEncodingException 1166 * @tests java.util.zip.Deflater#getBytesRead() 1167 */ 1168 @TestTargetNew( 1169 level = TestLevel.COMPLETE, 1170 notes = "", 1171 method = "getBytesRead", 1172 args = {} 1173 ) 1174 public void test_getBytesRead() throws DataFormatException, 1175 UnsupportedEncodingException { 1176 // Regression test for HARMONY-158 1177 Deflater def = new Deflater(); 1178 assertEquals(0, def.getTotalIn()); 1179 assertEquals(0, def.getTotalOut()); 1180 assertEquals(0, def.getBytesRead()); 1181 // Encode a String into bytes 1182 String inputString = "blahblahblah??"; 1183 byte[] input = inputString.getBytes("UTF-8"); 1184 1185 // Compress the bytes 1186 byte[] output = new byte[100]; 1187 def.setInput(input); 1188 def.finish(); 1189 int compressedDataLength = def.deflate(output); 1190 assertEquals(14, def.getTotalIn()); 1191 assertEquals(compressedDataLength, def.getTotalOut()); 1192 assertEquals(14, def.getBytesRead()); 1193 } 1194 1195 /** 1196 * @throws DataFormatException 1197 * @throws UnsupportedEncodingException 1198 * @tests java.util.zip.Deflater#getBytesRead() 1199 */ 1200 @TestTargetNew( 1201 level = TestLevel.COMPLETE, 1202 notes = "", 1203 method = "getBytesWritten", 1204 args = {} 1205 ) 1206 public void test_getBytesWritten() throws DataFormatException, 1207 UnsupportedEncodingException { 1208 // Regression test for HARMONY-158 1209 Deflater def = new Deflater(); 1210 assertEquals(0, def.getTotalIn()); 1211 assertEquals(0, def.getTotalOut()); 1212 assertEquals(0, def.getBytesWritten()); 1213 // Encode a String into bytes 1214 String inputString = "blahblahblah??"; 1215 byte[] input = inputString.getBytes("UTF-8"); 1216 1217 // Compress the bytes 1218 byte[] output = new byte[100]; 1219 def.setInput(input); 1220 def.finish(); 1221 int compressedDataLength = def.deflate(output); 1222 assertEquals(14, def.getTotalIn()); 1223 assertEquals(compressedDataLength, def.getTotalOut()); 1224 assertEquals(compressedDataLength, def.getBytesWritten()); 1225 } 1226 1227 // BEGIN android-removed 1228 // We use different default settings for deflating, so our output won't be 1229 // the 1230 // same. 1231 // //Regression Test for HARMONY-2481 1232 // public void test_deflate_beforeSetInput() throws Exception { 1233 // Deflater deflater = new Deflater(); 1234 // deflater.finish(); 1235 // byte[] buffer = new byte[1024]; 1236 // assertEquals(8, deflater.deflate(buffer)); 1237 // byte[] expectedBytes = { 120, -100, 3, 0, 0, 0, 0, 1 }; 1238 // for (int i = 0; i < expectedBytes.length; i++) { 1239 // assertEquals(expectedBytes[i], buffer[i]); 1240 // } 1241 // } 1242 // END android-removed 1243} 1244