|
1 | 1 | package edu.kit.datamanager.ro_crate.crate; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
4 | 5 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
5 | 6 | import static org.junit.jupiter.api.Assertions.assertTrue; |
6 | 7 |
|
@@ -206,4 +207,78 @@ void testDetectingEncodedFileNames(@TempDir Path tempDir) throws IOException { |
206 | 207 | assertEquals(idEncoded, filepath.getFileName().toString()); |
207 | 208 | } |
208 | 209 | } |
| 210 | + |
| 211 | + @Test |
| 212 | + void testFilenamesAreSelfHealing(@TempDir Path tempDir) throws IOException { |
| 213 | + // This is how we add the id. But the space will be encoded |
| 214 | + String id = "id 42"; |
| 215 | + // This is how we get it out (the encoded id as it will exist in the crate) |
| 216 | + String idEncoded = IdentifierUtils.encode(id).orElseThrow(); |
| 217 | + |
| 218 | + RoCrate.RoCrateBuilder builder = new RoCrate.RoCrateBuilder(); |
| 219 | + |
| 220 | + // add dummy file |
| 221 | + Path filepath_outside = tempDir.resolve("someFile.txt"); |
| 222 | + Files.writeString(filepath_outside, "File"); |
| 223 | + |
| 224 | + { |
| 225 | + // Add file entity without a file with id |
| 226 | + FileEntity.FileEntityBuilder fileEntityBuilder = |
| 227 | + new FileEntity.FileEntityBuilder(); |
| 228 | + fileEntityBuilder.setId(id); |
| 229 | + fileEntityBuilder.addTypes(List.of("File")); |
| 230 | + fileEntityBuilder.setLocation(filepath_outside); |
| 231 | + |
| 232 | + builder.addDataEntity(fileEntityBuilder.build()); |
| 233 | + } |
| 234 | + |
| 235 | + Path cratepath = tempDir.resolve("test1"); |
| 236 | + { |
| 237 | + Writers.newFolderWriter().save(builder.build(), cratepath.toString()); |
| 238 | + Path currentFilePath = cratepath.resolve(id); |
| 239 | + assertTrue(currentFilePath.toFile().exists()); |
| 240 | + // swap file names |
| 241 | + Path newFilePath = cratepath.resolve(idEncoded); |
| 242 | + Files.move(currentFilePath, newFilePath); |
| 243 | + assertFalse(currentFilePath.toFile().exists()); |
| 244 | + assertTrue(newFilePath.toFile().exists()); |
| 245 | + } |
| 246 | + |
| 247 | + { |
| 248 | + RoCrate crate = Readers.newFolderReader().readCrate( |
| 249 | + cratepath.toString() |
| 250 | + ); |
| 251 | + |
| 252 | + DataEntity entity = crate.getDataEntityById(idEncoded); |
| 253 | + assertEquals(idEncoded, entity.getId()); |
| 254 | + |
| 255 | + // Even if a file's name is encoded, the path will work as expected |
| 256 | + Path filepath = entity.getPath(); |
| 257 | + assertNotNull(filepath); |
| 258 | + assertTrue(Files.exists(filepath)); |
| 259 | + assertEquals(idEncoded, filepath.getFileName().toString()); |
| 260 | + } |
| 261 | + |
| 262 | + // When saving the crate again, the file will be renamed to the decoded id |
| 263 | + Path cratepath2 = tempDir.resolve("test2"); |
| 264 | + { |
| 265 | + Writers.newFolderWriter().save(builder.build(), cratepath2.toString()); |
| 266 | + } |
| 267 | + |
| 268 | + { |
| 269 | + RoCrate crate = Readers.newFolderReader().readCrate( |
| 270 | + cratepath2.toString() |
| 271 | + ); |
| 272 | + |
| 273 | + DataEntity entity = crate.getDataEntityById(idEncoded); |
| 274 | + assertEquals(idEncoded, entity.getId()); |
| 275 | + |
| 276 | + // The filename is now decoded to the original id |
| 277 | + Path filepath = entity.getPath(); |
| 278 | + assertNotNull(filepath); |
| 279 | + assertTrue(Files.exists(filepath)); |
| 280 | + assertEquals(id, filepath.getFileName().toString()); |
| 281 | + } |
| 282 | + |
| 283 | + } |
209 | 284 | } |
0 commit comments