Return-Path: Delivered-To: apmail-incubator-harmony-commits-archive@www.apache.org Received: (qmail 9027 invoked from network); 26 Apr 2006 13:05:13 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 26 Apr 2006 13:05:13 -0000 Received: (qmail 20273 invoked by uid 500); 26 Apr 2006 13:05:10 -0000 Delivered-To: apmail-incubator-harmony-commits-archive@incubator.apache.org Received: (qmail 20178 invoked by uid 500); 26 Apr 2006 13:05:09 -0000 Mailing-List: contact harmony-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: harmony-dev@incubator.apache.org Delivered-To: mailing list harmony-commits@incubator.apache.org Received: (qmail 20077 invoked by uid 99); 26 Apr 2006 13:05:09 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Apr 2006 06:05:09 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Wed, 26 Apr 2006 06:05:06 -0700 Received: (qmail 8625 invoked by uid 65534); 26 Apr 2006 13:04:44 -0000 Message-ID: <20060426130444.8620.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r397192 [3/3] - in /incubator/harmony/enhanced/classlib/trunk: modules/archive/src/test/java/org/apache/harmony/archive/ modules/archive/src/test/java/org/apache/harmony/archive/tests/ modules/archive/src/test/java/org/apache/harmony/archiv... Date: Wed, 26 Apr 2006 13:04:23 -0000 To: harmony-commits@incubator.apache.org From: gharley@apache.org X-Mailer: svnmailer-1.0.8 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Added: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterInputStreamTest.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterInputStreamTest.java?rev=397192&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterInputStreamTest.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterInputStreamTest.java Wed Apr 26 06:04:19 2006 @@ -0,0 +1,408 @@ +/* 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 org.apache.harmony.archive.tests.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(); assertEquals("the fifth element of byteArray + * contained a non zero value", 0, byteArray[4]); } 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) { + } + assertEquals("Incorrect Byte Returned.", 5, iis.read()); + + try { + iis.skip(Integer.MIN_VALUE); + fail("IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) { + } + assertEquals("Incorrect Byte Returned.", 4, iis.read()); + + // Test to make sure the correct number of bytes were skipped + assertEquals("Incorrect Number Of Bytes Skipped.", 3, iis.skip(3)); + + // Test to see if the number of bytes skipped returned is true. + assertEquals("Incorrect Byte Returned.", 7, iis.read()); + + assertEquals("Incorrect Number Of Bytes Skipped.", 0, iis.skip(0)); + assertEquals("Incorrect Byte Returned.", 0, iis.read()); + + // Test for skipping more bytes than available in the stream + assertEquals("Incorrect Number Of Bytes Skipped.", 2, iis.skip(4)); + assertEquals("Incorrect Byte Returned.", -1, iis.read()); + 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); + assertEquals("method skip() returned wrong number of bytes skiped", + 5, skip); + + // test for skiping of 2 bytes + InputStream infile3 = Support_Resources + .getStream("hyts_constru(OD).txt"); + InflaterInputStream inflatIP3 = new InflaterInputStream(infile3); + skip = inflatIP3.skip(2); + assertEquals("the number of bytes returned by skip did not correspond with its input parameters", + 2, skip); + 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) + assertEquals("Bytes Available Should Return 0 ", + 0, available); + else + assertEquals("Bytes Available Should Return 1.", + 1, available); + } + + 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() { + } + +} Propchange: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterInputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java?rev=397192&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java Wed Apr 26 06:04:19 2006 @@ -0,0 +1,698 @@ +/* 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 org.apache.harmony.archive.tests.java.util.zip; + +import java.io.BufferedInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.zip.Adler32; +import java.io.UnsupportedEncodingException; +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; + } + assertEquals("inflate can still be used after end is called", 1, r); + + 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]); + } + assertEquals("final decompressed data contained more bytes than original - finished()", + 0, outPutInf[byteArray.length]); + } + + /** + * @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(); + assertEquals("upon creating an instance of inflate, getRemaining returned a non zero value", + 0, inflate.getRemaining()); + 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]); + } + assertEquals("final decompressed data contained more bytes than original - inflateB", + 0, outPutInf[byteArray.length]); + // 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]); + assertEquals("Final decompressed data does not equal zero", + 0, outPutInf[i]); + } + assertEquals("Final decompressed data contains more element than original data", + 0, outPutInf[emptyArray.length]); + } + + /** + * @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]); + } + assertEquals("final decompressed data contained more bytes than original - inflateB", + 0, outPutInf[byteArray.length]); + + // 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; + } + assertEquals("out of bounds error did not get caught", 1, r); + } + + /** + * @tests java.util.zip.Inflater#Inflater() + */ + public void test_Constructor() { + // test method of java.util.zip.inflater.Inflater() + try { + Inflater inflate = new Inflater(); + assertNotNull("failed to create the instance of inflater", + inflate); + + } 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); + assertNotNull("failed to create the instance of inflater", inflate); + 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++) { + assertEquals("the output array from inflate should contain 0 because the header of inflate and deflate did not match, but this faled", + 0, outPutBuff1[i]); + } + } catch (DataFormatException e) { + r = 1; + } + assertEquals("Error: exception should be thrown becuase of header inconsistancy", + 1, r); + + } + + /** + * @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 { + assertEquals("should return 0 because needs dictionary", + 0, inflateDiction.inflate(outPutInf)); + } 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"); + } + + // Regression test for HARMONY-86 + Inflater inf = new Inflater(); + assertFalse(inf.needsDictionary()); + assertEquals(0,inf.getTotalIn()); + assertEquals(0,inf.getTotalOut()); + assertEquals(0,inf.getBytesRead()); + assertEquals(0,inf.getBytesWritten()); + } + + /** + * @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]); + } + assertEquals("final decompressed data contained more bytes than original - reset", + 0, outPutInf[byteArray.length]); + + // 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]); + } + assertEquals("final decompressed data contained more bytes than original - reset", + 0, outPutInf[byteArray.length]); + + } + + /** + * @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; + } + assertEquals("invalid input to be decompressed due to dictionary not set", + 1, r); + // 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]); + } + assertEquals("final decompressed data contained more bytes than original - deflateB", + 0, outPutInf[byteArray.length]); + } + + /** + * @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; + } + assertEquals("boundary check is not present for setInput", 1, r); + } + + 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() { + } + + /** + * @tests java.util.zip.Deflater#getBytesRead() + */ + public void test_getBytesRead() throws DataFormatException, + UnsupportedEncodingException { + // Regression test for HARMONY-158 + Deflater def = new Deflater(); + Inflater inf = new Inflater(); + assertEquals(0, def.getTotalIn()); + assertEquals(0, def.getTotalOut()); + assertEquals(0, def.getBytesRead()); + // Encode a String into bytes + String inputString = "blahblahblah??"; + byte[] input = inputString.getBytes("UTF-8"); + + // Compress the bytes + byte[] output = new byte[100]; + def.setInput(input); + def.finish(); + def.deflate(output); + inf.setInput(output); + int compressedDataLength =inf.inflate(input); + assertEquals(16, inf.getTotalIn()); + assertEquals(compressedDataLength, inf.getTotalOut()); + assertEquals(16, inf.getBytesRead()); + } + + /** + * @tests java.util.zip.Deflater#getBytesRead() + */ + public void test_getBytesWritten() throws DataFormatException, UnsupportedEncodingException { + // Regression test for HARMONY-158 + Deflater def = new Deflater(); + Inflater inf = new Inflater(); + assertEquals(0, def.getTotalIn()); + assertEquals(0, def.getTotalOut()); + assertEquals(0, def.getBytesWritten()); + // Encode a String into bytes + String inputString = "blahblahblah??"; + byte[] input = inputString.getBytes("UTF-8"); + + // Compress the bytes + byte[] output = new byte[100]; + def.setInput(input); + def.finish(); + def.deflate(output); + inf.setInput(output); + int compressedDataLength =inf.inflate(input); + assertEquals(16, inf.getTotalIn()); + assertEquals(compressedDataLength, inf.getTotalOut()); + assertEquals(14, inf.getBytesWritten()); + } +} Propchange: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipEntryTest.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipEntryTest.java?rev=397192&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipEntryTest.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipEntryTest.java Wed Apr 26 06:04:19 2006 @@ -0,0 +1,479 @@ +/* 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 org.apache.harmony.archive.tests.java.util.zip; + +import java.util.TimeZone; +import java.util.zip.ZipEntry; + +import tests.support.resource.Support_Resources; + +public class ZipEntryTest extends junit.framework.TestCase { + // zip file hyts_ZipFile.zip must be included as a resource + java.util.zip.ZipEntry zentry; + + java.util.zip.ZipFile zfile; + + private static String platformId = System.getProperty( + "com.ibm.oti.configuration", "JDK") + + System.getProperty("java.vm.version"); + + static final String tempFileName = platformId + "zfzezi.zip"; + + long orgSize; + + long orgCompressedSize; + + long orgCrc; + + long orgTime; + + String orgComment; + + /** + * @tests java.util.zip.ZipEntry#ZipEntry(java.lang.String) + */ + public void test_ConstructorLjava_lang_String() { + // Test for method java.util.zip.ZipEntry(java.lang.String) + zentry = zfile.getEntry("File3.txt"); + assertNotNull("Failed to create ZipEntry", zentry); + try { + zentry = zfile.getEntry(null); + fail("NullPointerException not thrown"); + } catch (NullPointerException e) { + } + StringBuffer s = new StringBuffer(); + for (int i = 0; i < 65535; i++) + s.append('a'); + try { + zentry = new ZipEntry(s.toString()); + } catch (IllegalArgumentException e) { + fail("Unexpected IllegalArgumentException During Test."); + } + try { + s.append('a'); + zentry = new ZipEntry(s.toString()); + fail("IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) { + } + try { + String n = null; + zentry = new ZipEntry(n); + fail("NullPointerException not thrown"); + } catch (NullPointerException e) { + } + } + + /** + * @tests java.util.zip.ZipEntry#getComment() + */ + public void test_getComment() { + // Test for method java.lang.String java.util.zip.ZipEntry.getComment() + ZipEntry zipEntry = new ZipEntry("zippy.zip"); + assertNull("Incorrect Comment Returned.", zipEntry.getComment()); + zipEntry.setComment("This Is A Comment"); + assertEquals("Incorrect Comment Returned.", + "This Is A Comment", zipEntry.getComment()); + } + + /** + * @tests java.util.zip.ZipEntry#getCompressedSize() + */ + public void test_getCompressedSize() { + // Test for method long java.util.zip.ZipEntry.getCompressedSize() + assertTrue("Incorrect compressed size returned", zentry + .getCompressedSize() == orgCompressedSize); + } + + /** + * @tests java.util.zip.ZipEntry#getCrc() + */ + public void test_getCrc() { + // Test for method long java.util.zip.ZipEntry.getCrc() + assertTrue("Failed to get Crc", zentry.getCrc() == orgCrc); + } + + /** + * @tests java.util.zip.ZipEntry#getExtra() + */ + public void test_getExtra() { + // Test for method byte [] java.util.zip.ZipEntry.getExtra() + assertNull("Incorrect extra information returned", + zentry.getExtra()); + byte[] ba = { 'T', 'E', 'S', 'T' }; + zentry = new ZipEntry("test.tst"); + zentry.setExtra(ba); + assertTrue("Incorrect Extra Information Returned.", + zentry.getExtra() == ba); + } + + /** + * @tests java.util.zip.ZipEntry#getMethod() + */ + public void test_getMethod() { + // Test for method int java.util.zip.ZipEntry.getMethod() + zentry = zfile.getEntry("File1.txt"); + assertTrue("Incorrect compression method returned", + zentry.getMethod() == java.util.zip.ZipEntry.STORED); + zentry = zfile.getEntry("File3.txt"); + assertTrue("Incorrect compression method returned", + zentry.getMethod() == java.util.zip.ZipEntry.DEFLATED); + zentry = new ZipEntry("test.tst"); + assertEquals("Incorrect Method Returned.", -1, zentry.getMethod()); + } + + /** + * @tests java.util.zip.ZipEntry#getName() + */ + public void test_getName() { + // Test for method java.lang.String java.util.zip.ZipEntry.getName() + assertEquals("Incorrect name returned - Note return result somewhat ambiguous in spec", + "File1.txt", zentry.getName()); + } + + /** + * @tests java.util.zip.ZipEntry#getSize() + */ + public void test_getSize() { + // Test for method long java.util.zip.ZipEntry.getSize() + assertTrue("Incorrect size returned", zentry.getSize() == orgSize); + } + + /** + * @tests java.util.zip.ZipEntry#getTime() + */ + public void test_getTime() { + // Test for method long java.util.zip.ZipEntry.getTime() + assertTrue("Failed to get time", zentry.getTime() == orgTime); + } + + /** + * @tests java.util.zip.ZipEntry#isDirectory() + */ + public void test_isDirectory() { + // Test for method boolean java.util.zip.ZipEntry.isDirectory() + assertTrue("Entry should not answer true to isDirectory", !zentry + .isDirectory()); + zentry = new ZipEntry("Directory/"); + assertTrue("Entry should answer true to isDirectory", zentry + .isDirectory()); + } + + /** + * @tests java.util.zip.ZipEntry#setComment(java.lang.String) + */ + public void test_setCommentLjava_lang_String() { + // Test for method void + // java.util.zip.ZipEntry.setComment(java.lang.String) + zentry = zfile.getEntry("File1.txt"); + zentry.setComment("Set comment using api"); + assertEquals("Comment not correctly set", + "Set comment using api", zentry.getComment()); + String n = null; + zentry.setComment(n); + assertNull("Comment not correctly set", zentry.getComment()); + StringBuffer s = new StringBuffer(); + for (int i = 0; i < 0xFFFF; i++) + s.append('a'); + try { + zentry.setComment(s.toString()); + } catch (IllegalArgumentException e) { + fail("Unexpected IllegalArgumentException During Test."); + } + try { + s.append('a'); + zentry.setComment(s.toString()); + fail("IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) { + } + } + + /** + * @tests java.util.zip.ZipEntry#setCompressedSize(long) + */ + public void test_setCompressedSizeJ() { + // Test for method void java.util.zip.ZipEntry.setCompressedSize(long) + zentry.setCompressedSize(orgCompressedSize + 10); + assertTrue("Set compressed size failed", + zentry.getCompressedSize() == (orgCompressedSize + 10)); + zentry.setCompressedSize(0); + assertEquals("Set compressed size failed", + 0, zentry.getCompressedSize()); + zentry.setCompressedSize(-25); + assertEquals("Set compressed size failed", + -25, zentry.getCompressedSize()); + zentry.setCompressedSize(4294967296l); + assertTrue("Set compressed size failed", + zentry.getCompressedSize() == 4294967296l); + } + + /** + * @tests java.util.zip.ZipEntry#setCrc(long) + */ + public void test_setCrcJ() { + // Test for method void java.util.zip.ZipEntry.setCrc(long) + zentry.setCrc(orgCrc + 100); + assertTrue("Failed to set Crc", zentry.getCrc() == (orgCrc + 100)); + zentry.setCrc(0); + assertEquals("Failed to set Crc", 0, zentry.getCrc()); + try { + zentry.setCrc(-25); + fail("IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) { + } + try { + zentry.setCrc(4294967295l); + } catch (IllegalArgumentException e) { + fail("Unexpected IllegalArgumentException during test"); + } + try { + zentry.setCrc(4294967296l); + fail("IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) { + } + } + + /** + * @tests java.util.zip.ZipEntry#setExtra(byte[]) + */ + public void test_setExtra$B() { + // Test for method void java.util.zip.ZipEntry.setExtra(byte []) + zentry = zfile.getEntry("File1.txt"); + zentry.setExtra("Test setting extra information".getBytes()); + assertEquals("Extra information not written properly", "Test setting extra information", new String(zentry + .getExtra(), 0, zentry.getExtra().length) + ); + zentry = new ZipEntry("test.tst"); + byte[] ba = new byte[0xFFFF]; + try { + zentry.setExtra(ba); + } catch (IllegalArgumentException e) { + fail("Unexpected IllegalArgumentException during test"); + } + try { + ba = new byte[0xFFFF + 1]; + zentry.setExtra(ba); + fail("IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) { + } + + // One constructor + ZipEntry zeInput = new ZipEntry("InputZIP"); + byte[] extraB = { 'a', 'b', 'd', 'e' }; + zeInput.setExtra(extraB); + assertEquals(extraB, zeInput.getExtra()); + assertEquals(extraB[3], zeInput.getExtra()[3]); + assertEquals(extraB.length, zeInput.getExtra().length); + + // test another constructor + ZipEntry zeOutput = new ZipEntry(zeInput); + assertEquals(zeInput.getExtra()[3], zeOutput.getExtra()[3]); + assertEquals(zeInput.getExtra().length, zeOutput.getExtra().length); + assertEquals(extraB[3], zeOutput.getExtra()[3]); + assertEquals(extraB.length, zeOutput.getExtra().length); + } + + /** + * @tests java.util.zip.ZipEntry#setMethod(int) + */ + public void test_setMethodI() { + // Test for method void java.util.zip.ZipEntry.setMethod(int) + zentry = zfile.getEntry("File3.txt"); + zentry.setMethod(ZipEntry.STORED); + assertTrue("Failed to set compression method", + zentry.getMethod() == ZipEntry.STORED); + zentry.setMethod(ZipEntry.DEFLATED); + assertTrue("Failed to set compression method", + zentry.getMethod() == ZipEntry.DEFLATED); + try { + int error = 1; + zentry = new ZipEntry("test.tst"); + zentry.setMethod(error); + fail("IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) { + } + } + + /** + * @tests java.util.zip.ZipEntry#setSize(long) + */ + public void test_setSizeJ() { + // Test for method void java.util.zip.ZipEntry.setSize(long) + zentry.setSize(orgSize + 10); + assertTrue("Set size failed", zentry.getSize() == (orgSize + 10)); + zentry.setSize(0); + assertEquals("Set size failed", 0, zentry.getSize()); + try { + zentry.setSize(-25); + fail("IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) { + } + try { + zentry.setCrc(4294967295l); + } catch (IllegalArgumentException e) { + fail("Unexpected IllegalArgumentException during test"); + } + try { + zentry.setCrc(4294967296l); + fail("IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) { + } + } + + /** + * @tests java.util.zip.ZipEntry#setTime(long) + */ + public void test_setTimeJ() { + // Test for method void java.util.zip.ZipEntry.setTime(long) + zentry.setTime(orgTime + 10000); + assertTrue("Test 1: Failed to set time: " + zentry.getTime(), zentry + .getTime() == (orgTime + 10000)); + zentry.setTime(orgTime - 10000); + assertTrue("Test 2: Failed to set time: " + zentry.getTime(), zentry + .getTime() == (orgTime - 10000)); + TimeZone zone = TimeZone.getDefault(); + try { + TimeZone.setDefault(TimeZone.getTimeZone("EST")); + zentry.setTime(0); + assertTrue("Test 3: Failed to set time: " + zentry.getTime(), + zentry.getTime() == 315550800000L); + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + assertTrue("Test 3a: Failed to set time: " + zentry.getTime(), + zentry.getTime() == 315532800000L); + zentry.setTime(0); + TimeZone.setDefault(TimeZone.getTimeZone("EST")); + assertTrue("Test 3b: Failed to set time: " + zentry.getTime(), + zentry.getTime() == 315550800000L); + + zentry.setTime(-25); + assertTrue("Test 4: Failed to set time: " + zentry.getTime(), + zentry.getTime() == 315550800000L); + zentry.setTime(4354837200000L); + assertTrue("Test 5: Failed to set time: " + zentry.getTime(), + zentry.getTime() == 315550800000L); + } finally { + TimeZone.setDefault(zone); + } + } + + /** + * @tests java.util.zip.ZipEntry#toString() + */ + public void test_toString() { + // Test for method java.lang.String java.util.zip.ZipEntry.toString() + assertTrue("Returned incorrect entry name", zentry.toString().indexOf( + "File1.txt") >= 0); + } + + /** + * @tests java.util.zip.ZipEntry#ZipEntry(java.util.zip.ZipEntry) + */ + public void test_ConstructorLjava_util_zip_ZipEntry() { + // Test for method java.util.zip.ZipEntry(util.zip.ZipEntry) + zentry.setSize(2); + zentry.setCompressedSize(4); + zentry.setComment("Testing"); + ZipEntry zentry2 = new ZipEntry(zentry); + assertEquals("ZipEntry Created With Incorrect Size.", + 2, zentry2.getSize()); + assertEquals("ZipEntry Created With Incorrect Compressed Size.", 4, zentry2 + .getCompressedSize()); + assertEquals("ZipEntry Created With Incorrect Comment.", "Testing", zentry2 + .getComment()); + assertTrue("ZipEntry Created With Incorrect Crc.", + zentry2.getCrc() == orgCrc); + assertTrue("ZipEntry Created With Incorrect Time.", + zentry2.getTime() == orgTime); + } + + /** + * @tests java.util.zip.ZipEntry#clone() + */ + public void test_clone() { + // Test for method java.util.zip.ZipEntry.clone() + Object obj = zentry.clone(); + assertTrue("toString()", obj.toString().equals(zentry.toString())); + assertTrue("hashCode()", obj.hashCode() == zentry.hashCode()); + + // One constructor + ZipEntry zeInput = new ZipEntry("InputZIP"); + byte[] extraB = { 'a', 'b', 'd', 'e' }; + zeInput.setExtra(extraB); + assertEquals(extraB, zeInput.getExtra()); + assertEquals(extraB[3], zeInput.getExtra()[3]); + assertEquals(extraB.length, zeInput.getExtra().length); + + // test Clone() + ZipEntry zeOutput = (ZipEntry) zeInput.clone(); + assertEquals(zeInput.getExtra()[3], zeOutput.getExtra()[3]); + assertEquals(zeInput.getExtra().length, zeOutput.getExtra().length); + assertEquals(extraB[3], zeOutput.getExtra()[3]); + assertEquals(extraB.length, zeOutput.getExtra().length); + } + + /** + * Sets up the fixture, for example, open a network connection. This method + * is called before a test is executed. + */ + + protected void setUp() { + java.io.File f = null; + try { + byte[] rbuf = new byte[2000]; + // Create a local copy of the file since some tests want to alter + // information. + f = new java.io.File(tempFileName); + // Create absolute filename as ZipFile does not resolve using + // user.dir + f = new java.io.File(f.getAbsolutePath()); + f.delete(); + java.io.InputStream is = Support_Resources + .getStream("hyts_ZipFile.zip"); + java.io.FileOutputStream fos = new java.io.FileOutputStream(f); + rbuf = new byte[(int) is.available()]; + is.read(rbuf, 0, rbuf.length); + fos.write(rbuf, 0, rbuf.length); + is.close(); + fos.close(); + zfile = new java.util.zip.ZipFile(f); + zentry = zfile.getEntry("File1.txt"); + orgSize = zentry.getSize(); + orgCompressedSize = zentry.getCompressedSize(); + orgCrc = zentry.getCrc(); + orgTime = zentry.getTime(); + orgComment = zentry.getComment(); + } catch (Exception e) { + System.out.println("Exception during ZipFile setup <" + + f.getAbsolutePath() + ">: "); + e.printStackTrace(); + } + } + + /** + * Tears down the fixture, for example, close a network connection. This + * method is called after a test is executed. + */ + + protected void tearDown() { + try { + if (zfile != null) + zfile.close(); + java.io.File f = new java.io.File(tempFileName); + f.delete(); + } catch (java.io.IOException e) { + System.out.println("Exception durnig tearDown"); + } + } + +} Propchange: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipEntryTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java?rev=397192&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java Wed Apr 26 06:04:19 2006 @@ -0,0 +1,290 @@ +/* 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 org.apache.harmony.archive.tests.java.util.zip; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import tests.support.Support_PlatformFile; +import tests.support.resource.Support_Resources; + +public class ZipFileTest extends junit.framework.TestCase { + + // the file hyts_zipFile.zip in setup must be included as a resource + String tempFileName; + + private java.util.zip.ZipFile zfile; + + /** + * @tests java.util.zip.ZipFile#ZipFile(java.io.File) + */ + public void test_ConstructorLjava_io_File() { + // Test for method java.util.zip.ZipFile(java.io.File) + assertTrue("Used to test", true); + } + + /** + * @tests java.util.zip.ZipFile#ZipFile(java.io.File, int) + */ + public void test_ConstructorLjava_io_FileI() { + try { + zfile.close(); // about to reopen the same temp file + File file = new File(tempFileName); + ZipFile zip = new ZipFile(file, ZipFile.OPEN_DELETE + | ZipFile.OPEN_READ); + zip.close(); + assertTrue("Zip should not exist", !file.exists()); + } catch (IOException e) { + fail("Unexpected exception: " + e); + } + } + + /** + * @tests java.util.zip.ZipFile#ZipFile(java.lang.String) + */ + public void test_ConstructorLjava_lang_String() { + // Test for method java.util.zip.ZipFile(java.lang.String) + /* + * try { zfile = new java.util.zip.ZipFile(zipName); zfile.close(); } + * catch (java.io.IOException e) {fail( "Failed to construct + * ZipFile" );} + */ + } + + protected ZipEntry test_finalize1(ZipFile zip) { + return zip.getEntry("File1.txt"); + } + + protected ZipFile test_finalize2(File file) { + try { + return new ZipFile(file); + } catch (IOException e) { + fail("Unexpected exception: " + e); + } + return null; + } + + /** + * @tests java.util.zip.ZipFile#finalize() + */ + public void test_finalize() { + try { + InputStream in = Support_Resources.getStream("hyts_ZipFile.zip"); + File file = Support_Resources.createTempFile(".jar"); + OutputStream out = new FileOutputStream(file); + int result; + byte[] buf = new byte[4096]; + while ((result = in.read(buf)) != -1) + out.write(buf, 0, result); + in.close(); + out.close(); + /* + * ZipFile zip = new ZipFile(file); ZipEntry entry1 = + * zip.getEntry("File1.txt"); assertNotNull("Did not find entry", + * entry1); entry1 = null; zip = null; + */ + + assertNotNull("Did not find entry", + test_finalize1(test_finalize2(file))); + System.gc(); + System.gc(); + System.runFinalization(); + file.delete(); + assertTrue("Zip should not exist", !file.exists()); + } catch (IOException e) { + fail("Unexpected exception: " + e); + } + } + + /** + * @tests java.util.zip.ZipFile#close() + */ + public void test_close() { + // Test for method void java.util.zip.ZipFile.close() + try { + zfile.close(); + zfile.getInputStream(zfile.getEntry("ztest/file1.txt")); + } catch (Exception e) { + return; + } + fail("Close test failed"); + } + + /** + * @tests java.util.zip.ZipFile#entries() + */ + public void test_entries() { + // Test for method java.util.Enumeration java.util.zip.ZipFile.entries() + java.util.Enumeration enumer = zfile.entries(); + int c = 0; + while (enumer.hasMoreElements()) { + ++c; + enumer.nextElement(); + } + assertTrue("Incorrect number of entries returned: " + c, c == 6); + + try { + Enumeration enumeration = zfile.entries(); + zfile.close(); + zfile = null; + boolean pass = false; + try { + enumeration.hasMoreElements(); + } catch (IllegalStateException e) { + pass = true; + } + assertTrue("did not detect closed jar file", pass); + } catch (Exception e) { + fail("Exception during entries test: " + e.toString()); + } + } + + /** + * @tests java.util.zip.ZipFile#getEntry(java.lang.String) + */ + public void test_getEntryLjava_lang_String() { + // Test for method java.util.zip.ZipEntry + // java.util.zip.ZipFile.getEntry(java.lang.String) + java.util.zip.ZipEntry zentry = zfile.getEntry("File1.txt"); + assertNotNull("Could not obtain ZipEntry", zentry); + + zentry = zfile.getEntry("testdir1/File1.txt"); + assertNotNull("Could not obtain ZipEntry: testdir1/File1.txt", + zentry); + try { + int r; + InputStream in; + zentry = zfile.getEntry("testdir1/"); + assertNotNull("Could not obtain ZipEntry: testdir1/", zentry); + in = zfile.getInputStream(zentry); + assertNotNull("testdir1/ should not have null input stream", + in); + r = in.read(); + in.close(); + assertEquals("testdir1/ should not contain data", -1, r); + + zentry = zfile.getEntry("testdir1"); + assertNotNull("Could not obtain ZipEntry: testdir1", zentry); + in = zfile.getInputStream(zentry); + assertNotNull("testdir1 should not have null input stream", in); + r = in.read(); + in.close(); + assertEquals("testdir1 should not contain data", -1, r); + + zentry = zfile.getEntry("testdir1/testdir1"); + assertNotNull("Could not obtain ZipEntry: testdir1/testdir1", + zentry); + in = zfile.getInputStream(zentry); + byte[] buf = new byte[256]; + r = in.read(buf); + in.close(); + assertEquals("incorrect contents", "This is also text", new String(buf, 0, r) + ); + } catch (IOException e) { + fail("Unexpected: " + e); + } + } + + /** + * @tests java.util.zip.ZipFile#getInputStream(java.util.zip.ZipEntry) + */ + public void test_getInputStreamLjava_util_zip_ZipEntry() { + // Test for method java.io.InputStream + // java.util.zip.ZipFile.getInputStream(java.util.zip.ZipEntry) + java.io.InputStream is = null; + try { + java.util.zip.ZipEntry zentry = zfile.getEntry("File1.txt"); + is = zfile.getInputStream(zentry); + byte[] rbuf = new byte[1000]; + int r; + is.read(rbuf, 0, r = (int) zentry.getSize()); + assertEquals("getInputStream read incorrect data", "This is text", new String(rbuf, + 0, r)); + } catch (java.io.IOException e) { + fail("IOException during getInputStream"); + } finally { + try { + is.close(); + } catch (java.io.IOException e) { + fail("Failed to close input stream"); + } + } + } + + /** + * @tests java.util.zip.ZipFile#getName() + */ + public void test_getName() { + // Test for method java.lang.String java.util.zip.ZipFile.getName() + assertTrue("Returned incorrect name: " + zfile.getName(), zfile + .getName().equals(tempFileName)); + } + + /** + * Sets up the fixture, for example, open a network connection. This method + * is called before a test is executed. + */ + protected void setUp() { + try { + byte[] rbuf = new byte[2000]; + // Create a local copy of the file since some tests want to alter + // information. + tempFileName = System.getProperty("user.dir"); + String separator = System.getProperty("file.separator"); + if (tempFileName.charAt(tempFileName.length() - 1) == separator + .charAt(0)) + tempFileName = Support_PlatformFile.getNewPlatformFile( + tempFileName, "gabba.zip"); + else + tempFileName = Support_PlatformFile.getNewPlatformFile( + tempFileName + separator, "gabba.zip"); + + File f = new File(tempFileName); + f.delete(); + InputStream is = Support_Resources.getStream("hyts_ZipFile.zip"); + FileOutputStream fos = new FileOutputStream(f); + rbuf = new byte[(int) is.available()]; + is.read(rbuf, 0, rbuf.length); + fos.write(rbuf, 0, rbuf.length); + is.close(); + fos.close(); + zfile = new ZipFile(f); + } catch (Exception e) { + System.out.println("Exception during ZipFile setup:"); + e.printStackTrace(); + } + } + + /** + * Tears down the fixture, for example, close a network connection. This + * method is called after a test is executed. + */ + protected void tearDown() { + try { + if (zfile != null) + // Note zfile is a user-defined zip file used by other tests and + // should not be deleted + zfile.close(); + } catch (Exception e) { + } + } + +} Propchange: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java?rev=397192&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java Wed Apr 26 06:04:19 2006 @@ -0,0 +1,188 @@ +/* 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 org.apache.harmony.archive.tests.java.util.zip; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; + +import tests.support.resource.Support_Resources; + +public class ZipInputStreamTest extends junit.framework.TestCase { + // the file hyts_zipFile.zip used in setup needs to included as a resource + java.util.zip.ZipFile zfile; + + java.util.zip.ZipEntry zentry; + + java.util.zip.ZipInputStream zis; + + byte[] zipBytes; + + byte[] dataBytes = "Some data in my file".getBytes(); + + + /** + * @tests java.util.zip.ZipInputStream#ZipInputStream(java.io.InputStream) + */ + public void test_ConstructorLjava_io_InputStream() { + // Test for method java.util.zip.ZipInputStream(java.io.InputStream) + try { + zentry = zis.getNextEntry(); + zis.closeEntry(); + } catch (java.io.IOException e) { + fail("Failed to create ZipInputStream"); + } + } + + /** + * @tests java.util.zip.ZipInputStream#close() + */ + public void test_close() { + // Test for method void java.util.zip.ZipInputStream.close() + try { + zis.close(); + byte[] rbuf = new byte[10]; + zis.read(rbuf, 0, 1); + } catch (java.io.IOException e) { + return; + } + fail("Read data after stream was closed--is this an error?"); + } + + /** + * @tests java.util.zip.ZipInputStream#closeEntry() + */ + public void test_closeEntry() { + // Test for method void java.util.zip.ZipInputStream.closeEntry() + try { + zentry = zis.getNextEntry(); + zis.closeEntry(); + } catch (java.io.IOException e) { + fail("Exception during closeEntry test"); + } + } + + /** + * @tests java.util.zip.ZipInputStream#getNextEntry() + */ + public void test_getNextEntry() { + // Test for method java.util.zip.ZipEntry + // java.util.zip.ZipInputStream.getNextEntry() + try { + assertNotNull("getNextEntry failed", zis.getNextEntry()); + } catch (java.io.IOException e) { + fail("Exception during getNextEntry test"); + } + } + + /** + * @tests java.util.zip.ZipInputStream#read(byte[], int, int) + */ + public void test_read$BII() { + // Test for method int java.util.zip.ZipInputStream.read(byte [], int, + // int) + try { + zentry = zis.getNextEntry(); + byte[] rbuf = new byte[(int) zentry.getSize()]; + int r = zis.read(rbuf, 0, rbuf.length); + new String(rbuf, 0, r); + assertEquals("Failed to read entry", 12, r); + } catch (java.io.IOException e) { + fail("Exception during read test"); + } + } + + /** + * @tests java.util.zip.ZipInputStream#skip(long) + */ + public void test_skipJ() throws Exception { + // Test for method long java.util.zip.ZipInputStream.skip(long) + try { + zentry = zis.getNextEntry(); + byte[] rbuf = new byte[(int) zentry.getSize()]; + zis.skip(2); + int r = zis.read(rbuf, 0, rbuf.length); + assertEquals("Failed to skip data", 10, r); + } catch (java.io.IOException e) { + fail("Unexpected1: " + e); + } + + try { + zentry = zis.getNextEntry(); + zentry = zis.getNextEntry(); + long s = zis.skip(1025); + assertTrue("invalid skip: " + s, s == 1025); + } catch (java.io.IOException e) { + fail("Unexpected2: " + e); + } + + ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream( + zipBytes)); + zis.getNextEntry(); + long skipLen = dataBytes.length / 2; + assertEquals("Assert 0: failed valid skip", skipLen, zis.skip(skipLen)); + zis.skip(dataBytes.length); + assertEquals("Assert 1: performed invalid skip", 0, zis.skip(1)); + assertEquals("Assert 2: failed zero len skip", 0, zis.skip(0)); + try { + zis.skip(-1); + fail("Assert 3: Expected Illegal argument exception"); + } catch (IllegalArgumentException e) { + // Expected + } + } + + /** + * Sets up the fixture, for example, open a network connection. This method + * is called before a test is executed. + */ + protected void setUp() { + try { + java.io.InputStream is = Support_Resources + .getStream("hyts_ZipFile.zip"); + if (is == null) + System.out.println("file hyts_ZipFile.zip can not be found"); + zis = new ZipInputStream(is); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ZipOutputStream zos = new ZipOutputStream(bos); + ZipEntry entry = new ZipEntry("myFile"); + zos.putNextEntry(entry); + zos.write(dataBytes); + zos.closeEntry(); + zos.close(); + zipBytes = bos.toByteArray(); + } catch (Exception e) { + System.out.println("Exception during ZipFile setup:"); + e.printStackTrace(); + } + } + + /** + * Tears down the fixture, for example, close a network connection. This + * method is called after a test is executed. + */ + protected void tearDown() { + + if (zis != null) + try { + zis.close(); + } catch (Exception e) { + } + } +} Propchange: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java?rev=397192&view=auto ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java (added) +++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java Wed Apr 26 06:04:19 2006 @@ -0,0 +1,287 @@ +/* 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 org.apache.harmony.archive.tests.java.util.zip; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.CRC32; +import java.util.zip.ZipEntry; +import java.util.zip.ZipException; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; + +public class ZipOutputStreamTest extends junit.framework.TestCase { + + ZipOutputStream zos; + + ByteArrayOutputStream bos; + + ZipInputStream zis; + + static final String data = "HelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorld"; + + /** + * @tests java.util.zip.ZipOutputStream#close() + */ + public void test_close() throws Exception { + boolean thrown = false; + try { + zos.close(); + } catch (ZipException e) { + // Correct + thrown = true; + } catch (IOException e) { + fail("Exception closing on stream with no entries"); + } + if (!thrown) + fail("Close on empty stream failed to throw exception"); + try { + zos = new ZipOutputStream(bos); + zos.putNextEntry(new ZipEntry("XX")); + zos.closeEntry(); + zos.close(); + } catch (IOException e) { + fail("Exception during close test: " + e.toString()); + } + + // Regression for HARMONY-97 + ZipOutputStream zos = new ZipOutputStream(new ByteArrayOutputStream()); + zos.putNextEntry(new ZipEntry("myFile")); + zos.close(); + zos.close(); // Should be a no-op + } + + /** + * @tests java.util.zip.ZipOutputStream#closeEntry() + */ + public void test_closeEntry() { + try { + ZipEntry ze = new ZipEntry("testEntry"); + ze.setTime(System.currentTimeMillis()); + zos.putNextEntry(ze); + zos.write("Hello World".getBytes()); + zos.closeEntry(); + assertTrue("closeEntry failed to update required fields", ze + .getSize() == 11 + && ze.getCompressedSize() == 13); + + } catch (IOException e) { + fail("Exception during closeEntry: " + e.toString()); + } + } + + /** + * @tests java.util.zip.ZipOutputStream#finish() + */ + public void test_finish() throws Exception { + try { + ZipEntry ze = new ZipEntry("test"); + zos.putNextEntry(ze); + zos.write("Hello World".getBytes()); + zos.finish(); + assertEquals("Finish failed to closeCurrentEntry", 11, ze.getSize()); + } catch (IOException e) { + fail("Exception during finish test: " + e.toString()); + } + + ZipOutputStream zos = new ZipOutputStream(new ByteArrayOutputStream()); + zos.putNextEntry(new ZipEntry("myFile")); + zos.finish(); + zos.close(); + try { + zos.finish(); + fail("Assert 0: Expected IOException"); + } catch (IOException e) { + // Expected + } + } + + /** + * @tests java.util.zip.ZipOutputStream#putNextEntry(java.util.zip.ZipEntry) + */ + public void test_putNextEntryLjava_util_zip_ZipEntry() { + try { + ZipEntry ze = new ZipEntry("testEntry"); + ze.setTime(System.currentTimeMillis()); + zos.putNextEntry(ze); + zos.write("Hello World".getBytes()); + zos.closeEntry(); + zos.close(); + zis = new ZipInputStream( + new ByteArrayInputStream(bos.toByteArray())); + ZipEntry ze2 = zis.getNextEntry(); + zis.closeEntry(); + assertTrue("Failed to write correct entry", ze.getName().equals( + ze2.getName()) + && ze.getCrc() == ze2.getCrc()); + try { + zos.putNextEntry(ze); + } catch (IOException e) { + // Correct + return; + } + fail( + "Entry with incorrect setting failed to throw exception"); + } catch (IOException e) { + fail("Exception during putNextEntry: " + e.toString()); + } + + } + + /** + * @tests java.util.zip.ZipOutputStream#setComment(java.lang.String) + */ + public void test_setCommentLjava_lang_String() { + // There is no way to get the comment back, so no way to determine if + // the comment is set correct + try { + zos.setComment("test setComment"); + } catch (Exception e) { + fail("Trying to set comment failed"); + } + try { + zos.setComment(new String(new byte[0xFFFF + 1])); + fail("Comment over 0xFFFF in length should throw exception"); + } catch (IllegalArgumentException e) { + // Passed + } + } + + /** + * @tests java.util.zip.ZipOutputStream#setLevel(int) + */ + public void test_setLevelI() { + try { + ZipEntry ze = new ZipEntry("test"); + zos.putNextEntry(ze); + zos.write(data.getBytes()); + zos.closeEntry(); + long csize = ze.getCompressedSize(); + zos.setLevel(9); // Max Compression + zos.putNextEntry(ze = new ZipEntry("test2")); + zos.write(data.getBytes()); + zos.closeEntry(); + assertTrue("setLevel failed", csize <= ze.getCompressedSize()); + } catch (IOException e) { + fail("Exception during setLevel test: " + e.toString()); + } + } + + /** + * @tests java.util.zip.ZipOutputStream#setMethod(int) + */ + public void test_setMethodI() { + try { + ZipEntry ze = new ZipEntry("test"); + zos.setMethod(ZipOutputStream.STORED); + CRC32 tempCrc = new CRC32(); + tempCrc.update(data.getBytes()); + ze.setCrc(tempCrc.getValue()); + ze.setSize(new String(data).length()); + zos.putNextEntry(ze); + zos.write(data.getBytes()); + zos.closeEntry(); + long csize = ze.getCompressedSize(); + zos.setMethod(ZipOutputStream.DEFLATED); + zos.putNextEntry(ze = new ZipEntry("test2")); + zos.write(data.getBytes()); + zos.closeEntry(); + assertTrue("setLevel failed", csize >= ze.getCompressedSize()); + } catch (IOException e) { + fail("Exception during setLevel test: " + e.toString()); + } + } + + /** + * @tests java.util.zip.ZipOutputStream#write(byte[], int, int) + */ + public void test_write$BII() { + try { + ZipEntry ze = new ZipEntry("test"); + zos.putNextEntry(ze); + zos.write(data.getBytes()); + zos.closeEntry(); + zos.close(); + zos = null; + zis = new ZipInputStream( + new ByteArrayInputStream(bos.toByteArray())); + zis.getNextEntry(); + byte[] b = new byte[data.length()]; + int r = 0; + int count = 0; + while (count != b.length + && (r = zis.read(b, count, b.length)) != -1) + count += r; + zis.closeEntry(); + assertTrue("Write failed to write correct bytes", new String(b) + .equals(data)); + } catch (IOException e) { + fail("Exception during write test: " + e.toString()); + } + + try { + File f = File.createTempFile("testZip", "tst"); + f.deleteOnExit(); + FileOutputStream stream = new FileOutputStream(f); + ZipOutputStream zip = new ZipOutputStream(stream); + zip.setMethod(ZipEntry.STORED); + + try { + zip.putNextEntry(new ZipEntry("Second")); + fail("Not set an entry. Should have thrown ZipException."); + } catch (Exception e) { + assertTrue(e instanceof ZipException); + } // We have not set an entry + + try { + // We try to write data without entry + zip.write(new byte[2]); + fail("Writing data without an entry. Should have thrown IOException"); + } catch (Exception e) { + assertTrue(e instanceof IOException); + } + + try { + // Try to write without an entry and with nonsense offset and + // length + zip.write(new byte[2], 0, 12); + fail("Writing data without an entry. Should have thrown IndexOutOfBoundsException"); + } catch (Exception e) { + assertTrue("Caught a " + e.getClass().getName(), + e instanceof IndexOutOfBoundsException); + } + } catch (IOException e) { + fail("ERROR: " + e); + } + } + + protected void setUp() { + zos = new ZipOutputStream(bos = new ByteArrayOutputStream()); + } + + protected void tearDown() { + + try { + if (zos != null) + zos.close(); + if (zis != null) + zis.close(); + } catch (Exception e) { + } + } +} Propchange: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: incubator/harmony/enhanced/classlib/trunk/support/src/test/java/tests/main/AllTests.java URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/support/src/test/java/tests/main/AllTests.java?rev=397192&r1=397191&r2=397192&view=diff ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/support/src/test/java/tests/main/AllTests.java (original) +++ incubator/harmony/enhanced/classlib/trunk/support/src/test/java/tests/main/AllTests.java Wed Apr 26 06:04:19 2006 @@ -32,7 +32,7 @@ public static Test suite() { TestSuite suite = new TestSuite("All test suites"); // $JUnit-BEGIN$ - suite.addTest(tests.archive.AllTests.suite()); + suite.addTest(org.apache.harmony.archive.tests.AllTests.suite()); suite.addTest(tests.luni.AllTests.suite()); suite.addTest(tests.nio_char.AllTests.suite()); suite.addTest(org.apache.harmony.text.tests.AllTests.suite());