harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r822846 [2/2] - in /harmony/enhanced/classlib/trunk: modules/archive/src/main/java/java/util/jar/ modules/archive/src/main/java/java/util/zip/ modules/archive/src/main/native/archive/shared/ modules/archive/src/main/native/archive/unix/ mod...
Date Wed, 07 Oct 2009 18:54:09 GMT
Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/DeflaterOutputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/DeflaterOutputStreamTest.java?rev=822846&r1=822845&r2=822846&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/DeflaterOutputStreamTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/DeflaterOutputStreamTest.java
Wed Oct  7 18:54:08 2009
@@ -32,6 +32,8 @@
 public class DeflaterOutputStreamTest extends TestCase {
 
     private class MyDeflaterOutputStream extends DeflaterOutputStream {
+        boolean deflateFlag = false;
+
         MyDeflaterOutputStream(OutputStream out) {
             super(out);
         }
@@ -48,8 +50,13 @@
             return buf;
         }
 
-        void myDeflate() throws IOException {
-            deflate();
+        protected void deflate() throws IOException {
+            deflateFlag = true;
+            super.deflate();
+        }
+
+        boolean getDaflateFlag() {
+            return deflateFlag;
         }
     }
 
@@ -168,22 +175,23 @@
      * @tests java.util.zip.DeflaterOutputStream#close()
      */
     public void test_close() throws Exception {
-        File f1 = new File("close.tst");
-        FileOutputStream fos = new FileOutputStream(f1);
-        DeflaterOutputStream dos = new DeflaterOutputStream(fos);
-        byte byteArray[] = { 1, 3, 4, 6 };
-        dos.write(byteArray);
+        File f1 = File.createTempFile("close", ".tst");
 
-        FileInputStream fis = new FileInputStream(f1);
-        InflaterInputStream iis = new InflaterInputStream(fis);
+        InflaterInputStream iis = new InflaterInputStream(new FileInputStream(f1));
         try {
             iis.read();
             fail("EOFException Not Thrown");
         } catch (EOFException e) {
         }
 
+        FileOutputStream fos = new FileOutputStream(f1);
+        DeflaterOutputStream dos = new DeflaterOutputStream(fos);
+        byte byteArray[] = {1, 3, 4, 6};
+        dos.write(byteArray);
         dos.close();
 
+        iis = new InflaterInputStream(new FileInputStream(f1));
+
         // Test to see if the finish method wrote the bytes to the file.
         assertEquals("Incorrect Byte Returned.", 1, iis.read());
         assertEquals("Incorrect Byte Returned.", 3, iis.read());
@@ -379,4 +387,16 @@
 
         f2.delete();
     }
+
+    public void test_deflate() throws Exception {
+        File f1 = File.createTempFile("writeI1", ".tst");
+        FileOutputStream fos = new FileOutputStream(f1);
+        MyDeflaterOutputStream dos = new MyDeflaterOutputStream(fos);
+        assertFalse(dos.getDaflateFlag());
+        for (int i = 0; i < 3; i++) {
+            dos.write(i);
+        }
+        assertTrue(dos.getDaflateFlag());
+        dos.close();
+    }
 }

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterInputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterInputStreamTest.java?rev=822846&r1=822845&r2=822846&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterInputStreamTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterInputStreamTest.java
Wed Oct  7 18:54:08 2009
@@ -21,6 +21,8 @@
 import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.File;
+import java.io.FileInputStream;
 import java.util.zip.DeflaterOutputStream;
 import java.util.zip.Inflater;
 import java.util.zip.InflaterInputStream;
@@ -158,6 +160,42 @@
 		}
 	}
 
