From facc5cf989c078a5c23eb79884a09ce0beeb7da7 Mon Sep 17 00:00:00 2001 From: Valeriy Svydenko Date: Fri, 14 Sep 2018 08:50:27 +0000 Subject: [PATCH] Fix for RunPlainJavaProjectTest test (#11133) Signed-off-by: Valeriy Svydenko --- .../valueproviders/ClasspathMacro.java | 3 +- .../valueproviders/OutputDirMacro.java | 19 +++----- .../che/ide/ext/java/shared/Constants.java | 1 + .../che-plugin-java-plain-server/pom.xml | 4 ++ .../projecttype/PlainJavaInitHandler.java | 44 ++++++++++++++++++- .../PlainJavaProjectUpdateUtil.java | 1 + .../PlainJavaValueProviderFactory.java | 9 +++- .../JavaLanguageServerExtensionService.java | 23 +++++++--- .../JavaLanguageServerLauncher.java | 11 +++++ .../CommandsEditorTest.java | 1 + .../ConfigureSomeSourceFoldersTest.java | 1 + .../plainjava/RunPlainJavaProjectTest.java | 6 +-- 12 files changed, 96 insertions(+), 27 deletions(-) diff --git a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/command/valueproviders/ClasspathMacro.java b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/command/valueproviders/ClasspathMacro.java index 50811330eff..08d3f4d3e7e 100644 --- a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/command/valueproviders/ClasspathMacro.java +++ b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/command/valueproviders/ClasspathMacro.java @@ -39,7 +39,6 @@ */ @Singleton public class ClasspathMacro implements Macro { - private static final String KEY = "${project.java.classpath}"; private final ClasspathContainer classpathContainer; @@ -99,7 +98,7 @@ public Promise expand() { Set libs = classpathResolver.getLibs(); StringBuilder classpath = new StringBuilder(); for (String lib : libs) { - classpath.append(lib).append(':'); + classpath.append(appContext.getProjectsRoot()).append(lib).append(':'); } for (ClasspathEntry container : classpathResolver.getContainers()) { diff --git a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/command/valueproviders/OutputDirMacro.java b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/command/valueproviders/OutputDirMacro.java index 5916fdb01ac..cf6075c8dab 100644 --- a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/command/valueproviders/OutputDirMacro.java +++ b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/command/valueproviders/OutputDirMacro.java @@ -14,7 +14,6 @@ import static org.eclipse.che.ide.ext.java.client.util.JavaUtil.isJavaProject; import static org.eclipse.che.ide.ext.java.shared.Constants.OUTPUT_FOLDER; -import com.google.common.base.Optional; import com.google.inject.Inject; import com.google.inject.Singleton; import org.eclipse.che.api.promises.client.Promise; @@ -66,28 +65,22 @@ public Promise expand() { if (resources != null && resources.length == 1) { final Resource resource = resources[0]; - final Optional project = resource.getRelatedProject(); + Project project = resource.getProject(); - if (!project.isPresent()) { + if (!project.exists() || !isJavaProject(project)) { return promises.resolve(""); } - Project relatedProject = project.get(); - - if (!isJavaProject(relatedProject)) { - return promises.resolve(""); - } - - if (relatedProject.getAttributes().containsKey(OUTPUT_FOLDER)) { + if (project.getAttributes().containsKey(OUTPUT_FOLDER)) { return promises.resolve( appContext .getProjectsRoot() - .append(relatedProject.getLocation()) - .append(relatedProject.getAttributes().get(OUTPUT_FOLDER).get(0)) + .append(project.getLocation()) + .append(project.getAttributes().get(OUTPUT_FOLDER).get(0)) .toString()); } else { return promises.resolve( - appContext.getProjectsRoot().append(relatedProject.getLocation()).toString()); + appContext.getProjectsRoot().append(project.getLocation()).toString()); } } diff --git a/plugins/plugin-java/che-plugin-java-ext-lang-shared/src/main/java/org/eclipse/che/ide/ext/java/shared/Constants.java b/plugins/plugin-java/che-plugin-java-ext-lang-shared/src/main/java/org/eclipse/che/ide/ext/java/shared/Constants.java index 97e10e03fff..e64d00ba764 100644 --- a/plugins/plugin-java/che-plugin-java-ext-lang-shared/src/main/java/org/eclipse/che/ide/ext/java/shared/Constants.java +++ b/plugins/plugin-java/che-plugin-java-ext-lang-shared/src/main/java/org/eclipse/che/ide/ext/java/shared/Constants.java @@ -26,6 +26,7 @@ public final class Constants { public static final String CONTAINS_JAVA_FILES = "containsJavaFiles"; public static final String SOURCE_FOLDER = "java.source.folder"; public static final String OUTPUT_FOLDER = "java.output.folder"; + public static final String DEFAULT_OUTPUT_FOLDER_VALUE = "bin"; public static final String JAVAC = "javac"; diff --git a/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/pom.xml b/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/pom.xml index 928d338538c..8abdcc2f638 100644 --- a/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/pom.xml +++ b/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/pom.xml @@ -26,6 +26,10 @@ com.google.code.gson gson + + com.google.guava + guava + com.google.inject guice diff --git a/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/src/main/java/org/eclipse/che/plugin/java/plain/server/projecttype/PlainJavaInitHandler.java b/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/src/main/java/org/eclipse/che/plugin/java/plain/server/projecttype/PlainJavaInitHandler.java index cb0c4972aee..2c2526d2963 100644 --- a/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/src/main/java/org/eclipse/che/plugin/java/plain/server/projecttype/PlainJavaInitHandler.java +++ b/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/src/main/java/org/eclipse/che/plugin/java/plain/server/projecttype/PlainJavaInitHandler.java @@ -11,13 +11,23 @@ */ package org.eclipse.che.plugin.java.plain.server.projecttype; +import static com.google.common.base.Strings.isNullOrEmpty; +import static org.eclipse.che.api.fs.server.WsPathUtils.absolutize; import static org.eclipse.che.ide.ext.java.shared.Constants.JAVAC; +import static org.eclipse.che.plugin.java.plain.shared.PlainJavaProjectConstants.LIBRARY_FOLDER; +import com.google.inject.Inject; +import com.google.inject.Provider; +import java.util.List; import org.eclipse.che.api.core.ConflictException; import org.eclipse.che.api.core.ForbiddenException; import org.eclipse.che.api.core.NotFoundException; import org.eclipse.che.api.core.ServerException; +import org.eclipse.che.api.core.model.workspace.config.ProjectConfig; +import org.eclipse.che.api.project.server.ProjectManager; import org.eclipse.che.api.project.server.handlers.ProjectInitHandler; +import org.eclipse.che.plugin.java.languageserver.JavaLanguageServerExtensionService; +import org.eclipse.che.plugin.java.languageserver.JavaLanguageServerLauncher; /** * Init handler for simple java project. @@ -26,6 +36,20 @@ * @author Valeriy Svydenko */ public class PlainJavaInitHandler implements ProjectInitHandler { + private final Provider extensionService; + private final Provider languageServerLauncher; + private final Provider projectRegistryProvider; + + @Inject + public PlainJavaInitHandler( + Provider extensionService, + Provider languageServerLauncher, + Provider projectRegistryProvider) { + this.extensionService = extensionService; + this.languageServerLauncher = languageServerLauncher; + this.projectRegistryProvider = projectRegistryProvider; + } + @Override public String getProjectType() { return JAVAC; @@ -33,5 +57,23 @@ public String getProjectType() { @Override public void onProjectInitialized(String projectFolder) - throws ServerException, ForbiddenException, ConflictException, NotFoundException {} + throws ServerException, ForbiddenException, ConflictException, NotFoundException { + if (!languageServerLauncher.get().isStarted()) { + return; + } + String wsPath = absolutize(projectFolder); + ProjectConfig project = + projectRegistryProvider + .get() + .get(wsPath) + .orElseThrow(() -> new ServerException("Can't find a project: " + wsPath)); + + List library = project.getAttributes().get(LIBRARY_FOLDER); + if (library != null && !library.isEmpty()) { + String libraryFolder = library.get(0); + if (!isNullOrEmpty(libraryFolder)) { + extensionService.get().addJars(project.getPath(), libraryFolder); + } + } + } } diff --git a/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/src/main/java/org/eclipse/che/plugin/java/plain/server/projecttype/PlainJavaProjectUpdateUtil.java b/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/src/main/java/org/eclipse/che/plugin/java/plain/server/projecttype/PlainJavaProjectUpdateUtil.java index c3c493f666a..a1cb6f815b9 100644 --- a/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/src/main/java/org/eclipse/che/plugin/java/plain/server/projecttype/PlainJavaProjectUpdateUtil.java +++ b/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/src/main/java/org/eclipse/che/plugin/java/plain/server/projecttype/PlainJavaProjectUpdateUtil.java @@ -46,6 +46,7 @@ public static CompletableFuture updateProjectConfig( NewProjectConfig projectConfig = new NewProjectConfigImpl( projectWsPath, project.getName(), project.getType(), project.getSource()); + projectConfig.setAttributes(project.getAttributes()); RegisteredProject result = projectManager.update(projectConfig); return CompletableFuture.completedFuture(result.getPath()); } diff --git a/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/src/main/java/org/eclipse/che/plugin/java/plain/server/projecttype/PlainJavaValueProviderFactory.java b/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/src/main/java/org/eclipse/che/plugin/java/plain/server/projecttype/PlainJavaValueProviderFactory.java index 2e1995154bc..3883296ce49 100644 --- a/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/src/main/java/org/eclipse/che/plugin/java/plain/server/projecttype/PlainJavaValueProviderFactory.java +++ b/plugins/plugin-java/che-plugin-java-plain/che-plugin-java-plain-server/src/main/java/org/eclipse/che/plugin/java/plain/server/projecttype/PlainJavaValueProviderFactory.java @@ -11,6 +11,7 @@ */ package org.eclipse.che.plugin.java.plain.server.projecttype; +import static com.google.common.base.Strings.isNullOrEmpty; import static java.lang.String.format; import static java.util.Collections.singletonList; import static java.util.stream.Collectors.toList; @@ -38,6 +39,7 @@ import org.eclipse.che.api.project.server.type.ValueProvider; import org.eclipse.che.api.project.server.type.ValueProviderFactory; import org.eclipse.che.api.project.server.type.ValueStorageException; +import org.eclipse.che.ide.ext.java.shared.Constants; import org.eclipse.che.plugin.java.languageserver.JavaLanguageServerExtensionService; import org.eclipse.che.plugin.java.languageserver.NotifyJsonRpcTransmitter; import org.slf4j.Logger; @@ -119,9 +121,12 @@ private List getOutputFolder() throws ValueStorageException { String outputDir; try { outputDir = extensionService.getOutputDir(wsPath); + if (isNullOrEmpty(outputDir)) { + outputDir = Constants.DEFAULT_OUTPUT_FOLDER_VALUE; + } } catch (Exception e) { - throw new ValueStorageException( - format("Failed to get '%s'. ", OUTPUT_FOLDER), e.getCause()); + // can't read output dir + outputDir = Constants.DEFAULT_OUTPUT_FOLDER_VALUE; } String fsPath = transformer.transform(wsPath).toString(); diff --git a/plugins/plugin-java/che-plugin-java-server/src/main/java/org/eclipse/che/plugin/java/languageserver/JavaLanguageServerExtensionService.java b/plugins/plugin-java/che-plugin-java-server/src/main/java/org/eclipse/che/plugin/java/languageserver/JavaLanguageServerExtensionService.java index 166a119f499..555d050ce6e 100644 --- a/plugins/plugin-java/che-plugin-java-server/src/main/java/org/eclipse/che/plugin/java/languageserver/JavaLanguageServerExtensionService.java +++ b/plugins/plugin-java/che-plugin-java-server/src/main/java/org/eclipse/che/plugin/java/languageserver/JavaLanguageServerExtensionService.java @@ -350,11 +350,24 @@ public void configureMethods() { public String getOutputDir(String projectPath) { checkLanguageServerInitialized(); - String projectUri = LanguageServiceUtils.prefixURI(projectPath); + String projectUri = prefixURI(projectPath); Type type = new TypeToken() {}.getType(); return doGetOne(GET_OUTPUT_DIR_COMMAND, singletonList(projectUri), type); } + /** + * Adds all jars from the folder into the classpath. + * + * @param projectPath project's path + * @param library path to the folder with libraries + */ + public void addJars(String projectPath, String library) { + checkLanguageServerInitialized(); + + String projectUri = prefixURI(projectPath); + executeCommand(Commands.ADD_JARS_COMMAND, Arrays.asList(projectUri, library)); + } + /** * Compute output directory of the project. * @@ -447,7 +460,7 @@ private ClasspathEntry fixEntry(ClasspathEntry e) { public List getSourceFolders(String projectPath) { checkLanguageServerInitialized(); - String projectUri = LanguageServiceUtils.prefixURI(projectPath); + String projectUri = prefixURI(projectPath); Type type = new TypeToken>() {}.getType(); List result = doGetList(GET_SOURCE_FOLDERS, projectUri, type); return result.stream().map(LanguageServiceUtils::removePrefixUri).collect(Collectors.toList()); @@ -578,9 +591,7 @@ private List executeFileStructure( } public ImplementersResponseDto findImplementers(TextDocumentPositionParams params) { - params - .getTextDocument() - .setUri(LanguageServiceUtils.prefixURI(params.getTextDocument().getUri())); + params.getTextDocument().setUri(prefixURI(params.getTextDocument().getUri())); CompletableFuture result = executeCommand(FIND_IMPLEMENTERS_COMMAND, singletonList(params)); @@ -1036,7 +1047,7 @@ private void fixLocation(ExtendedSymbolInformation symbol) { } private UsagesResponse usages(TextDocumentPositionParams parameters) { - String uri = LanguageServiceUtils.prefixURI(parameters.getUri()); + String uri = prefixURI(parameters.getUri()); parameters.setUri(uri); parameters.getTextDocument().setUri(uri); try { diff --git a/plugins/plugin-java/che-plugin-java-server/src/main/java/org/eclipse/che/plugin/java/languageserver/JavaLanguageServerLauncher.java b/plugins/plugin-java/che-plugin-java-server/src/main/java/org/eclipse/che/plugin/java/languageserver/JavaLanguageServerLauncher.java index d75e9497b04..5d9ccb45bd4 100644 --- a/plugins/plugin-java/che-plugin-java-server/src/main/java/org/eclipse/che/plugin/java/languageserver/JavaLanguageServerLauncher.java +++ b/plugins/plugin-java/che-plugin-java-server/src/main/java/org/eclipse/che/plugin/java/languageserver/JavaLanguageServerLauncher.java @@ -33,6 +33,7 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicBoolean; import org.eclipse.che.api.core.BadRequestException; import org.eclipse.che.api.core.ConflictException; import org.eclipse.che.api.core.ForbiddenException; @@ -72,6 +73,8 @@ public class JavaLanguageServerLauncher implements LanguageServerConfig { private final EventService eventService; private final ProjectManager projectManager; + private AtomicBoolean isStarted; + @Inject public JavaLanguageServerLauncher( ProcessorJsonRpcCommunication processorJsonRpcCommunication, @@ -84,16 +87,24 @@ public JavaLanguageServerLauncher( this.notifyTransmitter = notifyTransmitter; this.eventService = eventService; this.projectManager = projectManager; + + isStarted = new AtomicBoolean(false); launchScript = Paths.get(System.getenv("HOME"), "che/ls-java/launch.sh"); } public void sendStatusReport(StatusReport report) { LOG.info("{}: {}", report.getType(), report.getMessage()); if ("Started".equals(report.getType())) { + isStarted.set(true); updateWorkspaceOnLSStarted(); } } + /** @return {@code true} if jd.ls has started, otherwise {@code false}. */ + public boolean isStarted() { + return isStarted.get(); + } + private void updateWorkspaceOnLSStarted() { projectManager .getAll() diff --git a/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/intelligencecommand/CommandsEditorTest.java b/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/intelligencecommand/CommandsEditorTest.java index f851b0765f8..1bdaf993c92 100644 --- a/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/intelligencecommand/CommandsEditorTest.java +++ b/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/intelligencecommand/CommandsEditorTest.java @@ -64,6 +64,7 @@ public void prepare() throws Exception { PROJECT_NAME, ProjectTemplates.PLAIN_JAVA); ide.open(testWorkspace); + consoles.waitJDTLSProjectResolveFinishedMessage(PROJECT_NAME); } @Test diff --git a/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/plainjava/ConfigureSomeSourceFoldersTest.java b/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/plainjava/ConfigureSomeSourceFoldersTest.java index f1176ccf613..007e5fc3092 100644 --- a/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/plainjava/ConfigureSomeSourceFoldersTest.java +++ b/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/plainjava/ConfigureSomeSourceFoldersTest.java @@ -66,6 +66,7 @@ public void prepare() throws Exception { testProjectServiceClient.importProject( ws.getId(), Paths.get(resource.toURI()), PROJECT_NAME, ProjectTemplates.PLAIN_JAVA); ide.open(ws); + consoles.waitJDTLSProjectResolveFinishedMessage(PROJECT_NAME); } @Test diff --git a/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/plainjava/RunPlainJavaProjectTest.java b/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/plainjava/RunPlainJavaProjectTest.java index bb5340de37e..660af536ac9 100644 --- a/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/plainjava/RunPlainJavaProjectTest.java +++ b/selenium/che-selenium-test/src/test/java/org/eclipse/che/selenium/plainjava/RunPlainJavaProjectTest.java @@ -29,7 +29,6 @@ import com.google.inject.name.Named; import java.nio.file.Path; import java.nio.file.Paths; -import org.eclipse.che.commons.lang.NameGenerator; import org.eclipse.che.selenium.core.client.TestGitHubRepository; import org.eclipse.che.selenium.core.client.TestUserPreferencesServiceClient; import org.eclipse.che.selenium.core.constant.TestProjectExplorerContextMenuConstants.ContextMenuFirstLevelItems; @@ -53,7 +52,7 @@ /** @author Aleksandr Shmaraev */ public class RunPlainJavaProjectTest { - private static final String PROJECT_NAME = NameGenerator.generate("RunningPlainJavaProject", 4); + private static final String PROJECT_NAME = "run-plain-java-project"; private static final String NEW_PACKAGE = "base.test"; private static final String NAME_COMMAND = "startApp"; private static final String COMMAND = @@ -94,6 +93,7 @@ public void prepare() throws Exception { testRepo.addContent(entryPath); ide.open(ws); + consoles.waitJDTLSStartedMessage(); } @Test @@ -110,7 +110,7 @@ public void checkRunPlainJavaProject() { menu.runCommand(PROJECT, CONFIGURE_CLASSPATH); configureClasspath.waitConfigureClasspathFormIsOpen(); configureClasspath.waitExpectedTextJarsAndFolderArea( - "testLibrary.jar - /projects/" + PROJECT_NAME + "/store"); + "testLibrary.jar - /" + PROJECT_NAME + "/store"); configureClasspath.closeConfigureClasspathFormByIcon(); // create the instance of the library