Home | History | Annotate | Download | only in file
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 
     17 package libcore.java.nio.file;
     18 
     19 import org.junit.Rule;
     20 import org.junit.Test;
     21 import org.junit.runner.RunWith;
     22 
     23 import java.io.File;
     24 import java.io.FileOutputStream;
     25 import java.io.IOException;
     26 import java.io.InputStream;
     27 import java.io.OutputStream;
     28 import java.net.URI;
     29 import java.nio.file.FileSystem;
     30 import java.nio.file.FileSystemAlreadyExistsException;
     31 import java.nio.file.FileSystems;
     32 import java.nio.file.Path;
     33 import java.nio.file.Paths;
     34 import java.nio.file.ProviderNotFoundException;
     35 import java.util.HashMap;
     36 import java.util.Map;
     37 
     38 import dalvik.system.PathClassLoader;
     39 import junitparams.JUnitParamsRunner;
     40 import sun.misc.IOUtils;
     41 
     42 import static org.junit.Assert.assertEquals;
     43 import static org.junit.Assert.assertNotNull;
     44 import static org.junit.Assert.assertSame;
     45 import static org.junit.Assert.fail;
     46 
     47 @RunWith(JUnitParamsRunner.class)
     48 public class FileSystemsTest {
     49 
     50     @Rule
     51     public FilesSetup filesSetup = new FilesSetup();
     52 
     53     @Test
     54     public void test_getDefault() {
     55         FileSystem fs = FileSystems.getDefault();
     56         assertNotNull(fs.provider());
     57     }
     58 
     59     @Test
     60     public void test_getFileSystem() {
     61         Path testPath = Paths.get("/");
     62         FileSystem fs = FileSystems.getFileSystem(testPath.toUri());
     63         assertNotNull(fs.provider());
     64 
     65         try {
     66             FileSystems.getFileSystem(null);
     67             fail();
     68         } catch (NullPointerException expected) {}
     69     }
     70 
     71     @Test
     72     public void test_newFileSystem$URI$Map() throws IOException {
     73         Path testPath = Paths.get("/");
     74         Map<String, String> stubEnv = new HashMap<>();
     75         try {
     76             FileSystems.newFileSystem(testPath.toUri(), stubEnv);
     77             fail();
     78         } catch (FileSystemAlreadyExistsException expected) {}
     79 
     80         try {
     81             FileSystems.newFileSystem(null, stubEnv);
     82             fail();
     83         } catch (NullPointerException expected) {}
     84 
     85         try {
     86             FileSystems.newFileSystem(testPath, null);
     87             fail();
     88         } catch (ProviderNotFoundException expected) {}
     89     }
     90 
     91     @Test
     92     public void test_newFileSystem$URI$Map$ClassLoader() throws Exception {
     93         Path testPath = Paths.get("/");
     94         Map<String, String> stubEnv = new HashMap<>();
     95         try {
     96             FileSystems.newFileSystem(testPath.toUri(), stubEnv, getClass().getClassLoader());
     97             fail();
     98         } catch (FileSystemAlreadyExistsException expected) {}
     99 
    100         try {
    101             FileSystems.newFileSystem(null, stubEnv,
    102                     Thread.currentThread().getContextClassLoader());
    103             fail();
    104         } catch (NullPointerException expected) {}
    105 
    106         try {
    107             FileSystems.newFileSystem(testPath.toUri(), null,
    108                     Thread.currentThread().getContextClassLoader());
    109             fail();
    110         } catch (FileSystemAlreadyExistsException expected) {}
    111 
    112         try {
    113             FileSystems.newFileSystem(testPath.toUri(), stubEnv, null);
    114             fail();
    115         } catch (FileSystemAlreadyExistsException expected) {}
    116     }
    117 
    118     @Test
    119     public void test_newFileSystem$URI$Map$ClassLoader_customClassLoader() throws Exception {
    120         Map<String, String> stubEnv = new HashMap<>();
    121         // Verify that the Thread's classloader cannot load mypackage.MockFileSystem.
    122         try {
    123             Thread.currentThread().getContextClassLoader().loadClass("mypackage.MockFileSystem");
    124             fail();
    125         } catch (ClassNotFoundException expected) {}
    126 
    127         ClassLoader fileSystemsClassLoader = createClassLoaderForTestFileSystems();
    128 
    129         // The file system configured in filesystemstest.jar is for scheme "stubScheme://
    130         URI stubURI = new URI("stubScheme://sometext");
    131         FileSystem fs = FileSystems.newFileSystem(stubURI, stubEnv, fileSystemsClassLoader);
    132         assertEquals("mypackage.MockFileSystem", fs.getClass().getName());
    133         assertSame(stubURI, fs.getClass().getDeclaredMethod("getURI").invoke(fs));
    134         assertSame(stubEnv, fs.getClass().getDeclaredMethod("getEnv").invoke(fs));
    135     }
    136 
    137     @Test
    138     public void test_newFileSystem$Path$ClassLoader() throws Exception {
    139         Path testPath = Paths.get("/");
    140         try {
    141             FileSystems.newFileSystem(testPath, Thread.currentThread().getContextClassLoader());
    142             fail();
    143         } catch (ProviderNotFoundException expected) {}
    144 
    145         try {
    146             FileSystems.newFileSystem(null, Thread.currentThread().getContextClassLoader());
    147             fail();
    148         } catch (NullPointerException expected) {}
    149 
    150         try {
    151             FileSystems.newFileSystem(testPath, null);
    152             fail();
    153         } catch (ProviderNotFoundException expected) {}
    154     }
    155 
    156     @Test
    157     public void test_newFileSystem$Path$ClassLoader_customClassLoader() throws Exception  {
    158         // Verify that the Thread's classloader cannot load mypackage.MockFileSystem.
    159         try {
    160             Thread.currentThread().getContextClassLoader().loadClass(
    161                     "mypackage.MockFileSystem");
    162             fail();
    163         } catch (ClassNotFoundException expected) {}
    164 
    165         ClassLoader fileSystemsClassLoader = createClassLoaderForTestFileSystems();
    166         FileSystem fs = FileSystems.newFileSystem(filesSetup.getDataFilePath(),
    167                 fileSystemsClassLoader);
    168 
    169         assertEquals("mypackage.MockFileSystem", fs.getClass().getName());
    170 
    171         Path pathValue = (Path)fs.getClass().getDeclaredMethod("getPath").invoke(fs);
    172         assertEquals(filesSetup.getDataFilePath(), pathValue);
    173     }
    174 
    175     /**
    176      * The method creates a custom classloader for the mock FileSystem and FileSystemProvider
    177      * classes. The custom classloader is created by providing filesystemtest.jar which contains
    178      * MockFileSystemProvider and MockFileSystem classes.
    179      * @throws Exception
    180      */
    181     ClassLoader createClassLoaderForTestFileSystems() throws Exception {
    182         File jarFile = new File(filesSetup.getTestDir().toString(), "filesystemstset.jar");
    183         InputStream jis = getClass().getResource("/filesystemstest.jar").openStream();
    184         OutputStream jos = new FileOutputStream(jarFile);
    185         jos.write(IOUtils.readFully(jis, -1, true));
    186 
    187         return new PathClassLoader(jarFile.getAbsolutePath(), getClass().getClassLoader());
    188     }
    189 }
    190