Search in sources :

Example 96 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project jimfs by google.

the class JimfsUnixLikeFileSystemTest method testWriteFile_withStandardOptions.

@Test
public void testWriteFile_withStandardOptions() throws IOException {
    Path test = path("/test");
    byte[] bytes = { 0, 1, 2, 3 };
    try {
        // CREATE and CREATE_NEW not specified
        Files.write(test, bytes, WRITE);
        fail();
    } catch (NoSuchFileException expected) {
        assertEquals(test.toString(), expected.getMessage());
    }
    // succeeds, file does not exist
    Files.write(test, bytes, CREATE_NEW);
    assertThatPath("/test").containsBytes(bytes);
    try {
        // CREATE_NEW requires file not exist
        Files.write(test, bytes, CREATE_NEW);
        fail();
    } catch (FileAlreadyExistsException expected) {
        assertEquals(test.toString(), expected.getMessage());
    }
    // succeeds, ok for file to already exist
    Files.write(test, new byte[] { 4, 5 }, CREATE);
    // did not truncate or append, so overwrote
    assertThatPath("/test").containsBytes(4, 5, 2, 3);
    // default options
    Files.write(test, bytes, WRITE, CREATE, TRUNCATE_EXISTING);
    assertThatPath("/test").containsBytes(bytes);
    Files.write(test, bytes, WRITE, APPEND);
    assertThatPath("/test").containsBytes(0, 1, 2, 3, 0, 1, 2, 3);
    Files.write(test, bytes, WRITE, CREATE, TRUNCATE_EXISTING, APPEND, SPARSE, DSYNC, SYNC);
    assertThatPath("/test").containsBytes(bytes);
    try {
        // READ not allowed
        Files.write(test, bytes, READ, WRITE);
        fail();
    } catch (UnsupportedOperationException expected) {
    }
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) NoSuchFileException(java.nio.file.NoSuchFileException) Test(org.junit.Test)

Example 97 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project j2objc by google.

the class Files2Test method test_newByteChannel_openOption_WRITE.

