|
25 | 25 | import java.lang.annotation.Retention;
|
26 | 26 | import java.lang.annotation.Target;
|
27 | 27 | import java.nio.ByteBuffer;
|
| 28 | +import java.nio.channels.FileChannel; |
| 29 | +import java.nio.channels.FileLock; |
28 | 30 | import java.nio.channels.NonReadableChannelException;
|
29 | 31 | import java.nio.channels.SeekableByteChannel;
|
30 | 32 | import java.nio.file.DirectoryStream;
|
|
34 | 36 | import java.nio.file.NoSuchFileException;
|
35 | 37 | import java.nio.file.Path;
|
36 | 38 | import java.nio.file.PathMatcher;
|
| 39 | +import java.nio.file.StandardOpenOption; |
37 | 40 | import java.nio.file.attribute.BasicFileAttributeView;
|
38 | 41 | import java.util.ArrayList;
|
39 | 42 | import java.util.Arrays;
|
@@ -365,4 +368,41 @@ void subPath(boolean useDefault) {
|
365 | 368 | assertThrows(IllegalArgumentException.class, () -> target.subpath(0, 0));
|
366 | 369 | }
|
367 | 370 |
|
| 371 | + @CompatibilityTest |
| 372 | + void moveWhileOpen(boolean useDefault) throws IOException { |
| 373 | + FileSystem fileSystem = this.getFileSystem(useDefault); |
| 374 | + Path folder = fileSystem.getPath("target"); |
| 375 | + if (!useDefault) { |
| 376 | + Files.createDirectory(folder); |
| 377 | + } |
| 378 | + Path originalFileName = Files.createTempFile(folder, "locked", ".txt"); |
| 379 | + Path newFileName = folder.resolve("new_name.txt"); |
| 380 | + try (FileChannel channel = FileChannel.open(originalFileName, StandardOpenOption.WRITE)) { |
| 381 | + Files.move(originalFileName, newFileName); |
| 382 | + } finally { |
| 383 | + Files.deleteIfExists(originalFileName); |
| 384 | + Files.deleteIfExists(newFileName); |
| 385 | + } |
| 386 | + } |
| 387 | + |
| 388 | + @CompatibilityTest |
| 389 | + void moveWhileLocked(boolean useDefault) throws IOException { |
| 390 | + FileSystem fileSystem = this.getFileSystem(useDefault); |
| 391 | + Path folder = fileSystem.getPath("target"); |
| 392 | + if (!useDefault) { |
| 393 | + Files.createDirectory(folder); |
| 394 | + } |
| 395 | + Path originalFileName = Files.createTempFile(folder, "locked", ".txt"); |
| 396 | + Path newFileName = folder.resolve("new_name.txt"); |
| 397 | + try (FileChannel channel = FileChannel.open(originalFileName, StandardOpenOption.WRITE)) { |
| 398 | + try (FileLock lock = channel.tryLock()) { |
| 399 | + assertNotNull(lock); |
| 400 | + Files.move(originalFileName, newFileName); |
| 401 | + } |
| 402 | + } finally { |
| 403 | + Files.deleteIfExists(originalFileName); |
| 404 | + Files.deleteIfExists(newFileName); |
| 405 | + } |
| 406 | + } |
| 407 | + |
368 | 408 | }
|
0 commit comments