Allow reload to use a trigger file

Update `FileSystemWatcher` to support the concept of a "trigger file"
which could be written by an IDE when a reload needs to occur.

Fixes gh-3157
This commit is contained in:
Phillip Webb
2015-06-08 22:07:22 -07:00
parent 196b9c9b2a
commit 7bcd6567ba
11 changed files with 280 additions and 44 deletions

View File

@@ -30,6 +30,7 @@ import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfigurati
import org.springframework.boot.devtools.classpath.ClassPathChangedEvent;
import org.springframework.boot.devtools.classpath.ClassPathFileSystemWatcher;
import org.springframework.boot.devtools.filewatch.ChangedFiles;
import org.springframework.boot.devtools.filewatch.FileSystemWatcher;
import org.springframework.boot.devtools.livereload.LiveReloadServer;
import org.springframework.boot.devtools.restart.MockRestartInitializer;
import org.springframework.boot.devtools.restart.MockRestarter;
@@ -39,10 +40,12 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.SocketUtils;
import org.thymeleaf.templateresolver.TemplateResolver;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
@@ -164,6 +167,16 @@ public class LocalDevToolsAutoConfigurationTests {
this.context.getBean(ClassPathFileSystemWatcher.class);
}
@Test
public void restartWithTriggerFile() throws Exception {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("spring.devtools.restart.trigger-file", "somefile.txt");
this.context = initializeAndRun(Config.class, properties);
FileSystemWatcher watcher = this.context.getBean(FileSystemWatcher.class);
Object filter = ReflectionTestUtils.getField(watcher, "triggerFilter");
assertThat(filter, instanceOf(TriggerFileFilter.class));
}
private ConfigurableApplicationContext initializeAndRun(Class<?> config) {
return initializeAndRun(config, Collections.<String, Object> emptyMap());
}
@@ -188,15 +201,13 @@ public class LocalDevToolsAutoConfigurationTests {
}
@Configuration
@Import({ LocalDevToolsAutoConfiguration.class,
ThymeleafAutoConfiguration.class })
@Import({ LocalDevToolsAutoConfiguration.class, ThymeleafAutoConfiguration.class })
public static class Config {
}
@Configuration
@Import({ LocalDevToolsAutoConfiguration.class,
ThymeleafAutoConfiguration.class })
@Import({ LocalDevToolsAutoConfiguration.class, ThymeleafAutoConfiguration.class })
public static class ConfigWithMockLiveReload {
@Bean

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2012-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.devtools.autoconfigure;
import java.io.File;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link TriggerFileFilter}.
*
* @author Phillip Webb
*/
public class TriggerFileFilterTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@Test
public void nameMustNotBeNull() throws Exception {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Name must not be null");
new TriggerFileFilter(null);
}
@Test
public void acceptNameMatch() throws Exception {
File file = this.temp.newFile("thefile.txt");
assertThat(new TriggerFileFilter("thefile.txt").accept(file), equalTo(true));
}
@Test
public void doesNotAcceptNameMismatch() throws Exception {
File file = this.temp.newFile("notthefile.txt");
assertThat(new TriggerFileFilter("thefile.txt").accept(file), equalTo(false));
}
}

View File

@@ -17,6 +17,7 @@
package org.springframework.boot.devtools.filewatch;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -32,10 +33,6 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.devtools.filewatch.ChangedFile;
import org.springframework.boot.devtools.filewatch.ChangedFiles;
import org.springframework.boot.devtools.filewatch.FileChangeListener;
import org.springframework.boot.devtools.filewatch.FileSystemWatcher;
import org.springframework.boot.devtools.filewatch.ChangedFile.Type;
import org.springframework.util.FileCopyUtils;
@@ -221,6 +218,33 @@ public class FileSystemWatcherTests {
assertEquals(expected, actual);
}
@Test
public void withTriggerFilter() throws Exception {
File folder = this.temp.newFolder();
File file = touch(new File(folder, "file.txt"));
File trigger = touch(new File(folder, "trigger.txt"));
this.watcher.addSourceFolder(folder);
this.watcher.setTriggerFilter(new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().equals("trigger.txt");
}
});
this.watcher.start();
FileCopyUtils.copy("abc".getBytes(), file);
Thread.sleep(100);
assertThat(this.changes.size(), equalTo(0));
FileCopyUtils.copy("abc".getBytes(), trigger);
this.watcher.stopAfter(1);
ChangedFiles changedFiles = getSingleChangedFiles();
Set<ChangedFile> actual = changedFiles.getFiles();
Set<ChangedFile> expected = new HashSet<ChangedFile>();
expected.add(new ChangedFile(folder, file, Type.MODIFY));
assertEquals(expected, actual);
}
private void setupWatcher(long idleTime, long quietTime) {
this.watcher = new FileSystemWatcher(false, idleTime, quietTime);
this.watcher.addListener(new FileChangeListener() {

View File

@@ -24,9 +24,6 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.devtools.filewatch.ChangedFile;
import org.springframework.boot.devtools.filewatch.ChangedFiles;
import org.springframework.boot.devtools.filewatch.FolderSnapshot;
import org.springframework.boot.devtools.filewatch.ChangedFile.Type;
import org.springframework.util.FileCopyUtils;
@@ -104,7 +101,7 @@ public class FolderSnapshotTests {
public void getChangedFilesSnapshotMustNotBeNull() throws Exception {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Snapshot must not be null");
this.initialSnapshot.getChangedFiles(null);
this.initialSnapshot.getChangedFiles(null, null);
}
@Test
@@ -112,13 +109,13 @@ public class FolderSnapshotTests {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Snapshot source folder must be '" + this.folder + "'");
this.initialSnapshot.getChangedFiles(new FolderSnapshot(
createTestFolderStructure()));
createTestFolderStructure()), null);
}
@Test
public void getChangedFilesWhenNothingHasChanged() throws Exception {
FolderSnapshot updatedSnapshot = new FolderSnapshot(this.folder);
this.initialSnapshot.getChangedFiles(updatedSnapshot);
this.initialSnapshot.getChangedFiles(updatedSnapshot, null);
}
@Test
@@ -131,7 +128,8 @@ public class FolderSnapshotTests {
file2.delete();
newFile.createNewFile();
FolderSnapshot updatedSnapshot = new FolderSnapshot(this.folder);
ChangedFiles changedFiles = this.initialSnapshot.getChangedFiles(updatedSnapshot);
ChangedFiles changedFiles = this.initialSnapshot.getChangedFiles(updatedSnapshot,
null);
assertThat(changedFiles.getSourceFolder(), equalTo(this.folder));
assertThat(getChangedFile(changedFiles, file1).getType(), equalTo(Type.MODIFY));
assertThat(getChangedFile(changedFiles, file2).getType(), equalTo(Type.DELETE));