Search in sources :

Example 1 with ClassLoaderFile

use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFile in project spring-boot by spring-projects.

the class ClassPathChangeUploaderTests method verifyUploadRequest.

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();
}
Also used : SourceFolder(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder) ClassLoaderFiles(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile)

Example 2 with ClassLoaderFile

use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFile in project spring-boot by spring-projects.

the class RestartServerTests method updateAndRestart.

@Test
public void updateAndRestart() throws Exception {
    URL url1 = new URL("file:/proj/module-a.jar!/");
    URL url2 = new URL("file:/proj/module-b.jar!/");
    URL url3 = new URL("file:/proj/module-c.jar!/");
    URL url4 = new URL("file:/proj/module-d.jar!/");
    URLClassLoader classLoaderA = new URLClassLoader(new URL[] { url1, url2 });
    URLClassLoader classLoaderB = new URLClassLoader(new URL[] { url3, url4 }, classLoaderA);
    SourceFolderUrlFilter filter = new DefaultSourceFolderUrlFilter();
    MockRestartServer server = new MockRestartServer(filter, classLoaderB);
    ClassLoaderFiles files = new ClassLoaderFiles();
    ClassLoaderFile fileA = new ClassLoaderFile(Kind.ADDED, new byte[0]);
    ClassLoaderFile fileB = new ClassLoaderFile(Kind.ADDED, new byte[0]);
    files.addFile("my/module-a", "ClassA.class", fileA);
    files.addFile("my/module-c", "ClassB.class", fileB);
    server.updateAndRestart(files);
    Set<URL> expectedUrls = new LinkedHashSet<>(Arrays.asList(url1, url3));
    assertThat(server.restartUrls).isEqualTo(expectedUrls);
    assertThat(server.restartFiles).isEqualTo(files);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ClassLoaderFiles(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile) Test(org.junit.Test)

Example 3 with ClassLoaderFile

use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFile in project spring-boot by spring-projects.

the class RestartServerTests method updateReplacesLocalFilesWhenPossible.

@Test
public void updateReplacesLocalFilesWhenPossible() throws Exception {
    // This is critical for Cloud Foundry support where the application is
    // run exploded and resources can be found from the servlet root (outside of the
    // classloader)
    File folder = this.temp.newFolder();
    File classFile = new File(folder, "ClassA.class");
    FileCopyUtils.copy("abc".getBytes(), classFile);
    URL url = folder.toURI().toURL();
    URLClassLoader classLoader = new URLClassLoader(new URL[] { url });
    SourceFolderUrlFilter filter = new DefaultSourceFolderUrlFilter();
    MockRestartServer server = new MockRestartServer(filter, classLoader);
    ClassLoaderFiles files = new ClassLoaderFiles();
    ClassLoaderFile fileA = new ClassLoaderFile(Kind.ADDED, "def".getBytes());
    files.addFile("my/module-a", "ClassA.class", fileA);
    server.updateAndRestart(files);
    assertThat(FileCopyUtils.copyToByteArray(classFile)).isEqualTo("def".getBytes());
}
Also used : ClassLoaderFiles(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles) URLClassLoader(java.net.URLClassLoader) File(java.io.File) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile) URL(java.net.URL) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile) Test(org.junit.Test)

Example 4 with ClassLoaderFile

use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFile in project spring-boot by spring-projects.

the class RestartServerTests method updateSetsJarLastModified.

@Test
public void updateSetsJarLastModified() throws Exception {
    long startTime = System.currentTimeMillis();
    File folder = this.temp.newFolder();
    File jarFile = new File(folder, "module-a.jar");
    new FileOutputStream(jarFile).close();
    jarFile.setLastModified(0);
    URL url = jarFile.toURI().toURL();
    URLClassLoader classLoader = new URLClassLoader(new URL[] { url });
    SourceFolderUrlFilter filter = new DefaultSourceFolderUrlFilter();
    MockRestartServer server = new MockRestartServer(filter, classLoader);
    ClassLoaderFiles files = new ClassLoaderFiles();
    ClassLoaderFile fileA = new ClassLoaderFile(Kind.ADDED, new byte[0]);
    files.addFile("my/module-a", "ClassA.class", fileA);
    server.updateAndRestart(files);
    assertThat(jarFile.lastModified()).isGreaterThan(startTime - 1000);
}
Also used : ClassLoaderFiles(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles) FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) File(java.io.File) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile) URL(java.net.URL) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile) Test(org.junit.Test)

Example 5 with ClassLoaderFile

use of org.springframework.boot.devtools.restart.classloader.ClassLoaderFile in project spring-boot by spring-projects.

the class RestarterTests method addClassLoaderFiles.

@Test
public void addClassLoaderFiles() throws Exception {
    ClassLoaderFiles classLoaderFiles = new ClassLoaderFiles();
    classLoaderFiles.addFile("f", new ClassLoaderFile(Kind.ADDED, "abc".getBytes()));
    Restarter restarter = Restarter.getInstance();
    restarter.addClassLoaderFiles(classLoaderFiles);
    restarter.restart();
    ClassLoader classLoader = ((TestableRestarter) restarter).getRelaunchClassLoader();
    assertThat(FileCopyUtils.copyToByteArray(classLoader.getResourceAsStream("f"))).isEqualTo("abc".getBytes());
}
Also used : ClassLoaderFiles(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles) URLClassLoader(java.net.URLClassLoader) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile) Test(org.junit.Test)

Aggregations

ClassLoaderFile (org.springframework.boot.devtools.restart.classloader.ClassLoaderFile)13 Test (org.junit.Test)7 ClassLoaderFiles (org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles)6 File (java.io.File)5 URL (java.net.URL)5 URLClassLoader (java.net.URLClassLoader)4 SourceFolder (org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder)4 Resource (org.springframework.core.io.Resource)3 ServletContextResource (org.springframework.web.context.support.ServletContextResource)3 IOException (java.io.IOException)2 LinkedHashSet (java.util.LinkedHashSet)2 DeletedClassLoaderFileResource (org.springframework.boot.devtools.restart.ClassLoaderFilesResourcePatternResolver.DeletedClassLoaderFileResource)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 Kind (org.springframework.boot.devtools.restart.classloader.ClassLoaderFile.Kind)1 ClassLoaderFileURLStreamHandler (org.springframework.boot.devtools.restart.classloader.ClassLoaderFileURLStreamHandler)1 AbstractResource (org.springframework.core.io.AbstractResource)1 UrlResource (org.springframework.core.io.UrlResource)1 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)1