Search in sources :

Example 1 with MavenProjectCache

use of org.springframework.ide.vscode.commons.maven.java.MavenProjectCache in project sts4 by spring-projects.

the class MavenProjectCacheTest method testPomFileChanges.

@Test
public void testPomFileChanges() throws Exception {
    MavenProjectCache cache = new MavenProjectCache(server, MavenCore.getDefault(), false, null);
    IJavaProject[] projectChanged = new IJavaProject[] { null };
    IJavaProject[] projectDeleted = new IJavaProject[] { null };
    cache.addListener(new Listener() {

        @Override
        public void created(IJavaProject project) {
        }

        @Override
        public void changed(IJavaProject project) {
            projectChanged[0] = project;
        }

        @Override
        public void deleted(IJavaProject project) {
            projectDeleted[0] = project;
        }
    });
    // Get the project from cache
    MavenJavaProject cachedProject = cache.project(pomFile);
    assertNotNull(cachedProject);
    ImmutableList<Path> calculatedClassPath = cachedProject.getClasspath().getClasspathEntries();
    assertEquals(48, calculatedClassPath.size());
    fileObserver.notifyFileChanged(pomFile.toURI().toString());
    assertNull(projectChanged[0]);
    writeContent(pomFile, new String(Files.readAllBytes(testProjectPath.resolve("pom.newxml")), Charset.defaultCharset()));
    fileObserver.notifyFileChanged(pomFile.toURI().toString());
    assertNotNull(projectChanged[0]);
    assertEquals(cachedProject, projectChanged[0]);
    calculatedClassPath = cachedProject.getClasspath().getClasspathEntries();
    assertEquals(49, calculatedClassPath.size());
    fileObserver.notifyFileDeleted(pomFile.toURI().toString());
    assertEquals(cachedProject, projectDeleted[0]);
}
Also used : Path(java.nio.file.Path) MavenProjectCache(org.springframework.ide.vscode.commons.maven.java.MavenProjectCache) IJavaProject(org.springframework.ide.vscode.commons.java.IJavaProject) Listener(org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver.Listener) MavenJavaProject(org.springframework.ide.vscode.commons.maven.java.MavenJavaProject) Test(org.junit.Test)

Example 2 with MavenProjectCache

use of org.springframework.ide.vscode.commons.maven.java.MavenProjectCache in project sts4 by spring-projects.

the class BootLanguageServerParams method createFallbackProjectCache.

private static JavaProjectsService createFallbackProjectCache(SimpleLanguageServer server) {
    CompositeJavaProjectFinder javaProjectFinder = new CompositeJavaProjectFinder();
    MavenProjectCache mavenProjectCache = new MavenProjectCache(server, MavenCore.getDefault(), true, Paths.get(IJavaProject.PROJECT_CACHE_FOLDER));
    javaProjectFinder.addJavaProjectFinder(new MavenProjectFinder(mavenProjectCache));
    GradleProjectCache gradleProjectCache = new GradleProjectCache(server, GradleCore.getDefault(), true, Paths.get(IJavaProject.PROJECT_CACHE_FOLDER));
    javaProjectFinder.addJavaProjectFinder(new GradleProjectFinder(gradleProjectCache));
    CompositeProjectOvserver projectObserver = new CompositeProjectOvserver(Arrays.asList(mavenProjectCache, gradleProjectCache));
    return new JavaProjectsService() {

        @Override
        public void removeListener(Listener listener) {
            projectObserver.removeListener(listener);
        }

        @Override
        public void addListener(Listener listener) {
            projectObserver.addListener(listener);
        }

        @Override
        public Optional<IJavaProject> find(TextDocumentIdentifier doc) {
            return javaProjectFinder.find(doc);
        }
    };
}
Also used : GradleProjectCache(org.springframework.ide.vscode.commons.gradle.GradleProjectCache) MavenProjectCache(org.springframework.ide.vscode.commons.maven.java.MavenProjectCache) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CompositeJavaProjectFinder(org.springframework.ide.vscode.commons.languageserver.java.CompositeJavaProjectFinder) Listener(org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver.Listener) IJavaProject(org.springframework.ide.vscode.commons.java.IJavaProject) CompositeProjectOvserver(org.springframework.ide.vscode.commons.languageserver.java.CompositeProjectOvserver) MavenProjectFinder(org.springframework.ide.vscode.commons.maven.java.MavenProjectFinder) JavaProjectsService(org.springframework.ide.vscode.boot.jdt.ls.JavaProjectsService) GradleProjectFinder(org.springframework.ide.vscode.commons.gradle.GradleProjectFinder)

Example 3 with MavenProjectCache

use of org.springframework.ide.vscode.commons.maven.java.MavenProjectCache in project sts4 by spring-projects.

the class MavenProjectCacheTest method testClasspathCaching.

