/** * Update the current running application with the specified {@link ClassLoaderFiles} * and trigger a reload. * @param files updated class loader files */ public void updateAndRestart(ClassLoaderFiles files) { Set<URL> urls = new LinkedHashSet<URL>(); Set<URL> classLoaderUrls = getClassLoaderUrls(); for (SourceFolder folder : files.getSourceFolders()) { for (Entry<String, ClassLoaderFile> entry : folder.getFilesEntrySet()) { for (URL url : classLoaderUrls) { if (updateFileSystem(url, entry.getKey(), entry.getValue())) { urls.add(url); } } } urls.addAll(getMatchingUrls(classLoaderUrls, folder.getName())); } updateTimeStamp(urls); restart(urls, files); }
@Test public void getSourceFolders() throws Exception { ClassLoaderFile file1 = new ClassLoaderFile(Kind.ADDED, new byte[10]); ClassLoaderFile file2 = new ClassLoaderFile(Kind.MODIFIED, new byte[10]); ClassLoaderFile file3 = new ClassLoaderFile(Kind.MODIFIED, new byte[10]); ClassLoaderFile file4 = new ClassLoaderFile(Kind.MODIFIED, new byte[10]); this.files.addFile("a", "myfile1", file1); this.files.addFile("a", "myfile2", file2); this.files.addFile("b", "myfile3", file3); this.files.addFile("b", "myfile4", file4); Iterator<SourceFolder> sourceFolders = this.files.getSourceFolders().iterator(); SourceFolder sourceFolder1 = sourceFolders.next(); SourceFolder sourceFolder2 = sourceFolders.next(); assertThat(sourceFolders.hasNext()).isFalse(); assertThat(sourceFolder1.getName()).isEqualTo("a"); assertThat(sourceFolder2.getName()).isEqualTo("b"); assertThat(sourceFolder1.getFiles()).containsOnly(file1, file2); assertThat(sourceFolder2.getFiles()).containsOnly(file3, file4); }
@Test public void addAll() throws Exception { ClassLoaderFile file1 = new ClassLoaderFile(Kind.ADDED, new byte[10]); this.files.addFile("a", "myfile1", file1); ClassLoaderFiles toAdd = new ClassLoaderFiles(); ClassLoaderFile file2 = new ClassLoaderFile(Kind.MODIFIED, new byte[10]); ClassLoaderFile file3 = new ClassLoaderFile(Kind.MODIFIED, new byte[10]); toAdd.addFile("a", "myfile2", file2); toAdd.addFile("b", "myfile3", file3); this.files.addAll(toAdd); Iterator<SourceFolder> sourceFolders = this.files.getSourceFolders().iterator(); SourceFolder sourceFolder1 = sourceFolders.next(); SourceFolder sourceFolder2 = sourceFolders.next(); assertThat(sourceFolders.hasNext()).isFalse(); assertThat(sourceFolder1.getName()).isEqualTo("a"); assertThat(sourceFolder2.getName()).isEqualTo("b"); assertThat(sourceFolder1.getFiles()).containsOnly(file1, file2); }
@Test public void getSourceFolders() throws Exception { ClassLoaderFile file1 = new ClassLoaderFile(Kind.ADDED, new byte[10]); ClassLoaderFile file2 = new ClassLoaderFile(Kind.MODIFIED, new byte[10]); ClassLoaderFile file3 = new ClassLoaderFile(Kind.MODIFIED, new byte[10]); ClassLoaderFile file4 = new ClassLoaderFile(Kind.MODIFIED, new byte[10]); this.files.addFile("a", "myfile1", file1); this.files.addFile("a", "myfile2", file2); this.files.addFile("b", "myfile3", file3); this.files.addFile("b", "myfile4", file4); Iterator<SourceFolder> sourceFolders = this.files.getSourceFolders().iterator(); SourceFolder sourceFolder1 = sourceFolders.next(); SourceFolder sourceFolder2 = sourceFolders.next(); assertThat(sourceFolders.hasNext(), equalTo(false)); assertThat(sourceFolder1.getName(), equalTo("a")); assertThat(sourceFolder2.getName(), equalTo("b")); assertThat(new ArrayList<ClassLoaderFile>(sourceFolder1.getFiles()), equalTo(Arrays.asList(file1, file2))); assertThat(new ArrayList<ClassLoaderFile>(sourceFolder2.getFiles()), equalTo(Arrays.asList(file3, file4))); }
@Test public void addAll() throws Exception { ClassLoaderFile file1 = new ClassLoaderFile(Kind.ADDED, new byte[10]); this.files.addFile("a", "myfile1", file1); ClassLoaderFiles toAdd = new ClassLoaderFiles(); ClassLoaderFile file2 = new ClassLoaderFile(Kind.MODIFIED, new byte[10]); ClassLoaderFile file3 = new ClassLoaderFile(Kind.MODIFIED, new byte[10]); toAdd.addFile("a", "myfile2", file2); toAdd.addFile("b", "myfile3", file3); this.files.addAll(toAdd); Iterator<SourceFolder> sourceFolders = this.files.getSourceFolders().iterator(); SourceFolder sourceFolder1 = sourceFolders.next(); SourceFolder sourceFolder2 = sourceFolders.next(); assertThat(sourceFolders.hasNext(), equalTo(false)); assertThat(sourceFolder1.getName(), equalTo("a")); assertThat(sourceFolder2.getName(), equalTo("b")); assertThat(new ArrayList<ClassLoaderFile>(sourceFolder1.getFiles()), equalTo(Arrays.asList(file1, file2))); }
private void verifyUploadRequest(File sourceFolder, MockClientHttpRequest request) throws IOException, ClassNotFoundException { 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 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 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)); }