< prev index next >

test/jdk/jdk/nio/zipfs/ZipFSTester.java

Print this page


   1 /*
   2  * Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  64 import java.util.zip.ZipFile;
  65 import java.util.zip.ZipOutputStream;
  66 
  67 import static java.nio.file.StandardOpenOption.*;
  68 import static java.nio.file.StandardCopyOption.*;
  69 
  70 /*
  71  * Tests various zipfs operations.
  72  *
  73  * @test
  74  * @bug 6990846 7009092 7009085 7015391 7014948 7005986 7017840 7007596
  75  *      7157656 8002390 7012868 7012856 8015728 8038500 8040059 8069211
  76  *      8131067 8034802 8210899
  77  * @summary Test Zip filesystem provider
  78  * @modules jdk.zipfs
  79  * @run main ZipFSTester
  80  * @run main/othervm/java.security.policy=test.policy ZipFSTester
  81  */
  82 
  83 public class ZipFSTester {
  84 
  85     public static void main(String[] args) throws Exception {
  86         // create JAR file for test, actual contents don't matter
  87         Path jarFile = Utils.createJarFile("tester.jar",
  88                 "META-INF/MANIFEST.MF",
  89                 "dir1/foo",
  90                 "dir2/bar",
  91                 "dir1/dir3/fooo");
  92 
  93         try (FileSystem fs = newZipFileSystem(jarFile, Collections.emptyMap())) {
  94             test0(fs);
  95             test1(fs);
  96             test2(fs);   // more tests
  97         }
  98         testStreamChannel();
  99         testTime(jarFile);
 100         test8069211();
 101         test8131067();
 102     }
 103 
 104     private static Random rdm = new Random();


 129     {
 130         // prepare a src for testing
 131         Path src = getTempPath();
 132         String tmpName = src.toString();
 133         try (OutputStream os = Files.newOutputStream(src)) {
 134             byte[] bits = new byte[12345];
 135             rdm.nextBytes(bits);
 136             os.write(bits);
 137         }
 138 
 139         // clone a fs from fs0 and test on it
 140         Path tmpfsPath = getTempPath();
 141         Map<String, Object> env = new HashMap<String, Object>();
 142         env.put("create", "true");
 143         try (FileSystem copy = newZipFileSystem(tmpfsPath, env)) {
 144             z2zcopy(fs0, copy, "/", 0);
 145 
 146             // copy the test jar itself in
 147             Files.copy(Paths.get(fs0.toString()), copy.getPath("/foo.jar"));
 148             Path zpath = copy.getPath("/foo.jar");
 149             try (FileSystem zzfs = FileSystems.newFileSystem(zpath, null)) {
 150                 Files.copy(src, zzfs.getPath("/srcInjarjar"));
 151             }
 152         }
 153 
 154         try (FileSystem fs = newZipFileSystem(tmpfsPath, new HashMap<String, Object>())) {
 155 
 156             FileSystemProvider provider = fs.provider();
 157             // newFileSystem(path...) should not throw exception
 158             try (FileSystem fsPath = provider.newFileSystem(tmpfsPath, new HashMap<String, Object>())){}
 159             try (FileSystem fsUri = provider.newFileSystem(
 160                      new URI("jar", tmpfsPath.toUri().toString(), null),
 161                      new HashMap<String, Object>()))
 162             {
 163                 throw new RuntimeException("newFileSystem(URI...) does not throw exception");
 164             } catch (FileSystemAlreadyExistsException fsaee) {}
 165 
 166             try {
 167                 provider.newFileSystem(new File(System.getProperty("test.src", ".")).toPath(),
 168                                        new HashMap<String, Object>());
 169                 throw new RuntimeException("newFileSystem() opens a directory as zipfs");


 237                 rmdirs(parent);
 238             } catch (IOException x) {
 239                 x.printStackTrace();
 240             }
 241 
 242             // newFileChannel() copy in, out and verify via fch
 243             fchCopy(src, dst);    // in
 244             checkEqual(src, dst);
 245             Path tmp = Paths.get(tmpName + "_Tmp");
 246             fchCopy(dst, tmp);   //  out
 247             checkEqual(src, tmp);
 248             Files.delete(tmp);
 249 
 250             // test channels
 251             channel(fs, dst);
 252             Files.delete(dst);
 253 
 254             // test foo.jar in jar/zipfs #8034802
 255             Path jpath = fs.getPath("/foo.jar");
 256             System.out.println("walking: " + jpath);
 257             try (FileSystem zzfs = FileSystems.newFileSystem(jpath, null)) {
 258                 walk(zzfs.getPath("/"));
 259                 // foojar:/srcInjarjar
 260                 checkEqual(src, zzfs.getPath("/srcInjarjar"));
 261 
 262                 dst = getPathWithParents(zzfs, tmpName);
 263                 fchCopy(src, dst);
 264                 checkEqual(src, dst);
 265                 tmp = Paths.get(tmpName + "_Tmp");
 266                 fchCopy(dst, tmp);   //  out
 267                 checkEqual(src, tmp);
 268                 Files.delete(tmp);
 269 
 270                 channel(zzfs, dst);
 271                 Files.delete(dst);
 272             }
 273         } finally {
 274             Files.deleteIfExists(tmpfsPath);
 275             Files.deleteIfExists(src);
 276         }
 277     }


   1 /*
   2  * Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  64 import java.util.zip.ZipFile;
  65 import java.util.zip.ZipOutputStream;
  66 
  67 import static java.nio.file.StandardOpenOption.*;
  68 import static java.nio.file.StandardCopyOption.*;
  69 
  70 /*
  71  * Tests various zipfs operations.
  72  *
  73  * @test
  74  * @bug 6990846 7009092 7009085 7015391 7014948 7005986 7017840 7007596
  75  *      7157656 8002390 7012868 7012856 8015728 8038500 8040059 8069211
  76  *      8131067 8034802 8210899
  77  * @summary Test Zip filesystem provider
  78  * @modules jdk.zipfs
  79  * @run main ZipFSTester
  80  * @run main/othervm/java.security.policy=test.policy ZipFSTester
  81  */
  82 
  83 public class ZipFSTester {

  84     public static void main(String[] args) throws Exception {
  85         // create JAR file for test, actual contents don't matter
  86         Path jarFile = Utils.createJarFile("tester.jar",
  87                 "META-INF/MANIFEST.MF",
  88                 "dir1/foo",
  89                 "dir2/bar",
  90                 "dir1/dir3/fooo");
  91 
  92         try (FileSystem fs = newZipFileSystem(jarFile, Collections.emptyMap())) {
  93             test0(fs);
  94             test1(fs);
  95             test2(fs);   // more tests
  96         }
  97         testStreamChannel();
  98         testTime(jarFile);
  99         test8069211();
 100         test8131067();
 101     }
 102 
 103     private static Random rdm = new Random();


 128     {
 129         // prepare a src for testing
 130         Path src = getTempPath();
 131         String tmpName = src.toString();
 132         try (OutputStream os = Files.newOutputStream(src)) {
 133             byte[] bits = new byte[12345];
 134             rdm.nextBytes(bits);
 135             os.write(bits);
 136         }
 137 
 138         // clone a fs from fs0 and test on it
 139         Path tmpfsPath = getTempPath();
 140         Map<String, Object> env = new HashMap<String, Object>();
 141         env.put("create", "true");
 142         try (FileSystem copy = newZipFileSystem(tmpfsPath, env)) {
 143             z2zcopy(fs0, copy, "/", 0);
 144 
 145             // copy the test jar itself in
 146             Files.copy(Paths.get(fs0.toString()), copy.getPath("/foo.jar"));
 147             Path zpath = copy.getPath("/foo.jar");
 148             try (FileSystem zzfs = FileSystems.newFileSystem(zpath, (ClassLoader)null)) {
 149                 Files.copy(src, zzfs.getPath("/srcInjarjar"));
 150             }
 151         }
 152 
 153         try (FileSystem fs = newZipFileSystem(tmpfsPath, new HashMap<String, Object>())) {
 154 
 155             FileSystemProvider provider = fs.provider();
 156             // newFileSystem(path...) should not throw exception
 157             try (FileSystem fsPath = provider.newFileSystem(tmpfsPath, new HashMap<String, Object>())){}
 158             try (FileSystem fsUri = provider.newFileSystem(
 159                      new URI("jar", tmpfsPath.toUri().toString(), null),
 160                      new HashMap<String, Object>()))
 161             {
 162                 throw new RuntimeException("newFileSystem(URI...) does not throw exception");
 163             } catch (FileSystemAlreadyExistsException fsaee) {}
 164 
 165             try {
 166                 provider.newFileSystem(new File(System.getProperty("test.src", ".")).toPath(),
 167                                        new HashMap<String, Object>());
 168                 throw new RuntimeException("newFileSystem() opens a directory as zipfs");


 236                 rmdirs(parent);
 237             } catch (IOException x) {
 238                 x.printStackTrace();
 239             }
 240 
 241             // newFileChannel() copy in, out and verify via fch
 242             fchCopy(src, dst);    // in
 243             checkEqual(src, dst);
 244             Path tmp = Paths.get(tmpName + "_Tmp");
 245             fchCopy(dst, tmp);   //  out
 246             checkEqual(src, tmp);
 247             Files.delete(tmp);
 248 
 249             // test channels
 250             channel(fs, dst);
 251             Files.delete(dst);
 252 
 253             // test foo.jar in jar/zipfs #8034802
 254             Path jpath = fs.getPath("/foo.jar");
 255             System.out.println("walking: " + jpath);
 256             try (FileSystem zzfs = FileSystems.newFileSystem(jpath, (ClassLoader)null)) {
 257                 walk(zzfs.getPath("/"));
 258                 // foojar:/srcInjarjar
 259                 checkEqual(src, zzfs.getPath("/srcInjarjar"));
 260 
 261                 dst = getPathWithParents(zzfs, tmpName);
 262                 fchCopy(src, dst);
 263                 checkEqual(src, dst);
 264                 tmp = Paths.get(tmpName + "_Tmp");
 265                 fchCopy(dst, tmp);   //  out
 266                 checkEqual(src, tmp);
 267                 Files.delete(tmp);
 268 
 269                 channel(zzfs, dst);
 270                 Files.delete(dst);
 271             }
 272         } finally {
 273             Files.deleteIfExists(tmpfsPath);
 274             Files.deleteIfExists(src);
 275         }
 276     }


< prev index next >