You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/03/15 12:47:39 UTC
svn commit: r386058 [3/49] - in /incubator/harmony/enhanced/classlib/trunk:
make/ modules/archive/make/common/ modules/archive/src/test/java/tests/
modules/archive/src/test/java/tests/api/
modules/archive/src/test/java/tests/api/java/ modules/archive/s...
Added: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/DeflaterTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/DeflaterTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/DeflaterTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/DeflaterTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,1138 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package tests.api.java.util.zip;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.Adler32;
+import java.util.zip.DataFormatException;
+import java.util.zip.Deflater;
+import java.util.zip.Inflater;
+
+import tests.support.resource.Support_Resources;
+
+public class DeflaterTest extends junit.framework.TestCase {
+
+ class MyDeflater extends java.util.zip.Deflater {
+ MyDeflater() {
+ super();
+ }
+
+ MyDeflater(int lvl) {
+ super(lvl);
+ }
+
+ MyDeflater(int lvl, boolean noHeader) {
+ super(lvl, noHeader);
+ }
+
+ void myFinalize() {
+ finalize();
+ }
+
+ int getDefCompression() {
+ return DEFAULT_COMPRESSION;
+ }
+
+ int getDefStrategy() {
+ return DEFAULT_STRATEGY;
+ }
+
+ int getHuffman() {
+ return HUFFMAN_ONLY;
+ }
+
+ int getFiltered() {
+ return FILTERED;
+ }
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#deflate(byte[])
+ */
+ public void test_deflate$B() {
+ // test method of java.util.zip.deflater.deflate(byte)
+ byte outPutBuf[] = new byte[50];
+ byte byteArray[] = { 1, 3, 4, 7, 8 };
+ byte outPutInf[] = new byte[50];
+ int x = 0;
+
+ Deflater defl = new Deflater();
+ defl.setInput(byteArray);
+ defl.finish();
+ while (!defl.finished())
+ x += defl.deflate(outPutBuf);
+ assertTrue("Deflater at end of stream, should return 0", defl
+ .deflate(outPutBuf) == 0);
+ int totalOut = defl.getTotalOut();
+ int totalIn = defl.getTotalIn();
+ assertTrue(
+ "The total number of bytes from deflate did not equal getTotalOut",
+ x == totalOut);
+ assertTrue(
+ "The number of input bytes from the array did not correspond with getTotalIn: Was "
+ + totalIn + " Should Be " + byteArray.length,
+ totalIn == byteArray.length);
+ defl.end();
+
+ Inflater infl = new Inflater();
+ try {
+ infl.setInput(outPutBuf);
+ while (!infl.finished())
+ infl.inflate(outPutInf);
+ } catch (DataFormatException e) {
+ fail("Invalid input to be decompressed");
+ }
+ assertTrue(
+ "Inflates getTotalOut() did not correspond with deflates getTotalIn()",
+ infl.getTotalOut() == totalIn);
+ assertTrue(
+ "Inflates getTotalIn() did not correspond with deflates getTotalOut()",
+ infl.getTotalIn() == totalOut);
+ for (int i = 0; i < byteArray.length; i++)
+ assertTrue(
+ "Final decompressed data does not equal the original data",
+ byteArray[i] == outPutInf[i]);
+ assertTrue(
+ "Final decompressed data contained more bytes than original",
+ outPutInf[byteArray.length] == 0);
+ infl.end();
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#deflate(byte[], int, int)
+ */
+ public void test_deflate$BII() {
+ // test method of java.util.zip.deflater.deflate(byte,int,int)
+ byte outPutBuf[] = new byte[50];
+ byte byteArray[] = { 5, 2, 3, 7, 8 };
+ byte outPutInf[] = new byte[50];
+ int offSet = 1;
+ int length = outPutBuf.length - 1;
+ int x = 0;
+
+ Deflater defl = new Deflater();
+ defl.setInput(byteArray);
+ defl.finish();
+ while (!defl.finished())
+ x += defl.deflate(outPutBuf, offSet, length);
+ assertTrue("Deflater at end of stream, should return 0", defl.deflate(
+ outPutBuf, offSet, length) == 0);
+ int totalOut = defl.getTotalOut();
+ int totalIn = defl.getTotalIn();
+ assertTrue(
+ "The total number of bytes from deflate did not equal getTotalOut",
+ totalOut == x);
+ assertTrue(
+ "The number of input bytes from the array did not correspond with getTotalIn - side effect of deflate()",
+ totalIn == byteArray.length);
+ defl.end();
+
+ Inflater infl = new Inflater();
+ try {
+ infl.setInput(outPutBuf, offSet, length);
+ while (!infl.finished())
+ infl.inflate(outPutInf);
+ } catch (DataFormatException e) {
+ fail("Invalid input to be decompressed");
+ }
+ assertTrue(
+ "inflates getTotalOut() did not correspond with deflates getTotalIn()",
+ infl.getTotalOut() == totalIn);
+ assertTrue(
+ "inflates getTotalIn() did not correspond with deflates getTotalOut()",
+ infl.getTotalIn() == totalOut);
+ for (int i = 0; i < byteArray.length; i++)
+ assertTrue(
+ "Final decompressed data does not equal the original data",
+ byteArray[i] == outPutInf[i]);
+ assertTrue(
+ "Final decompressed data contained more bytes than original",
+ outPutInf[byteArray.length] == 0);
+ infl.end();
+
+ // Set of tests testing the boundaries of the offSet/length
+ defl = new Deflater();
+ outPutBuf = new byte[100];
+ defl.setInput(byteArray);
+ for (int i = 0; i < 2; i++) {
+ if (i == 0) {
+ offSet = outPutBuf.length + 1;
+ length = outPutBuf.length;
+ } else {
+ offSet = 0;
+ length = outPutBuf.length + 1;
+ }
+ try {
+ defl.deflate(outPutBuf, offSet, length);
+ fail("Test " + i
+ + ": ArrayIndexOutOfBoundsException not thrown");
+ } catch (ArrayIndexOutOfBoundsException e) {
+ }
+ }
+ defl.end();
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#end()
+ */
+ public void test_end() {
+ // test method java.util.zip.deflater.end();
+
+ byte byteArray[] = { 5, 2, 3, 7, 8 };
+ byte outPutBuf[] = new byte[100];
+
+ Deflater defl = new Deflater();
+ defl.setInput(byteArray);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ defl.end();
+ helper_end_test(defl, "end");
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#finalize()
+ */
+ public void test_finalize() {
+ // test method java.util.zip.deflater.finalize()
+ MyDeflater mdefl = new MyDeflater();
+ mdefl.myFinalize();
+ System.gc();
+ helper_end_test(mdefl, "finalize");
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#finish()
+ */
+ public void test_finish() {
+ // test method java.util.zip.deflater.finish()
+ // This test already here, its the same as test_deflate()
+ byte byteArray[] = { 5, 2, 3, 7, 8 };
+ byte outPutBuf[] = new byte[100];
+ byte outPutInf[] = new byte[100];
+ int x = 0;
+ Deflater defl = new Deflater();
+ defl.setInput(byteArray);
+ defl.finish();
+
+ // needsInput should never return true after finish() is called
+ if (System.getProperty("java.vendor").startsWith("IBM"))
+ assertTrue(
+ "needsInput() should return false after finish() is called",
+ !defl.needsInput());
+
+ while (!defl.finished())
+ x += defl.deflate(outPutBuf);
+ int totalOut = defl.getTotalOut();
+ int totalIn = defl.getTotalIn();
+ assertTrue(
+ "The total number of bytes from deflate did not equal getTotalOut",
+ x == totalOut);
+ assertTrue(
+ "The number of input bytes from the array did not correspond with getTotalIn",
+ totalIn == byteArray.length);
+ defl.end();
+
+ Inflater infl = new Inflater();
+ try {
+ infl.setInput(outPutBuf);
+ while (!infl.finished())
+ infl.inflate(outPutInf);
+ } catch (DataFormatException e) {
+ fail("Invalid input to be decompressed");
+ }
+ assertTrue(
+ "Inflates getTotalOut() did not correspond with deflates getTotalIn()",
+ infl.getTotalOut() == totalIn);
+ assertTrue(
+ "Inflates getTotalIn() did not correspond with deflates getTotalOut()",
+ infl.getTotalIn() == totalOut);
+ for (int i = 0; i < byteArray.length; i++)
+ assertTrue(
+ "Final decompressed data does not equal the original data",
+ byteArray[i] == outPutInf[i]);
+ assertTrue(
+ "Final decompressed data contained more bytes than original",
+ outPutInf[byteArray.length] == 0);
+ infl.end();
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#finished()
+ */
+ public void test_finished() {
+ // test method java.util.zip.deflater.finish()
+ byte byteArray[] = { 5, 2, 3, 7, 8 };
+ byte outPutBuf[] = new byte[100];
+ Deflater defl = new Deflater();
+ assertTrue("Test 1: Deflater should not be finished.", !defl.finished());
+ defl.setInput(byteArray);
+ assertTrue("Test 2: Deflater should not be finished.", !defl.finished());
+ defl.finish();
+ assertTrue("Test 3: Deflater should not be finished.", !defl.finished());
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ assertTrue("Test 4: Deflater should be finished.", defl.finished());
+ defl.end();
+ assertTrue("Test 5: Deflater should be finished.", defl.finished());
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#getAdler()
+ */
+ public void test_getAdler() {
+ // test method for java.util.zip.Deflater.getAdler()
+ byte byteArray[] = { 'a', 'b', 'c', 1, 2, 3 };
+ byte outPutBuf[] = new byte[100];
+ Deflater defl = new Deflater();
+
+ // getting the checkSum value using the Adler
+ defl.setInput(byteArray);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ long checkSumD = defl.getAdler();
+ defl.end();
+
+ // getting the checkSum value through the Adler32 class
+ Adler32 adl = new Adler32();
+ adl.update(byteArray);
+ long checkSumR = adl.getValue();
+ assertTrue(
+ "The checksum value returned by getAdler() is not the same as the checksum returned by creating the adler32 instance",
+ checkSumR == checkSumD);
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#getTotalIn()
+ */
+ public void test_getTotalIn() {
+ // test method java.util.zip.deflater.getTotalIn()
+ byte outPutBuf[] = new byte[5];
+ byte byteArray[] = { 1, 3, 4, 7, 8 };
+
+ Deflater defl = new Deflater();
+ defl.setInput(byteArray);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ assertTrue(
+ "The number of input byte from the array did not correspond with getTotalIn",
+ defl.getTotalIn() == byteArray.length);
+ defl.end();
+
+ defl = new Deflater();
+ int offSet = 2;
+ int length = 3;
+ outPutBuf = new byte[5];
+ defl.setInput(byteArray, offSet, length);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ assertTrue(
+ "The number of input byte sent to setInputBII() did not corrsepond with getTotalIn",
+ defl.getTotalIn() == length);
+ defl.end();
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#getTotalOut()
+ */
+ public void test_getTotalOut() {
+ // test method java.util.zip.deflater.getTotalOut()
+ // the getTotalOut should equal the sum of value returned by deflate()
+ byte outPutBuf[] = new byte[5];
+ byte byteArray[] = { 5, 2, 3, 7, 8 };
+ int x = 0;
+ Deflater defl = new Deflater();
+ defl.setInput(byteArray);
+ defl.finish();
+ while (!defl.finished())
+ x += defl.deflate(outPutBuf);
+ assertTrue(
+ "The total number of bytes from deflate() did not equal getTotalOut",
+ x == defl.getTotalOut());
+ defl.end();
+
+ x = 0;
+ int offSet = 2;
+ int length = 3;
+ defl = new Deflater();
+ outPutBuf = new byte[5];
+ defl.setInput(byteArray, offSet, length);
+ defl.finish();
+ while (!defl.finished())
+ x += defl.deflate(outPutBuf);
+ assertTrue(
+ "The total number of bytes from deflateBII() did not equal getTotalOut",
+ x == defl.getTotalOut());
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#needsInput()
+ */
+ public void test_needsInput() {
+ // test method of java.util.zip.deflater.needsInput()
+ Deflater defl = new Deflater();
+ assertTrue(
+ "needsInput give the wrong boolean value as a result of no input buffer",
+ defl.needsInput());
+ byte byteArray[] = { 1, 2, 3 };
+ defl.setInput(byteArray);
+ assertTrue(
+ "needsInput give wrong boolean value as a result of a full input buffer",
+ !defl.needsInput());
+ byte[] outPutBuf = new byte[50];
+ while (!defl.needsInput())
+ defl.deflate(outPutBuf);
+ byte emptyByteArray[] = new byte[0];
+ defl.setInput(emptyByteArray);
+ assertTrue(
+ "needsInput give wrong boolean value as a result of an empty input buffer",
+ defl.needsInput());
+ defl.setInput(byteArray);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ // needsInput should NOT return true after finish() has been
+ // called.
+ if (System.getProperty("java.vendor").startsWith("IBM"))
+ assertTrue(
+ "needsInput gave wrong boolean value as a result of finish() being called",
+ !defl.needsInput());
+ defl.end();
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#reset()
+ */
+ public void test_reset() {
+ // test method of java.util.zip.deflater.reset()
+ byte outPutBuf[] = new byte[100];
+ byte outPutInf[] = new byte[100];
+ byte curArray[] = new byte[5];
+ byte byteArray[] = { 1, 3, 4, 7, 8 };
+ byte byteArray2[] = { 8, 7, 4, 3, 1 };
+ int x = 0;
+ int orgValue = 0;
+ Deflater defl = new Deflater();
+
+ for (int i = 0; i < 3; i++) {
+ if (i == 0)
+ curArray = byteArray;
+ else if (i == 1)
+ curArray = byteArray2;
+ else
+ defl.reset();
+
+ defl.setInput(curArray);
+ defl.finish();
+ while (!defl.finished())
+ x += defl.deflate(outPutBuf);
+
+ if (i == 0)
+ assertTrue(
+ "The total number of bytes from deflate did not equal getTotalOut",
+ x == defl.getTotalOut());
+ else if (i == 1)
+ assertTrue(
+ "The total number of bytes from deflate should still be the same ("
+ + x + ")", x == orgValue);
+ else
+ assertTrue(
+ "The total number of bytes from deflate should be doubled ("
+ + orgValue * 2 + ")", x == orgValue * 2);
+
+ if (i == 0)
+ orgValue = x;
+
+ try {
+ Inflater infl = new Inflater();
+ infl.setInput(outPutBuf);
+ while (!infl.finished())
+ infl.inflate(outPutInf);
+ infl.end();
+ } catch (DataFormatException e) {
+ fail("Test " + i + ": Invalid input to be decompressed");
+ }
+
+ if (i == 1)
+ curArray = byteArray;
+
+ for (int j = 0; j < curArray.length; j++)
+ assertTrue(
+ "Test "
+ + i
+ + ": Final decompressed data does not equal the original data",
+ curArray[j] == outPutInf[j]);
+ assertTrue(
+ "Test "
+ + i
+ + ": Final decompressed data contained more bytes than original",
+ outPutInf[curArray.length] == 0);
+ }
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#setDictionary(byte[])
+ */
+ public void test_setDictionary$B() {
+ // test method of java.util.zip.deflater.setDictionary(byte[])
+ // This test is very close to getAdler()
+ byte dictionaryArray[] = { 'e', 'r', 't', 'a', 'b', 2, 3 };
+ byte byteArray[] = { 4, 5, 3, 2, 'a', 'b', 6, 7, 8, 9, 0, 's', '3',
+ 'w', 'r' };
+ byte outPutBuf[] = new byte[100];
+
+ Deflater defl = new Deflater();
+ long deflAdler = defl.getAdler();
+ assertTrue(
+ "No dictionary set, no data deflated, getAdler should return 1",
+ deflAdler == 1);
+ defl.setDictionary(dictionaryArray);
+ deflAdler = defl.getAdler();
+
+ // getting the checkSum value through the Adler32 class
+ Adler32 adl = new Adler32();
+ adl.update(dictionaryArray);
+ long realAdler = adl.getValue();
+ assertTrue(
+ "Dictionary is set, getAdler() should equal the adler value of the dictionaryArray",
+ deflAdler == realAdler);
+
+ defl.setInput(byteArray);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ deflAdler = defl.getAdler();
+ adl = new Adler32();
+ adl.update(byteArray);
+ realAdler = adl.getValue();
+ // Deflate is finished and there were bytes deflated that did not occur
+ // in the dictionaryArray, therefore a new dictionary was automatically
+ // set.
+ assertTrue("getAdler() returned " + deflAdler + " should be "
+ + realAdler, deflAdler == realAdler);
+ defl.end();
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#setDictionary(byte[], int, int)
+ */
+ public void test_setDictionary$BII() {
+ // test method of java.util.zip.deflater.setDictionary(byte)
+ // This test is very close to getAdler()
+ byte dictionaryArray[] = { 'e', 'r', 't', 'a', 'b', 2, 3, 'o', 't' };
+ byte byteArray[] = { 4, 5, 3, 2, 'a', 'b', 6, 7, 8, 9, 0, 's', '3',
+ 'w', 'r', 't', 'u', 'i', 'o', 4, 5, 6, 7 };
+ byte outPutBuf[] = new byte[500];
+
+ int offSet = 4;
+ int length = 5;
+
+ Deflater defl = new Deflater();
+ long deflAdler = defl.getAdler();
+ assertTrue(
+ "No dictionary set, no data deflated, getAdler should return 1",
+ deflAdler == 1);
+ defl.setDictionary(dictionaryArray, offSet, length);
+ deflAdler = defl.getAdler();
+
+ // getting the checkSum value through the Adler32 class
+ Adler32 adl = new Adler32();
+ adl.update(dictionaryArray, offSet, length);
+ long realAdler = adl.getValue();
+ assertTrue(
+ "Dictionary is set, getAdler() should equal the adler value of the dictionaryArray",
+ deflAdler == realAdler);
+
+ defl.setInput(byteArray);
+ while (!defl.needsInput())
+ defl.deflate(outPutBuf);
+ deflAdler = defl.getAdler();
+ adl = new Adler32();
+ adl.update(byteArray);
+ realAdler = adl.getValue();
+ // Deflate is finished and there were bytes deflated that did not occur
+ // in the dictionaryArray, therefore a new dictionary was automatically
+ // set.
+ assertTrue("getAdler() returned " + deflAdler + " should be "
+ + realAdler, deflAdler == realAdler);
+ defl.end();
+
+ // boundary check
+ defl = new Deflater();
+ for (int i = 0; i < 2; i++) {
+ if (i == 0) {
+ offSet = 0;
+ length = dictionaryArray.length + 1;
+ } else {
+ offSet = dictionaryArray.length + 1;
+ length = 1;
+ }
+ try {
+ defl.setDictionary(dictionaryArray, offSet, length);
+ fail(
+ "Test "
+ + i
+ + ": boundary check for setDictionary failed for offset "
+ + offSet + " and length " + length);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ }
+ }
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#setInput(byte[])
+ */
+ public void test_setInput$B() {
+ // test method of java.util.zip.Deflater.setInput(byte)
+ byte[] byteArray = { 1, 2, 3 };
+ byte[] outPutBuf = new byte[50];
+ byte[] outPutInf = new byte[50];
+
+ Deflater defl = new Deflater();
+ defl.setInput(byteArray);
+ assertTrue("the array buffer in setInput() is empty", !defl
+ .needsInput());
+ // The second setInput() should be ignored since needsInput() return
+ // false
+ defl.setInput(byteArray);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ defl.end();
+
+ Inflater infl = new Inflater();
+ try {
+ infl.setInput(outPutBuf);
+ while (!infl.finished())
+ infl.inflate(outPutInf);
+ } catch (DataFormatException e) {
+ fail("Invalid input to be decompressed");
+ }
+ for (int i = 0; i < byteArray.length; i++)
+ assertTrue(
+ "Final decompressed data does not equal the original data",
+ byteArray[i] == outPutInf[i]);
+ assertTrue(
+ "Inflater.getTotalOut should have been equal to the length of the input",
+ infl.getTotalOut() == byteArray.length);
+ infl.end();
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#setInput(byte[], int, int)
+ */
+ public void test_setInput$BII() {
+ // test methods of java.util.zip.Deflater.setInput(byte,int,int)
+ byte[] byteArray = { 1, 2, 3, 4, 5 };
+ byte[] outPutBuf = new byte[50];
+ byte[] outPutInf = new byte[50];
+ int offSet = 1;
+ int length = 3;
+
+ Deflater defl = new Deflater();
+ defl.setInput(byteArray, offSet, length);
+ assertTrue("the array buffer in setInput() is empty", !defl
+ .needsInput());
+ // The second setInput() should be ignored since needsInput() return
+ // false
+ defl.setInput(byteArray, offSet, length);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ defl.end();
+
+ Inflater infl = new Inflater();
+ try {
+ infl.setInput(outPutBuf);
+ while (!infl.finished())
+ infl.inflate(outPutInf);
+ } catch (DataFormatException e) {
+ fail("Invalid input to be decompressed");
+ }
+ for (int i = 0; i < length; i++)
+ assertTrue(
+ "Final decompressed data does not equal the original data",
+ byteArray[i + offSet] == outPutInf[i]);
+ assertTrue(
+ "Inflater.getTotalOut should have been equal to the length of the input",
+ infl.getTotalOut() == length);
+ infl.end();
+
+ // boundary check
+ defl = new Deflater();
+ for (int i = 0; i < 2; i++) {
+ if (i == 0) {
+ offSet = 0;
+ length = byteArray.length + 1;
+ } else {
+ offSet = byteArray.length + 1;
+ length = 1;
+ }
+ try {
+ defl.setInput(byteArray, offSet, length);
+ fail("Test " + i
+ + ": boundary check for setInput failed for offset "
+ + offSet + " and length " + length);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ }
+ }
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#setLevel(int)
+ */
+ public void test_setLevelI() {
+ // test methods of java.util.zip.deflater.setLevel(int)
+ // Very similar to test_Constructor(int)
+ byte[] byteArray = new byte[100];
+ try {
+ InputStream inFile = Support_Resources
+ .getStream("hyts_checkInput.txt");
+ inFile.read(byteArray);
+ inFile.close();
+ } catch (IOException e) {
+ fail("Unexpected IOException " + e + " during test");
+ }
+
+ byte[] outPutBuf;
+ int totalOut;
+ for (int i = 0; i < 10; i++) {
+ Deflater defl = new Deflater();
+ defl.setLevel(i);
+ outPutBuf = new byte[500];
+ defl.setInput(byteArray);
+ while (!defl.needsInput())
+ defl.deflate(outPutBuf);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ totalOut = defl.getTotalOut();
+ defl.end();
+
+ outPutBuf = new byte[500];
+ defl = new Deflater(i);
+ defl.setInput(byteArray);
+ while (!defl.needsInput())
+ defl.deflate(outPutBuf);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ assertTrue(
+ "getTotalOut() not equal comparing two Deflaters with same compression level.",
+ defl.getTotalOut() == totalOut);
+ defl.end();
+ }
+
+ // testing boundaries
+ try {
+ Deflater boundDefl = new Deflater();
+ // Level must be between 0-9
+ boundDefl.setLevel(-2);
+ fail(
+ "IllegalArgumentException not thrown when setting level to a number < 0.");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ Deflater boundDefl = new Deflater();
+ boundDefl.setLevel(10);
+ fail(
+ "IllegalArgumentException not thrown when setting level to a number > 9.");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#setStrategy(int)
+ */
+ public void test_setStrategyI() {
+ // test method of java.util.zip.deflater.setStrategy(int)
+ byte[] byteArray = new byte[100];
+ try {
+ InputStream inFile = Support_Resources
+ .getStream("hyts_checkInput.txt");
+ inFile.read(byteArray);
+ inFile.close();
+ } catch (IOException e) {
+ fail("Unexpected IOException " + e + " during test.");
+ }
+
+ for (int i = 0; i < 3; i++) {
+ byte outPutBuf[] = new byte[500];
+ MyDeflater mdefl = new MyDeflater();
+
+ if (i == 0)
+ mdefl.setStrategy(mdefl.getDefStrategy());
+ else if (i == 1)
+ mdefl.setStrategy(mdefl.getHuffman());
+ else
+ mdefl.setStrategy(mdefl.getFiltered());
+
+ mdefl.setInput(byteArray);
+ while (!mdefl.needsInput())
+ mdefl.deflate(outPutBuf);
+ mdefl.finish();
+ while (!mdefl.finished())
+ mdefl.deflate(outPutBuf);
+
+ if (i == 0) {
+ // System.out.println(mdefl.getTotalOut());
+ // ran JDK and found that getTotalOut() = 86 for this particular
+ // file
+ assertTrue(
+ "getTotalOut() for the default strategy did not correspond with JDK",
+ mdefl.getTotalOut() == 86);
+ } else if (i == 1) {
+ // System.out.println(mdefl.getTotalOut());
+ // ran JDK and found that getTotalOut() = 100 for this
+ // particular file
+ assertTrue(
+ "getTotalOut() for the Huffman strategy did not correspond with JDK",
+ mdefl.getTotalOut() == 100);
+ } else {
+ // System.out.println(mdefl.getTotalOut());
+ // ran JDK and found that totalOut = 93 for this particular file
+ assertTrue(
+ "Total Out for the Filtered strategy did not correspond with JDK",
+ mdefl.getTotalOut() == 93);
+ }
+ mdefl.end();
+ }
+
+ // Attempting to setStrategy to an invalid value
+ try {
+ Deflater defl = new Deflater();
+ defl.setStrategy(-412);
+ fail(
+ "IllegalArgumentException not thrown when setting strategy to an invalid value.");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#Deflater()
+ */
+ public void test_Constructor() {
+ // test methods of java.util.zip.Deflater()
+ byte[] byteArray = new byte[100];
+ try {
+
+ InputStream inFile = Support_Resources
+ .getStream("hyts_checkInput.txt");
+ inFile.read(byteArray);
+ inFile.close();
+ } catch (IOException e) {
+ fail("Unexpected IOException " + e + " during test");
+ }
+
+ Deflater defl = new Deflater();
+ byte[] outPutBuf = new byte[500];
+ defl.setInput(byteArray);
+ while (!defl.needsInput())
+ defl.deflate(outPutBuf);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ int totalOut = defl.getTotalOut();
+ defl.end();
+
+ // creating a Deflater using the DEFAULT_COMPRESSION as the int
+ MyDeflater mdefl = new MyDeflater();
+ mdefl = new MyDeflater(mdefl.getDefCompression());
+ outPutBuf = new byte[500];
+ mdefl.setInput(byteArray);
+ while (!mdefl.needsInput())
+ mdefl.deflate(outPutBuf);
+ mdefl.finish();
+ while (!mdefl.finished())
+ mdefl.deflate(outPutBuf);
+ assertTrue(
+ "getTotalOut() not equal comparing two Deflaters with same compression level.",
+ mdefl.getTotalOut() == totalOut);
+ mdefl.end();
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#Deflater(int, boolean)
+ */
+ public void test_ConstructorIZ() {
+ // test methods of java.util.zip.deflater(int,bool)
+ byte byteArray[] = { 4, 5, 3, 2, 'a', 'b', 6, 7, 8, 9, 0, 's', '3',
+ 'w', 'r' };
+
+ Deflater defl = new Deflater();
+ byte outPutBuf[] = new byte[500];
+ defl.setLevel(2);
+ defl.setInput(byteArray);
+ while (!defl.needsInput())
+ defl.deflate(outPutBuf);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ int totalOut = defl.getTotalOut();
+ defl.end();
+
+ outPutBuf = new byte[500];
+ defl = new Deflater(2, false);
+ defl.setInput(byteArray);
+ while (!defl.needsInput())
+ defl.deflate(outPutBuf);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ assertTrue(
+ "getTotalOut() not equal comparing two Deflaters with same compression level.",
+ defl.getTotalOut() == totalOut);
+ defl.end();
+
+ outPutBuf = new byte[500];
+ defl = new Deflater(2, true);
+ defl.setInput(byteArray);
+ while (!defl.needsInput())
+ defl.deflate(outPutBuf);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ assertTrue(
+ "getTotalOut() should not be equal comparing two Deflaters with different header options.",
+ defl.getTotalOut() != totalOut);
+ defl.end();
+
+ byte outPutInf[] = new byte[500];
+ Inflater infl = new Inflater(true);
+ try {
+ while (!infl.finished()) {
+ if (infl.needsInput())
+ infl.setInput(outPutBuf);
+ infl.inflate(outPutInf);
+ }
+ } catch (DataFormatException e) {
+ fail(
+ "invalid input to inflate - called in test constructorIZ");
+ }
+ for (int i = 0; i < byteArray.length; i++)
+ assertTrue(
+ "Final decompressed data does not equal the original data",
+ byteArray[i] == outPutInf[i]);
+ assertTrue(
+ "final decompressed data contained more bytes than original - construcotrIZ",
+ outPutInf[byteArray.length] == 0);
+ infl.end();
+
+ infl = new Inflater(false);
+ outPutInf = new byte[500];
+ int r = 0;
+ try {
+ while (!infl.finished()) {
+ if (infl.needsInput())
+ infl.setInput(outPutBuf);
+ infl.inflate(outPutInf);
+ }
+ } catch (DataFormatException e) {
+ r = 1;
+ }
+ assertTrue("header option did not correspond", r == 1);
+
+ // testing boundaries
+ try {
+ Deflater boundDefl = new Deflater();
+ // Level must be between 0-9
+ boundDefl.setLevel(-2);
+ fail(
+ "IllegalArgumentException not thrown when setting level to a number < 0.");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ Deflater boundDefl = new Deflater();
+ boundDefl.setLevel(10);
+ fail(
+ "IllegalArgumentException not thrown when setting level to a number > 9.");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ /**
+ * @tests java.util.zip.Deflater#Deflater(int)
+ */
+ public void test_ConstructorI() {
+ // test methods of java.util.zip.Deflater(int)
+ byte[] byteArray = new byte[100];
+ try {
+ InputStream inFile = Support_Resources
+ .getStream("hyts_checkInput.txt");
+ inFile.read(byteArray);
+ inFile.close();
+ } catch (IOException e) {
+ fail("Unexpected IOException " + e + " during test");
+ }
+
+ byte outPutBuf[] = new byte[500];
+ Deflater defl = new Deflater(3);
+ defl.setInput(byteArray);
+ while (!defl.needsInput())
+ defl.deflate(outPutBuf);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ int totalOut = defl.getTotalOut();
+ defl.end();
+
+ // test to see if the compression ratio is the same as setting the level
+ // on a deflater
+ outPutBuf = new byte[500];
+ defl = new Deflater();
+ defl.setLevel(3);
+ defl.setInput(byteArray);
+ while (!defl.needsInput())
+ defl.deflate(outPutBuf);
+ defl.finish();
+ while (!defl.finished())
+ defl.deflate(outPutBuf);
+ assertTrue(
+ "getTotalOut() not equal comparing two Deflaters with same compression level.",
+ defl.getTotalOut() == totalOut);
+ defl.end();
+
+ // testing boundaries
+ try {
+ Deflater boundDefl = new Deflater();
+ // Level must be between 0-9
+ boundDefl.setLevel(-2);
+ fail(
+ "IllegalArgumentException not thrown when setting level to a number < 0.");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ Deflater boundDefl = new Deflater();
+ boundDefl.setLevel(10);
+ fail(
+ "IllegalArgumentException not thrown when setting level to a number > 9.");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ private void helper_end_test(Deflater defl, String desc) {
+ // Help tests for test_end() and test_reset().
+ byte byteArray[] = { 5, 2, 3, 7, 8 };
+
+ // Methods where we expect IllegalStateException or NullPointerException
+ // to be thrown
+ try {
+ defl.getTotalOut();
+ fail("defl.getTotalOut() can still be used after " + desc
+ + " is called in test_" + desc);
+ } catch (IllegalStateException e) {
+ } catch (NullPointerException e) {
+ }
+ try {
+ defl.getTotalIn();
+ fail("defl.getTotalIn() can still be used after " + desc
+ + " is called in test_" + desc);
+ } catch (IllegalStateException e) {
+ } catch (NullPointerException e) {
+ }
+ try {
+ defl.getAdler();
+ fail("defl.getAdler() can still be used after " + desc
+ + " is called in test_" + desc);
+ } catch (IllegalStateException e) {
+ } catch (NullPointerException e) {
+ }
+ try {
+ byte[] dict = { 'a', 'b', 'c' };
+ defl.setDictionary(dict);
+ fail("defl.setDictionary() can still be used after " + desc
+ + " is called in test_" + desc);
+ } catch (IllegalStateException e) {
+ } catch (NullPointerException e) {
+ }
+ try {
+ defl.getTotalIn();
+ fail("defl.getTotalIn() can still be used after " + desc
+ + " is called in test_" + desc);
+ } catch (IllegalStateException e) {
+ } catch (NullPointerException e) {
+ }
+ try {
+ defl.getTotalIn();
+ fail("defl.getTotalIn() can still be used after " + desc
+ + " is called in test_" + desc);
+ } catch (IllegalStateException e) {
+ } catch (NullPointerException e) {
+ }
+ try {
+ defl.deflate(byteArray);
+ fail("defl.deflate() can still be used after " + desc
+ + " is called in test_" + desc);
+ } catch (IllegalStateException e) {
+ } catch (NullPointerException e) {
+ }
+
+ // Methods where we expect NullPointerException to be thrown
+ try {
+ defl.reset();
+ fail("defl.reset() can still be used after " + desc
+ + " is called in test_" + desc);
+ } catch (NullPointerException e) {
+ }
+
+ // Methods that should be allowed to be called after end() is called
+ try {
+ defl.needsInput();
+ } catch (Exception e) {
+ fail("Test 1: No exception should have been thrown: " + e
+ + " in test_" + desc);
+ }
+ try {
+ defl.setStrategy(1);
+ } catch (IllegalStateException e) {
+ fail("Test 2: No exception should have been thrown: " + e
+ + " in test_" + desc);
+ }
+ try {
+ defl.setLevel(1);
+ } catch (IllegalStateException e) {
+ fail("Test 3: No exception should have been thrown: " + e
+ + " in test_" + desc);
+ }
+ try {
+ defl.end();
+ } catch (Exception e) {
+ fail("Test 4: No exception should have been thrown: " + e
+ + " in test_" + desc);
+ }
+
+ // Methods where exceptions should be thrown
+ String vendor = System.getProperty("java.vendor");
+ if (vendor.indexOf("IBM") != -1) {
+ try {
+ defl.setInput(byteArray);
+ fail("defl.setInput() can still be used after " + desc
+ + " is called in test_" + desc);
+ } catch (IllegalStateException e) {
+ }
+ }
+ }
+
+ protected void setUp() {
+ }
+
+ protected void tearDown() {
+ }
+
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/GZIPInputStreamTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/GZIPInputStreamTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/GZIPInputStreamTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/GZIPInputStreamTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,244 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package tests.api.java.util.zip;
+
+
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.zip.Checksum;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+
+import tests.support.resource.Support_Resources;
+
+public class GZIPInputStreamTest extends junit.framework.TestCase {
+ File resources;
+
+ class TestGZIPInputStream extends GZIPInputStream {
+ TestGZIPInputStream(InputStream in) throws IOException {
+ super(in);
+ }
+
+ TestGZIPInputStream(InputStream in, int size) throws IOException {
+ super(in, size);
+ }
+
+ Checksum getChecksum() {
+ return crc;
+ }
+
+ boolean endofInput() {
+ return eos;
+ }
+ }
+
+ /**
+ * @tests java.util.zip.GZIPInputStream#GZIPInputStream(java.io.InputStream)
+ */
+ public void test_ConstructorLjava_io_InputStream() {
+ // test method java.util.zip.GZIPInputStream.constructor
+ try {
+ Support_Resources.copyFile(resources, "GZIPInputStream",
+ "hyts_gInput.txt");
+ final URL gInput = new File(resources.toString()
+ + "/GZIPInputStream/hyts_gInput.txt").toURL();
+ TestGZIPInputStream inGZIP = new TestGZIPInputStream(gInput
+ .openConnection().getInputStream());
+ assertTrue("the constructor for GZIPInputStream is null",
+ inGZIP != null);
+ assertTrue("the CRC value of the inputStream is not zero", inGZIP
+ .getChecksum().getValue() == 0);
+ inGZIP.close();
+ } catch (IOException e) {
+ fail(
+ "an IO error occured while trying to open the input file");
+ }
+ }
+
+ /**
+ * @tests java.util.zip.GZIPInputStream#GZIPInputStream(java.io.InputStream,
+ * int)
+ */
+ public void test_ConstructorLjava_io_InputStreamI() {
+ // test method java.util.zip.GZIPInputStream.constructorI
+ try {
+ Support_Resources.copyFile(resources, "GZIPInputStream",
+ "hyts_gInput.txt");
+ final URL gInput = new File(resources.toString()
+ + "/GZIPInputStream/hyts_gInput.txt").toURL();
+ TestGZIPInputStream inGZIP = new TestGZIPInputStream(gInput
+ .openConnection().getInputStream(), 200);
+ assertTrue("the constructor for GZIPInputStream is null",
+ inGZIP != null);
+ assertTrue("the CRC value of the inputStream is not zero", inGZIP
+ .getChecksum().getValue() == 0);
+ inGZIP.close();
+ } catch (IOException e) {
+ fail(
+ "an IO error occured while trying to open the input file");
+ }
+ }
+
+ /**
+ * @tests java.util.zip.GZIPInputStream#read(byte[], int, int)
+ */
+ public void test_read$BII() {
+ // test method java.util.zip.GZIPInputStream.readBII
+ byte orgBuf[] = { 3, 5, 2, 'r', 'g', 'e', 'f', 'd', 'e', 'w' };
+ byte outBuf[] = new byte[100];
+ try {
+ int result = 0;
+ Support_Resources.copyFile(resources, "GZIPInputStream",
+ "hyts_gInput.txt");
+ String resPath = resources.toString();
+ if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\')
+ resPath = resPath.substring(1);
+ final URL gInput = new URL("file:/" + resPath
+ + "/GZIPInputStream/hyts_gInput.txt");
+ TestGZIPInputStream inGZIP = new TestGZIPInputStream(gInput
+ .openConnection().getInputStream());
+ while (!(inGZIP.endofInput())) {
+ result += inGZIP.read(outBuf, result, outBuf.length - result);
+ }
+ assertTrue(
+ "the checkSum value of the compressed and decompressed data does not equal",
+ inGZIP.getChecksum().getValue() == 3097700292L);
+ for (int i = 0; i < orgBuf.length; i++) {
+ assertTrue(
+ "the decompressed data does not equal the orginal data decompressed",
+ orgBuf[i] == outBuf[i]);
+ // System.out.println(orgBuf[i] + " " + outBuf[i]);
+ }
+ int r = 0;
+ try {
+ inGZIP.read(outBuf, 100, 1);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ r = 1;
+ }
+ inGZIP.close();
+ assertTrue("Boundary Check was not present", r == 1);
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail("unexpected: " + e);
+ }
+
+ try {
+ // Create compressed data which is exactly 512 bytes (after the
+ // header),
+ // the size of the InflaterStream internal buffer
+ byte[] test = new byte[507];
+ for (int i = 0; i < 256; i++)
+ test[i] = (byte) i;
+ for (int i = 256; i < test.length; i++)
+ test[i] = (byte) (256 - i);
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ GZIPOutputStream out = new GZIPOutputStream(bout);
+ out.write(test);
+ out.close();
+ byte[] comp = bout.toByteArray();
+ GZIPInputStream gin2 = new GZIPInputStream(
+ new ByteArrayInputStream(comp), 512);
+ int result, total = 0;
+ while ((result = gin2.read(test)) != -1)
+ total += result;
+ assertTrue("Should return -1", gin2.read() == -1);
+ gin2.close();
+ assertTrue("Incorrectly decompressed", total == test.length);
+
+ gin2 = new GZIPInputStream(new ByteArrayInputStream(comp), 512);
+ total = 0;
+ while ((result = gin2.read(new byte[200])) != -1) {
+ total += result;
+ }
+ assertTrue("Should return -1", gin2.read() == -1);
+ gin2.close();
+ assertTrue("Incorrectly decompressed", total == test.length);
+
+ gin2 = new GZIPInputStream(new ByteArrayInputStream(comp), 516);
+ total = 0;
+ while ((result = gin2.read(new byte[200])) != -1) {
+ total += result;
+ }
+ assertTrue("Should return -1", gin2.read() == -1);
+ gin2.close();
+ assertTrue("Incorrectly decompressed", total == test.length);
+
+ comp[40] = 0;
+ gin2 = new GZIPInputStream(new ByteArrayInputStream(comp), 512);
+ boolean exception = false;
+ try {
+ while (gin2.read(test) != -1)
+ ;
+ } catch (IOException e) {
+ exception = true;
+ }
+ assertTrue("Exception expected", exception);
+ } catch (IOException e) {
+ fail("Unexpected: " + e);
+ }
+ }
+
+ /**
+ * @tests java.util.zip.GZIPInputStream#close()
+ */
+ public void test_close() {
+ // test method java.util.zip.GZIPInputStream.close
+ byte outBuf[] = new byte[100];
+ try {
+ int result = 0;
+ Support_Resources.copyFile(resources, "GZIPInputStream",
+ "hyts_gInput.txt");
+ String resPath = resources.toString();
+ if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\')
+ resPath = resPath.substring(1);
+ final URL gInput = new URL("file:/" + resPath
+ + "/GZIPInputStream/hyts_gInput.txt");
+ TestGZIPInputStream inGZIP = new TestGZIPInputStream(gInput
+ .openConnection().getInputStream());
+ while (!(inGZIP.endofInput())) {
+ result += inGZIP.read(outBuf, result, outBuf.length - result);
+ }
+ assertTrue(
+ "the checkSum value of the compressed and decompressed data does not equal",
+ inGZIP.getChecksum().getValue() == 3097700292L);
+ inGZIP.close();
+ int r = 0;
+ try {
+ inGZIP.read(outBuf, 0, 1);
+ } catch (IOException e) {
+ r = 1;
+ }
+ assertTrue(
+ "GZIPInputStream can still be used after close is called",
+ r == 1);
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail("unexpected: " + e);
+ }
+ }
+
+ protected void setUp() {
+ resources = Support_Resources.createTempFolder();
+ }
+
+ protected void tearDown() {
+ }
+
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/GZIPOutputStreamTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/GZIPOutputStreamTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/GZIPOutputStreamTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/GZIPOutputStreamTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,181 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package tests.api.java.util.zip;
+
+
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.zip.Checksum;
+import java.util.zip.GZIPOutputStream;
+
+public class GZIPOutputStreamTest extends junit.framework.TestCase {
+
+ class TestGZIPOutputStream extends GZIPOutputStream {
+ TestGZIPOutputStream(OutputStream out) throws IOException {
+ super(out);
+ }
+
+ TestGZIPOutputStream(OutputStream out, int size) throws IOException {
+ super(out, size);
+ }
+
+ Checksum getChecksum() {
+ return crc;
+ }
+ }
+
+ /**
+ * @tests java.util.zip.GZIPOutputStream#GZIPOutputStream(java.io.OutputStream)
+ */
+ public void test_ConstructorLjava_io_OutputStream() {
+ try {
+ FileOutputStream outFile = new FileOutputStream("GZIPOutCon.txt");
+ TestGZIPOutputStream outGZIP = new TestGZIPOutputStream(outFile);
+ assertTrue("the constructor for GZIPOutputStream is null",
+ outGZIP != null);
+ assertTrue("the CRC value of the outputStream is not zero", outGZIP
+ .getChecksum().getValue() == 0);
+ outGZIP.close();
+ } catch (IOException e) {
+ fail(
+ "an IO error occured while trying to find the output file or creating GZIP constructor");
+ }
+ }
+
+ /**
+ * @tests java.util.zip.GZIPOutputStream#GZIPOutputStream(java.io.OutputStream,
+ * int)
+ */
+ public void test_ConstructorLjava_io_OutputStreamI() {
+ try {
+ FileOutputStream outFile = new FileOutputStream("GZIPOutCon.txt");
+ TestGZIPOutputStream outGZIP = new TestGZIPOutputStream(outFile,
+ 100);
+ assertTrue("the constructor for GZIPOutputStream is null",
+ outGZIP != null);
+ assertTrue("the CRC value of the outputStream is not zero", outGZIP
+ .getChecksum().getValue() == 0);
+ outGZIP.close();
+ } catch (IOException e) {
+ fail(
+ "an IO error occured while trying to find the output file or creating GZIP constructor");
+ }
+ }
+
+ /**
+ * @tests java.util.zip.GZIPOutputStream#finish()
+ */
+ public void test_finish() {
+ // test method java.util.zip.GZIPOutputStream.finish()
+ byte byteArray[] = { 3, 5, 2, 'r', 'g', 'e', 'f', 'd', 'e', 'w' };
+ try {
+ FileOutputStream outFile = new FileOutputStream("GZIPOutFinish.txt");
+ TestGZIPOutputStream outGZIP = new TestGZIPOutputStream(outFile);
+
+ outGZIP.finish();
+ int r = 0;
+ try {
+ outGZIP.write(byteArray, 0, 1);
+ } catch (IOException e) {
+ r = 1;
+ }
+
+ assertTrue(
+ "GZIP instance can still be used after finish is called",
+ r == 1);
+ outGZIP.close();
+ } catch (IOException e) {
+ fail(
+ "an IO error occured while trying to find the output file or creating GZIP constructor");
+ }
+ }
+
+ /**
+ * @tests java.util.zip.GZIPOutputStream#close()
+ */
+ public void test_close() {
+ // test method java.util.zip.GZIPOutputStream.close()
+ byte byteArray[] = { 3, 5, 2, 'r', 'g', 'e', 'f', 'd', 'e', 'w' };
+ try {
+ FileOutputStream outFile = new FileOutputStream("GZIPOutClose2.txt");
+ TestGZIPOutputStream outGZIP = new TestGZIPOutputStream(outFile);
+ outGZIP.close();
+ int r = 0;
+ try {
+ outGZIP.write(byteArray, 0, 1);
+ } catch (IOException e) {
+ r = 1;
+ }
+ assertTrue("GZIP instance can still be used after close is called",
+ r == 1);
+ } catch (IOException e) {
+ fail(
+ "an IO error occured while trying to find the output file or creating GZIP constructor");
+ }
+ }
+
+ /**
+ * @tests java.util.zip.GZIPOutputStream#write(byte[], int, int)
+ */
+ public void test_write$BII() {
+ // test method java.util.zip.GZIPOutputStream.writeBII
+ byte byteArray[] = { 3, 5, 2, 'r', 'g', 'e', 'f', 'd', 'e', 'w' };
+ try {
+ FileOutputStream outFile = new FileOutputStream("GZIPOutWrite.txt");
+ TestGZIPOutputStream outGZIP = new TestGZIPOutputStream(outFile);
+ outGZIP.write(byteArray, 0, 10);
+ // ran JDK and found this CRC32 value is 3097700292
+ // System.out.print(outGZIP.getChecksum().getValue());
+ assertTrue(
+ "the checksum value was incorrect result of write from GZIP",
+ outGZIP.getChecksum().getValue() == 3097700292L);
+
+ // test for boundary check
+ int r = 0;
+ try {
+ outGZIP.write(byteArray, 0, 11);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ r = 1;
+ }
+ assertTrue("out of bounds exception is not present", r == 1);
+ outGZIP.close();
+ } catch (IOException e) {
+ fail(
+ "an IO error occured while trying to find the output file or creating GZIP constructor");
+ }
+ }
+
+ protected void setUp() {
+ }
+
+ protected void tearDown() {
+
+ try {
+ File dFile = new File("GZIPOutCon.txt");
+ dFile.delete();
+ /*
+ * File dFile2 = new File("GZIPOutFinish.txt"); dFile2.delete();
+ * File dFile3 = new File("GZIPOutClose.txt"); dFile3.delete(); File
+ * dFile4 = new File("GZIPOutWrite.txt"); dFile4.delete();
+ */
+ } catch (SecurityException e) {
+ fail("Cannot delete file for security reasons");
+ }
+ }
+
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/InflaterInputStreamTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/InflaterInputStreamTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/InflaterInputStreamTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/InflaterInputStreamTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,411 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package tests.api.java.util.zip;
+
+
+
+import java.io.ByteArrayInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.Inflater;
+import java.util.zip.InflaterInputStream;
+import java.util.zip.ZipException;
+
+import tests.support.resource.Support_Resources;
+
+public class InflaterInputStreamTest extends junit.framework.TestCase {
+
+ // files hyts_constru(O),hyts_constru(OD),hyts_constru(ODI) needs to be
+ // included as resources
+ byte outPutBuf[] = new byte[500];
+
+ class MyInflaterInputStream extends java.util.zip.InflaterInputStream {
+ MyInflaterInputStream(InputStream in) {
+ super(in);
+ }
+
+ MyInflaterInputStream(InputStream in, Inflater infl) {
+ super(in, infl);
+ }
+
+ MyInflaterInputStream(InputStream in, Inflater infl, int size) {
+ super(in, infl, size);
+ }
+
+ void myFill() throws IOException {
+ fill();
+ }
+ }
+
+ /**
+ * @tests java.util.zip.InflaterInputStream#InflaterInputStream(java.io.InputStream)
+ */
+ public void test_ConstructorLjava_io_InputStream() {
+ // test method
+ // java.util.zip.inflaterInputStream.InflaterInputStream(InputStream)
+ int result = 0;
+ int buffer[] = new int[500];
+ try {
+ InputStream infile = Support_Resources
+ .getStream("hyts_constru(O).txt");
+
+ InflaterInputStream inflatIP = new InflaterInputStream(infile);
+
+ int i = 0;
+ while ((result = inflatIP.read()) != -1) {
+ buffer[i] = result;
+ i++;
+ }
+ inflatIP.close();
+ } catch (FileNotFoundException e) {
+ fail(
+ "input file to test InflaterInputStream constructor is not found");
+ } catch (ZipException e) {
+ fail(
+ "read() threw an zip exception while testing constructor");
+ } catch (IOException e) {
+ fail("read() threw an exception while testing constructor");
+ }
+ }
+
+ /**
+ * @tests java.util.zip.InflaterInputStream#InflaterInputStream(java.io.InputStream,
+ * java.util.zip.Inflater)
+ */
+ public void test_ConstructorLjava_io_InputStreamLjava_util_zip_Inflater() {
+ // test method
+ // java.util.zip.inflaterInputStream.InflaterInputStream(InputStream,Inflater)
+
+ byte byteArray[] = new byte[100];
+ try {
+ InputStream infile = Support_Resources
+ .getStream("hyts_constru(OD).txt");
+ Inflater inflate = new Inflater();
+ InflaterInputStream inflatIP = new InflaterInputStream(infile,
+ inflate);
+
+ inflatIP.read(byteArray, 0, 5);// ony suppose to read in 5 bytes
+ inflatIP.close();
+ } catch (FileNotFoundException e) {
+ fail(
+ "input file to test InflaterInputStream constructor is not found");
+ } catch (ZipException e) {
+ fail(
+ "read() threw an zip exception while testing constructor");
+ } catch (IOException e) {
+ fail("read() threw an exception while testing constructor");
+ }
+ }
+
+ /**
+ * @tests java.util.zip.InflaterInputStream#InflaterInputStream(java.io.InputStream,
+ * java.util.zip.Inflater, int)
+ */
+ public void test_ConstructorLjava_io_InputStreamLjava_util_zip_InflaterI() {
+ // test method
+ // java.util.zip.inflaterInputStream.InflaterInputStream(InputStream,Inflater,int)
+ int result = 0;
+ int buffer[] = new int[500];
+ try {
+ InputStream infile = Support_Resources
+ .getStream("hyts_constru(ODI).txt");
+ Inflater inflate = new Inflater();
+ InflaterInputStream inflatIP = new InflaterInputStream(infile,
+ inflate, 1);
+
+ int i = 0;
+ while ((result = inflatIP.read()) != -1) {
+ buffer[i] = result;
+ i++;
+ }
+ inflatIP.close();
+ } catch (FileNotFoundException e) {
+ fail(
+ "input file to test InflaterInputStream constructor is not found");
+ } catch (ZipException e) {
+ fail(
+ "read() threw an zip exception while testing constructor");
+ } catch (IOException e) {
+ fail("read() threw an exception while testing constructor");
+ }
+ }
+
+ /**
+ * @tests java.util.zip.InflaterInputStream#read()
+ */
+ public void test_read() {
+ // test method java.util.zip.inflaterInputStream.Read()
+ int result = 0;
+ int buffer[] = new int[500];
+ byte orgBuffer[] = { 1, 3, 4, 7, 8 };
+ try {
+ InputStream infile = Support_Resources
+ .getStream("hyts_constru(OD).txt");
+ Inflater inflate = new Inflater();
+ InflaterInputStream inflatIP = new InflaterInputStream(infile,
+ inflate);
+
+ int i = 0;
+ while ((result = inflatIP.read()) != -1) {
+ buffer[i] = result;
+ i++;
+ }
+ inflatIP.close();
+ } catch (FileNotFoundException e) {
+ fail(
+ "input file to test InflaterInputStream constructor is not found");
+ } catch (ZipException e) {
+ fail(
+ "read() threw an zip exception while testing constructor");
+ } catch (IOException e) {
+ fail("read() threw an exception while testing constructor");
+ }
+
+ for (int j = 0; j < orgBuffer.length; j++) {
+ assertTrue(
+ "orginal compressed data did not equal decompressed data",
+ buffer[j] == orgBuffer[j]);
+ }
+ }
+
+ /**
+ * @tests java.util.zip.InflaterInputStream#read(byte[], int, int)
+ */
+ public void test_read$BII() {
+ /*
+ * // test method java.util.zip.inflaterInputStream.read(byte,int,int)
+ * byte byteArray[] = new byte[100]; byte orgBuffer[] = { 1, 3, 4, 7, 8 };
+ * try { InputStream infile = Support_Resources
+ * .getStream("hyts_constru(OD).txt"); Inflater inflate = new
+ * Inflater(); InflaterInputStream inflatIP = new
+ * InflaterInputStream(infile, inflate);
+ *
+ * inflatIP.read(byteArray, 0, 4);// ony suppose to read in 4 bytes
+ * inflatIP.close(); assertTrue( "the fifth element of byteArray
+ * contained a non zero value", byteArray[4] == 0); } catch
+ * (FileNotFoundException e) { fail( "input file to test
+ * InflaterInputStream constructor is not found"); } catch
+ * (ZipException e) { fail( "read() threw an zip exception while
+ * testing constructor"); } catch (IOException e) {
+ * assertTrue("read() threw an exception while testing constructor",
+ * false); }
+ *
+ * for (int j = 0; j < 4; j++) { assertTrue( "orginal compressed data
+ * did not equal decompressed data", byteArray[j] == orgBuffer[j]); }
+ *
+ * InflaterInputStream iis = setupIISForReadTest(); try { int n =
+ * iis.read(); n = iis.read(new byte[1], -1, 0); fail("FAILED: expected
+ * IOOBE, but the method returns: " //$NON-NLS-1$ + n); } catch
+ * (IOException e) { fail("FAILED: unexpected " + e); //$NON-NLS-1$ }
+ * catch (IndexOutOfBoundsException e2) { // NO OP }
+ *
+ * iis = setupIISForReadTest(); try { int n = iis.read(); n =
+ * iis.read(null, 0, 1); fail("FAILED: expected NPE, but the method
+ * returns: " //$NON-NLS-1$ + n); } catch (IOException e) {
+ * fail("FAILED: unexpected " + e); //$NON-NLS-1$ } catch
+ * (NullPointerException e2) { // NO OP }
+ *
+ * iis = setupIISForReadTest(); try { int n = iis.read(); n =
+ * iis.read(new byte[1], 0, 0); assertEquals(0, n); } catch (Exception
+ * e) { fail("FAILED: unexpected " + e); //$NON-NLS-1$ }
+ */
+ }
+
+ /**
+ * @return
+ */
+ private InflaterInputStream setupIISForReadTest() {
+ /*
+ * ByteArrayOutputStream baos; DeflaterOutputStream dos;
+ * ByteArrayInputStream bais; InflaterInputStream iis;
+ *
+ * baos = new ByteArrayOutputStream(); dos = new
+ * DeflaterOutputStream(baos); try { dos.write(2); dos.close(); } catch
+ * (IOException e3) { fail(); e3.printStackTrace(); } bais = new
+ * ByteArrayInputStream(baos.toByteArray()); iis = new
+ * InflaterInputStream(bais); return iis;
+ */
+ return null;
+ }
+
+ /**
+ * @tests java.util.zip.InflaterInputStream#skip(long)
+ */
+ public void test_skipJ() {
+ // Test for method java.util.zip.Inflater.InputStream.skip(long)
+
+ try {
+ InputStream is = Support_Resources.getStream("hyts_available.tst");
+ InflaterInputStream iis = new InflaterInputStream(is);
+
+ // Tests for skipping a negative number of bytes.
+ try {
+ iis.skip(-3);
+ fail("IllegalArgumentException not thrown");
+ } catch (IllegalArgumentException e) {
+ }
+ assertTrue("Incorrect Byte Returned.", iis.read() == 5);
+
+ try {
+ iis.skip(Integer.MIN_VALUE);
+ fail("IllegalArgumentException not thrown");
+ } catch (IllegalArgumentException e) {
+ }
+ assertTrue("Incorrect Byte Returned.", iis.read() == 4);
+
+ // Test to make sure the correct number of bytes were skipped
+ assertTrue("Incorrect Number Of Bytes Skipped.", iis.skip(3) == 3);
+
+ // Test to see if the number of bytes skipped returned is true.
+ assertTrue("Incorrect Byte Returned.", iis.read() == 7);
+
+ assertTrue("Incorrect Number Of Bytes Skipped.", iis.skip(0) == 0);
+ assertTrue("Incorrect Byte Returned.", iis.read() == 0);
+
+ // Test for skipping more bytes than available in the stream
+ assertTrue("Incorrect Number Of Bytes Skipped.", iis.skip(4) == 2);
+ assertTrue("Incorrect Byte Returned.", iis.read() == -1);
+ iis.close();
+ } catch (IOException e) {
+ fail("Unexpected IOException during test");
+ }
+ }
+
+ /**
+ * @tests java.util.zip.InflaterInputStream#skip(long)
+ */
+ public void test_skipJ2() {
+ // test method java.util.zip.inflaterInputStream.skip(long)
+ int result = 0;
+ int buffer[] = new int[100];
+ byte orgBuffer[] = { 1, 3, 4, 7, 8 };
+ try {
+ // testing for negative input to skip
+ InputStream infile = Support_Resources
+ .getStream("hyts_constru(OD).txt");
+ Inflater inflate = new Inflater();
+ InflaterInputStream inflatIP = new InflaterInputStream(infile,
+ inflate, 10);
+ long skip;
+ try {
+ skip = inflatIP.skip(Integer.MIN_VALUE);
+ fail("Expected IllegalArgumentException when skip() is called with negative parameter");
+ } catch (IllegalArgumentException e) {
+ }
+ inflatIP.close();
+
+ // testing for number of bytes greater than input.
+ InputStream infile2 = Support_Resources
+ .getStream("hyts_constru(OD).txt");
+ InflaterInputStream inflatIP2 = new InflaterInputStream(infile2);
+
+ // looked at how many bytes the skip skipped. It is
+ // 5 and its supposed to be the entire input stream.
+
+ skip = inflatIP2.skip(Integer.MAX_VALUE);
+ // System.out.println(skip);
+ assertTrue("method skip() returned wrong number of bytes skiped",
+ skip == 5);
+
+ // test for skiping of 2 bytes
+ InputStream infile3 = Support_Resources
+ .getStream("hyts_constru(OD).txt");
+ InflaterInputStream inflatIP3 = new InflaterInputStream(infile3);
+ skip = inflatIP3.skip(2);
+ assertTrue(
+ "the number of bytes returned by skip did not correspond with its input parameters",
+ skip == 2);
+ int i = 0;
+ result = 0;
+ while ((result = inflatIP3.read()) != -1) {
+ buffer[i] = result;
+ i++;
+ }
+ inflatIP2.close();
+
+ for (int j = 2; j < orgBuffer.length; j++) {
+ assertTrue(
+ "orginal compressed data did not equal decompressed data",
+ buffer[j - 2] == orgBuffer[j]);
+ }
+ } catch (FileNotFoundException e) {
+ fail(
+ "input file to test InflaterInputStream constructor is not found");
+ } catch (ZipException e) {
+ fail(
+ "read() threw an zip exception while testing constructor");
+ } catch (IOException e) {
+ fail("read() threw an exception while testing constructor");
+ }
+ }
+
+ /**
+ * @tests java.util.zip.InflaterInputStream#available()
+ */
+ public void test_available() {
+ // Test for method java.util.zip.Inflater.InputStream.available()
+
+ try {
+
+ InputStream is = Support_Resources.getStream("hyts_available.tst");
+ InflaterInputStream iis = new InflaterInputStream(is);
+
+ int available;
+ int read;
+ for (int i = 0; i < 11; i++) {
+ read = iis.read();
+ available = iis.available();
+ if (read == -1)
+ assertTrue("Bytes Available Should Return 0 ",
+ available == 0);
+ else
+ assertTrue("Bytes Available Should Return 1.",
+ available == 1);
+ }
+
+ iis.close();
+ try {
+ iis.available();
+ fail("available after close should throw IOException.");
+ } catch (IOException e) {
+ }
+ } catch (IOException e) {
+ fail("Unexpected IOException during test");
+ }
+ }
+
+ /**
+ * @tests java.util.zip.InflaterInputStream#close()
+ */
+ public void test_close() {
+ InflaterInputStream iin = new InflaterInputStream(
+ new ByteArrayInputStream(new byte[0]));
+ try {
+ iin.close();
+ // test for exception
+ iin.close();
+ } catch (IOException e) {
+ fail("Threw exception");
+ }
+ }
+
+ protected void setUp() {
+ }
+
+ protected void tearDown() {
+ }
+
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/InflaterTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/InflaterTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/InflaterTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/tests/api/java/util/zip/InflaterTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,651 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package tests.api.java.util.zip;
+
+
+
+import java.io.BufferedInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.zip.Adler32;
+import java.util.zip.DataFormatException;
+import java.util.zip.Deflater;
+import java.util.zip.Inflater;
+import java.util.zip.ZipException;
+
+import tests.support.resource.Support_Resources;
+
+public class InflaterTest extends junit.framework.TestCase {
+ byte outPutBuff1[] = new byte[500];
+
+ byte outPutDiction[] = new byte[500];
+
+ /**
+ * @tests java.util.zip.Inflater#end()
+ */
+ public void test_end() {
+ // test method of java.util.zip.inflater.end()
+ byte byteArray[] = { 5, 2, 3, 7, 8 };
+
+ int r = 0;
+ Inflater inflate = new Inflater();
+ inflate.setInput(byteArray);
+ inflate.end();
+ try {
+ inflate.reset();
+ inflate.setInput(byteArray);
+ } catch (NullPointerException e) {
+ r = 1;
+ }
+ assertTrue("inflate can still be used after end is called", r == 1);
+
+ Inflater i = new Inflater();
+ i.end();
+ // check for exception
+ i.end();
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#finished()
+ */
+ public void test_finished() {
+ // test method of java.util.zip.inflater.finished()
+ byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
+ Inflater inflate = new Inflater(false);
+ byte outPutInf[] = new byte[500];
+ try {
+ while (!(inflate.finished())) {
+ if (inflate.needsInput()) {
+ inflate.setInput(outPutBuff1);
+ }
+
+ inflate.inflate(outPutInf);
+ }
+ assertTrue(
+ "the method finished() returned false when no more data needs to be decompressed",
+ inflate.finished() == true);
+ } catch (DataFormatException e) {
+ fail("Invalid input to be decompressed");
+ }
+ for (int i = 0; i < byteArray.length; i++) {
+ assertTrue(
+ "Final decompressed data does not equal the original data",
+ byteArray[i] == outPutInf[i]);
+ }
+ assertTrue(
+ "final decompressed data contained more bytes than original - finished()",
+ outPutInf[byteArray.length] == 0);
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#getAdler()
+ */
+ public void test_getAdler() {
+ // test method of java.util.zip.inflater.getAdler()
+ byte dictionaryArray[] = { 'e', 'r', 't', 'a', 'b', 2, 3 };
+
+ Inflater inflateDiction = new Inflater();
+ inflateDiction.setInput(outPutDiction);
+ if (inflateDiction.needsDictionary() == true) {
+ // getting the checkSum value through the Adler32 class
+ Adler32 adl = new Adler32();
+ adl.update(dictionaryArray);
+ long checkSumR = adl.getValue();
+ assertTrue(
+ "the checksum value returned by getAdler() is not the same as the checksum returned by creating the adler32 instance",
+ checkSumR == inflateDiction.getAdler());
+ }
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#getRemaining()
+ */
+ public void test_getRemaining() {
+ // test method of java.util.zip.inflater.getRemaining()
+ byte byteArray[] = { 1, 3, 5, 6, 7 };
+ Inflater inflate = new Inflater();
+ assertTrue(
+ "upon creating an instance of inflate, getRemaining returned a non zero value",
+ inflate.getRemaining() == 0);
+ inflate.setInput(byteArray);
+ assertTrue(
+ "getRemaining returned zero when there is input in the input buffer",
+ inflate.getRemaining() != 0);
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#getTotalIn()
+ */
+ public void test_getTotalIn() {
+ // test method of java.util.zip.inflater.getTotalIn()
+ // creating the decompressed data
+ byte outPutBuf[] = new byte[500];
+ byte byteArray[] = { 1, 3, 4, 7, 8 };
+ byte outPutInf[] = new byte[500];
+ int x = 0;
+ Deflater deflate = new Deflater(1);
+ deflate.setInput(byteArray);
+ while (!(deflate.needsInput())) {
+ x += deflate.deflate(outPutBuf, x, outPutBuf.length - x);
+ }
+ deflate.finish();
+ while (!(deflate.finished())) {
+ x = x + deflate.deflate(outPutBuf, x, outPutBuf.length - x);
+ }
+
+ Inflater inflate = new Inflater();
+ try {
+ while (!(inflate.finished())) {
+ if (inflate.needsInput()) {
+ inflate.setInput(outPutBuf);
+ }
+
+ inflate.inflate(outPutInf);
+ }
+ } catch (DataFormatException e) {
+ fail("Input to inflate is invalid or corrupted - getTotalIn");
+ }
+ // System.out.print(deflate.getTotalOut() + " " + inflate.getTotalIn());
+ assertTrue(
+ "the total byte in outPutBuf did not equal the byte returned in getTotalIn",
+ inflate.getTotalIn() == deflate.getTotalOut());
+
+ Inflater inflate2 = new Inflater();
+ int offSet = 0;// seems only can start as 0
+ int length = 4;
+ try {
+ // seems no while loops allowed
+ if (inflate2.needsInput()) {
+ inflate2.setInput(outPutBuff1, offSet, length);
+ }
+
+ inflate2.inflate(outPutInf);
+
+ } catch (DataFormatException e) {
+ fail("Input to inflate is invalid or corrupted - getTotalIn");
+ }
+ // System.out.print(inflate2.getTotalIn() + " " + length);
+ assertTrue(
+ "total byte dictated by length did not equal byte returned in getTotalIn",
+ inflate2.getTotalIn() == length);
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#getTotalOut()
+ */
+ public void test_getTotalOut() {
+ // test method of java.util.zip.inflater.Inflater()
+ // creating the decompressed data
+ byte outPutBuf[] = new byte[500];
+ byte byteArray[] = { 1, 3, 4, 7, 8 };
+ int y = 0;
+ int x = 0;
+ Deflater deflate = new Deflater(1);
+ deflate.setInput(byteArray);
+ while (!(deflate.needsInput())) {
+ x += deflate.deflate(outPutBuf, x, outPutBuf.length - x);
+ }
+ deflate.finish();
+ while (!(deflate.finished())) {
+ x = x + deflate.deflate(outPutBuf, x, outPutBuf.length - x);
+ }
+
+ Inflater inflate = new Inflater();
+ byte outPutInf[] = new byte[500];
+ try {
+ while (!(inflate.finished())) {
+ if (inflate.needsInput()) {
+ inflate.setInput(outPutBuf);
+ }
+
+ y += inflate.inflate(outPutInf);
+ }
+ } catch (DataFormatException e) {
+ fail("Input to inflate is invalid or corrupted - getTotalIn");
+ }
+
+ assertTrue(
+ "the sum of the bytes returned from inflate does not equal the bytes of getTotalOut()",
+ y == inflate.getTotalOut());
+ assertTrue(
+ "the total number of bytes to be compressed does not equal the total bytes decompressed",
+ inflate.getTotalOut() == deflate.getTotalIn());
+
+ // testing inflate(byte,int,int)
+ inflate.reset();
+ y = 0;
+ int offSet = 0;// seems only can start as 0
+ int length = 4;
+ try {
+ while (!(inflate.finished())) {
+ if (inflate.needsInput()) {
+ inflate.setInput(outPutBuf);
+ }
+
+ y += inflate.inflate(outPutInf, offSet, length);
+ }
+ } catch (DataFormatException e) {
+ System.out
+ .println("Input to inflate is invalid or corrupted - getTotalIn");
+ }
+ assertTrue(
+ "the sum of the bytes returned from inflate does not equal the bytes of getTotalOut()",
+ y == inflate.getTotalOut());
+ assertTrue(
+ "the total number of bytes to be compressed does not equal the total bytes decompressed",
+ inflate.getTotalOut() == deflate.getTotalIn());
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#inflate(byte[])
+ */
+ public void test_inflate$B() {
+ // test method of java.util.zip.inflater.inflate(byte)
+
+ byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
+ byte outPutInf[] = new byte[500];
+ Inflater inflate = new Inflater();
+ try {
+ while (!(inflate.finished())) {
+ if (inflate.needsInput()) {
+ inflate.setInput(outPutBuff1);
+ }
+ inflate.inflate(outPutInf);
+ }
+ } catch (DataFormatException e) {
+ fail("Invalid input to be decompressed");
+ }
+ for (int i = 0; i < byteArray.length; i++) {
+ assertTrue(
+ "Final decompressed data does not equal the original data",
+ byteArray[i] == outPutInf[i]);
+ }
+ assertTrue(
+ "final decompressed data contained more bytes than original - inflateB",
+ outPutInf[byteArray.length] == 0);
+ // testing for an empty input array
+ byte outPutBuf[] = new byte[500];
+ byte emptyArray[] = new byte[11];
+ int x = 0;
+ Deflater defEmpty = new Deflater(3);
+ defEmpty.setInput(emptyArray);
+ while (!(defEmpty.needsInput())) {
+ x += defEmpty.deflate(outPutBuf, x, outPutBuf.length - x);
+ }
+ defEmpty.finish();
+ while (!(defEmpty.finished())) {
+ x += defEmpty.deflate(outPutBuf, x, outPutBuf.length - x);
+ }
+ assertTrue(
+ "the total number of byte from deflate did not equal getTotalOut - inflate(byte)",
+ x == defEmpty.getTotalOut());
+ assertTrue(
+ "the number of input byte from the array did not correspond with getTotalIn - inflate(byte)",
+ defEmpty.getTotalIn() == emptyArray.length);
+ Inflater infEmpty = new Inflater();
+ try {
+ while (!(infEmpty.finished())) {
+ if (infEmpty.needsInput()) {
+ infEmpty.setInput(outPutBuf);
+ }
+ infEmpty.inflate(outPutInf);
+ }
+ } catch (DataFormatException e) {
+ fail("Invalid input to be decompressed");
+ }
+ for (int i = 0; i < emptyArray.length; i++) {
+ assertTrue(
+ "Final decompressed data does not equal the original data",
+ emptyArray[i] == outPutInf[i]);
+ assertTrue("Final decompressed data does not equal zero",
+ outPutInf[i] == 0);
+ }
+ assertTrue(
+ "Final decompressed data contains more element than original data",
+ outPutInf[emptyArray.length] == 0);
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#inflate(byte[], int, int)
+ */
+ public void test_inflate$BII() {
+ // test method of java.util.zip.inflater.inflate(byte,int,int)
+
+ byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
+ byte outPutInf[] = new byte[100];
+ int y = 0;
+ Inflater inflate = new Inflater();
+ try {
+ while (!(inflate.finished())) {
+ if (inflate.needsInput()) {
+ inflate.setInput(outPutBuff1);
+ }
+ y += inflate.inflate(outPutInf, y, outPutInf.length - y);
+ }
+ } catch (DataFormatException e) {
+ fail("Invalid input to be decompressed");
+ }
+ for (int i = 0; i < byteArray.length; i++) {
+ assertTrue(
+ "Final decompressed data does not equal the original data",
+ byteArray[i] == outPutInf[i]);
+ }
+ assertTrue(
+ "final decompressed data contained more bytes than original - inflateB",
+ outPutInf[byteArray.length] == 0);
+
+ // test boundary checks
+ inflate.reset();
+ int r = 0;
+ int offSet = 0;
+ int lengthError = 101;
+ try {
+ if (inflate.needsInput()) {
+ inflate.setInput(outPutBuff1);
+ }
+ inflate.inflate(outPutInf, offSet, lengthError);
+
+ } catch (DataFormatException e) {
+ fail("Invalid input to be decompressed");
+ } catch (ArrayIndexOutOfBoundsException e) {
+ r = 1;
+ }
+ assertTrue("out of bounds error did not get caught", r == 1);
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#Inflater()
+ */
+ public void test_Constructor() {
+ // test method of java.util.zip.inflater.Inflater()
+ try {
+ Inflater inflate = new Inflater();
+ assertTrue("failed to create the instance of inflater",
+ inflate != null);
+
+ } catch (Exception e) {
+
+ assertTrue("Inflate () constructor threw an exception", true);
+ }
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#Inflater(boolean)
+ */
+ public void test_ConstructorZ() {
+ // test method of java.util.zip.inflater.Inflater(boolean)
+ // note does not throw exception if deflater has a header, but inflater
+ // doesn't or vice versa.
+ byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
+ Inflater inflate = new Inflater(true);
+ assertTrue("failed to create the instance of inflater", inflate != null);
+ byte outPutInf[] = new byte[500];
+ int r = 0;
+ try {
+ while (!(inflate.finished())) {
+ if (inflate.needsInput()) {
+ inflate.setInput(outPutBuff1);
+ }
+
+ inflate.inflate(outPutInf);
+ }
+ for (int i = 0; i < byteArray.length; i++) {
+ assertTrue(
+ "the output array from inflate should contain 0 because the header of inflate and deflate did not match, but this faled",
+ outPutBuff1[i] == 0);
+ }
+ } catch (DataFormatException e) {
+ r = 1;
+ }
+ assertTrue(
+ "Error: exception should be thrown becuase of header inconsistancy",
+ r == 1);
+
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#needsDictionary()
+ */
+ public void test_needsDictionary() {
+ // test method of java.util.zip.inflater.needsDictionary()
+ // note: this flag is set after inflate is called
+ byte outPutInf[] = new byte[500];
+
+ // testing with dictionary set.
+ Inflater inflateDiction = new Inflater();
+ if (inflateDiction.needsInput()) {
+ inflateDiction.setInput(outPutDiction);
+ }
+ try {
+ assertTrue("should return 0 because needs dictionary",
+ inflateDiction.inflate(outPutInf) == 0);
+ } catch (DataFormatException e) {
+ fail("Should not cause exception");
+ }
+ assertTrue(
+ "method needsDictionary returned false when dictionary was used in deflater",
+ inflateDiction.needsDictionary() == true);
+
+ // testing without dictionary
+ Inflater inflate = new Inflater();
+ try {
+ inflate.setInput(outPutBuff1);
+ inflate.inflate(outPutInf);
+ assertTrue(
+ "method needsDictionary returned true when dictionary was not used in deflater",
+ inflate.needsDictionary() == false);
+ } catch (DataFormatException e) {
+ fail(
+ "Input to inflate is invalid or corrupted - needsDictionary");
+ }
+
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#needsInput()
+ */
+ public void test_needsInput() {
+ // test method of java.util.zip.inflater.needsInput()
+ Inflater inflate = new Inflater();
+ assertTrue(
+ "needsInput give the wrong boolean value as a result of no input buffer",
+ inflate.needsInput() == true);
+
+ byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
+ inflate.setInput(byteArray);
+ assertTrue(
+ "methodNeedsInput returned true when the input buffer is full",
+ inflate.needsInput() == false);
+
+ inflate.reset();
+ byte byteArrayEmpty[] = new byte[0];
+ inflate.setInput(byteArrayEmpty);
+ assertTrue(
+ "needsInput give wrong boolean value as a result of an empty input buffer",
+ inflate.needsInput() == true);
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#reset()
+ */
+ public void test_reset() {
+ // test method of java.util.zip.inflater.reset()
+ byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
+ byte outPutInf[] = new byte[100];
+ int y = 0;
+ Inflater inflate = new Inflater();
+ try {
+ while (!(inflate.finished())) {
+ if (inflate.needsInput()) {
+ inflate.setInput(outPutBuff1);
+ }
+ y += inflate.inflate(outPutInf, y, outPutInf.length - y);
+ }
+ } catch (DataFormatException e) {
+ fail("Invalid input to be decompressed");
+ }
+ for (int i = 0; i < byteArray.length; i++) {
+ assertTrue(
+ "Final decompressed data does not equal the original data",
+ byteArray[i] == outPutInf[i]);
+ }
+ assertTrue(
+ "final decompressed data contained more bytes than original - reset",
+ outPutInf[byteArray.length] == 0);
+
+ // testing that resetting the inflater will also return the correct
+ // decompressed data
+
+ inflate.reset();
+ try {
+ while (!(inflate.finished())) {
+ if (inflate.needsInput()) {
+ inflate.setInput(outPutBuff1);
+ }
+ inflate.inflate(outPutInf);
+ }
+ } catch (DataFormatException e) {
+ fail("Invalid input to be decompressed");
+ }
+ for (int i = 0; i < byteArray.length; i++) {
+ assertTrue(
+ "Final decompressed data does not equal the original data",
+ byteArray[i] == outPutInf[i]);
+ }
+ assertTrue(
+ "final decompressed data contained more bytes than original - reset",
+ outPutInf[byteArray.length] == 0);
+
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#setDictionary(byte[])
+ */
+ public void test_setDictionary$B() {
+ // test method of java.tuil.zip.inflater.setDictionary(byte)
+ byte dictionaryArray[] = { 'e', 'r', 't', 'a', 'b', 2, 3 };
+ byte byteArray[] = { 4, 5, 3, 2, 'a', 'b', 6, 7, 8, 9, 0, 's', '3',
+ 'w', 'r' };
+
+ byte outPutInf[] = new byte[100];
+
+ // trying to inflate without seting a dictionary
+
+ Inflater inflateWO = new Inflater();
+ byte outPutInf2[] = new byte[100];
+ int r = 0;
+ try {
+ while (!(inflateWO.finished())) {
+ if (inflateWO.needsInput()) {
+ inflateWO.setInput(outPutDiction);
+ }
+ inflateWO.inflate(outPutInf2);
+ }
+ } catch (DataFormatException e) {
+ r = 1;
+ }
+ assertTrue(
+ "invalid input to be decompressed due to dictionary not set",
+ r == 1);
+ // now setting the dictionary in inflater
+ Inflater inflate = new Inflater();
+ try {
+ while (!(inflate.finished())) {
+ if (inflate.needsInput()) {
+ inflate.setInput(outPutDiction);
+ }
+ if (inflate.needsDictionary()) {
+ inflate.setDictionary(dictionaryArray);
+ }
+ inflate.inflate(outPutInf);
+ }
+ } catch (DataFormatException e) {
+ fail("Invalid input to be decompressed");
+ }
+ for (int i = 0; i < byteArray.length; i++) {
+ assertTrue(
+ "Final decompressed data does not equal the original data",
+ byteArray[i] == outPutInf[i]);
+ }
+ assertTrue(
+ "final decompressed data contained more bytes than original - deflateB",
+ outPutInf[byteArray.length] == 0);
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#setInput(byte[])
+ */
+ public void test_setInput$B() {
+ // test method of java.util.zip.inflater.setInput(byte)
+ byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
+ Inflater inflate = new Inflater();
+ inflate.setInput(byteArray);
+ assertTrue("setInputB did not deliver any byte to the input buffer",
+ inflate.getRemaining() != 0);
+ }
+
+ /**
+ * @tests java.util.zip.Inflater#setInput(byte[], int, int)
+ */
+ public void test_setInput$BII() {
+ // test method of java.util.zip.inflater.setInput(byte,int,int)
+ byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
+ int offSet = 6;
+ int length = 6;
+ Inflater inflate = new Inflater();
+ inflate.setInput(byteArray, offSet, length);
+ assertTrue(
+ "setInputBII did not deliever the right number of bytes to the input buffer",
+ inflate.getRemaining() == length);
+ // boundary check
+ inflate.reset();
+ int r = 0;
+ try {
+ inflate.setInput(byteArray, 100, 100);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ r = 1;
+ }
+ assertTrue("boundary check is not present for setInput", r == 1);
+ }
+
+ protected void setUp() {
+ try {
+ java.io.InputStream infile = Support_Resources
+ .getStream("hyts_compressD.txt");
+ BufferedInputStream inflatIP = new BufferedInputStream(infile);
+ inflatIP.read(outPutBuff1, 0, outPutBuff1.length);
+ inflatIP.close();
+
+ java.io.InputStream infile2 = Support_Resources
+ .getStream("hyts_compDiction.txt");
+ BufferedInputStream inflatIP2 = new BufferedInputStream(infile2);
+ inflatIP2.read(outPutDiction, 0, outPutDiction.length);
+ inflatIP2.close();
+
+ } catch (FileNotFoundException e) {
+ fail(
+ "input file to test InflaterInputStream constructor is not found");
+ } catch (ZipException e) {
+ fail(
+ "read() threw an zip exception while testing constructor");
+ } catch (IOException e) {
+ fail("read() threw an exception while testing constructor");
+ }
+ }
+
+ protected void tearDown() {
+ }
+
+}