Merge branch 'master' of github.com:spring-projects/sts4

Conflicts:
	headless-services/boot-java-language-server/src/main/java/org/springframework/ide/vscode/boot/java/utils/ASTUtils.java
This commit is contained in:
Kris De Volder
2017-10-31 14:36:27 -07:00
13 changed files with 419 additions and 104 deletions

View File

@@ -0,0 +1,156 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.utils.test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ide.vscode.boot.java.BootJavaLanguageServer;
import org.springframework.ide.vscode.commons.maven.MavenCore;
import org.springframework.ide.vscode.commons.util.text.LanguageId;
import org.springframework.ide.vscode.commons.util.text.TextDocument;
import org.springframework.ide.vscode.languageserver.testharness.LanguageServerHarness;
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness;
import org.springframework.ide.vscode.project.harness.ProjectsHarness;
/**
* CU Cache tests
*
* @author Alex Boyko
*
*/
public class CompilationUnitCacheTest {
private LanguageServerHarness<BootJavaLanguageServer> harness;
@Before
public void setup() throws Exception {
harness = BootLanguageServerHarness.builder().build();
}
@Test
public void cu_cached() throws Exception {
harness.intialize(null);
TextDocument doc = new TextDocument(harness.createTempUri(), LanguageId.JAVA, 0, "package my.package\n" +
"\n" +
"public class SomeClass {\n" +
"\n" +
"}\n");
CompilationUnit cu = harness.getServer().getCompilationUnitCache().getCompilationUnit(doc);
assertNotNull(cu);
CompilationUnit cuAnother = harness.getServer().getCompilationUnitCache().getCompilationUnit(doc);
assertTrue(cu == cuAnother);
}
@Test
public void cu_cache_invalidated_by_file_change() throws Exception {
harness.intialize(null);
TextDocument doc = new TextDocument(harness.createTempUri(), LanguageId.JAVA, 0, "package my.package\n" +
"\n" +
"public class SomeClass {\n" +
"\n" +
"}\n");
CompilationUnit cu = harness.getServer().getCompilationUnitCache().getCompilationUnit(doc);
assertNotNull(cu);
harness.changeFile(doc.getUri());
CompilationUnit cuAnother = harness.getServer().getCompilationUnitCache().getCompilationUnit(doc);
assertNotNull(cuAnother);
assertFalse(cu == cuAnother);
CompilationUnit cuYetAnother = harness.getServer().getCompilationUnitCache().getCompilationUnit(doc);
assertTrue(cuAnother == cuYetAnother);
}
@Test
public void cu_cache_invalidated_by_file_removal() throws Exception {
harness.intialize(null);
TextDocument doc = new TextDocument(harness.createTempUri(), LanguageId.JAVA, 0, "package my.package\n" +
"\n" +
"public class SomeClass {\n" +
"\n" +
"}\n");
CompilationUnit cu = harness.getServer().getCompilationUnitCache().getCompilationUnit(doc);
assertNotNull(cu);
harness.deleteFile(doc.getUri());
CompilationUnit cuAnother = harness.getServer().getCompilationUnitCache().getCompilationUnit(doc);
assertNotNull(cuAnother);
assertFalse(cu == cuAnother);
CompilationUnit cuYetAnother = harness.getServer().getCompilationUnitCache().getCompilationUnit(doc);
assertTrue(cuAnother == cuYetAnother);
}
@Test
public void cu_cache_invalidated_by_project_change() throws Exception {
File directory = new File(
ProjectsHarness.class.getResource("/test-projects/test-request-mapping-live-hover/").toURI());
String docUri = "file://" +directory.getAbsolutePath() + "/src/main/java/example/HelloWorldController.java";
harness.intialize(directory);
URI fileUri = new URI(docUri);
Path path = Paths.get(fileUri);
String content = new String(Files.readAllBytes(path));
TextDocument document = new TextDocument(docUri, LanguageId.JAVA, 0, content);
CompilationUnit cu = harness.getServer().getCompilationUnitCache().getCompilationUnit(document);
assertNotNull(cu);
CompilationUnit cuAnother = harness.getServer().getCompilationUnitCache().getCompilationUnit(document);
assertTrue(cu == cuAnother);
harness.changeFile(directory.toPath().resolve(MavenCore.POM_XML).toUri().toString());
cuAnother = harness.getServer().getCompilationUnitCache().getCompilationUnit(document);
assertNotNull(cuAnother);
assertFalse(cu == cuAnother);
}
@Test
public void cu_cache_invalidated_by_project_deletion() throws Exception {
File directory = new File(
ProjectsHarness.class.getResource("/test-projects/test-request-mapping-live-hover/").toURI());
String docUri = "file://" +directory.getAbsolutePath() + "/src/main/java/example/HelloWorldController.java";
harness.intialize(directory);
URI fileUri = new URI(docUri);
Path path = Paths.get(fileUri);
String content = new String(Files.readAllBytes(path));
TextDocument document = new TextDocument(docUri, LanguageId.JAVA, 0, content);
CompilationUnit cu = harness.getServer().getCompilationUnitCache().getCompilationUnit(document);
assertNotNull(cu);
CompilationUnit cuAnother = harness.getServer().getCompilationUnitCache().getCompilationUnit(document);
assertTrue(cu == cuAnother);
harness.deleteFile(directory.toPath().resolve(MavenCore.POM_XML).toUri().toString());
cuAnother = harness.getServer().getCompilationUnitCache().getCompilationUnit(document);
assertNotNull(cuAnother);
assertFalse(cu == cuAnother);
}
}