From 1f4dc77715545e49cc58ea6cddd56a8cadb2e9ac Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 30 Sep 2015 10:54:51 +0100 Subject: [PATCH] Improve error message when a non-directory is added to FileSystemWatcher MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, if a file or non-existent directory was added to FileSystemWatcher, it would fail with the message “Folder must not be a file”. While it suggests that the folder needs to be a directory, it doesn’t make it clear that it also needs to exist. It also doesn’t tell the user which folder caused the problem. This commit updates the message to make it clear that the folder must exist and must be a directory, and the include the name of the problematic folder in the error message. Closes gh-3918 --- .../devtools/filewatch/FileSystemWatcher.java | 3 ++- .../filewatch/FileSystemWatcherTests.java | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java index 7531e4ee04..0f46a0c0e9 100644 --- a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java +++ b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java @@ -114,7 +114,8 @@ public class FileSystemWatcher { */ public synchronized void addSourceFolder(File folder) { Assert.notNull(folder, "Folder must not be null"); - Assert.isTrue(folder.isDirectory(), "Folder must not be a file"); + Assert.isTrue(folder.isDirectory(), "Folder '" + folder + "' must exist and must" + + " be a directory"); checkNotStarted(); this.folders.put(folder, null); } diff --git a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java index 1af417ffad..37e799e59f 100644 --- a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java +++ b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java @@ -38,6 +38,7 @@ import org.springframework.util.FileCopyUtils; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; @@ -107,6 +108,25 @@ public class FileSystemWatcherTests { this.watcher.addSourceFolder(null); } + @Test + public void sourceFolderMustExist() throws Exception { + File folder = new File("does/not/exist"); + assertThat(folder.exists(), is(false)); + this.thrown.expect(IllegalArgumentException.class); + this.thrown + .expectMessage("Folder 'does/not/exist' must exist and must be a directory"); + this.watcher.addSourceFolder(folder); + } + + @Test + public void sourceFolderMustBeADirectory() throws Exception { + File folder = new File("pom.xml"); + assertThat(folder.isFile(), is(true)); + this.thrown.expect(IllegalArgumentException.class); + this.thrown.expectMessage("Folder 'pom.xml' must exist and must be a directory"); + this.watcher.addSourceFolder(new File("pom.xml")); + } + @Test public void cannotAddSourceFolderToStartedListener() throws Exception { this.thrown.expect(IllegalStateException.class);