|
38 | 38 | import org.junit.jupiter.api.AfterEach; |
39 | 39 | import org.junit.jupiter.api.BeforeAll; |
40 | 40 | import org.junit.jupiter.api.BeforeEach; |
41 | | -import org.junit.jupiter.api.Disabled; |
42 | 41 | import org.junit.jupiter.api.Test; |
43 | 42 |
|
44 | 43 |
|
@@ -171,4 +170,82 @@ void moveRegularFile() throws IOException { |
171 | 170 | .events().kind(DELETED).rootPath(source).any()); |
172 | 171 | } |
173 | 172 | } |
| 173 | + |
| 174 | + @Test |
| 175 | + void moveDirectory() throws IOException { |
| 176 | + var parent = testDir.getTestDirectory(); |
| 177 | + var child1 = Files.createDirectories(parent.resolve("from")); |
| 178 | + var child2 = Files.createDirectories(parent.resolve("to")); |
| 179 | + |
| 180 | + var directory = Files.createDirectory(child1.resolve("directory")); |
| 181 | + var regularFile1 = Files.createFile(directory.resolve("file1.txt")); |
| 182 | + var regularFile2 = Files.createFile(directory.resolve("file2.txt")); |
| 183 | + |
| 184 | + var parentWatchBookkeeper = new TestHelper.Bookkeeper(); |
| 185 | + var parentWatchConfig = Watch |
| 186 | + .build(parent, WatchScope.PATH_AND_ALL_DESCENDANTS) |
| 187 | + .on(parentWatchBookkeeper); |
| 188 | + |
| 189 | + var child1WatchBookkeeper = new TestHelper.Bookkeeper(); |
| 190 | + var child1WatchConfig = Watch |
| 191 | + .build(child1, WatchScope.PATH_AND_CHILDREN) |
| 192 | + .on(child1WatchBookkeeper); |
| 193 | + |
| 194 | + var child2WatchBookkeeper = new TestHelper.Bookkeeper(); |
| 195 | + var child2WatchConfig = Watch |
| 196 | + .build(child2, WatchScope.PATH_AND_CHILDREN) |
| 197 | + .on(child2WatchBookkeeper); |
| 198 | + |
| 199 | + var directoryWatchBookkeeper = new TestHelper.Bookkeeper(); |
| 200 | + var directoryWatchConfig = Watch |
| 201 | + .build(directory, WatchScope.PATH_ONLY) |
| 202 | + .on(directoryWatchBookkeeper); |
| 203 | + |
| 204 | + try (var parentWatch = parentWatchConfig.start(); |
| 205 | + var child1Watch = child1WatchConfig.start(); |
| 206 | + var child2Watch = child2WatchConfig.start(); |
| 207 | + var fileWatch = directoryWatchConfig.start()) { |
| 208 | + |
| 209 | + var sourceDirectory = child1.resolve(directory.getFileName()); |
| 210 | + var sourceRegularFile1 = sourceDirectory.resolve(regularFile1.getFileName()); |
| 211 | + var sourceRegularFile2 = sourceDirectory.resolve(regularFile2.getFileName()); |
| 212 | + |
| 213 | + var targetDirectory = child2.resolve(directory.getFileName()); |
| 214 | + var targetRegularFile1 = targetDirectory.resolve(regularFile1.getFileName()); |
| 215 | + var targetRegularFile2 = targetDirectory.resolve(regularFile2.getFileName()); |
| 216 | + |
| 217 | + Files.move(sourceDirectory, targetDirectory); |
| 218 | + |
| 219 | + for (var e : new WatchEvent[] { |
| 220 | + new WatchEvent(DELETED, parent, parent.relativize(sourceDirectory)), |
| 221 | + new WatchEvent(CREATED, parent, parent.relativize(targetDirectory)), |
| 222 | + // The following events currently *aren't* observed by the |
| 223 | + // `parent` watch for the whole file tree: moving a directory |
| 224 | + // doesn't trigger events for the deletion/creation of the files |
| 225 | + // contained in it (neither using the general default/JDK |
| 226 | + // implementation of Watch Service, nor using our special macOS |
| 227 | + // implementation). |
| 228 | + // |
| 229 | + // new WatchEvent(DELETED, parent, parent.relativize(sourceRegularFile1)), |
| 230 | + // new WatchEvent(DELETED, parent, parent.relativize(sourceRegularFile2)), |
| 231 | + // new WatchEvent(CREATED, parent, parent.relativize(targetRegularFile1)), |
| 232 | + // new WatchEvent(CREATED, parent, parent.relativize(targetRegularFile2)) |
| 233 | + }) { |
| 234 | + await("Move should be observed as delete/create by `parent` watch (file tree): " + e) |
| 235 | + .until(() -> parentWatchBookkeeper.events().any(e)); |
| 236 | + } |
| 237 | + |
| 238 | + await("Move should be observed as delete by `child1` watch (single directory)") |
| 239 | + .until(() -> child1WatchBookkeeper |
| 240 | + .events().kind(DELETED).rootPath(child1).relativePath(child1.relativize(sourceDirectory)).any()); |
| 241 | + |
| 242 | + await("Move should be observed as create by `child2` watch (single directory)") |
| 243 | + .until(() -> child2WatchBookkeeper |
| 244 | + .events().kind(CREATED).rootPath(child2).relativePath(child2.relativize(targetDirectory)).any()); |
| 245 | + |
| 246 | + await("Move should be observed as delete by `directory` watch") |
| 247 | + .until(() -> directoryWatchBookkeeper |
| 248 | + .events().kind(DELETED).rootPath(sourceDirectory).any()); |
| 249 | + } |
| 250 | + } |
174 | 251 | } |
0 commit comments