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