1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package libcore.java.util.zip; 19 20 import java.io.ByteArrayOutputStream; 21 import java.io.File; 22 import java.io.FileOutputStream; 23 import java.io.IOException; 24 import java.io.InputStream; 25 import java.util.zip.ZipEntry; 26 import java.util.zip.ZipFile; 27 import tests.support.resource.Support_Resources; 28 29 public class OldZipFileTest extends junit.framework.TestCase { 30 31 public byte[] getAllBytesFromStream(InputStream is) throws IOException { 32 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 33 byte[] buf = new byte[512]; 34 int iRead; 35 while ((iRead = is.read(buf, 0, buf.length)) != -1) { 36 bs.write(buf, 0, iRead); 37 } 38 return bs.toByteArray(); 39 } 40 41 public void test_size() throws IOException { 42 zfile.close(); 43 try { 44 zfile.size(); 45 fail("IllegalStateException expected"); 46 } catch (IllegalStateException expected) { 47 } 48 } 49 50 public void test_getEntryLjava_lang_String_AndroidOnly() throws IOException { 51 java.util.zip.ZipEntry zentry = zfile.getEntry("File1.txt"); 52 assertNotNull("Could not obtain ZipEntry", zentry); 53 int r; 54 InputStream in; 55 56 zentry = zfile.getEntry("testdir1"); 57 assertNotNull("Must be able to obtain ZipEntry: testdir1", zentry); 58 in = zfile.getInputStream(zentry); 59 /* 60 * Android delivers empty InputStream, RI no InputStream at all. The 61 * spec doesn't clarify this, so we need to deal with both situations. 62 */ 63 int data = -1; 64 if (in != null) { 65 data = in.read(); 66 in.close(); 67 } 68 assertEquals("Must not be able to read directory data", -1, data); 69 } 70 71 // the file hyts_zipFile.zip in setup must be included as a resource 72 private String tempFileName; 73 74 private ZipFile zfile; 75 76 /** 77 * @throws IOException 78 * java.util.zip.ZipFile#close() 79 */ 80 public void test_close() throws IOException { 81 // Test for method void java.util.zip.ZipFile.close() 82 File fl = new File(tempFileName); 83 ZipFile zf = new ZipFile(fl); 84 InputStream is1 = zf.getInputStream(zf.getEntry("File1.txt")); 85 InputStream is2 = zf.getInputStream(zf.getEntry("File2.txt")); 86 87 is1.read(); 88 is2.read(); 89 90 zf.close(); 91 92 try { 93 is1.read(); 94 fail("IOException expected"); 95 } catch (IOException ee) { 96 // expected 97 } 98 99 try { 100 is2.read(); 101 fail("IOException expected"); 102 } catch (IOException ee) { 103 // expected 104 } 105 } 106 107 public void test_getEntryLjava_lang_String_Ex() throws IOException { 108 java.util.zip.ZipEntry zentry = zfile.getEntry("File1.txt"); 109 assertNotNull("Could not obtain ZipEntry", zentry); 110 111 zfile.close(); 112 try { 113 zfile.getEntry("File2.txt"); 114 fail("IllegalStateException expected"); 115 } catch (IllegalStateException ee) { 116 } 117 } 118 119 /** 120 * @throws IOException 121 * java.util.zip.ZipFile#getInputStream(java.util.zip.ZipEntry) 122 */ 123 public void test_getInputStreamLjava_util_zip_ZipEntry() throws IOException { 124 // Test for method java.io.InputStream 125 // java.util.zip.ZipFile.getInputStream(java.util.zip.ZipEntry) 126 ZipEntry zentry = null; 127 InputStream is = null; 128 129 zentry = zfile.getEntry("File2.txt"); 130 zfile.close(); 131 try { 132 is = zfile.getInputStream(zentry); 133 fail("IllegalStateException expected"); 134 } catch (IllegalStateException ee) { 135 // expected 136 } 137 } 138 139 /** 140 * Sets up the fixture, for example, open a network connection. This method 141 * is called before a test is executed. 142 */ 143 @Override 144 protected void setUp() throws IOException { 145 // Create a local copy of the file since some tests want to alter information. 146 File tempFile = File.createTempFile("OldZipFileTest", "zip"); 147 tempFileName = tempFile.getAbsolutePath(); 148 InputStream is = Support_Resources.getStream("hyts_ZipFile.zip"); 149 FileOutputStream fos = new FileOutputStream(tempFile); 150 byte[] rbuf = getAllBytesFromStream(is); 151 fos.write(rbuf, 0, rbuf.length); 152 is.close(); 153 fos.close(); 154 zfile = new ZipFile(tempFile); 155 } 156 157 /** 158 * Tears down the fixture, for example, close a network connection. This 159 * method is called after a test is executed. 160 */ 161 @Override 162 protected void tearDown() throws IOException { 163 // Note zfile is a user-defined zip file used by other tests and 164 // should not be deleted 165 zfile.close(); 166 } 167 } 168