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 */ 17package org.apache.harmony.archive.tests.java.util.zip; 18 19import java.io.File; 20import java.io.FileOutputStream; 21import java.io.IOException; 22import java.io.InputStream; 23import java.io.OutputStream; 24import java.io.ByteArrayOutputStream; 25import java.util.Enumeration; 26import java.util.zip.ZipEntry; 27import java.util.zip.ZipFile; 28 29import tests.support.Support_PlatformFile; 30import tests.support.resource.Support_Resources; 31 32public class ZipFileTest extends junit.framework.TestCase { 33 34 public byte[] getAllBytesFromStream(InputStream is) throws IOException { 35 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 36 byte[] buf = new byte[512]; 37 int iRead; 38 int off; 39 while (is.available() > 0) { 40 iRead = is.read(buf, 0, buf.length); 41 if (iRead > 0) bs.write(buf, 0, iRead); 42 } 43 return bs.toByteArray(); 44 } 45 46 // the file hyts_zipFile.zip in setup must be included as a resource 47 private String tempFileName; 48 49 private ZipFile zfile; 50 51 /** 52 * @throws IOException 53 * @tests java.util.zip.ZipFile#ZipFile(java.io.File) 54 */ 55 public void test_ConstructorLjava_io_File() throws IOException { 56 // Test for method java.util.zip.ZipFile(java.io.File) 57 zfile.close(); // about to reopen the same temp file 58 File file = new File(tempFileName); 59 ZipFile zip = new ZipFile(file); 60 assertTrue("Zip should exist", file.exists()); 61 zip.close(); 62 } 63 64 /** 65 * @tests java.util.zip.ZipFile#ZipFile(java.io.File, int) 66 */ 67 public void test_ConstructorLjava_io_FileI() throws IOException { 68 zfile.close(); // about to reopen the same temp file 69 File file = new File(tempFileName); 70 ZipFile zip = new ZipFile(file, ZipFile.OPEN_DELETE 71 | ZipFile.OPEN_READ); 72 zip.close(); 73 assertTrue("Zip should not exist", !file.exists()); 74 75 file = new File(tempFileName); 76 try{ 77 zip = new ZipFile(file, 3); //passing unkown argument 78 fail("Should throw IllegalArgumentException"); 79 }catch(IllegalArgumentException iae ){ 80 //expected 81 } 82 } 83 84 /** 85 * @tests java.util.zip.ZipFile#ZipFile(java.lang.String) 86 */ 87 public void test_ConstructorLjava_lang_String() { 88 // Test for method java.util.zip.ZipFile(java.lang.String) 89 /* 90 * try { zfile = new java.util.zip.ZipFile(zipName); zfile.close(); } 91 * catch (java.io.IOException e) {fail( "Failed to construct 92 * ZipFile" );} 93 */ 94 } 95 96 protected ZipEntry test_finalize1(ZipFile zip) { 97 return zip.getEntry("File1.txt"); 98 } 99 100 protected ZipFile test_finalize2(File file) throws IOException { 101 return new ZipFile(file); 102 } 103 104 /** 105 * @tests java.util.zip.ZipFile#finalize() 106 */ 107 public void test_finalize() throws IOException { 108 InputStream in = Support_Resources.getStream("hyts_ZipFile.zip"); 109 File file = Support_Resources.createTempFile(".jar"); 110 OutputStream out = new FileOutputStream(file); 111 int result; 112 byte[] buf = new byte[4096]; 113 while ((result = in.read(buf)) != -1) { 114 out.write(buf, 0, result); 115 } 116 in.close(); 117 out.close(); 118 /* 119 * ZipFile zip = new ZipFile(file); ZipEntry entry1 = 120 * zip.getEntry("File1.txt"); assertNotNull("Did not find entry", 121 * entry1); entry1 = null; zip = null; 122 */ 123 124 assertNotNull("Did not find entry", 125 test_finalize1(test_finalize2(file))); 126 System.gc(); 127 System.gc(); 128 System.runFinalization(); 129 file.delete(); 130 assertTrue("Zip should not exist", !file.exists()); 131 } 132 133 /** 134 * @tests java.util.zip.ZipFile#close() 135 */ 136 public void test_close() { 137 // Test for method void java.util.zip.ZipFile.close() 138 try { 139 zfile.close(); 140 zfile.getInputStream(zfile.getEntry("ztest/file1.txt")); 141 } catch (Exception e) { 142 return; 143 } 144 fail("Close test failed"); 145 } 146 147 /** 148 * @tests java.util.zip.ZipFile#entries() 149 */ 150 public void test_entries() throws Exception { 151 // Test for method java.util.Enumeration java.util.zip.ZipFile.entries() 152 Enumeration<? extends ZipEntry> enumer = zfile.entries(); 153 int c = 0; 154 while (enumer.hasMoreElements()) { 155 ++c; 156 enumer.nextElement(); 157 } 158 assertEquals("Incorrect number of entries returned: " + c, 6, c); 159 160 Enumeration<? extends ZipEntry> enumeration = zfile.entries(); 161 zfile.close(); 162 zfile = null; 163 boolean pass = false; 164 try { 165 enumeration.hasMoreElements(); 166 } catch (IllegalStateException e) { 167 pass = true; 168 } 169 assertTrue("did not detect closed jar file", pass); 170 } 171 172 /** 173 * @tests java.util.zip.ZipFile#getEntry(java.lang.String) 174 */ 175 public void test_getEntryLjava_lang_String() throws IOException { 176 // Test for method java.util.zip.ZipEntry 177 // java.util.zip.ZipFile.getEntry(java.lang.String) 178 java.util.zip.ZipEntry zentry = zfile.getEntry("File1.txt"); 179 assertNotNull("Could not obtain ZipEntry", zentry); 180 181 zentry = zfile.getEntry("testdir1/File1.txt"); 182 assertNotNull("Could not obtain ZipEntry: testdir1/File1.txt", 183 zentry); 184 int r; 185 InputStream in; 186 zentry = zfile.getEntry("testdir1/"); 187 assertNotNull("Could not obtain ZipEntry: testdir1/", zentry); 188 in = zfile.getInputStream(zentry); 189 assertNotNull("testdir1/ should not have null input stream", 190 in); 191 r = in.read(); 192 in.close(); 193 assertEquals("testdir1/ should not contain data", -1, r); 194 195 zentry = zfile.getEntry("testdir1"); 196 assertNotNull("Could not obtain ZipEntry: testdir1", zentry); 197 in = zfile.getInputStream(zentry); 198 assertNotNull("testdir1 should not have null input stream", in); 199 r = in.read(); 200 in.close(); 201 assertEquals("testdir1 should not contain data", -1, r); 202 203 zentry = zfile.getEntry("testdir1/testdir1"); 204 assertNotNull("Could not obtain ZipEntry: testdir1/testdir1", 205 zentry); 206 in = zfile.getInputStream(zentry); 207 byte[] buf = new byte[256]; 208 r = in.read(buf); 209 in.close(); 210 assertEquals("incorrect contents", 211 "This is also text", new String(buf, 0, r, "UTF-8")); 212 } 213 214 public void test_getEntryLjava_lang_String_Ex() throws IOException { 215 java.util.zip.ZipEntry zentry = zfile.getEntry("File1.txt"); 216 assertNotNull("Could not obtain ZipEntry", zentry); 217 218 zfile.close(); 219 try { 220 zfile.getEntry("File2.txt"); 221 fail("IllegalStateException expected"); 222 } catch (IllegalStateException ee) { 223 // expected 224 } 225 } 226 227 /** 228 * @tests java.util.zip.ZipFile#getInputStream(java.util.zip.ZipEntry) 229 */ 230 public void test_getInputStreamLjava_util_zip_ZipEntry() { 231 // Test for method java.io.InputStream 232 // java.util.zip.ZipFile.getInputStream(java.util.zip.ZipEntry) 233 java.io.InputStream is = null; 234 try { 235 java.util.zip.ZipEntry zentry = zfile.getEntry("File1.txt"); 236 is = zfile.getInputStream(zentry); 237 byte[] rbuf = new byte[1000]; 238 int r; 239 is.read(rbuf, 0, r = (int) zentry.getSize()); 240 assertEquals("getInputStream read incorrect data", "This is text", new String(rbuf, 241 0, r, "UTF-8")); 242 } catch (java.io.IOException e) { 243 fail("IOException during getInputStream"); 244 } finally { 245 try { 246 is.close(); 247 } catch (java.io.IOException e) { 248 fail("Failed to close input stream"); 249 } 250 } 251 } 252 253 /** 254 * @tests java.util.zip.ZipFile#getName() 255 */ 256 public void test_getName() { 257 // Test for method java.lang.String java.util.zip.ZipFile.getName() 258 assertEquals("Returned incorrect name: " + zfile.getName(), tempFileName, 259 zfile.getName()); 260 } 261 262 /** 263 * @tests java.util.zip.ZipFile#size() 264 */ 265 public void test_size() { 266 assertEquals(6, zfile.size()); 267 } 268 269 /** 270 * @tests java.io.InputStream#reset() 271 */ 272 public void test_reset() throws IOException { 273 // read an uncompressed entry 274 ZipEntry zentry = zfile.getEntry("File1.txt"); 275 InputStream is = zfile.getInputStream(zentry); 276 byte[] rbuf1 = new byte[6]; 277 byte[] rbuf2 = new byte[6]; 278 int r1, r2; 279 r1 = is.read(rbuf1); 280 assertEquals(rbuf1.length, r1); 281 r2 = is.read(rbuf2); 282 assertEquals(rbuf2.length, r2); 283 284 try { 285 is.reset(); 286 fail(); 287 } catch (IOException expected) { 288 } 289 is.close(); 290 291 // read a compressed entry 292 byte[] rbuf3 = new byte[4185]; 293 ZipEntry zentry2 = zfile.getEntry("File3.txt"); 294 is = zfile.getInputStream(zentry2); 295 r1 = is.read(rbuf3); 296 assertEquals(4183, r1); 297 try { 298 is.reset(); 299 fail(); 300 } catch (IOException expected) { 301 } 302 is.close(); 303 304 is = zfile.getInputStream(zentry2); 305 r1 = is.read(rbuf3, 0, 3000); 306 assertEquals(3000, r1); 307 try { 308 is.reset(); 309 fail(); 310 } catch (IOException expected) { 311 } 312 is.close(); 313 } 314 315 /** 316 * @tests java.io.InputStream#reset() 317 */ 318 public void test_reset_subtest0() throws IOException { 319 // read an uncompressed entry 320 ZipEntry zentry = zfile.getEntry("File1.txt"); 321 InputStream is = zfile.getInputStream(zentry); 322 byte[] rbuf1 = new byte[12]; 323 byte[] rbuf2 = new byte[12]; 324 int r = is.read(rbuf1, 0, 4); 325 assertEquals(4, r); 326 is.mark(0); 327 r = is.read(rbuf1); 328 assertEquals(8, r); 329 assertEquals(-1, is.read()); 330 331 try { 332 is.reset(); 333 fail(); 334 } catch (IOException expected) { 335 } 336 337 is.close(); 338 339 // read a compressed entry 340 byte[] rbuf3 = new byte[4185]; 341 ZipEntry zentry2 = zfile.getEntry("File3.txt"); 342 is = zfile.getInputStream(zentry2); 343 r = is.read(rbuf3, 0, 3000); 344 assertEquals(3000, r); 345 is.mark(0); 346 r = is.read(rbuf3); 347 assertEquals(1183, r); 348 assertEquals(-1, is.read()); 349 350 try { 351 is.reset(); 352 fail(); 353 } catch (IOException expected) { 354 } 355 356 is.close(); 357 } 358 359 /** 360 * Sets up the fixture, for example, open a network connection. This method 361 * is called before a test is executed. 362 */ 363 @Override 364 protected void setUp() { 365 try { 366 // Create a local copy of the file since some tests want to alter 367 // information. 368 tempFileName = System.getProperty("user.dir"); 369 String separator = System.getProperty("file.separator"); 370 if (tempFileName.charAt(tempFileName.length() - 1) == separator 371 .charAt(0)) { 372 tempFileName = Support_PlatformFile.getNewPlatformFile( 373 tempFileName, "gabba.zip"); 374 } else { 375 tempFileName = Support_PlatformFile.getNewPlatformFile( 376 tempFileName + separator, "gabba.zip"); 377 } 378 379 File f = new File(tempFileName); 380 f.delete(); 381 InputStream is = Support_Resources.getStream("hyts_ZipFile.zip"); 382 FileOutputStream fos = new FileOutputStream(f); 383 byte[] rbuf = getAllBytesFromStream(is); 384 fos.write(rbuf, 0, rbuf.length); 385 is.close(); 386 fos.close(); 387 zfile = new ZipFile(f); 388 } catch (Exception e) { 389 System.out.println("Exception during ZipFile setup:"); 390 e.printStackTrace(); 391 } 392 } 393 394 /** 395 * Tears down the fixture, for example, close a network connection. This 396 * method is called after a test is executed. 397 */ 398 @Override 399 protected void tearDown() { 400 try { 401 if (zfile != null) { 402 // Note zfile is a user-defined zip file used by other tests and 403 // should not be deleted 404 zfile.close(); 405 tempFileName = System.getProperty("user.dir"); 406 String separator = System.getProperty("file.separator"); 407 if (tempFileName.charAt(tempFileName.length() - 1) == separator 408 .charAt(0)) { 409 tempFileName = Support_PlatformFile.getNewPlatformFile( 410 tempFileName, "gabba.zip"); 411 } else { 412 tempFileName = Support_PlatformFile.getNewPlatformFile( 413 tempFileName + separator, "gabba.zip"); 414 } 415 416 File f = new File(tempFileName); 417 f.delete(); 418 } 419 } catch (Exception e) { 420 } 421 } 422 423} 424