+    public void testAvailableNonEmptySource() throws Exception {
+        // this byte[] is a deflation of these bytes: { 1, 3, 4, 6 }
+        byte[] deflated = { 72, -119, 99, 100, 102, 97, 3, 0, 0, 31, 0, 15, 0 };
+        InputStream in = new InflaterInputStream(new ByteArrayInputStream(deflated));
+        // InflaterInputStream.available() returns either 1 or 0, even though
+        // that contradicts the behavior defined in InputStream.available()
+        assertEquals(1, in.read());
+        assertEquals(1, in.available());
+        assertEquals(3, in.read());
+        assertEquals(1, in.available());
+        assertEquals(4, in.read());
+        assertEquals(1, in.available());
+        assertEquals(6, in.read());
+        assertEquals(0, in.available());
+        assertEquals(-1, in.read());
+        assertEquals(-1, in.read());
+    }
+
+    public void testAvailableSkip() throws Exception {
+        // this byte[] is a deflation of these bytes: { 1, 3, 4, 6 }
+        byte[] deflated = { 72, -119, 99, 100, 102, 97, 3, 0, 0, 31, 0, 15, 0 };
+        InputStream in = new InflaterInputStream(new ByteArrayInputStream(deflated));
+        assertEquals(1, in.available());
+        assertEquals(4, in.skip(4));
+        assertEquals(0, in.available());
+    }
+
+    public void testAvailableEmptySource() throws Exception {
+        // this byte[] is a deflation of the empty file
+        byte[] deflated = { 120, -100, 3, 0, 0, 0, 0, 1 };
+        InputStream in = new InflaterInputStream(new ByteArrayInputStream(deflated));
+        assertEquals(-1, in.read());
+        assertEquals(-1, in.read());
+        assertEquals(0, in.available());
+    }
+
 	/**
 	 * @tests java.util.zip.InflaterInputStream#read(byte[], int, int)
 	 */
@@ -192,6 +230,39 @@
         }
 	}
 
+    public void test_read$BII2() throws IOException {
+        File resources = Support_Resources.createTempFolder();
+        Support_Resources.copyFile(resources, null, "Broken_manifest.jar");
+        FileInputStream fis = new FileInputStream(new File(resources,
+                "Broken_manifest.jar"));
+        InflaterInputStream iis = new InflaterInputStream(fis);
+        byte[] outBuf = new byte[530];
+
+        iis.close();
+        try {
+            iis.read(outBuf, 0, 5);
+            fail("IOException expected");
+        } catch (IOException ee) {
+            // expected.
+        }
+    }
+
+    public void test_read$BII3() throws IOException {
+        File resources = Support_Resources.createTempFolder();
+        Support_Resources.copyFile(resources, null, "Broken_manifest.jar");
+        FileInputStream fis = new FileInputStream(new File(resources,
+                "Broken_manifest.jar"));
+        InflaterInputStream iis = new InflaterInputStream(fis);
+        byte[] outBuf = new byte[530];
+
+        try {
+            iis.read();
+            fail("IOException expected.");
+        } catch (IOException ee) {
+            // expected
+        }
+    }
+
     /**
      * @tests java.util.zip.InflaterInputStream#reset()
      */
@@ -310,19 +381,16 @@
 		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);
+        int available;
+        for (int i = 0; i < 11; i++) {
+            iis.read();
+            available = iis.available();
+            if (available == 0) {
+                assertEquals("Expected no more bytes to read", -1, iis.read());
             } else {
-                assertEquals("Bytes Available Should Return 1.",
-						1, available);
+                assertEquals("Bytes Available Should Return 1.", 1, available);
             }
-		}
+        }
 
 		iis.close();
 		try {

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java?rev=822846&r1=822845&r2=822846&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java
Wed Oct  7 18:54:08 2009
@@ -19,6 +19,7 @@
 import java.io.BufferedInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.zip.Adler32;
 import java.io.UnsupportedEncodingException;
 import java.util.zip.DataFormatException;
@@ -314,6 +315,41 @@
 				0, outPutInf[emptyArray.length]);
 	}
 
+    public void test_inflate$B1() {
+        byte codedData[] = {
+                120, -38, 75, -54, 73, -52, 80, 40, 46, 41, -54, -52, 75, 87,
+                72, -50, -49, 43, 73, -52, -52, 43, 86, 72, 2, 10, 34, 99,
+                -123, -60, -68, 20, -80, 32, 0, -101, -69, 17, 84};
+        String codedString = "blah string contains blahblahblahblah and blah";
+
+        Inflater infl1 = new Inflater();
+        Inflater infl2 = new Inflater();
+
+        byte[] result = new byte[100];
+        int decLen = 0;
+
+        infl1.setInput(codedData, 0, codedData.length);
+        try {
+            decLen = infl1.inflate(result);
+        } catch (DataFormatException e) {
+            fail("Unexpected DataFormatException");
+        }
+
+        infl1.end();
+        assertEquals(codedString, new String(result, 0, decLen));
+        codedData[5] = 0;
+
+        infl2.setInput(codedData, 0, codedData.length);
+        try {
+            decLen = infl2.inflate(result);
+            fail("Expected DataFormatException");
+        } catch (DataFormatException e) {
+            // expected
+        }
+
+        infl2.end();
+    }
+
 	/**
 	 * @tests java.util.zip.Inflater#inflate(byte[], int, int)
 	 */
