You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/06/09 10:16:16 UTC
svn commit: r412974 - in
/incubator/harmony/enhanced/classlib/trunk/modules/archive/src:
main/java/java/util/zip/ZipOutputStream.java
test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java
Author: mloenko
Date: Fri Jun 9 01:16:16 2006
New Revision: 412974
URL: http://svn.apache.org/viewvc?rev=412974&view=rev
Log:
fixed bug described in HARMONY-577
([classlib][util] java.util.zip.ZipOutputStream.write(byte[]) doesn't throw IOException if an entry is not set):
applied patch ZipOutputStreamTest.diffs
the code was fixed in a different way, ZipOutputStream.diffs not applied
the test's style improved
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/ZipOutputStream.java
incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/ZipOutputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/ZipOutputStream.java?rev=412974&r1=412973&r2=412974&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/ZipOutputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/ZipOutputStream.java Fri Jun 9 01:16:16 2006
@@ -210,6 +210,8 @@
* @see #write
*/
public void putNextEntry(ZipEntry ze) throws java.io.IOException {
+ if (currentEntry != null)
+ closeEntry();
if (ze.getMethod() == STORED
|| (compressMethod == STORED && ze.getMethod() == -1)) {
if (ze.crc == -1)
@@ -226,8 +228,6 @@
/* [MSG "K0059", "Stream is closed"] */
if (cDir == null)
throw new IOException(Msg.getString("K0059"));
- if (currentEntry != null)
- closeEntry();
if (entries.contains(ze.name))
/* [MSG "K0066", "Entry already exists: {0}"] */
throw new ZipException(Msg.getString("K0066", ze.name));
@@ -343,9 +343,9 @@
}
if (currentEntry == null) {
- /* [MSG "K00ab", "No active entry"] */
- throw new ZipException(Msg.getString("K00ab"));
- }
+ /* [MSG "K00ab", "No active entry"] */
+ throw new ZipException(Msg.getString("K00ab"));
+ }
if (currentEntry.getMethod() == STORED)
out.write(buffer, off, nbytes);
Modified: 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/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java?rev=412974&r1=412973&r2=412974&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java Fri Jun 9 01:16:16 2006
@@ -36,69 +36,52 @@
static final String data = "HelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorld";
/**
- * @tests java.util.zip.ZipOutputStream#close()
- */
+ * @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());
- }
+ try {
+ zos.close();
+ fail("Close on empty stream failed to throw exception");
+ } catch (ZipException e) {
+ // expected
+ }
+
+ zos = new ZipOutputStream(bos);
+ zos.putNextEntry(new ZipEntry("XX"));
+ zos.closeEntry();
+ zos.close();
// 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() throws IOException {
+ 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);
+
+ }
+
+ /**
+ * @tests java.util.zip.ZipOutputStream#finish()
+ */
+ public void test_finish() throws Exception {
+ ZipEntry ze = new ZipEntry("test");
+ zos.putNextEntry(ze);
+ zos.write("Hello World".getBytes());
+ zos.finish();
+ assertEquals("Finish failed to closeCurrentEntry", 11, ze.getSize());
- /**
- * @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();
@@ -109,179 +92,182 @@
} catch (IOException e) {
// Expected
}
- }
+ }
+
+ /**
+ * @tests java.util.zip.ZipOutputStream#putNextEntry(java.util.zip.ZipEntry)
+ */
+ public void test_putNextEntryLjava_util_zip_ZipEntry() throws IOException {
+ 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);
+ fail("Entry with incorrect setting failed to throw exception");
+ } catch (IOException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @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#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) {
- }
- }
+ /**
+ * @tests java.util.zip.ZipOutputStream#setLevel(int)
+ */
+ public void test_setLevelI() throws IOException {
+ 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());
+ }
+
+ /**
+ * @tests java.util.zip.ZipOutputStream#setMethod(int)
+ */
+ public void test_setMethodI() throws IOException {
+ 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());
+ }
+
+ /**
+ * @tests java.util.zip.ZipOutputStream#write(byte[], int, int)
+ */
+ public void test_write$BII() throws IOException {
+ 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));
+
+ 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 (ZipException e) {
+ // expected -- 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 (IOException e) {
+ // expected
+ }
+
+ 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 (IndexOutOfBoundsException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests java.util.zip.ZipOutputStream#write(byte[], int, int)
+ */
+ public void test_write$BII_2() throws IOException {
+ // Regression for HARMONY-577
+ File f1 = File.createTempFile("testZip1", "tst");
+ f1.deleteOnExit();
+ FileOutputStream stream1 = new FileOutputStream(f1);
+ ZipOutputStream zip1 = new ZipOutputStream(stream1);
+ zip1.putNextEntry(new ZipEntry("one"));
+ zip1.setMethod(ZipOutputStream.STORED);
+ zip1.setMethod(ZipEntry.STORED);
+
+ zip1.write(new byte[2]);
+
+ try {
+ zip1.putNextEntry(new ZipEntry("Second"));
+ fail("ZipException expected");
+ } catch (ZipException e) {
+ // expected - We have not set an entry
+ }
+
+ try {
+ zip1.write(new byte[2]); // try to write data without entry
+ fail("expected IOE there");
+ } catch (IOException e2) {
+ // expected
+ }
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ zos = new ZipOutputStream(bos = new ByteArrayOutputStream());
+ }
+
+ protected void tearDown() throws Exception {
+ try {
+ if (zos != null)
+ zos.close();
+ if (zis != null)
+ zis.close();
+ } catch (Exception e) {}
+ super.tearDown();
+ }
}