|
|
@ -1,5 +1,5 @@
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Copyright 2012-2020 the original author or authors.
|
|
|
|
* Copyright 2012-2021 the original author or authors.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
@ -23,10 +23,10 @@ import java.time.Duration;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
@ -44,6 +44,7 @@ import static org.mockito.Mockito.mock;
|
|
|
|
* Tests for {@link FileSystemWatcher}.
|
|
|
|
* Tests for {@link FileSystemWatcher}.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author Phillip Webb
|
|
|
|
* @author Phillip Webb
|
|
|
|
|
|
|
|
* @author Andy Wilkinson
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
class FileSystemWatcherTests {
|
|
|
|
class FileSystemWatcherTests {
|
|
|
|
|
|
|
|
|
|
|
@ -120,9 +121,8 @@ class FileSystemWatcherTests {
|
|
|
|
File directory = startWithNewDirectory();
|
|
|
|
File directory = startWithNewDirectory();
|
|
|
|
File file = touch(new File(directory, "test.txt"));
|
|
|
|
File file = touch(new File(directory, "test.txt"));
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
ChangedFiles changedFiles = getSingleChangedFiles();
|
|
|
|
|
|
|
|
ChangedFile expected = new ChangedFile(directory, file, Type.ADD);
|
|
|
|
ChangedFile expected = new ChangedFile(directory, file, Type.ADD);
|
|
|
|
assertThat(changedFiles.getFiles()).contains(expected);
|
|
|
|
assertThat(getAllFileChanges()).containsExactly(expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -130,9 +130,8 @@ class FileSystemWatcherTests {
|
|
|
|
File directory = startWithNewDirectory();
|
|
|
|
File directory = startWithNewDirectory();
|
|
|
|
File file = touch(new File(new File(directory, "sub"), "text.txt"));
|
|
|
|
File file = touch(new File(new File(directory, "sub"), "text.txt"));
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
ChangedFiles changedFiles = getSingleChangedFiles();
|
|
|
|
|
|
|
|
ChangedFile expected = new ChangedFile(directory, file, Type.ADD);
|
|
|
|
ChangedFile expected = new ChangedFile(directory, file, Type.ADD);
|
|
|
|
assertThat(changedFiles.getFiles()).contains(expected);
|
|
|
|
assertThat(getAllFileChanges()).containsExactly(expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -144,9 +143,8 @@ class FileSystemWatcherTests {
|
|
|
|
directory.mkdirs();
|
|
|
|
directory.mkdirs();
|
|
|
|
File file = touch(new File(directory, "text.txt"));
|
|
|
|
File file = touch(new File(directory, "text.txt"));
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
ChangedFiles changedFiles = getSingleChangedFiles();
|
|
|
|
|
|
|
|
ChangedFile expected = new ChangedFile(directory, file, Type.ADD);
|
|
|
|
ChangedFile expected = new ChangedFile(directory, file, Type.ADD);
|
|
|
|
assertThat(changedFiles.getFiles()).contains(expected);
|
|
|
|
assertThat(getAllFileChanges()).containsExactly(expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -171,8 +169,7 @@ class FileSystemWatcherTests {
|
|
|
|
Thread.sleep(10);
|
|
|
|
Thread.sleep(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
ChangedFiles changedFiles = getSingleChangedFiles();
|
|
|
|
assertThat(getAllFileChanges()).hasSize(100);
|
|
|
|
assertThat(changedFiles.getFiles()).hasSize(100);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -184,9 +181,8 @@ class FileSystemWatcherTests {
|
|
|
|
this.watcher.start();
|
|
|
|
this.watcher.start();
|
|
|
|
File file = touch(new File(directory, "test2.txt"));
|
|
|
|
File file = touch(new File(directory, "test2.txt"));
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
ChangedFiles changedFiles = getSingleChangedFiles();
|
|
|
|
|
|
|
|
ChangedFile expected = new ChangedFile(directory, file, Type.ADD);
|
|
|
|
ChangedFile expected = new ChangedFile(directory, file, Type.ADD);
|
|
|
|
assertThat(changedFiles.getFiles()).contains(expected);
|
|
|
|
assertThat(getAllFileChanges()).contains(expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -201,7 +197,7 @@ class FileSystemWatcherTests {
|
|
|
|
File file1 = touch(new File(directory1, "test.txt"));
|
|
|
|
File file1 = touch(new File(directory1, "test.txt"));
|
|
|
|
File file2 = touch(new File(directory2, "test.txt"));
|
|
|
|
File file2 = touch(new File(directory2, "test.txt"));
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
Set<ChangedFiles> change = getSingleOnChange();
|
|
|
|
Set<ChangedFiles> change = this.changes.stream().flatMap(Set<ChangedFiles>::stream).collect(Collectors.toSet());
|
|
|
|
assertThat(change.size()).isEqualTo(2);
|
|
|
|
assertThat(change.size()).isEqualTo(2);
|
|
|
|
for (ChangedFiles changedFiles : change) {
|
|
|
|
for (ChangedFiles changedFiles : change) {
|
|
|
|
if (changedFiles.getSourceDirectory().equals(directory1)) {
|
|
|
|
if (changedFiles.getSourceDirectory().equals(directory1)) {
|
|
|
@ -219,16 +215,16 @@ class FileSystemWatcherTests {
|
|
|
|
void multipleListeners() throws Exception {
|
|
|
|
void multipleListeners() throws Exception {
|
|
|
|
File directory = new File(this.tempDir, UUID.randomUUID().toString());
|
|
|
|
File directory = new File(this.tempDir, UUID.randomUUID().toString());
|
|
|
|
directory.mkdir();
|
|
|
|
directory.mkdir();
|
|
|
|
final Set<ChangedFiles> listener2Changes = new LinkedHashSet<>();
|
|
|
|
final List<Set<ChangedFiles>> listener2Changes = new ArrayList<>();
|
|
|
|
this.watcher.addSourceDirectory(directory);
|
|
|
|
this.watcher.addSourceDirectory(directory);
|
|
|
|
this.watcher.addListener(listener2Changes::addAll);
|
|
|
|
this.watcher.addListener(listener2Changes::add);
|
|
|
|
this.watcher.start();
|
|
|
|
this.watcher.start();
|
|
|
|
File file = touch(new File(directory, "test.txt"));
|
|
|
|
File file = touch(new File(directory, "test.txt"));
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
ChangedFiles changedFiles = getSingleChangedFiles();
|
|
|
|
|
|
|
|
ChangedFile expected = new ChangedFile(directory, file, Type.ADD);
|
|
|
|
ChangedFile expected = new ChangedFile(directory, file, Type.ADD);
|
|
|
|
assertThat(changedFiles.getFiles()).contains(expected);
|
|
|
|
Set<ChangedFile> changeSet = getAllFileChanges();
|
|
|
|
assertThat(listener2Changes).isEqualTo(this.changes.get(0));
|
|
|
|
assertThat(changeSet).contains(expected);
|
|
|
|
|
|
|
|
assertThat(getAllFileChanges(listener2Changes)).isEqualTo(changeSet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -243,8 +239,7 @@ class FileSystemWatcherTests {
|
|
|
|
delete.delete();
|
|
|
|
delete.delete();
|
|
|
|
File add = touch(new File(directory, "add.txt"));
|
|
|
|
File add = touch(new File(directory, "add.txt"));
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
ChangedFiles changedFiles = getSingleChangedFiles();
|
|
|
|
Set<ChangedFile> actual = getAllFileChanges();
|
|
|
|
Set<ChangedFile> actual = changedFiles.getFiles();
|
|
|
|
|
|
|
|
Set<ChangedFile> expected = new HashSet<>();
|
|
|
|
Set<ChangedFile> expected = new HashSet<>();
|
|
|
|
expected.add(new ChangedFile(directory, modify, Type.MODIFY));
|
|
|
|
expected.add(new ChangedFile(directory, modify, Type.MODIFY));
|
|
|
|
expected.add(new ChangedFile(directory, delete, Type.DELETE));
|
|
|
|
expected.add(new ChangedFile(directory, delete, Type.DELETE));
|
|
|
@ -266,8 +261,7 @@ class FileSystemWatcherTests {
|
|
|
|
assertThat(this.changes).isEmpty();
|
|
|
|
assertThat(this.changes).isEmpty();
|
|
|
|
FileCopyUtils.copy("abc".getBytes(), trigger);
|
|
|
|
FileCopyUtils.copy("abc".getBytes(), trigger);
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
this.watcher.stopAfter(1);
|
|
|
|
ChangedFiles changedFiles = getSingleChangedFiles();
|
|
|
|
Set<ChangedFile> actual = getAllFileChanges();
|
|
|
|
Set<ChangedFile> actual = changedFiles.getFiles();
|
|
|
|
|
|
|
|
Set<ChangedFile> expected = new HashSet<>();
|
|
|
|
Set<ChangedFile> expected = new HashSet<>();
|
|
|
|
expected.add(new ChangedFile(directory, file, Type.MODIFY));
|
|
|
|
expected.add(new ChangedFile(directory, file, Type.MODIFY));
|
|
|
|
assertThat(actual).isEqualTo(expected);
|
|
|
|
assertThat(actual).isEqualTo(expected);
|
|
|
@ -315,15 +309,13 @@ class FileSystemWatcherTests {
|
|
|
|
return directory;
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private ChangedFiles getSingleChangedFiles() {
|
|
|
|
private Set<ChangedFile> getAllFileChanges() {
|
|
|
|
Set<ChangedFiles> singleChange = getSingleOnChange();
|
|
|
|
return getAllFileChanges(this.changes);
|
|
|
|
assertThat(singleChange).hasSize(1);
|
|
|
|
|
|
|
|
return singleChange.iterator().next();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Set<ChangedFiles> getSingleOnChange() {
|
|
|
|
private Set<ChangedFile> getAllFileChanges(List<Set<ChangedFiles>> changes) {
|
|
|
|
assertThat(this.changes).hasSize(1);
|
|
|
|
return changes.stream().flatMap(Set<ChangedFiles>::stream)
|
|
|
|
return this.changes.get(0);
|
|
|
|
.flatMap((changedFiles) -> changedFiles.getFiles().stream()).collect(Collectors.toSet());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private File touch(File file) throws IOException {
|
|
|
|
private File touch(File file) throws IOException {
|
|
|
|