@@ -361,6 +397,41 @@
 		assertEquals("out of bounds error did not get caught", 1, r);
 	}
 
+    public void test_inflate$BII1() {
+        byte codedData[] = {
+                120, -38, 75, -54, 73, -52, 80, 40, 46, 41, -54, -52, 75, 87,
+                72, -50, -49, 43, 73, -52, -52, 43, 86, 72, 2, 10, 34, 99,
+                -123, -60, -68, 20, -80, 32, 0, -101, -69, 17, 84};
+        String codedString = "blah string";
+
+        Inflater infl1 = new Inflater();
+        Inflater infl2 = new Inflater();
+
+        byte[] result = new byte[100];
+        int decLen = 0;
+
+        infl1.setInput(codedData, 0, codedData.length);
+        try {
+            decLen = infl1.inflate(result, 10, 11);
+        } catch (DataFormatException e) {
+            fail("Unexpected DataFormatException");
+        }
+
+        infl1.end();
+        assertEquals(codedString, new String(result, 10, decLen));
+        codedData[5] = 0;
+
+        infl2.setInput(codedData, 0, codedData.length);
+        try {
+            decLen = infl2.inflate(result, 10, 11);
+            fail("Expected DataFormatException");
+        } catch (DataFormatException e) {
+            // expected
+        }
+
+        infl2.end();
+    }
+
 	/**
 	 * @tests java.util.zip.Inflater#Inflater()
 	 */
@@ -712,17 +783,249 @@
         byte[] b = new byte[1024];
         assertEquals(0, inflater.inflate(b));
         inflater.end();
-        
+
         // Regression for HARMONY-2510
         inflater = new Inflater();
