Skip to content

Commit 759a857

Browse files
committed
test case that shows the issue
1 parent bcd17e5 commit 759a857

1 file changed

Lines changed: 31 additions & 12 deletions

File tree

src/test/java/ch/vorburger/fswatch/test/DirectoryAndFileWatcherTest.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package ch.vorburger.fswatch.test;
2121

2222
import static com.google.common.base.Charsets.US_ASCII;
23+
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
2324
import static java.util.concurrent.TimeUnit.SECONDS;
2425
import static org.awaitility.Awaitility.await;
2526
import static org.hamcrest.Matchers.is;
@@ -35,6 +36,7 @@
3536
import com.google.common.io.MoreFiles;
3637
import java.io.File;
3738
import java.nio.file.FileSystems;
39+
import java.util.concurrent.atomic.AtomicReference;
3840
import org.junit.BeforeClass;
3941
import org.junit.Test;
4042

@@ -48,13 +50,11 @@ public class DirectoryAndFileWatcherTest {
4850
AssertableExceptionHandler assertableExceptionHandler;
4951
volatile boolean changed;
5052

51-
@BeforeClass
52-
static public void configureSlf4jSimpleShowAllLogs() {
53+
@BeforeClass static public void configureSlf4jSimpleShowAllLogs() {
5354
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "trace");
5455
}
5556

56-
@Test
57-
public void testFileWatcher() throws Throwable {
57+
@Test public void testFileWatcher() throws Throwable {
5858
assertableExceptionHandler = new AssertableExceptionHandler();
5959
final File dir = new File("target/tests/FileWatcherTest/");
6060
final File subDir = new File(dir.getParentFile(), "subDir");
@@ -105,8 +105,7 @@ public void testFileWatcher() throws Throwable {
105105
}
106106
}
107107

108-
@Test
109-
public void testDirectoryWatcher() throws Throwable {
108+
@Test public void testDirectoryWatcher() throws Throwable {
110109
assertableExceptionHandler = new AssertableExceptionHandler();
111110
final File dir = new File("target/tests/DirectoryWatcherTest/some/sub/directory");
112111
dir.mkdirs();
@@ -151,8 +150,7 @@ public void testDirectoryWatcher() throws Throwable {
151150
}
152151
}
153152

154-
@Test
155-
public void testExistingFilesDirectoryWatcher() throws Throwable {
153+
@Test public void testExistingFilesDirectoryWatcher() throws Throwable {
156154
assertableExceptionHandler = new AssertableExceptionHandler();
157155
File file = new File("target/tests/DirectoryWatcherTest/existing");
158156
MoreFiles.deleteRecursively(file.getParentFile().toPath());
@@ -178,8 +176,7 @@ public void testExistingFilesDirectoryWatcher() throws Throwable {
178176
}
179177
}
180178

181-
@Test
182-
public void testFilteredDirectoryWatcher() throws Throwable {
179+
@Test public void testFilteredDirectoryWatcher() throws Throwable {
183180
assertableExceptionHandler = new AssertableExceptionHandler();
184181
final File dir = new File("target/tests/DirectoryWatcherTest/some/sub/directory");
185182
dir.mkdirs();
@@ -209,8 +206,7 @@ public void testFilteredDirectoryWatcher() throws Throwable {
209206
}
210207
}
211208

212-
@Test(expected = AssertionError.class)
213-
public void testDirectoryWatcherListenerExceptionPropagation() throws Throwable {
209+
@Test(expected = AssertionError.class) public void testDirectoryWatcherListenerExceptionPropagation() throws Throwable {
214210
assertableExceptionHandler = new AssertableExceptionHandler();
215211
final File testFile = new File("target/tests/DirectoryWatcherTest/someFile");
216212
testFile.delete();
@@ -225,6 +221,29 @@ public void testDirectoryWatcherListenerExceptionPropagation() throws Throwable
225221
}
226222
}
227223

224+
@Test
225+
public void testOverwriteExistingFile() throws Throwable {
226+
assertableExceptionHandler = new AssertableExceptionHandler();
227+
final File dir = new File("target/tests/DirectoryWatcherTest/existing");
228+
dir.mkdirs();
229+
final File file = File.createTempFile("test", "txt");
230+
final File to = dir.toPath().resolve("test.txt").toFile();
231+
Files.copy(file, to);
232+
AtomicReference<ChangeKind> change = new AtomicReference<>();
233+
234+
try (DirectoryWatcher dw = new DirectoryWatcherBuilder().path(dir).listener((p, c) -> {
235+
System.out.println("c = " + c);
236+
change.set(c);
237+
}).exceptionHandler(assertableExceptionHandler).build()) {
238+
// We want it to call the listener once for setup, even without any change
239+
assertableExceptionHandler.assertNoErrorInTheBackgroundThread();
240+
java.nio.file.Files.move(file.toPath(), to.toPath(), REPLACE_EXISTING);
241+
242+
// Files.move(file, to);
243+
await().atMost(5, SECONDS).until(change::get, is(ChangeKind.CREATED));
244+
}
245+
}
246+
228247
@Test
229248
public void testFileWatcherWithSelectedChangeKinds() throws Throwable {
230249
assertableExceptionHandler = new AssertableExceptionHandler();

0 commit comments

Comments
 (0)