• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertSame;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 
25 import dalvik.system.PathClassLoader;
26 
27 import libcore.io.Streams;
28 
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.io.File;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38 import java.net.URI;
39 import java.nio.file.FileSystem;
40 import java.nio.file.FileSystemAlreadyExistsException;
41 import java.nio.file.FileSystems;
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44 import java.nio.file.ProviderNotFoundException;
45 import java.util.HashMap;
46 import java.util.Map;
47 
48 import junitparams.JUnitParamsRunner;
49 
50 @RunWith(JUnitParamsRunner.class)
51 public class FileSystemsTest {
52 
53     @Rule
54     public FilesSetup filesSetup = new FilesSetup();
55 
56     @Test
test_getDefault()57     public void test_getDefault() {
58         FileSystem fs = FileSystems.getDefault();
59         assertNotNull(fs.provider());
60     }
61 
62     @Test
test_getFileSystem()63     public void test_getFileSystem() {
64         Path testPath = Paths.get("/");
65         FileSystem fs = FileSystems.getFileSystem(testPath.toUri());
66         assertNotNull(fs.provider());
67 
68         try {
69             FileSystems.getFileSystem(null);
70             fail();
71         } catch (NullPointerException expected) {}
72     }
73 
74     @Test
test_newFileSystem$URI$Map()75     public void test_newFileSystem$URI$Map() throws IOException {
76         Path testPath = Paths.get("/");
77         Map<String, String> stubEnv = new HashMap<>();
78         try {
79             FileSystems.newFileSystem(testPath.toUri(), stubEnv);
80             fail();
81         } catch (FileSystemAlreadyExistsException expected) {}
82 
83         try {
84             FileSystems.newFileSystem(null, stubEnv);
85             fail();
86         } catch (NullPointerException expected) {}
87 
88         try {
89             FileSystems.newFileSystem(testPath, null);
90             fail();
91         } catch (ProviderNotFoundException expected) {}
92     }
93 
94     @Test
test_newFileSystem$URI$Map$ClassLoader()95     public void test_newFileSystem$URI$Map$ClassLoader() throws Exception {
96         Path testPath = Paths.get("/");
97         Map<String, String> stubEnv = new HashMap<>();
98         try {
99             FileSystems.newFileSystem(testPath.toUri(), stubEnv, getClass().getClassLoader());
100             fail();
101         } catch (FileSystemAlreadyExistsException expected) {}
102 
103         try {
104             FileSystems.newFileSystem(null, stubEnv,
105                     Thread.currentThread().getContextClassLoader());
106             fail();
107         } catch (NullPointerException expected) {}
108 
109         try {
110             FileSystems.newFileSystem(testPath.toUri(), null,
111                     Thread.currentThread().getContextClassLoader());
112             fail();
113         } catch (FileSystemAlreadyExistsException expected) {}
114 
115         try {
116             FileSystems.newFileSystem(testPath.toUri(), stubEnv, null);
117             fail();
118         } catch (FileSystemAlreadyExistsException expected) {}
119     }
120 
121     @Test
test_newFileSystem$URI$Map$ClassLoader_customClassLoader()122     public void test_newFileSystem$URI$Map$ClassLoader_customClassLoader() throws Exception {
123         Map<String, String> stubEnv = new HashMap<>();
124         // Verify that the Thread's classloader cannot load mypackage.MockFileSystem.
125         try {
126             Thread.currentThread().getContextClassLoader().loadClass("mypackage.MockFileSystem");
127             fail();
128         } catch (ClassNotFoundException expected) {}
129 
130         ClassLoader fileSystemsClassLoader = createClassLoaderForTestFileSystems();
131 
132         // The file system configured in filesystemstest.jar is for scheme "stubScheme://
133         URI stubURI = new URI("stubScheme://sometext");
134         FileSystem fs = FileSystems.newFileSystem(stubURI, stubEnv, fileSystemsClassLoader);
135         assertEquals("mypackage.MockFileSystem", fs.getClass().getName());
136         assertSame(stubURI, fs.getClass().getDeclaredMethod("getURI").invoke(fs));
137         assertSame(stubEnv, fs.getClass().getDeclaredMethod("getEnv").invoke(fs));
138     }
139 
140     @Test
test_newFileSystem$Path$ClassLoader()141     public void test_newFileSystem$Path$ClassLoader() throws Exception {
142         Path testPath = Paths.get("/");
143         try {
144             FileSystems.newFileSystem(testPath, Thread.currentThread().getContextClassLoader());
145             fail();
146         } catch (ProviderNotFoundException expected) {}
147 
148         try {
149             FileSystems.newFileSystem(null, Thread.currentThread().getContextClassLoader());
150             fail();
151         } catch (NullPointerException expected) {}
152 
153         try {
154             FileSystems.newFileSystem(testPath, null);
155             fail();
156         } catch (ProviderNotFoundException expected) {}
157     }
158 
159     @Test
test_newFileSystem$Path$ClassLoader_customClassLoader()160     public void test_newFileSystem$Path$ClassLoader_customClassLoader() throws Exception  {
161         // Verify that the Thread's classloader cannot load mypackage.MockFileSystem.
162         try {
163             Thread.currentThread().getContextClassLoader().loadClass(
164                     "mypackage.MockFileSystem");
165             fail();
166         } catch (ClassNotFoundException expected) {}
167 
168         ClassLoader fileSystemsClassLoader = createClassLoaderForTestFileSystems();
169         FileSystem fs = FileSystems.newFileSystem(filesSetup.getDataFilePath(),
170                 fileSystemsClassLoader);
171 
172         assertEquals("mypackage.MockFileSystem", fs.getClass().getName());
173 
174         Path pathValue = (Path)fs.getClass().getDeclaredMethod("getPath").invoke(fs);
175         assertEquals(filesSetup.getDataFilePath(), pathValue);
176     }
177 
178     /**
179      * The method creates a custom classloader for the mock FileSystem and FileSystemProvider
180      * classes. The custom classloader is created by providing filesystemtest.jar which contains
181      * MockFileSystemProvider and MockFileSystem classes.
182      * @throws Exception
183      */
createClassLoaderForTestFileSystems()184     ClassLoader createClassLoaderForTestFileSystems() throws IOException {
185         File jarFile = new File(filesSetup.getTestDir(), "filesystemstest.jar");
186         try (InputStream in = getClass().getResource("/filesystemstest.jar").openStream();
187              OutputStream out = new FileOutputStream(jarFile))
188         {
189             assertTrue(jarFile.setReadOnly());
190             Streams.copy(in, out);
191         }
192 
193         return new PathClassLoader(jarFile.getAbsolutePath(), getClass().getClassLoader());
194     }
195 }
196