-        byte[] input = new byte[] { -1 };
-        inflater.setInput(input);
+        inflater.setInput(new byte[] { -1 });
         try {
             inflater.inflate(b);
-            fail("should throw DataFormateException");
+
+            // The RI detects malformed data on the malformed input { -1 }. Both
+            // this implementation and the native zlib API return "need input"
+            // on that data. This is an error if the stream is exhausted, but
+            // not one that results in an exception in the Inflater API.
+            assertTrue(inflater.needsInput());
         } catch (DataFormatException e) {
             // expected
-        }       
-    }   
-    
+        }
+
+        inflater = new Inflater();
+        inflater.setInput(new byte[] { -1, -1, -1 });
+        try {
+            inflater.inflate(b);
+        } catch (DataFormatException e) {
+            // expected
+        }
+    }
+
+    public void testSetDictionary$B() throws Exception {
+        int i = 0;
+        String inputString = "blah string contains blahblahblahblah and blah";
+        String dictionary1 = "blah";
+        String dictionary2 = "1234";
+
+        byte[] outputNo = new byte[100];
+        byte[] output1 = new byte[100];
+        byte[] output2 = new byte[100];
+        Deflater defDictNo = new Deflater(9);
+        Deflater defDict1 = new Deflater(9);
+        Deflater defDict2 = new Deflater(9);
+
+        defDict1.setDictionary(dictionary1.getBytes());
+        defDict2.setDictionary(dictionary2.getBytes());
+
+        defDictNo.setInput(inputString.getBytes());
+        defDict1.setInput(inputString.getBytes());
+        defDict2.setInput(inputString.getBytes());
+
+        defDictNo.finish();
+        defDict1.finish();
+        defDict2.finish();
+
+        int dataLenNo = defDictNo.deflate(outputNo);
+        int dataLen1 = defDict1.deflate(output1);
+        int dataLen2 = defDict2.deflate(output2);
+
+        boolean passNo1 = false;
+        boolean passNo2 = false;
+        boolean pass12 = false;
+
+        for (i = 0; i < (dataLenNo < dataLen1 ? dataLenNo : dataLen1); i++) {
+            if (outputNo[i] != output1[i]) {
+                passNo1 = true;
+                break;
+            }
+        }
+        for (i = 0; i < (dataLenNo < dataLen1 ? dataLenNo : dataLen2); i++) {
+            if (outputNo[i] != output2[i]) {
+                passNo2 = true;
+                break;
+            }
+        }
+        for (i = 0; i < (dataLen1 < dataLen2 ? dataLen1 : dataLen2); i++) {
+            if (output1[i] != output2[i]) {
+                pass12 = true;
+                break;
+            }
+        }
+
+        assertTrue(
+                "Compressed data the same for stream with dictionary and without it.",
+                passNo1);
+        assertTrue(
+                "Compressed data the same for stream with dictionary and without it.",
+                passNo2);
+        assertTrue(
+                "Compressed data the same for stream with different dictionaries.",
+                pass12);
+
+        Inflater inflNo = new Inflater();
+        Inflater infl1 = new Inflater();
+        Inflater infl2 = new Inflater();
+
+        byte[] result = new byte[100];
+        int decLen;
+
+        inflNo.setInput(outputNo, 0, dataLenNo);
+        decLen = inflNo.inflate(result);
+
+        assertFalse(inflNo.needsDictionary());
+        inflNo.end();
+        assertEquals(inputString, new String(result, 0, decLen));
+
+        infl1.setInput(output1, 0, dataLen1);
+        decLen = infl1.inflate(result);
+
+        assertTrue(infl1.needsDictionary());
+        infl1.setDictionary(dictionary1.getBytes());
+        decLen = infl1.inflate(result);
+        infl1.end();
+        assertEquals(inputString, new String(result, 0, decLen));
+
+        infl2.setInput(output2, 0, dataLen2);
+        decLen = infl2.inflate(result);
+
+        assertTrue(infl2.needsDictionary());
+        infl2.setDictionary(dictionary2.getBytes());
+        decLen = infl2.inflate(result);
+        infl2.end();
+        assertEquals(inputString, new String(result, 0, decLen));
+
+
+        inflNo = new Inflater();
+        infl1 = new Inflater();
+        inflNo.setInput(outputNo, 0, dataLenNo);
+        try {
+            infl1.setDictionary(dictionary1.getBytes());
+            fail("IllegalArgumentException expected.");
+        } catch (IllegalArgumentException ee) {
+            // expected.
+        }
+        inflNo.end();
+
+        infl1.setInput(output1, 0, dataLen1);
+        decLen = infl1.inflate(result);
+
+        assertTrue(infl1.needsDictionary());
+        try {
+            infl1.setDictionary(dictionary2.getBytes());
+            fail("IllegalArgumentException expected.");
+        } catch (IllegalArgumentException ee) {
+            // expected.
+        }
+        infl1.end();
+    }
+
+    public void testSetDictionary$BII() throws Exception {
+        int i = 0;
+        String inputString = "blah string contains blahblahblahblah and blah";
+        String dictionary1 = "blah";
+        String dictionary2 = "blahblahblah";
+
+        byte[] output1 = new byte[100];
+        byte[] output2 = new byte[100];
+        byte[] output3 = new byte[100];
+
+        Deflater defDict1 = new Deflater(9);
+        Deflater defDict2 = new Deflater(9);
+        Deflater defDict3 = new Deflater(9);
+
+        defDict1.setDictionary(dictionary1.getBytes());
+        defDict2.setDictionary(dictionary2.getBytes());
+        defDict3.setDictionary(dictionary2.getBytes(), 4, 4);
+
+        defDict1.setInput(inputString.getBytes());
+        defDict2.setInput(inputString.getBytes());
+        defDict3.setInput(inputString.getBytes());
+
+        defDict1.finish();
+        defDict2.finish();
+        defDict3.finish();
+
+        int dataLen1 = defDict1.deflate(output1);
+        int dataLen2 = defDict2.deflate(output2);
+        int dataLen3 = defDict3.deflate(output3);
+
+        boolean pass12 = false;
+        boolean pass23 = false;
+        boolean pass13 = true;
+
+        for (i = 0; i < (dataLen1 < dataLen2 ? dataLen1 : dataLen2); i++) {
+            if (output1[i] != output2[i]) {
+                pass12 = true;
+                break;
+            }
+        }
+        for (i = 0; i < (dataLen2 < dataLen3 ? dataLen2 : dataLen3); i++) {
+            if (output2[i] != output3[i]) {
+                pass23 = true;
+                break;
+            }
+        }
+        for (i = 0; i < (dataLen1 < dataLen3 ? dataLen1 : dataLen3); i++) {
+            if (output1[i] != output3[i]) {
+                pass13 = false;
+                break;
+            }
+        }
+
+        assertTrue(
+                "Compressed data the same for stream with different dictionaries.",
+                pass12);
+        assertTrue(
+                "Compressed data the same for stream with different dictionaries.",
+                pass23);
+        assertTrue(
+                "Compressed data the differs for stream with the same dictionaries.",
+                pass13);
+
+        Inflater infl1 = new Inflater();
+        Inflater infl2 = new Inflater();
+        Inflater infl3 = new Inflater();
+
+        byte[] result = new byte[100];
+        int decLen;
+
+        infl1.setInput(output1, 0, dataLen1);
+        decLen = infl1.inflate(result);
+
+        assertTrue(infl1.needsDictionary());
+        infl1.setDictionary(dictionary2.getBytes(), 4, 4);
+        decLen = infl1.inflate(result);
+        infl1.end();
+        assertEquals(inputString, new String(result, 0, decLen));
+
+        infl2.setInput(output2, 0, dataLen2);
+        decLen = infl2.inflate(result);
+
+        assertTrue(infl2.needsDictionary());
+        try {
+            infl2.setDictionary(dictionary1.getBytes());
+            fail("IllegalArgumentException expected.");
+        } catch (IllegalArgumentException ee) {
+            // expected
+        }
+        infl2.end();
+
+        infl3.setInput(output3, 0, dataLen3);
+        decLen = infl3.inflate(result);
+
+        assertTrue(infl3.needsDictionary());
+        infl3.setDictionary(dictionary1.getBytes());
+        decLen = infl3.inflate(result);
+        infl3.end();
+        assertEquals(inputString, new String(result, 0, decLen));
+
+    }
+
 }

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipEntryTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipEntryTest.java?rev=822846&r1=822845&r2=822846&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipEntryTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipEntryTest.java
Wed Oct  7 18:54:08 2009
@@ -18,10 +18,26 @@
 
 import java.util.TimeZone;
 import java.util.zip.ZipEntry;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 
 import tests.support.resource.Support_Resources;
 
 public class ZipEntryTest extends junit.framework.TestCase {
+
+    public byte[] getAllBytesFromStream(InputStream is) throws IOException {
+        ByteArrayOutputStream bs = new ByteArrayOutputStream();
+        byte[] buf = new byte[512];
+        int iRead;
+        int off;
+        while (is.available() > 0) {
+            iRead = is.read(buf, 0, buf.length);
+            if (iRead > 0) bs.write(buf, 0, iRead);
+        }
+        return bs.toByteArray();
+    }
+
 	// zip file hyts_ZipFile.zip must be included as a resource
 	java.util.zip.ZipEntry zentry;
 
@@ -435,7 +451,6 @@
     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);
