2020package ch .vorburger .fswatch .test ;
2121
2222import static com .google .common .base .Charsets .US_ASCII ;
23+ import static java .nio .file .StandardCopyOption .REPLACE_EXISTING ;
2324import static java .util .concurrent .TimeUnit .SECONDS ;
2425import static org .awaitility .Awaitility .await ;
2526import static org .hamcrest .Matchers .is ;
3536import com .google .common .io .MoreFiles ;
3637import java .io .File ;
3738import java .nio .file .FileSystems ;
39+ import java .util .concurrent .atomic .AtomicReference ;
3840import org .junit .BeforeClass ;
3941import 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