test/jdk/nio/zipfs/ZipFSTester.java

Print this page




   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  */
  23 
  24 import java.io.File;
  25 import java.io.IOException;
  26 import java.io.InputStream;
  27 import java.io.OutputStream;
  28 import java.net.URI;

  29 import java.nio.ByteBuffer;
  30 import java.nio.channels.FileChannel;
  31 import java.nio.channels.SeekableByteChannel;
  32 import java.nio.file.DirectoryStream;
  33 import java.nio.file.FileAlreadyExistsException;
  34 import java.nio.file.FileSystem;
  35 import java.nio.file.FileSystemAlreadyExistsException;
  36 import java.nio.file.FileSystemException;
  37 import java.nio.file.FileSystems;
  38 import java.nio.file.FileVisitResult;
  39 import java.nio.file.Files;
  40 import java.nio.file.OpenOption;
  41 import java.nio.file.Path;
  42 import java.nio.file.Paths;
  43 import java.nio.file.SimpleFileVisitor;
  44 import java.nio.file.attribute.BasicFileAttributeView;
  45 import java.nio.file.attribute.BasicFileAttributes;
  46 import java.nio.file.spi.FileSystemProvider;
  47 import java.util.ArrayList;
  48 import java.util.Arrays;


  52 import java.util.HashSet;
  53 import java.util.Iterator;
  54 import java.util.LinkedList;
  55 import java.util.List;
  56 import java.util.Map;
  57 import java.util.Random;
  58 import java.util.Set;
  59 import java.util.concurrent.TimeUnit;
  60 import java.util.zip.ZipEntry;
  61 import java.util.zip.ZipFile;
  62 
  63 import static java.nio.file.StandardOpenOption.*;
  64 import static java.nio.file.StandardCopyOption.*;
  65 
  66 /*
  67  * Tests various zipfs operations.
  68  *
  69  * @test
  70  * @bug 6990846 7009092 7009085 7015391 7014948 7005986 7017840 7007596
  71  *      7157656 8002390 7012868 7012856 8015728 8038500 8040059 8069211

  72  * @summary Test Zip filesystem provider
  73  * @run main ZipFSTester
  74  * @run main/othervm/java.security.policy=test.policy ZipFSTester
  75  */
  76 
  77 public class ZipFSTester {
  78 
  79     public static void main(String[] args) throws Exception {
  80 
  81         // create JAR file for test, actual contents don't matter
  82         Path jarFile = Utils.createJarFile("tester.jar",
  83                 "META-INF/MANIFEST.MF",
  84                 "dir1/foo",
  85                 "dir2/bar");
  86 
  87         try (FileSystem fs = newZipFileSystem(jarFile, Collections.emptyMap())) {
  88             test0(fs);
  89             test1(fs);
  90             test2(fs);   // more tests
  91         }
  92         testTime(jarFile);
  93         test8069211();

  94     }
  95 
  96     static void test0(FileSystem fs)
  97         throws Exception
  98     {
  99         List<String> list = new LinkedList<>();
 100         try (ZipFile zf = new ZipFile(fs.toString())) {
 101             Enumeration<? extends ZipEntry> zes = zf.entries();
 102             while (zes.hasMoreElements()) {
 103                 list.add(zes.nextElement().getName());
 104             }
 105             for (String pname : list) {
 106                 Path path = fs.getPath(pname);
 107                 if (!Files.exists(path))
 108                     throw new RuntimeException("path existence check failed!");
 109                 while ((path = path.getParent()) != null) {
 110                     if (!Files.exists(path))
 111                         throw new RuntimeException("parent existence check failed!");
 112                 }
 113             }


 424         env.put("create", "true");
 425         Path fsPath = getTempPath();
 426         try (FileSystem fs = newZipFileSystem(fsPath, env);) {
 427             OutputStream out = Files.newOutputStream(fs.getPath("/foo"));
 428             out.write("hello".getBytes());
 429             out.close();
 430             out.close();
 431         }
 432         try (FileSystem fs = newZipFileSystem(fsPath, new HashMap<String, Object>())) {
 433             if (!Arrays.equals(Files.readAllBytes(fs.getPath("/foo")),
 434                                "hello".getBytes())) {
 435                 throw new RuntimeException("entry close() failed");
 436             }
 437         } catch (Exception x) {
 438             throw new RuntimeException("entry close() failed", x);
 439         } finally {
 440             Files.delete(fsPath);
 441         }
 442     }
 443 




















 444     private static FileSystem newZipFileSystem(Path path, Map<String, ?> env)
 445         throws Exception
 446     {


 447         return FileSystems.newFileSystem(
 448             new URI("jar", path.toUri().toString(), null), env, null);

 449     }
 450 
 451     private static Path getTempPath() throws IOException
 452     {
 453         File tmp = File.createTempFile("testzipfs_", "zip");
 454         tmp.delete();    // we need a clean path, no file
 455         return tmp.toPath();
 456     }
 457 
 458     private static void list(Path path, List<String> files, List<String> dirs )
 459         throws IOException
 460     {
 461         if (Files.isDirectory(path)) {
 462             try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
 463                 for (Path child : ds)
 464                     list(child, files, dirs);
 465             }
 466             dirs.add(path.toString());
 467         } else {
 468             files.add(path.toString());




   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  */
  23 
  24 import java.io.File;
  25 import java.io.IOException;
  26 import java.io.InputStream;
  27 import java.io.OutputStream;
  28 import java.net.URI;
  29 import java.net.URLDecoder;
  30 import java.nio.ByteBuffer;
  31 import java.nio.channels.FileChannel;
  32 import java.nio.channels.SeekableByteChannel;
  33 import java.nio.file.DirectoryStream;
  34 import java.nio.file.FileAlreadyExistsException;
  35 import java.nio.file.FileSystem;
  36 import java.nio.file.FileSystemAlreadyExistsException;
  37 import java.nio.file.FileSystemException;
  38 import java.nio.file.FileSystems;
  39 import java.nio.file.FileVisitResult;
  40 import java.nio.file.Files;
  41 import java.nio.file.OpenOption;
  42 import java.nio.file.Path;
  43 import java.nio.file.Paths;
  44 import java.nio.file.SimpleFileVisitor;
  45 import java.nio.file.attribute.BasicFileAttributeView;
  46 import java.nio.file.attribute.BasicFileAttributes;
  47 import java.nio.file.spi.FileSystemProvider;
  48 import java.util.ArrayList;
  49 import java.util.Arrays;


  53 import java.util.HashSet;
  54 import java.util.Iterator;
  55 import java.util.LinkedList;
  56 import java.util.List;
  57 import java.util.Map;
  58 import java.util.Random;
  59 import java.util.Set;
  60 import java.util.concurrent.TimeUnit;
  61 import java.util.zip.ZipEntry;
  62 import java.util.zip.ZipFile;
  63 
  64 import static java.nio.file.StandardOpenOption.*;
  65 import static java.nio.file.StandardCopyOption.*;
  66 
  67 /*
  68  * Tests various zipfs operations.
  69  *
  70  * @test
  71  * @bug 6990846 7009092 7009085 7015391 7014948 7005986 7017840 7007596
  72  *      7157656 8002390 7012868 7012856 8015728 8038500 8040059 8069211
  73  *      8131067
  74  * @summary Test Zip filesystem provider
  75  * @run main ZipFSTester
  76  * @run main/othervm/java.security.policy=test.policy ZipFSTester
  77  */
  78 
  79 public class ZipFSTester {
  80 
  81     public static void main(String[] args) throws Exception {
  82 
  83         // create JAR file for test, actual contents don't matter
  84         Path jarFile = Utils.createJarFile("tester.jar",
  85                 "META-INF/MANIFEST.MF",
  86                 "dir1/foo",
  87                 "dir2/bar");
  88 
  89         try (FileSystem fs = newZipFileSystem(jarFile, Collections.emptyMap())) {
  90             test0(fs);
  91             test1(fs);
  92             test2(fs);   // more tests
  93         }
  94         testTime(jarFile);
  95         test8069211();
  96         test8131067();
  97     }
  98 
  99     static void test0(FileSystem fs)
 100         throws Exception
 101     {
 102         List<String> list = new LinkedList<>();
 103         try (ZipFile zf = new ZipFile(fs.toString())) {
 104             Enumeration<? extends ZipEntry> zes = zf.entries();
 105             while (zes.hasMoreElements()) {
 106                 list.add(zes.nextElement().getName());
 107             }
 108             for (String pname : list) {
 109                 Path path = fs.getPath(pname);
 110                 if (!Files.exists(path))
 111                     throw new RuntimeException("path existence check failed!");
 112                 while ((path = path.getParent()) != null) {
 113                     if (!Files.exists(path))
 114                         throw new RuntimeException("parent existence check failed!");
 115                 }
 116             }


 427         env.put("create", "true");
 428         Path fsPath = getTempPath();
 429         try (FileSystem fs = newZipFileSystem(fsPath, env);) {
 430             OutputStream out = Files.newOutputStream(fs.getPath("/foo"));
 431             out.write("hello".getBytes());
 432             out.close();
 433             out.close();
 434         }
 435         try (FileSystem fs = newZipFileSystem(fsPath, new HashMap<String, Object>())) {
 436             if (!Arrays.equals(Files.readAllBytes(fs.getPath("/foo")),
 437                                "hello".getBytes())) {
 438                 throw new RuntimeException("entry close() failed");
 439             }
 440         } catch (Exception x) {
 441             throw new RuntimeException("entry close() failed", x);
 442         } finally {
 443             Files.delete(fsPath);
 444         }
 445     }
 446 
 447     static void test8131067() throws Exception {
 448         Map<String, Object> env = new HashMap<String, Object>();
 449         env.put("create", "true");
 450 
 451         // file name with space character for URI to quote it
 452         File tmp = File.createTempFile("test zipfs", "zip");
 453         tmp.delete();    // we need a clean path, no file
 454         Path fsPath = tmp.toPath();
 455         try (FileSystem fs = newZipFileSystem(fsPath, env);) {
 456             Files.write(fs.getPath("/foo"), "hello".getBytes());
 457             URI fooUri = fs.getPath("/foo").toUri();
 458             if (!Arrays.equals(Files.readAllBytes(Paths.get(fooUri)),
 459                                "hello".getBytes())) {
 460                 throw new RuntimeException("entry close() failed");
 461             }
 462         } finally {
 463             Files.delete(fsPath);
 464         }
 465     }
 466 
 467     private static FileSystem newZipFileSystem(Path path, Map<String, ?> env)
 468         throws Exception
 469     {
 470         // Use URLDecoder (for test only) to remove the double escaped space
 471         // character
 472         return FileSystems.newFileSystem(
 473             new URI("jar", URLDecoder.decode(path.toUri().toString(), "utf8"),
 474                 null), env, null);
 475     }
 476 
 477     private static Path getTempPath() throws IOException
 478     {
 479         File tmp = File.createTempFile("testzipfs_", "zip");
 480         tmp.delete();    // we need a clean path, no file
 481         return tmp.toPath();
 482     }
 483 
 484     private static void list(Path path, List<String> files, List<String> dirs )
 485         throws IOException
 486     {
 487         if (Files.isDirectory(path)) {
 488             try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
 489                 for (Path child : ds)
 490                     list(child, files, dirs);
 491             }
 492             dirs.add(path.toString());
 493         } else {
 494             files.add(path.toString());