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);
}
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);
}
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());
}
}
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;
}
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) {
}
}
Aggregations