1 /*
   2  * Copyright (c) 2012, 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  */
  23 
  24 /* @test
  25    @bug 7156873 8028480 8034773
  26    @summary ZipFileSystem regression tests
  27  */
  28 
  29 import java.io.OutputStream;
  30 import java.net.URI;
  31 import java.nio.ByteBuffer;
  32 import java.nio.channels.*;
  33 import java.nio.file.*;
  34 import java.nio.file.spi.*;
  35 import java.util.*;
  36 
  37 public class ZFSTests {
  38 
  39     public static void main(String[] args) throws Throwable {
  40         test7156873();
  41         testOpenOptions();
  42     }
  43 
  44     static void test7156873() throws Throwable {
  45         String DIRWITHSPACE = "testdir with spaces";
  46         Path dir = Paths.get(DIRWITHSPACE);
  47         Path path = Paths.get(DIRWITHSPACE, "file.zip");
  48         try {
  49             Files.createDirectory(dir);
  50             URI uri = URI.create("jar:" + path.toUri());
  51             Map<String, Object> env = new HashMap<String, Object>();
  52             env.put("create", "true");
  53             try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {}
  54         } finally {
  55             Files.deleteIfExists(path);
  56             Files.deleteIfExists(dir);
  57         }
  58     }
  59 
  60     static void testOpenOptions() throws Throwable {
  61         Path path = Paths.get("file.zip");
  62         try {
  63             URI uri = URI.create("jar:" + path.toUri());
  64             Map<String, Object> env = new HashMap<String, Object>();
  65             env.put("create", "true");
  66             try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {
  67                 FileSystemProvider fsp = fs.provider();
  68                 Set<? extends OpenOption> options;
  69                 Path p = fs.getPath("test.txt");
  70                 // 8028480
  71                 options = EnumSet.of(StandardOpenOption.CREATE,
  72                                      StandardOpenOption.WRITE,
  73                                      StandardOpenOption.APPEND);
  74                 try (FileChannel ch = fsp.newFileChannel(p, options)) {
  75                     ch.write(ByteBuffer.wrap("Hello!".getBytes("ASCII")));
  76                 }
  77                 // 8034773
  78                 try (OutputStream os = fsp.newOutputStream(p, new OpenOption[0])) {
  79                     os.write("Hello2!".getBytes("ASCII"));
  80                 }
  81                 if (!"Hello2!".equals(new String(
  82                         Files.readAllBytes(fs.getPath("test.txt"))))) {
  83                     throw new RuntimeException("failed to open as truncate_existing");
  84                 }
  85 
  86                 options = EnumSet.of(StandardOpenOption.CREATE,
  87                                      StandardOpenOption.APPEND,
  88                                      StandardOpenOption.TRUNCATE_EXISTING);
  89                 try (FileChannel ch = fsp.newFileChannel(p, options)) {
  90                     throw new RuntimeException("expected IAE not thrown!");
  91                 } catch (IllegalArgumentException x) {
  92                     // expected x.printStackTrace();
  93                 }
  94             }
  95         } finally {
  96             Files.deleteIfExists(path);
  97         }
  98     }
  99 }