@Test
public void test_newByteChannel_openOption_WRITE() throws IOException {
    // When file doesn't exist
    try (SeekableByteChannel sbc = Files.newByteChannel(filesSetup.getTestPath(), WRITE)) {
        fail();
    } catch (NoSuchFileException expected) {
    }
    try (SeekableByteChannel sbc = Files.newByteChannel(filesSetup.getDataFilePath(), WRITE)) {
        sbc.read(ByteBuffer.allocate(10));
        fail();
    } catch (NonReadableChannelException expected) {
    }
    // Write in file.
    try (SeekableByteChannel sbc = Files.newByteChannel(filesSetup.getDataFilePath(), WRITE)) {
        sbc.write(ByteBuffer.wrap(TEST_FILE_DATA_2.getBytes()));
        sbc.close();
        try (InputStream is = Files.newInputStream(filesSetup.getDataFilePath())) {
            String expectedFileData = TEST_FILE_DATA_2 + TEST_FILE_DATA.substring(TEST_FILE_DATA_2.length());
            assertEquals(expectedFileData, readFromInputStream(is));
        }
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) NonReadableChannelException(java.nio.channels.NonReadableChannelException) FilesSetup.readFromInputStream(libcore.java.nio.file.FilesSetup.readFromInputStream) InputStream(java.io.InputStream) NoSuchFileException(java.nio.file.NoSuchFileException) Test(org.junit.Test)

Example 98 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project j2objc by google.

the class Files2Test method test_walk$Path$FileVisitOption.

@Test
public void test_walk$Path$FileVisitOption() throws IOException {
    // Directory structure.
    // root
    // ├── dir1
    // │   ├── dir2
    // │   │   ├── dir3
    // │   │   └── file5
    // │   ├── dir4
    // │   └── file3
    // ├── dir5
    // └── file1
    // 
    // depth will be 2. file4, file5, dir3 is not reachable.
    Path rootDir = Paths.get(filesSetup.getTestDir(), "root");
    Path dir1 = Paths.get(filesSetup.getTestDir(), "root/dir1");
    Path dir2 = Paths.get(filesSetup.getTestDir(), "root/dir1/dir2");
    Path dir3 = Paths.get(filesSetup.getTestDir(), "root/dir1/dir2/dir3");
    Path dir4 = Paths.get(filesSetup.getTestDir(), "root/dir1/dir4");
    Path dir5 = Paths.get(filesSetup.getTestDir(), "root/dir5");
    Path file1 = Paths.get(filesSetup.getTestDir(), "root/file1");
    Path file3 = Paths.get(filesSetup.getTestDir(), "root/dir1/file3");
    Path file5 = Paths.get(filesSetup.getTestDir(), "root/dir1/dir2/file5");
    Files.createDirectories(dir3);
    Files.createDirectories(dir4);
    Files.createDirectories(dir5);
    Files.createFile(file1);
    Files.createFile(file3);
    Files.createFile(file5);
    Set<Path> expectedDirSet = new HashSet<>();
    expectedDirSet.add(rootDir);
    expectedDirSet.add(dir1);
    expectedDirSet.add(dir2);
    expectedDirSet.add(dir4);
    expectedDirSet.add(file3);
    expectedDirSet.add(dir5);
    expectedDirSet.add(file1);
    Set<Path> dirSet = new HashSet<>();
    try (Stream<Path> pathStream = Files.walk(rootDir, 2, FileVisitOption.FOLLOW_LINKS)) {
        pathStream.forEach(path -> dirSet.add(path));
    }
    assertEquals(expectedDirSet, dirSet);
    // Test case when Path doesn't exist.
    try (Stream<Path> pathStream = Files.walk(filesSetup.getTestPath(), 2, FileVisitOption.FOLLOW_LINKS)) {
        fail();
    } catch (NoSuchFileException expected) {
    }
    // Test case when Path is a not a directory.
    expectedDirSet.clear();
    dirSet.clear();
    expectedDirSet.add(filesSetup.getDataFilePath());
    try (Stream<Path> pathStream = Files.walk(filesSetup.getDataFilePath(), 2, FileVisitOption.FOLLOW_LINKS)) {
        pathStream.forEach(path -> dirSet.add(path));
    }
    assertEquals(expectedDirSet, dirSet);
    // Test case when Path doesn't exist.
    try (Stream<Path> pathStream = Files.walk(rootDir, -1, FileVisitOption.FOLLOW_LINKS)) {
        fail();
    } catch (IllegalArgumentException expected) {
    }
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 99 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project j2objc by google.

the class Files2Test method test_newBufferedReader.

@Test
public void test_newBufferedReader() throws IOException {
    // Test the case where file doesn't exists.
    try {
        Files.newBufferedReader(filesSetup.getTestPath());
        fail();
    } catch (NoSuchFileException expected) {
    }
    BufferedReader bufferedReader = Files.newBufferedReader(filesSetup.getDataFilePath());
    assertEquals(TEST_FILE_DATA, bufferedReader.readLine());
    // Test the case where the file content has unicode characters.
    writeToFile(filesSetup.getDataFilePath(), UTF_16_DATA);
    bufferedReader = Files.newBufferedReader(filesSetup.getDataFilePath());
    assertEquals(UTF_16_DATA, bufferedReader.readLine());
    bufferedReader.close();
    // Test the case where file is write-only.
    Set<PosixFilePermission> perm = PosixFilePermissions.fromString("-w-------");
    Files.setPosixFilePermissions(filesSetup.getDataFilePath(), perm);
    try {
        Files.newBufferedReader(filesSetup.getDataFilePath());
        fail();
    } catch (AccessDeniedException expected) {
    }
}
Also used : AccessDeniedException(java.nio.file.AccessDeniedException) NoSuchFileException(java.nio.file.NoSuchFileException) BufferedReader(java.io.BufferedReader) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Test(org.junit.Test)

Example 100 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project j2objc by google.

the class Files2Test method test_walk.

@Test
public void test_walk() throws IOException {
    // Directory structure.
    // root
    // ├── dir1
    // │   ├── dir2
    // │   │   ├── dir3
    // │   │   └── file5
    // │   ├── dir4
    // │   └── file3
    // ├── dir5
    // └── file1
    // 
    Path rootDir = Paths.get(filesSetup.getTestDir(), "root");
    Path dir1 = Paths.get(filesSetup.getTestDir(), "root/dir1");
    Path dir2 = Paths.get(filesSetup.getTestDir(), "root/dir1/dir2");
    Path dir3 = Paths.get(filesSetup.getTestDir(), "root/dir1/dir2/dir3");
    Path dir4 = Paths.get(filesSetup.getTestDir(), "root/dir1/dir4");
    Path dir5 = Paths.get(filesSetup.getTestDir(), "root/dir5");
    Path file1 = Paths.get(filesSetup.getTestDir(), "root/file1");
    Path file3 = Paths.get(filesSetup.getTestDir(), "root/dir1/file3");
    Path file5 = Paths.get(filesSetup.getTestDir(), "root/dir1/dir2/file5");
    Files.createDirectories(dir3);
    Files.createDirectories(dir4);
    Files.createDirectories(dir5);
    Files.createFile(file1);
    Files.createFile(file3);
    Files.createFile(file5);
    Set<Path> expectedDirSet = new HashSet<>();
    expectedDirSet.add(rootDir.getFileName());
    expectedDirSet.add(dir1.getFileName());
    expectedDirSet.add(dir2.getFileName());
    expectedDirSet.add(dir4.getFileName());
    expectedDirSet.add(file3.getFileName());
    expectedDirSet.add(dir5.getFileName());
    expectedDirSet.add(file1.getFileName());
    expectedDirSet.add(file5.getFileName());
    expectedDirSet.add(dir3.getFileName());
    Set<Path> dirSet = new HashSet<>();
    try (Stream<Path> pathStream = Files.walk(rootDir)) {
        pathStream.forEach(path -> dirSet.add(path.getFileName()));
    }
    assertEquals(expectedDirSet, dirSet);
    // Test case when Path doesn't exist.
    try (Stream<Path> pathStream = Files.walk(filesSetup.getTestPath())) {
        fail();
    } catch (NoSuchFileException expected) {
    }
    // Test case when Path is a not a directory.
    expectedDirSet.clear();
    dirSet.clear();
    expectedDirSet.add(filesSetup.getDataFilePath());
    try (Stream<Path> pathStream = Files.walk(filesSetup.getDataFilePath())) {
        pathStream.forEach(path -> dirSet.add(path));
    }
    assertEquals(expectedDirSet, dirSet);
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

NoSuchFileException (java.nio.file.NoSuchFileException)262 IOException (java.io.IOException)107 Path (java.nio.file.Path)104 FileNotFoundException (java.io.FileNotFoundException)41 Test (org.junit.Test)35 InputStream (java.io.InputStream)31 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)25 File (java.io.File)22 NotDirectoryException (java.nio.file.NotDirectoryException)19 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)18 ArrayList (java.util.ArrayList)16 HashSet (java.util.HashSet)16 OutputStream (java.io.OutputStream)15 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)15 FileChannel (java.nio.channels.FileChannel)14 AccessDeniedException (java.nio.file.AccessDeniedException)14 ByteBuffer (java.nio.ByteBuffer)13 HashMap (java.util.HashMap)13 Map (java.util.Map)12 SeekableByteChannel (java.nio.channels.SeekableByteChannel)11