Improve error message when a non-directory is added to FileSystemWatcher

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
This commit is contained in:
Andy Wilkinson 2015-09-30 10:54:51 +01:00
parent 968caf05b6
commit 1f4dc77715
2 changed files with 22 additions and 1 deletions

View File

@ -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);
}

View File

@ -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);