Search in sources :

Example 11 with BasicFileAttributeView

use of java.nio.file.attribute.BasicFileAttributeView in project cryptofs by cryptomator.

the class CryptoFileAttributeByNameProviderTest method testSetAttributeWithAttributeSucceeds.

@Test
public void testSetAttributeWithAttributeSucceeds() throws IOException {
    FileTime expectedFileTime = FileTime.fromMillis(4838388);
    BasicFileAttributeView fileAttributeView = mock(BasicFileAttributeView.class);
    when(fileAttributeViewProvider.getAttributeView(aPath, BasicFileAttributeView.class)).thenReturn(fileAttributeView);
    inTest.setAttribute(aPath, "creationTime", expectedFileTime);
    verify(fileAttributeView).setTimes(null, null, expectedFileTime);
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileTime(java.nio.file.attribute.FileTime) Test(org.junit.Test)

Example 12 with BasicFileAttributeView

use of java.nio.file.attribute.BasicFileAttributeView in project cryptofs by cryptomator.

the class CryptoFileAttributeByNameProviderTest method testSetAttributeWithBasicAttributeSucceeds.

@Test
public void testSetAttributeWithBasicAttributeSucceeds() throws IOException {
    FileTime expectedFileTime = FileTime.fromMillis(4838388);
    BasicFileAttributeView fileAttributeView = mock(BasicFileAttributeView.class);
    when(fileAttributeViewProvider.getAttributeView(aPath, BasicFileAttributeView.class)).thenReturn(fileAttributeView);
    inTest.setAttribute(aPath, "basic:creationTime", expectedFileTime);
    verify(fileAttributeView).setTimes(null, null, expectedFileTime);
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileTime(java.nio.file.attribute.FileTime) Test(org.junit.Test)

Example 13 with BasicFileAttributeView

use of java.nio.file.attribute.BasicFileAttributeView in project logging-log4j2 by apache.

the class OnStartupTriggeringPolicyTest method testPolicy.

@Test
public void testPolicy() throws Exception {
    final Configuration configuration = new DefaultConfiguration();
    final Path target = tempDir.resolve("testfile");
    final long timeStamp = Instant.now().minus(Duration.ofDays(1)).toEpochMilli();
    final String expectedDate = formatter.format(timeStamp);
    final Path rolled = tempDir.resolve("test1-" + expectedDate + "-1.log");
    final long copied;
    try (final InputStream is = new ByteArrayInputStream(TEST_DATA.getBytes(StandardCharsets.UTF_8))) {
        copied = Files.copy(is, target, StandardCopyOption.REPLACE_EXISTING);
    }
    final long size = Files.size(target);
    assertTrue(size > 0);
    assertEquals(copied, size);
    final FileTime fileTime = FileTime.fromMillis(timeStamp);
    final BasicFileAttributeView attrs = Files.getFileAttributeView(target, BasicFileAttributeView.class);
    attrs.setTimes(fileTime, fileTime, fileTime);
    final PatternLayout layout = PatternLayout.newBuilder().setPattern("%msg").setConfiguration(configuration).build();
    final RolloverStrategy strategy = DefaultRolloverStrategy.newBuilder().setCompressionLevelStr("0").setStopCustomActionsOnError(true).setConfig(configuration).build();
    final OnStartupTriggeringPolicy policy = OnStartupTriggeringPolicy.createPolicy(1);
    try (final RollingFileManager manager = RollingFileManager.getFileManager(target.toString(), tempDir.toString() + TARGET_PATTERN, true, false, policy, strategy, null, layout, 8192, true, false, null, null, null, configuration)) {
        manager.initialize();
        final String files;
        try (Stream<Path> contents = Files.list(tempDir)) {
            files = contents.map(Path::toString).collect(Collectors.joining(", ", "[", "]"));
        }
        assertTrue(Files.exists(target), target.toString() + ", files = " + files);
        assertEquals(0, Files.size(target), target.toString());
        assertTrue(Files.exists(rolled), "Missing: " + rolled.toString() + ", files on disk = " + files);
        assertEquals(size, Files.size(rolled), rolled.toString());
    }
}
Also used : Path(java.nio.file.Path) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) FileTime(java.nio.file.attribute.FileTime) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 14 with BasicFileAttributeView

use of java.nio.file.attribute.BasicFileAttributeView in project musiccabinet by hakko.

the class DirectoryBrowserService method getContent.

private DirectoryContent getContent(Directory dir) {
    Set<File> foundFiles = new HashSet<>();
    Set<String> foundSubDirs = new HashSet<>();
    DirectoryContent content = new DirectoryContent(dir.getPath(), foundSubDirs, foundFiles);
    Path path = Paths.get(dir.getPath());
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
        for (Path file : stream) {
            BasicFileAttributeView view = getFileAttributeView(file, BasicFileAttributeView.class);
            BasicFileAttributes attr = view.readAttributes();
            if (attr.isDirectory()) {
                foundSubDirs.add(file.toAbsolutePath().toString());
            } else if (attr.isRegularFile()) {
                foundFiles.add(new File(file, attr));
            }
        }
    } catch (IOException | DirectoryIteratorException e) {
        throw new ApplicationContextException("Couldn't read " + dir.getPath(), e);
    }
    return content;
}
Also used : Path(java.nio.file.Path) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) IOException(java.io.IOException) ApplicationContextException(org.springframework.context.ApplicationContextException) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) DirectoryContent(com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent) File(com.github.hakko.musiccabinet.domain.model.library.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) HashSet(java.util.HashSet)