@@ -446,8 +461,7 @@
 			java.io.InputStream is = Support_Resources
 					.getStream("hyts_ZipFile.zip");
 			java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
-			rbuf = new byte[is.available()];
-			is.read(rbuf, 0, rbuf.length);
+            byte[] rbuf = getAllBytesFromStream(is);
 			fos.write(rbuf, 0, rbuf.length);
 			is.close();
 			fos.close();

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java?rev=822846&r1=822845&r2=822846&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java
Wed Oct  7 18:54:08 2009
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.ByteArrayOutputStream;
 import java.util.Enumeration;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
@@ -30,6 +31,18 @@
 
 public class ZipFileTest extends junit.framework.TestCase {
 
+    public byte[] getAllBytesFromStream(InputStream is) throws IOException {
+        ByteArrayOutputStream bs = new ByteArrayOutputStream();
+        byte[] buf = new byte[512];
+        int iRead;
+        int off;
+        while (is.available() > 0) {
+            iRead = is.read(buf, 0, buf.length);
+            if (iRead > 0) bs.write(buf, 0, iRead);
+        }
+        return bs.toByteArray();
+    }
+
 	// the file hyts_zipFile.zip in setup must be included as a resource
 	private String tempFileName;
 
@@ -254,10 +267,12 @@
         assertEquals(rbuf1.length, r1);
         r2 = is.read(rbuf2);
         assertEquals(rbuf2.length, r2);
-        
-        is.reset();
-        r2 = is.read(rbuf2);
-        assertEquals(rbuf2.length, r2);
+
+        try {
+            is.reset();
+            fail();
+        } catch (IOException expected) {
+        }
         is.close();
 
         // read a compressed entry
@@ -266,18 +281,21 @@
         is = zfile.getInputStream(zentry2);
         r1 = is.read(rbuf3);
         assertEquals(4183, r1);
-        is.reset();
-        
-        r1 = is.read(rbuf3);
-        assertEquals(4183, r1);
+        try {
+            is.reset();
+            fail();
+        } catch (IOException expected) {
+        }
         is.close();
 
         is = zfile.getInputStream(zentry2);
         r1 = is.read(rbuf3, 0, 3000);
         assertEquals(3000, r1);
-        is.reset();
-        r1 = is.read(rbuf3, 0, 3000);
-        assertEquals(3000, r1);
+        try {
+            is.reset();
+            fail();
+        } catch (IOException expected) {
+        }
         is.close();
     }
     
