1 /*
   2  * Copyright (c) 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 import org.testng.annotations.AfterClass;
  25 import org.testng.annotations.BeforeClass;
  26 import org.testng.annotations.DataProvider;
  27 import org.testng.annotations.Test;
  28 
  29 import java.io.IOException;
  30 import java.net.URI;
  31 import java.nio.file.*;
  32 import java.nio.file.spi.FileSystemProvider;
  33 import java.util.Iterator;
  34 import java.util.Map;
  35 import java.util.NoSuchElementException;
  36 import java.util.regex.PatternSyntaxException;
  37 import java.util.zip.ZipException;
  38 
  39 import static org.testng.Assert.*;
  40 
  41 /**
  42  * @test
  43  * @bug 8218875
  44  * @summary ZIP File System tests that leverage Files.newFileSystem
  45  * @modules jdk.zipfs
  46  * @compile NewFileSystemTests.java
  47  * @run testng NewFileSystemTests
  48  * @run testng/othervm/java.security.policy=test.policy  NewFileSystemTests
  49 
  50  */
  51 public class NewFileSystemTests {
  52 
  53     // The Zip file system scheme
  54     public static final String ZIPFS_SCHEME = "jar";
  55     // Map to used for creating a ZIP archive
  56     private static final Map<String, String> ZIPFS_OPTIONS = Map.of("create", "true");
  57     // Primary jar file used for testing
  58     private static Path jarFile;
  59     // URI for jar file used for testing
  60     private static URI jarURI;
  61 
  62     /**
  63      * Create the JAR file used by the tests
  64      */
  65     @BeforeClass
  66     public void setUp() throws Exception {
  67         jarFile = Utils.createJarFile("basic.jar",
  68                 "README");
  69         jarURI = new URI(ZIPFS_SCHEME, jarFile.toUri().toString(), null);
  70 
  71     }
  72 
  73     /**
  74      * Remove JAR file used by test as part of clean-up
  75      */
  76     @AfterClass
  77     public void tearDown() throws Exception {
  78         Files.deleteIfExists(jarFile);
  79     }
  80 
  81     /**
  82      * Validate that {@code FileSystems.newFileSystem(Path, Map<String, ?>)}
  83      * will return a Zip file system
  84      *
  85      * @throws IOException
  86      */
  87     @Test
  88     public void test0000() throws IOException {
  89 
  90         try (FileSystem zipfs = FileSystems.newFileSystem(Path.of("basic.jar"),
  91                 ZIPFS_OPTIONS)) {
  92             checkFileSystem(zipfs);
  93         }
  94     }
  95 
  96     /**
  97      * Validate that {@code FileSystems.newFileSystem(Path, ClassLoader)}
  98      * will return a Zip file system
  99      *
 100      * @throws IOException
 101      */
 102     @Test(dataProvider = "classLoaders")
 103     public void test0001(ClassLoader cl) throws Exception {
 104 
 105         try (FileSystem zipfs = FileSystems.newFileSystem(Path.of("basic.jar"),
 106                 cl)) {
 107             checkFileSystem(zipfs);
 108         }
 109     }
 110 
 111     /**
 112      * Validate that {@code FileSystems.newFileSystem(Path, Map<String, ?>, ClassLoader)}
 113      * will return a Zip file system
 114      *
 115      * @throws IOException
 116      */
 117     @Test(dataProvider = "classLoaders")
 118     public void test0002(ClassLoader cl) throws Exception {
 119         try (FileSystem zipfs = FileSystems.newFileSystem(Path.of("basic.jar"),
 120                 ZIPFS_OPTIONS, cl)) {
 121             checkFileSystem(zipfs);
 122         }
 123     }
 124 
 125     /**
 126      * Validate that {@code FileSystems.newFileSystem(URI, Map<String, ?>)}
 127      * will return a Zip file system
 128      *
 129      * @throws IOException
 130      */
 131     @Test
 132     public void test0003() throws Exception {
 133         try (FileSystem zipfs = FileSystems.newFileSystem(jarURI, ZIPFS_OPTIONS)) {
 134             checkFileSystem(zipfs);
 135         }
 136     }
 137 
 138     /**
 139      * Validate that {@code FileSystems.newFileSystem(URI, Map<String, ?>, ClassLoader)}
 140      * will return a Zip file system
 141      *
 142      * @throws IOException
 143      */
 144     @Test(dataProvider = "classLoaders")
 145     public void test0004(ClassLoader cl) throws Exception {
 146         try (FileSystem zipfs = FileSystems.newFileSystem(jarURI, ZIPFS_OPTIONS,
 147                 cl)) {
 148             checkFileSystem(zipfs);
 149         }
 150     }
 151 
 152     /*
 153      * DataProvider used to verify that a Zip file system may be returned
 154      * when specifying a class loader
 155      */
 156     @DataProvider(name = "classLoaders")
 157     private Object[][] classLoaders() {
 158         return new Object[][]{
 159                 {null},
 160                 {ClassLoader.getSystemClassLoader()}
 161         };
 162     }
 163 
 164     /**
 165      * Validate that the given FileSystem is a Zip file system.
 166      *
 167      * @param fs File System to validate
 168      */
 169     private void checkFileSystem(FileSystem fs) {
 170 
 171         assertNotNull(fs, "Error: FileSystem was not returned");
 172         assertTrue(fs.provider().getScheme().equalsIgnoreCase(ZIPFS_SCHEME));
 173         assertTrue(fs.isOpen());
 174         assertEquals(fs.getSeparator(), "/");
 175 
 176         // one root
 177         Iterator<Path> roots = fs.getRootDirectories().iterator();
 178         assertTrue(roots.next().toString().equals("/"));
 179         assertFalse(roots.hasNext());
 180     }
 181 }