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