Home | History | Annotate | Download | only in file
      1 /*
      2  * Copyright (C) 2016 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.Before;
     20 import org.junit.Rule;
     21 import org.junit.Test;
     22 import org.mockito.Mock;
     23 import org.mockito.junit.MockitoJUnit;
     24 import org.mockito.junit.MockitoRule;
     25 
     26 import java.io.ByteArrayInputStream;
     27 import java.io.ByteArrayOutputStream;
     28 import java.io.IOException;
     29 import java.io.InputStream;
     30 import java.io.OutputStream;
     31 import java.nio.channels.FileChannel;
     32 import java.nio.file.CopyOption;
     33 import java.nio.file.DirectoryStream;
     34 import java.nio.file.FileAlreadyExistsException;
     35 import java.nio.file.FileSystem;
     36 import java.nio.file.Files;
     37 import java.nio.file.NoSuchFileException;
     38 import java.nio.file.NotDirectoryException;
     39 import java.nio.file.OpenOption;
     40 import java.nio.file.Path;
     41 import java.nio.file.attribute.FileAttribute;
     42 import java.nio.file.attribute.PosixFilePermission;
     43 import java.nio.file.attribute.PosixFilePermissions;
     44 import java.nio.file.spi.FileSystemProvider;
     45 import java.util.HashSet;
     46 import java.util.Set;
     47 import java.util.regex.PatternSyntaxException;
     48 
     49 import static java.nio.file.StandardOpenOption.APPEND;
     50 import static java.nio.file.StandardOpenOption.READ;
     51 import static org.junit.Assert.assertEquals;
     52 import static org.junit.Assert.assertFalse;
     53 import static org.junit.Assert.assertSame;
     54 import static org.junit.Assert.assertTrue;
     55 import static org.junit.Assert.fail;
     56 import static org.mockito.Mockito.mock;
     57 import static org.mockito.Mockito.verify;
     58 import static org.mockito.Mockito.when;
     59 
     60 public class FilesTest {
     61 
     62     @Rule
     63     public MockitoRule mockitoRule = MockitoJUnit.rule();
     64 
     65     @Rule
     66     public FilesSetup filesSetup = new FilesSetup();
     67 
     68     @Mock
     69     private Path mockPath;
     70     @Mock
     71     private Path mockPath2;
     72     @Mock
     73     private FileSystem mockFileSystem;
     74     @Mock
     75     private FileSystemProvider mockFileSystemProvider;
     76 
     77     @Before
     78     public void setUp() throws Exception {
     79         when(mockPath.getFileSystem()).thenReturn(mockFileSystem);
     80         when(mockPath2.getFileSystem()).thenReturn(mockFileSystem);
     81         when(mockFileSystem.provider()).thenReturn(mockFileSystemProvider);
     82     }
     83 
     84     @Test
     85     public void test_newInputStream() throws IOException {
     86         try (InputStream is = new ByteArrayInputStream(new byte[0])) {
     87 
     88             when(mockFileSystemProvider.newInputStream(mockPath, READ)).thenReturn(is);
     89 
     90             assertSame(is, Files.newInputStream(mockPath, READ));
     91 
     92             verify(mockFileSystemProvider).newInputStream(mockPath, READ);
     93         }
     94     }
     95 
     96     @Test
     97     public void test_newOutputStream() throws IOException {
     98         try (OutputStream os = new ByteArrayOutputStream()) {
     99 
    100             when(mockFileSystemProvider.newOutputStream(mockPath, APPEND)).thenReturn(os);
    101 
    102             assertSame(os, Files.newOutputStream(mockPath, APPEND));
    103 
    104             verify(mockFileSystemProvider).newOutputStream(mockPath, APPEND);
    105         }
    106     }
    107 
    108     @Test
    109     public void test_newByteChannel() throws IOException {
    110         try (FileChannel sfc = FileChannel.open(filesSetup.getDataFilePath())) {
    111             HashSet<OpenOption> openOptions = new HashSet<>();
    112             openOptions.add(READ);
    113 
    114             when(mockFileSystemProvider.newByteChannel(mockPath, openOptions)).thenReturn(sfc);
    115 
    116             assertSame(sfc, Files.newByteChannel(mockPath, READ));
    117 
    118             verify(mockFileSystemProvider).newByteChannel(mockPath, openOptions);
    119         }
    120     }
    121 
    122     @Test
    123     public void test_createFile() throws IOException {
    124         assertFalse(Files.exists(filesSetup.getTestPath()));
    125         Files.createFile(filesSetup.getTestPath());
    126         assertTrue(Files.exists(filesSetup.getTestPath()));
    127 
    128         // File with unicode name.
    129         Path unicodeFilePath = filesSetup.getPathInTestDir(" ");
    130         Files.createFile(unicodeFilePath);
    131         Files.exists(unicodeFilePath);
    132 
    133         // When file exists.
    134         try {
    135             Files.createFile(filesSetup.getDataFilePath());
    136             fail();
    137         } catch(FileAlreadyExistsException expected) {}
    138     }
    139 
    140     @Test
    141     public void test_createFile_NPE() throws IOException {
    142         try {
    143             Files.createFile(null);
    144             fail();
    145         } catch (NullPointerException expected) {}
    146     }
    147 
    148     @Test
    149     public void test_createFile$String$Attr() throws IOException {
    150         Set<PosixFilePermission> perm = PosixFilePermissions.fromString("rwx------");
    151         FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perm);
    152         Files.createFile(filesSetup.getTestPath(), attr);
    153         assertEquals(attr.value(), Files.getAttribute(filesSetup.getTestPath(), attr.name()));
    154 
    155         // Creating a new file and passing multiple attribute of the same name.
    156         perm = PosixFilePermissions.fromString("rw-------");
    157         FileAttribute<Set<PosixFilePermission>> attr1 = PosixFilePermissions.asFileAttribute(perm);
    158         Path filePath2 = filesSetup.getPathInTestDir("new_file");
    159         Files.createFile(filePath2, attr, attr1);
    160         // Value should be equal to the last attribute passed.
    161         assertEquals(attr1.value(), Files.getAttribute(filePath2, attr.name()));
    162 
    163         // When file exists.
    164         try {
    165             Files.createFile(filesSetup.getDataFilePath(), attr);
    166             fail();
    167         } catch(FileAlreadyExistsException expected) {}
    168     }
    169 
    170     @Test
    171     public void test_createDirectory_delegation() throws IOException {
    172         Set<PosixFilePermission> perm = PosixFilePermissions.fromString("rwx------");
    173         FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perm);
    174         assertEquals(mockPath, Files.createDirectory(mockPath, attr));
    175         verify(mockFileSystemProvider).createDirectory(mockPath, attr);
    176     }
    177 
    178     @Test
    179     public void test_createDirectories() throws IOException {
    180         // Should be able to create parent directories.
    181         Path dirPath = filesSetup.getPathInTestDir("dir1/dir2/dir3");
    182         assertFalse(Files.exists(dirPath));
    183         Files.createDirectories(dirPath);
    184         assertTrue(Files.isDirectory(dirPath));
    185 
    186         // Creating an existing directory. Should not throw any error.
    187         Files.createDirectories(dirPath);
    188     }
    189 
    190     @Test
    191     public void test_createDirectories_NPE() throws IOException {
    192         try {
    193             Files.createDirectories(null);
    194             fail();
    195         } catch (NullPointerException expected) {}
    196     }
    197 
    198     @Test
    199     public void test_createDirectories$Path$Attr() throws IOException {
    200         Path dirPath = filesSetup.getPathInTestDir("dir1/dir2/dir3");
    201         Set<PosixFilePermission> perm = PosixFilePermissions.fromString("rwx------");
    202         FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perm);
    203         assertFalse(Files.exists(dirPath));
    204         Files.createDirectories(dirPath, attr);
    205         assertEquals(attr.value(), Files.getAttribute(dirPath, attr.name()));
    206 
    207         // Creating an existing directory with new permissions.
    208         perm = PosixFilePermissions.fromString("rw-------");
    209         FileAttribute<Set<PosixFilePermission>> attr1 =  PosixFilePermissions.asFileAttribute(perm);
    210         Files.createDirectories(dirPath, attr);
    211 
    212         // Value should not change as the directory exists.
    213         assertEquals(attr.value(), Files.getAttribute(dirPath, attr.name()));
    214 
    215         // Creating a new directory and passing multiple attribute of the same name.
    216         Path dirPath2 = filesSetup.getPathInTestDir("dir1/dir2/dir4");
    217         Files.createDirectories(dirPath2, attr, attr1);
    218         // Value should be equal to the last attribute passed.
    219         assertEquals(attr1.value(), Files.getAttribute(dirPath2, attr.name()));
    220     }
    221 
    222     @Test
    223     public void test_createDirectories$Path$Attr_NPE() throws IOException {
    224         Path dirPath = filesSetup.getPathInTestDir("dir1/dir2/dir3");
    225         Set<PosixFilePermission> perm = PosixFilePermissions.fromString("rwx------");
    226         FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perm);
    227         try {
    228             Files.createDirectories(null, attr);
    229             fail();
    230         } catch(NullPointerException expected) {}
    231 
    232         try {
    233             Files.createDirectories(dirPath, (FileAttribute<?>[]) null);
    234             fail();
    235         } catch(NullPointerException expected) {}
    236     }
    237 
    238     @Test
    239     public void test_newDirectoryStream() throws IOException {
    240         // Directory setup.
    241         Path path_dir1 = filesSetup.getPathInTestDir("newDir1");
    242         Path path_dir2 = filesSetup.getPathInTestDir("newDir1/newDir2");
    243         Path path_dir3 = filesSetup.getPathInTestDir("newDir1/newDir3");
    244         Path path_file1 = filesSetup.getPathInTestDir("newDir1/newFile1");
    245         Path path_file2 = filesSetup.getPathInTestDir("newDir1/newFile2");
    246         Path path_file3 = filesSetup.getPathInTestDir("newDir1/newDir2/newFile3");
    247 
    248         Files.createDirectory(path_dir1);
    249         Files.createDirectory(path_dir2);
    250         Files.createDirectory(path_dir3);
    251         Files.createFile(path_file1);
    252         Files.createFile(path_file2);
    253         Files.createFile(path_file3);
    254 
    255         HashSet<Path> pathSet = new HashSet<>();
    256         HashSet<Path> expectedPathSet = new HashSet<>();
    257         expectedPathSet.add(path_dir2);
    258         expectedPathSet.add(path_dir3);
    259         expectedPathSet.add(path_file1);
    260         expectedPathSet.add(path_file2);
    261 
    262         try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path_dir1)) {
    263             directoryStream.forEach(k -> pathSet.add(k));
    264             assertEquals(expectedPathSet, pathSet);
    265         }
    266     }
    267 
    268     @Test
    269     public void test_newDirectoryStream_Exception() throws IOException {
    270 
    271         // Non existent directory.
    272         Path path_dir1 = filesSetup.getPathInTestDir("newDir1");
    273         try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path_dir1)) {
    274             fail();
    275         } catch (NoSuchFileException expected) {
    276         }
    277 
    278         // File instead of directory.
    279         Path path_file1 = filesSetup.getPathInTestDir("newFile1");
    280         Files.createFile(path_file1);
    281         try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path_file1)) {
    282             fail();
    283         } catch (NotDirectoryException expected) {
    284         }
    285 
    286         try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(null)) {
    287             fail();
    288         } catch (NullPointerException expected) {
    289         }
    290     }
    291 
    292     @Test
    293     public void test_newDirectoryStream$Path$String() throws IOException {
    294         // Directory setup.
    295         Path path_root = filesSetup.getPathInTestDir("dir");
    296         Path path_java1 = filesSetup.getPathInTestDir("dir/f1.java");
    297         Path path_java2 = filesSetup.getPathInTestDir("dir/f2.java");
    298         Path path_java3 = filesSetup.getPathInTestDir("dir/f3.java");
    299 
    300         Path path_txt1 = filesSetup.getPathInTestDir("dir/f1.txt");
    301         Path path_txt2 = filesSetup.getPathInTestDir("dir/f2.txt");
    302         Path path_txt3 = filesSetup.getPathInTestDir("dir/f3.txt");
    303 
    304         Files.createDirectory(path_root);
    305         // A directory with .java extension.
    306         Files.createDirectory(path_java1);
    307         Files.createFile(path_java2);
    308         Files.createFile(path_java3);
    309         Files.createFile(path_txt1);
    310         Files.createFile(path_txt2);
    311         Files.createFile(path_txt3);
    312 
    313         HashSet<Path> pathSet = new HashSet<>();
    314         HashSet<Path> expectedPathSet = new HashSet<>();
    315         expectedPathSet.add(path_java1);
    316         expectedPathSet.add(path_java2);
    317         expectedPathSet.add(path_java3);
    318 
    319         try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path_root, "*.java"))
    320         {
    321             directoryStream.forEach(k -> pathSet.add(k));
    322             assertEquals(expectedPathSet, pathSet);
    323         }
    324     }
    325 
    326     @Test
    327     public void test_newDirectoryStream$Path$String_Exception() throws IOException {
    328 
    329         // Non existent directory.
    330         Path path_dir1 = filesSetup.getPathInTestDir("newDir1");
    331         try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path_dir1, "*.c")) {
    332             fail();
    333         } catch (NoSuchFileException expected) {
    334         }
    335 
    336         // File instead of directory.
    337         Path path_file1 = filesSetup.getPathInTestDir("newFile1");
    338         Files.createFile(path_file1);
    339         try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path_file1, "*.c")) {
    340             fail();
    341         } catch (NotDirectoryException expected) {
    342         }
    343 
    344         Files.createFile(path_dir1);
    345         try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path_file1, "[a")) {
    346             fail();
    347         } catch (PatternSyntaxException expected) {
    348         }
    349 
    350         try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(null, "[a")) {
    351             fail();
    352         } catch (NullPointerException expected) {
    353         }
    354 
    355         try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path_dir1,
    356                 (String)null)) {
    357             fail();
    358         } catch (NullPointerException expected) {
    359         }
    360     }
    361 
    362     @Test
    363     public void test_createSymbolicLink() throws IOException {
    364         FileAttribute mockFileAttribute = mock(FileAttribute.class);
    365         assertEquals(mockPath, Files.createSymbolicLink(mockPath, mockPath2, mockFileAttribute));
    366         verify(mockFileSystemProvider).createSymbolicLink(mockPath, mockPath2, mockFileAttribute);
    367     }
    368 
    369     @Test
    370     public void test_delete() throws IOException {
    371         Files.delete(mockPath);
    372         verify(mockFileSystemProvider).delete(mockPath);
    373     }
    374 
    375     @Test
    376     public void test_deleteIfExist() throws IOException {
    377         when(mockFileSystemProvider.deleteIfExists(mockPath)).thenReturn(true);
    378         assertTrue(Files.deleteIfExists(mockPath));
    379         verify(mockFileSystemProvider).deleteIfExists(mockPath);
    380     }
    381 
    382     @Test
    383     public void test_copy() throws IOException {
    384         CopyOption copyOption = mock(CopyOption.class);
    385         Files.copy(mockPath, mockPath2, copyOption);
    386         verify(mockFileSystemProvider).copy(mockPath, mockPath2, copyOption);
    387     }
    388 }