@Test public void multipleListeners() throws Exception { File folder = this.temp.newFolder(); final Set<ChangedFiles> listener2Changes = new LinkedHashSet<ChangedFiles>(); this.watcher.addSourceFolder(folder); this.watcher.addListener(new FileChangeListener() { @Override public void onChange(Set<ChangedFiles> changeSet) { listener2Changes.addAll(changeSet); } }); this.watcher.start(); File file = touch(new File(folder, "test.txt")); this.watcher.stopAfter(1); ChangedFiles changedFiles = getSingleChangedFiles(); ChangedFile expected = new ChangedFile(folder, file, Type.ADD); assertThat(changedFiles.getFiles()).contains(expected); assertThat(listener2Changes).isEqualTo(this.changes.get(0)); }
@Test public void modifyDeleteAndAdd() throws Exception { File folder = this.temp.newFolder(); File modify = touch(new File(folder, "modify.txt")); File delete = touch(new File(folder, "delete.txt")); this.watcher.addSourceFolder(folder); this.watcher.start(); FileCopyUtils.copy("abc".getBytes(), modify); delete.delete(); File add = touch(new File(folder, "add.txt")); this.watcher.stopAfter(1); ChangedFiles changedFiles = getSingleChangedFiles(); Set<ChangedFile> actual = changedFiles.getFiles(); Set<ChangedFile> expected = new HashSet<ChangedFile>(); expected.add(new ChangedFile(folder, modify, Type.MODIFY)); expected.add(new ChangedFile(folder, delete, Type.DELETE)); expected.add(new ChangedFile(folder, add, Type.ADD)); assertThat(actual).isEqualTo(expected); }
@Test public void getChangedFilesWhenAFileIsAddedAndDeletedAndChanged() throws Exception { File folder1 = new File(this.folder, "folder1"); File file1 = new File(folder1, "file1"); File file2 = new File(folder1, "file2"); File newFile = new File(folder1, "newfile"); FileCopyUtils.copy("updatedcontent".getBytes(), file1); file2.delete(); newFile.createNewFile(); FolderSnapshot updatedSnapshot = new FolderSnapshot(this.folder); ChangedFiles changedFiles = this.initialSnapshot.getChangedFiles(updatedSnapshot, null); assertThat(changedFiles.getSourceFolder()).isEqualTo(this.folder); assertThat(getChangedFile(changedFiles, file1).getType()).isEqualTo(Type.MODIFY); assertThat(getChangedFile(changedFiles, file2).getType()).isEqualTo(Type.DELETE); assertThat(getChangedFile(changedFiles, newFile).getType()).isEqualTo(Type.ADD); }
@Test public void multipleListeners() throws Exception { File folder = this.temp.newFolder(); final Set<ChangedFiles> listener2Changes = new LinkedHashSet<ChangedFiles>(); this.watcher.addSourceFolder(folder); this.watcher.addListener(new FileChangeListener() { @Override public void onChange(Set<ChangedFiles> changeSet) { listener2Changes.addAll(changeSet); } }); this.watcher.start(); File file = touch(new File(folder, "test.txt")); this.watcher.stopAfter(1); ChangedFiles changedFiles = getSingleChangedFiles(); ChangedFile expected = new ChangedFile(folder, file, Type.ADD); assertThat(changedFiles.getFiles(), contains(expected)); assertEquals(this.changes.get(0), listener2Changes); }
@Test public void modifyDeleteAndAdd() throws Exception { File folder = this.temp.newFolder(); File modify = touch(new File(folder, "modify.txt")); File delete = touch(new File(folder, "delete.txt")); this.watcher.addSourceFolder(folder); this.watcher.start(); FileCopyUtils.copy("abc".getBytes(), modify); delete.delete(); File add = touch(new File(folder, "add.txt")); this.watcher.stopAfter(1); ChangedFiles changedFiles = getSingleChangedFiles(); Set<ChangedFile> actual = changedFiles.getFiles(); Set<ChangedFile> expected = new HashSet<ChangedFile>(); expected.add(new ChangedFile(folder, modify, Type.MODIFY)); expected.add(new ChangedFile(folder, delete, Type.DELETE)); expected.add(new ChangedFile(folder, add, Type.ADD)); assertEquals(expected, actual); }
@Test public void getChangedFilesWhenAFileIsAddedAndDeletedAndChanged() throws Exception { File folder1 = new File(this.folder, "folder1"); File file1 = new File(folder1, "file1"); File file2 = new File(folder1, "file2"); File newFile = new File(folder1, "newfile"); FileCopyUtils.copy("updatedcontent".getBytes(), file1); file2.delete(); newFile.createNewFile(); FolderSnapshot updatedSnapshot = new FolderSnapshot(this.folder); 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)); assertThat(getChangedFile(changedFiles, newFile).getType(), equalTo(Type.ADD)); }
@Test public void addFile() throws Exception { File folder = startWithNewFolder(); File file = touch(new File(folder, "test.txt")); this.watcher.stopAfter(1); ChangedFiles changedFiles = getSingleChangedFiles(); ChangedFile expected = new ChangedFile(folder, file, Type.ADD); assertThat(changedFiles.getFiles()).contains(expected); }
@Test public void addNestedFile() throws Exception { File folder = startWithNewFolder(); File file = touch(new File(new File(folder, "sub"), "text.txt")); this.watcher.stopAfter(1); ChangedFiles changedFiles = getSingleChangedFiles(); ChangedFile expected = new ChangedFile(folder, file, Type.ADD); assertThat(changedFiles.getFiles()).contains(expected); }
@Test public void withExistingFiles() throws Exception { File folder = this.temp.newFolder(); touch(new File(folder, "test.txt")); this.watcher.addSourceFolder(folder); this.watcher.start(); File file = touch(new File(folder, "test2.txt")); this.watcher.stopAfter(1); ChangedFiles changedFiles = getSingleChangedFiles(); ChangedFile expected = new ChangedFile(folder, file, Type.ADD); assertThat(changedFiles.getFiles()).contains(expected); }
@Test public void getRelativeName() throws Exception { File folder = this.temp.newFolder(); File subFolder = new File(folder, "A"); File file = new File(subFolder, "B.txt"); ChangedFile changedFile = new ChangedFile(folder, file, Type.ADD); assertThat(changedFile.getRelativeName()).isEqualTo("A/B.txt"); }
private ClassPathChangedEvent createClassPathChangedEvent(File sourceFolder) throws IOException { Set<ChangedFile> files = new LinkedHashSet<ChangedFile>(); File file1 = createFile(sourceFolder, "File1"); File file2 = createFile(sourceFolder, "File2"); File file3 = createFile(sourceFolder, "File3"); files.add(new ChangedFile(sourceFolder, file1, Type.ADD)); files.add(new ChangedFile(sourceFolder, file2, Type.MODIFY)); files.add(new ChangedFile(sourceFolder, file3, Type.DELETE)); Set<ChangedFiles> changeSet = new LinkedHashSet<ChangedFiles>(); changeSet.add(new ChangedFiles(sourceFolder, files)); ClassPathChangedEvent event = new ClassPathChangedEvent(this, changeSet, false); return event; }
@Test public void sendsClassLoaderFiles() throws Exception { File sourceFolder = this.temp.newFolder(); Set<ChangedFile> files = new LinkedHashSet<ChangedFile>(); File file1 = createFile(sourceFolder, "File1"); File file2 = createFile(sourceFolder, "File2"); File file3 = createFile(sourceFolder, "File3"); files.add(new ChangedFile(sourceFolder, file1, Type.ADD)); files.add(new ChangedFile(sourceFolder, file2, Type.MODIFY)); files.add(new ChangedFile(sourceFolder, file3, Type.DELETE)); Set<ChangedFiles> changeSet = new LinkedHashSet<ChangedFiles>(); changeSet.add(new ChangedFiles(sourceFolder, files)); ClassPathChangedEvent event = new ClassPathChangedEvent(this, changeSet, false); this.requestFactory.willRespond(HttpStatus.OK); this.uploader.onApplicationEvent(event); MockClientHttpRequest request = this.requestFactory.getExecutedRequests().get(0); ClassLoaderFiles classLoaderFiles = deserialize(request.getBodyAsBytes()); Collection<SourceFolder> sourceFolders = classLoaderFiles.getSourceFolders(); assertThat(sourceFolders.size()).isEqualTo(1); SourceFolder classSourceFolder = sourceFolders.iterator().next(); assertThat(classSourceFolder.getName()).isEqualTo(sourceFolder.getAbsolutePath()); Iterator<ClassLoaderFile> classFiles = classSourceFolder.getFiles().iterator(); assertClassFile(classFiles.next(), "File1", ClassLoaderFile.Kind.ADDED); assertClassFile(classFiles.next(), "File2", ClassLoaderFile.Kind.MODIFIED); assertClassFile(classFiles.next(), null, ClassLoaderFile.Kind.DELETED); assertThat(classFiles.hasNext()).isFalse(); }
@Test public void addFile() throws Exception { File folder = startWithNewFolder(); File file = touch(new File(folder, "test.txt")); this.watcher.stopAfter(1); ChangedFiles changedFiles = getSingleChangedFiles(); ChangedFile expected = new ChangedFile(folder, file, Type.ADD); assertThat(changedFiles.getFiles(), contains(expected)); }
@Test public void addNestedFile() throws Exception { File folder = startWithNewFolder(); File file = touch(new File(new File(folder, "sub"), "text.txt")); this.watcher.stopAfter(1); ChangedFiles changedFiles = getSingleChangedFiles(); ChangedFile expected = new ChangedFile(folder, file, Type.ADD); assertThat(changedFiles.getFiles(), contains(expected)); }
@Test public void withExistingFiles() throws Exception { File folder = this.temp.newFolder(); touch(new File(folder, "test.txt")); this.watcher.addSourceFolder(folder); this.watcher.start(); File file = touch(new File(folder, "test2.txt")); this.watcher.stopAfter(1); ChangedFiles changedFiles = getSingleChangedFiles(); ChangedFile expected = new ChangedFile(folder, file, Type.ADD); assertThat(changedFiles.getFiles(), contains(expected)); }
@Test public void getRelativeName() throws Exception { File folder = this.temp.newFolder(); File subFolder = new File(folder, "A"); File file = new File(subFolder, "B.txt"); ChangedFile changedFile = new ChangedFile(folder, file, Type.ADD); assertThat(changedFile.getRelativeName(), equalTo("A/B.txt")); }
@Test public void sendsClassLoaderFiles() throws Exception { File sourceFolder = this.temp.newFolder(); Set<ChangedFile> files = new LinkedHashSet<ChangedFile>(); File file1 = createFile(sourceFolder, "File1"); File file2 = createFile(sourceFolder, "File2"); File file3 = createFile(sourceFolder, "File3"); files.add(new ChangedFile(sourceFolder, file1, Type.ADD)); files.add(new ChangedFile(sourceFolder, file2, Type.MODIFY)); files.add(new ChangedFile(sourceFolder, file3, Type.DELETE)); Set<ChangedFiles> changeSet = new LinkedHashSet<ChangedFiles>(); changeSet.add(new ChangedFiles(sourceFolder, files)); ClassPathChangedEvent event = new ClassPathChangedEvent(this, changeSet, false); this.requestFactory.willRespond(HttpStatus.OK); this.uploader.onApplicationEvent(event); MockClientHttpRequest request = this.requestFactory.getExecutedRequests().get(0); ClassLoaderFiles classLoaderFiles = deserialize(request.getBodyAsBytes()); Collection<SourceFolder> sourceFolders = classLoaderFiles.getSourceFolders(); assertThat(sourceFolders.size(), equalTo(1)); SourceFolder classSourceFolder = sourceFolders.iterator().next(); assertThat(classSourceFolder.getName(), equalTo(sourceFolder.getAbsolutePath())); Iterator<ClassLoaderFile> classFiles = classSourceFolder.getFiles().iterator(); assertClassFile(classFiles.next(), "File1", ClassLoaderFile.Kind.ADDED); assertClassFile(classFiles.next(), "File2", ClassLoaderFile.Kind.MODIFIED); assertClassFile(classFiles.next(), null, ClassLoaderFile.Kind.DELETED); assertThat(classFiles.hasNext(), equalTo(false)); }
@Test public void sourceFolderMustNotBeNull() throws Exception { this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("SourceFolder must not be null"); new ChangedFile(null, this.temp.newFile(), Type.ADD); }
@Test public void fileMustNotBeNull() throws Exception { this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("File must not be null"); new ChangedFile(this.temp.newFolder(), null, Type.ADD); }
@Test public void getFile() throws Exception { File file = this.temp.newFile(); ChangedFile changedFile = new ChangedFile(this.temp.newFolder(), file, Type.ADD); assertThat(changedFile.getFile()).isEqualTo(file); }
@Test public void getType() throws Exception { ChangedFile changedFile = new ChangedFile(this.temp.newFolder(), this.temp.newFile(), Type.DELETE); assertThat(changedFile.getType()).isEqualTo(Type.DELETE); }
private ChangedFile mockFile(String relativeName) { return new ChangedFile(new File("."), new File("./" + relativeName), Type.ADD); }
@Test public void getFile() throws Exception { File file = this.temp.newFile(); ChangedFile changedFile = new ChangedFile(this.temp.newFolder(), file, Type.ADD); assertThat(changedFile.getFile(), equalTo(file)); }
@Test public void getType() throws Exception { ChangedFile changedFile = new ChangedFile(this.temp.newFolder(), this.temp.newFile(), Type.DELETE); assertThat(changedFile.getType(), equalTo(Type.DELETE)); }