@@ -296,11 +314,13 @@
         r = is.read(rbuf1);
         assertEquals(8, r);
         assertEquals(-1, is.read());
-        
-        is.reset();
-        r = is.read(rbuf2);
-        assertEquals(8, r);
-        assertEquals(-1, is.read());
+
+        try {
+            is.reset();
+            fail();
+        } catch (IOException expected) {
+        }
+
         is.close();
 
         // read a compressed entry
@@ -313,11 +333,13 @@
         r = is.read(rbuf3);
         assertEquals(1183, r);
         assertEquals(-1, is.read());
-        
-        is.reset();
-        r = is.read(rbuf3);
-        assertEquals(1183, r);
-        assertEquals(-1, is.read());
+
+        try {
+            is.reset();
+            fail();
+        } catch (IOException expected) {
+        }
+
         is.close();
     }
 
@@ -328,7 +350,6 @@
 	@Override
     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");
@@ -346,8 +367,7 @@
 			f.delete();
 			InputStream is = Support_Resources.getStream("hyts_ZipFile.zip");
 			FileOutputStream fos = new FileOutputStream(f);
-			rbuf = new byte[is.available()];
-			is.read(rbuf, 0, rbuf.length);
+            byte[] rbuf = getAllBytesFromStream(is);
 			fos.write(rbuf, 0, rbuf.length);
 			is.close();
 			fos.close();

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java?rev=822846&r1=822845&r2=822846&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java
Wed Oct  7 18:54:08 2009
@@ -207,4 +207,39 @@
             // Expected
         }
     }
+
+    public void test_available() throws Exception {
+
+        File resources = Support_Resources.createTempFolder();
+        Support_Resources.copyFile(resources, null, "hyts_ZipFile.zip");
+        File fl = new File(resources, "hyts_ZipFile.zip");
+        FileInputStream fis = new FileInputStream(fl);
+
+        ZipInputStream zis1 = new ZipInputStream(fis);
+        ZipEntry entry = zis1.getNextEntry();
+        assertNotNull("No entry in the archive.", entry);
+        long entrySize = entry.getSize();
+        assertTrue("Entry size was < 1", entrySize > 0);
+        int i = 0;
+        while (zis1.available() > 0) {
+            zis1.skip(1);
+            i++;
+        }
+        if (i != entrySize) {
+            fail("ZipInputStream.available or ZipInputStream.skip does not " +
+                    "working properly. Only skipped " + i +
+                    " bytes instead of " + entrySize);
+        }
+        assertEquals(0, zis1.skip(1));
+        assertEquals(0, zis1.available());
+        zis1.closeEntry();
+        assertEquals(1, zis.available());
+        zis1.close();
+        try {
+            zis1.available();
+            fail("IOException expected");
+        } catch (IOException ee) {
+            // expected
+        }
+    }
 }

Added: harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/EmptyEntries_signed.jar
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/EmptyEntries_signed.jar?rev=822846&view=auto
==============================================================================
Binary file - no diff available.

Propchange: harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/EmptyEntries_signed.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message