From 032d5488cd790ac9d895a3d85cfa61d8373cc03b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 2 Jul 2018 16:25:10 +0100 Subject: [PATCH] Tolerate non-existent source folders in DevTools Closes gh-13620 --- .../devtools/filewatch/FileSystemWatcher.java | 3 +- .../devtools/filewatch/FolderSnapshot.java | 2 +- .../filewatch/FileSystemWatcherTests.java | 29 ++++++++++--------- .../filewatch/FolderSnapshotTests.java | 14 +++++++-- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java index 16606ee09b..d25edc4f99 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FileSystemWatcher.java @@ -125,8 +125,7 @@ public class FileSystemWatcher { */ public void addSourceFolder(File folder) { Assert.notNull(folder, "Folder must not be null"); - Assert.isTrue(folder.isDirectory(), - "Folder '" + folder + "' must exist and must" + " be a directory"); + Assert.isTrue(!folder.isFile(), "Folder '" + folder + "' must not be a file"); synchronized (this.monitor) { checkNotStarted(); this.folders.put(folder, null); diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FolderSnapshot.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FolderSnapshot.java index 688ae2944e..434288b65d 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FolderSnapshot.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/filewatch/FolderSnapshot.java @@ -52,7 +52,7 @@ class FolderSnapshot { */ FolderSnapshot(File folder) { Assert.notNull(folder, "Folder must not be null"); - Assert.isTrue(folder.isDirectory(), "Folder must not be a file"); + Assert.isTrue(!folder.isFile(), "Folder '" + folder + "' must not be a file"); this.folder = folder; this.time = new Date(); Set files = new LinkedHashSet<>(); diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java index 0f5a6d9e81..a084d6680a 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -106,21 +106,11 @@ public class FileSystemWatcherTests { } @Test - public void sourceFolderMustExist() { - File folder = new File("does/not/exist"); - assertThat(folder.exists()).isFalse(); - this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage( - "Folder '" + folder + "' must exist and must be a directory"); - this.watcher.addSourceFolder(folder); - } - - @Test - public void sourceFolderMustBeADirectory() { + public void sourceFolderMustNotBeAFile() { File folder = new File("pom.xml"); assertThat(folder.isFile()).isTrue(); this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Folder 'pom.xml' must exist and must be a directory"); + this.thrown.expectMessage("Folder 'pom.xml' must not be a file"); this.watcher.addSourceFolder(new File("pom.xml")); } @@ -152,6 +142,19 @@ public class FileSystemWatcherTests { assertThat(changedFiles.getFiles()).contains(expected); } + @Test + public void createSourceFolderAndAddFile() throws IOException { + File folder = new File(this.temp.getRoot(), "does/not/exist"); + assertThat(folder.exists()).isFalse(); + this.watcher.addSourceFolder(folder); + this.watcher.start(); + folder.mkdirs(); + touch(new File(folder, "text.txt")); + this.watcher.stopAfter(1); + ChangedFiles changedFiles = getSingleChangedFiles(); + System.out.println(changedFiles); + } + @Test public void waitsForPollingInterval() throws Exception { setupWatcher(10, 1); diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FolderSnapshotTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FolderSnapshotTests.java index 1a20ea3613..269c7a153e 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FolderSnapshotTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FolderSnapshotTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,9 +62,17 @@ public class FolderSnapshotTests { @Test public void folderMustNotBeFile() throws Exception { + File file = this.temporaryFolder.newFile(); this.thrown.expect(IllegalArgumentException.class); - this.thrown.expectMessage("Folder must not be a file"); - new FolderSnapshot(this.temporaryFolder.newFile()); + this.thrown.expectMessage("Folder '" + file + "' must not be a file"); + new FolderSnapshot(file); + } + + @Test + public void folderDoesNotHaveToExist() throws Exception { + File file = new File(this.temporaryFolder.getRoot(), "does/not/exist"); + FolderSnapshot snapshot = new FolderSnapshot(file); + assertThat(snapshot).isEqualTo(new FolderSnapshot(file)); } @Test