@Test
public void testClasspathCaching() throws Exception {
    Path testProjectPath = Paths.get(DependencyTreeTest.class.getResource("/empty-boot-project-with-classpath-file").toURI());
    Path cacheFolder = testProjectPath.resolve(IJavaProject.PROJECT_CACHE_FOLDER);
    final File classpathCacheFile = cacheFolder.resolve(ClasspathFileBasedCache.CLASSPATH_DATA_CACHE_FILE).toFile();
    AtomicBoolean progressDone = new AtomicBoolean();
    ProgressService progressService = mock(ProgressService.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            progressDone.set(true);
            return null;
        }
    }).when(progressService).progressEvent(any(String.class), (String) isNull());
    when(server.getProgressService()).thenReturn(progressService);
    assertFalse(classpathCacheFile.exists());
    MavenProjectCache cache = new MavenProjectCache(server, MavenCore.getDefault(), true, cacheFolder);
    MavenJavaProject project = cache.project(pomFile);
    assertTrue(project.getClasspath().getClasspathEntries().isEmpty());
    CompletableFuture.runAsync(() -> {
        while (!progressDone.get()) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).get(30, TimeUnit.SECONDS);
    assertTrue(classpathCacheFile.exists());
    assertEquals(48, project.getClasspath().getClasspathEntries().size());
    progressDone.set(false);
    // Reset the cache
    cache = new MavenProjectCache(server, MavenCore.getDefault(), true, cacheFolder);
    // Check loaded from cache file
    project = cache.project(pomFile);
    assertEquals(48, project.getClasspath().getClasspathEntries().size());
}
Also used : Path(java.nio.file.Path) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MavenProjectCache(org.springframework.ide.vscode.commons.maven.java.MavenProjectCache) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MavenJavaProject(org.springframework.ide.vscode.commons.maven.java.MavenJavaProject) ProgressService(org.springframework.ide.vscode.commons.languageserver.ProgressService) File(java.io.File) Test(org.junit.Test)

Example 4 with MavenProjectCache

use of org.springframework.ide.vscode.commons.maven.java.MavenProjectCache in project sts4 by spring-projects.

the class MavenProjectCacheTest method testErrorLoadingProject.

@Test
public void testErrorLoadingProject() throws Exception {
    Path testProjectPath = Paths.get(DependencyTreeTest.class.getResource("/empty-boot-project-with-classpath-file").toURI());
    Path cacheFolder = testProjectPath.resolve(IJavaProject.PROJECT_CACHE_FOLDER);
    AtomicBoolean progressDone = new AtomicBoolean();
    ProgressService progressService = mock(ProgressService.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            progressDone.set(true);
            return null;
        }
    }).when(progressService).progressEvent(any(String.class), (String) isNull());
    when(server.getProgressService()).thenReturn(progressService);
    DiagnosticService diagnosticService = mock(DiagnosticService.class);
    when(server.getDiagnosticService()).thenReturn(diagnosticService);
    MavenProjectCache cache = new MavenProjectCache(server, MavenCore.getDefault(), true, cacheFolder);
    MavenJavaProject project = cache.project(pomFile);
    assertTrue(project.getClasspath().getClasspathEntries().isEmpty());
    CompletableFuture.runAsync(() -> {
        while (!progressDone.get()) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).get(30, TimeUnit.SECONDS);
    progressDone.set(false);
    verify(diagnosticService, never()).diagnosticEvent(any(ShowMessageException.class));
    writeContent(pomFile, "");
    fileObserver.notifyFileChanged(pomFile.toURI().toString());
    CompletableFuture.runAsync(() -> {
        while (!progressDone.get()) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).get(30, TimeUnit.SECONDS);
    progressDone.set(false);
    verify(diagnosticService, times(1)).diagnosticEvent(any(ShowMessageException.class));
    assertTrue(project.getClasspath().getClasspathEntries().isEmpty());
    assertFalse(cacheFolder.resolve(ClasspathFileBasedCache.CLASSPATH_DATA_CACHE_FILE).toFile().exists());
}
Also used : Path(java.nio.file.Path) ShowMessageException(org.springframework.ide.vscode.commons.languageserver.util.ShowMessageException) DiagnosticService(org.springframework.ide.vscode.commons.languageserver.DiagnosticService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MavenProjectCache(org.springframework.ide.vscode.commons.maven.java.MavenProjectCache) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MavenJavaProject(org.springframework.ide.vscode.commons.maven.java.MavenJavaProject) ProgressService(org.springframework.ide.vscode.commons.languageserver.ProgressService) Test(org.junit.Test)

Aggregations

MavenProjectCache (org.springframework.ide.vscode.commons.maven.java.MavenProjectCache)4 Path (java.nio.file.Path)3 Test (org.junit.Test)3 MavenJavaProject (org.springframework.ide.vscode.commons.maven.java.MavenJavaProject)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 IJavaProject (org.springframework.ide.vscode.commons.java.IJavaProject)2 ProgressService (org.springframework.ide.vscode.commons.languageserver.ProgressService)2 Listener (org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver.Listener)2 File (java.io.File)1 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)1 JavaProjectsService (org.springframework.ide.vscode.boot.jdt.ls.JavaProjectsService)1 GradleProjectCache (org.springframework.ide.vscode.commons.gradle.GradleProjectCache)1 GradleProjectFinder (org.springframework.ide.vscode.commons.gradle.GradleProjectFinder)1 DiagnosticService (org.springframework.ide.vscode.commons.languageserver.DiagnosticService)1 CompositeJavaProjectFinder (org.springframework.ide.vscode.commons.languageserver.java.CompositeJavaProjectFinder)1 CompositeProjectOvserver (org.springframework.ide.vscode.commons.languageserver.java.CompositeProjectOvserver)1 ShowMessageException (org.springframework.ide.vscode.commons.languageserver.util.ShowMessageException)1 MavenProjectFinder (org.springframework.ide.vscode.commons.maven.java.MavenProjectFinder)1