|
4 | 4 |
|
5 | 5 | import java.io.IOException; |
6 | 6 | import java.nio.file.*; |
| 7 | +import java.nio.file.attribute.BasicFileAttributeView; |
| 8 | +import java.nio.file.attribute.BasicFileAttributes; |
| 9 | +import java.nio.file.attribute.FileAttributeView; |
7 | 10 | import java.nio.file.spi.FileSystemProvider; |
8 | 11 | import java.util.List; |
9 | 12 | import java.util.Map; |
@@ -161,4 +164,50 @@ void testNested() { |
161 | 164 | var input = assertDoesNotThrow(() -> Files.newInputStream(npath)); |
162 | 165 | var data = assertDoesNotThrow(() -> input.readAllBytes()); |
163 | 166 | } |
| 167 | + |
| 168 | + @Test |
| 169 | + void testFileAttributes() { |
| 170 | + final var dir1 = Paths.get("src", "test", "resources", "dir1.zip").toAbsolutePath().normalize(); |
| 171 | + var fsp = (UnionFileSystemProvider)FileSystemProvider.installedProviders().stream().filter(fs-> fs.getScheme().equals("union")).findFirst().orElseThrow(); |
| 172 | + var ufs = fsp.newFileSystem((a,b) -> true, dir1); |
| 173 | + var path = (UnionPath) ufs.getPath("subdir1"); |
| 174 | + var nonExistentPath = (UnionPath) ufs.getPath("non-existent-path"); |
| 175 | + |
| 176 | + // Non-union path |
| 177 | + assertDoesNotThrow(() -> { |
| 178 | + assertNull(fsp.getFileAttributeView(Paths.get("subdir1"), BasicFileAttributeView.class)); |
| 179 | + }); |
| 180 | + // Unsupported attribute view |
| 181 | + assertDoesNotThrow(() -> { |
| 182 | + assertNull(fsp.getFileAttributeView(path, FileAttributeView.class)); |
| 183 | + }); |
| 184 | + // Non-existent path w/ supported attribute view |
| 185 | + assertThrows(NoSuchFileException.class, () -> { |
| 186 | + var nonExistentView = assertDoesNotThrow(() -> { |
| 187 | + var view = fsp.getFileAttributeView(nonExistentPath, BasicFileAttributeView.class); |
| 188 | + assertNotNull(view); |
| 189 | + return view; |
| 190 | + }); |
| 191 | + nonExistentView.readAttributes(); |
| 192 | + }); |
| 193 | + // Non-existent path |
| 194 | + assertThrows(NoSuchFileException.class, () -> { |
| 195 | + ufs.readAttributes(nonExistentPath, BasicFileAttributes.class); |
| 196 | + }); |
| 197 | + |
| 198 | + // Union path w/ supported attribute view |
| 199 | + var validViewAttributes = assertDoesNotThrow(() -> { |
| 200 | + var view = fsp.getFileAttributeView(path, BasicFileAttributeView.class); |
| 201 | + assertNotNull(view); |
| 202 | + return view.readAttributes(); |
| 203 | + }); |
| 204 | + // Known existing path |
| 205 | + var validAttributes = assertDoesNotThrow(() -> { |
| 206 | + var attributes = ufs.readAttributes(path, BasicFileAttributes.class); |
| 207 | + assertNotNull(attributes); |
| 208 | + return attributes; |
| 209 | + }); |
| 210 | + // Ensure the attributes are the same through both methods |
| 211 | + assertEquals(validAttributes, validViewAttributes); |
| 212 | + } |
164 | 213 | } |
0 commit comments