Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test case that shows the issue #96

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package ch.vorburger.fswatch.test;

import static com.google.common.base.Charsets.US_ASCII;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.is;
Expand All @@ -35,6 +36,7 @@
import com.google.common.io.MoreFiles;
import java.io.File;
import java.nio.file.FileSystems;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -48,13 +50,11 @@ public class DirectoryAndFileWatcherTest {
AssertableExceptionHandler assertableExceptionHandler;
volatile boolean changed;

@BeforeClass
static public void configureSlf4jSimpleShowAllLogs() {
@BeforeClass static public void configureSlf4jSimpleShowAllLogs() {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "trace");
}

@Test
public void testFileWatcher() throws Throwable {
@Test public void testFileWatcher() throws Throwable {
assertableExceptionHandler = new AssertableExceptionHandler();
final File dir = new File("target/tests/FileWatcherTest/");
final File subDir = new File(dir.getParentFile(), "subDir");
Expand Down Expand Up @@ -105,8 +105,7 @@ public void testFileWatcher() throws Throwable {
}
}

@Test
public void testDirectoryWatcher() throws Throwable {
@Test public void testDirectoryWatcher() throws Throwable {
assertableExceptionHandler = new AssertableExceptionHandler();
final File dir = new File("target/tests/DirectoryWatcherTest/some/sub/directory");
dir.mkdirs();
Expand Down Expand Up @@ -151,8 +150,7 @@ public void testDirectoryWatcher() throws Throwable {
}
}

@Test
public void testExistingFilesDirectoryWatcher() throws Throwable {
@Test public void testExistingFilesDirectoryWatcher() throws Throwable {
assertableExceptionHandler = new AssertableExceptionHandler();
File file = new File("target/tests/DirectoryWatcherTest/existing");
MoreFiles.deleteRecursively(file.getParentFile().toPath());
Expand All @@ -178,8 +176,7 @@ public void testExistingFilesDirectoryWatcher() throws Throwable {
}
}

@Test
public void testFilteredDirectoryWatcher() throws Throwable {
@Test public void testFilteredDirectoryWatcher() throws Throwable {
assertableExceptionHandler = new AssertableExceptionHandler();
final File dir = new File("target/tests/DirectoryWatcherTest/some/sub/directory");
dir.mkdirs();
Expand Down Expand Up @@ -209,8 +206,7 @@ public void testFilteredDirectoryWatcher() throws Throwable {
}
}

@Test(expected = AssertionError.class)
public void testDirectoryWatcherListenerExceptionPropagation() throws Throwable {
@Test(expected = AssertionError.class) public void testDirectoryWatcherListenerExceptionPropagation() throws Throwable {
assertableExceptionHandler = new AssertableExceptionHandler();
final File testFile = new File("target/tests/DirectoryWatcherTest/someFile");
testFile.delete();
Expand All @@ -225,6 +221,29 @@ public void testDirectoryWatcherListenerExceptionPropagation() throws Throwable
}
}

@Test
public void testOverwriteExistingFile() throws Throwable {
assertableExceptionHandler = new AssertableExceptionHandler();
final File dir = new File("target/tests/DirectoryWatcherTest/existing");
dir.mkdirs();
final File file = File.createTempFile("test", "txt");
final File to = dir.toPath().resolve("test.txt").toFile();
Files.copy(file, to);
AtomicReference<ChangeKind> change = new AtomicReference<>();

try (DirectoryWatcher dw = new DirectoryWatcherBuilder().path(dir).listener((p, c) -> {
System.out.println("c = " + c);
change.set(c);
}).exceptionHandler(assertableExceptionHandler).build()) {
// We want it to call the listener once for setup, even without any change
assertableExceptionHandler.assertNoErrorInTheBackgroundThread();
java.nio.file.Files.move(file.toPath(), to.toPath(), REPLACE_EXISTING);

// Files.move(file, to);
await().atMost(5, SECONDS).until(change::get, is(ChangeKind.CREATED));
}
}

@Test
public void testFileWatcherWithSelectedChangeKinds() throws Throwable {
assertableExceptionHandler = new AssertableExceptionHandler();
Expand Down