Example 15 with BasicFileAttributeView

use of java.nio.file.attribute.BasicFileAttributeView in project jimfs by google.

the class JimfsUnixLikeFileSystemTest method testClosedSecureDirectoryStreamAttributeViewAndIterator.

@SuppressWarnings("StreamResourceLeak")
@Test
public void testClosedSecureDirectoryStreamAttributeViewAndIterator() throws IOException {
    Files.createDirectory(path("/foo"));
    Files.createDirectory(path("/foo/bar"));
    SecureDirectoryStream<Path> stream = (SecureDirectoryStream<Path>) Files.newDirectoryStream(path("/foo"));
    Iterator<Path> iter = stream.iterator();
    BasicFileAttributeView view1 = stream.getFileAttributeView(BasicFileAttributeView.class);
    BasicFileAttributeView view2 = stream.getFileAttributeView(path("bar"), BasicFileAttributeView.class);
    try {
        stream.iterator();
        fail("expected IllegalStateException");
    } catch (IllegalStateException expected) {
    }
    stream.close();
    try {
        iter.next();
        fail("expected ClosedDirectoryStreamException");
    } catch (ClosedDirectoryStreamException expected) {
    }
    try {
        view1.readAttributes();
        fail("expected ClosedDirectoryStreamException");
    } catch (ClosedDirectoryStreamException expected) {
    }
    try {
        view2.readAttributes();
        fail("expected ClosedDirectoryStreamException");
    } catch (ClosedDirectoryStreamException expected) {
    }
    try {
        view1.setTimes(null, null, null);
        fail("expected ClosedDirectoryStreamException");
    } catch (ClosedDirectoryStreamException expected) {
    }
    try {
        view2.setTimes(null, null, null);
        fail("expected ClosedDirectoryStreamException");
    } catch (ClosedDirectoryStreamException expected) {
    }
}
Also used : Path(java.nio.file.Path) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) ClosedDirectoryStreamException(java.nio.file.ClosedDirectoryStreamException) SecureDirectoryStream(java.nio.file.SecureDirectoryStream) Test(org.junit.Test)

Aggregations

BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)28 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)13 Path (java.nio.file.Path)11 FileTime (java.nio.file.attribute.FileTime)11 IOException (java.io.IOException)7 Test (org.junit.Test)7 File (java.io.File)3 NoSuchFileException (java.nio.file.NoSuchFileException)3 FileOwnerAttributeView (java.nio.file.attribute.FileOwnerAttributeView)3 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)3 InputStream (java.io.InputStream)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 FileStore (java.nio.file.FileStore)2 SecureDirectoryStream (java.nio.file.SecureDirectoryStream)2 StandardCopyOption (java.nio.file.StandardCopyOption)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2