From e5a74e53c3522cd1710671d3836e30c782995162 Mon Sep 17 00:00:00 2001 From: "Vladislav.Soroka" Date: Wed, 8 Oct 2014 14:33:03 +0400 Subject: [PATCH] external system: fail importting tests if there was an error during project import --- .../externalSystem/importing/ImportSpec.java | 12 +++++ .../importing/ImportSpecBuilder.java | 9 ++++ .../util/ExternalSystemUtil.java | 9 +++- .../test/ExternalSystemImportingTestCase.java | 49 ++++++++++++++++++- 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/importing/ImportSpec.java b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/importing/ImportSpec.java index a7f59e4643c4..c4469b05598a 100644 --- a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/importing/ImportSpec.java +++ b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/importing/ImportSpec.java @@ -17,8 +17,10 @@ package com.intellij.openapi.externalSystem.importing; import com.intellij.openapi.externalSystem.model.ProjectSystemId; import com.intellij.openapi.externalSystem.service.execution.ProgressExecutionMode; +import com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback; import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author Vladislav.Soroka @@ -30,6 +32,7 @@ public class ImportSpec { @NotNull private ProgressExecutionMode myProgressExecutionMode; private boolean forceWhenUptodate; private boolean whenAutoImportEnabled; + @Nullable private ExternalProjectRefreshCallback myCallback; //private boolean isPreviewMode; //private boolean isReportRefreshError; @@ -74,6 +77,15 @@ public class ImportSpec { this.whenAutoImportEnabled = whenAutoImportEnabled; } + public void setCallback(@Nullable ExternalProjectRefreshCallback callback) { + myCallback = callback; + } + + @Nullable + public ExternalProjectRefreshCallback getCallback() { + return myCallback; + } + //public boolean isPreviewMode() { // return isPreviewMode; //} diff --git a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/importing/ImportSpecBuilder.java b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/importing/ImportSpecBuilder.java index b67189d93e77..06b472e7b359 100644 --- a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/importing/ImportSpecBuilder.java +++ b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/importing/ImportSpecBuilder.java @@ -17,8 +17,10 @@ package com.intellij.openapi.externalSystem.importing; import com.intellij.openapi.externalSystem.model.ProjectSystemId; import com.intellij.openapi.externalSystem.service.execution.ProgressExecutionMode; +import com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback; import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author Vladislav.Soroka @@ -31,6 +33,7 @@ public class ImportSpecBuilder { @NotNull private ProgressExecutionMode myProgressExecutionMode; private boolean myForceWhenUptodate; private boolean myWhenAutoImportEnabled; + @Nullable private ExternalProjectRefreshCallback myCallback; //private boolean isPreviewMode; //private boolean isReportRefreshError; @@ -59,6 +62,11 @@ public class ImportSpecBuilder { return this; } + public ImportSpecBuilder callback(@Nullable ExternalProjectRefreshCallback callback) { + myCallback = callback; + return this; + } + //public ImportSpecBuilder usePreviewMode() { // isPreviewMode = true; // return this; @@ -69,6 +77,7 @@ public class ImportSpecBuilder { mySpec.setWhenAutoImportEnabled(myWhenAutoImportEnabled); mySpec.setProgressExecutionMode(myProgressExecutionMode); mySpec.setForceWhenUptodate(myForceWhenUptodate); + mySpec.setCallback(myCallback); //mySpec.setPreviewMode(isPreviewMode); //mySpec.setReportRefreshError(isReportRefreshError); return mySpec; diff --git a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/util/ExternalSystemUtil.java b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/util/ExternalSystemUtil.java index 762ec55cd685..62bf8a5dcad7 100644 --- a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/util/ExternalSystemUtil.java +++ b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/util/ExternalSystemUtil.java @@ -247,8 +247,13 @@ public class ExternalSystemUtil { final ProjectDataManager projectDataManager = ServiceManager.getService(ProjectDataManager.class); final int[] counter = new int[1]; - ExternalProjectRefreshCallback callback = - new MyMultiExternalProjectRefreshCallback(spec.getProject(), projectDataManager, counter, spec.getExternalSystemId()); + final ExternalProjectRefreshCallback callback; + if (spec.getCallback() == null) { + callback = new MyMultiExternalProjectRefreshCallback(spec.getProject(), projectDataManager, counter, spec.getExternalSystemId()); + } + else { + callback = spec.getCallback(); + } Map modificationStamps = manager.getLocalSettingsProvider().fun(spec.getProject()).getExternalConfigModificationStamps(); diff --git a/platform/external-system-impl/testSrc/com/intellij/openapi/externalSystem/test/ExternalSystemImportingTestCase.java b/platform/external-system-impl/testSrc/com/intellij/openapi/externalSystem/test/ExternalSystemImportingTestCase.java index d55ceb0fe97c..1182c019b2cf 100644 --- a/platform/external-system-impl/testSrc/com/intellij/openapi/externalSystem/test/ExternalSystemImportingTestCase.java +++ b/platform/external-system-impl/testSrc/com/intellij/openapi/externalSystem/test/ExternalSystemImportingTestCase.java @@ -17,11 +17,17 @@ package com.intellij.openapi.externalSystem.test; import com.intellij.openapi.application.AccessToken; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.externalSystem.importing.ImportSpecBuilder; +import com.intellij.openapi.externalSystem.model.DataNode; import com.intellij.openapi.externalSystem.model.ProjectSystemId; +import com.intellij.openapi.externalSystem.model.project.ProjectData; import com.intellij.openapi.externalSystem.service.execution.ProgressExecutionMode; +import com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback; +import com.intellij.openapi.externalSystem.service.project.manage.ProjectDataManager; import com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings; import com.intellij.openapi.externalSystem.settings.ExternalProjectSettings; +import com.intellij.openapi.externalSystem.util.DisposeAwareProjectChange; import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil; import com.intellij.openapi.externalSystem.util.ExternalSystemUtil; import com.intellij.openapi.module.Module; @@ -29,10 +35,13 @@ import com.intellij.openapi.module.ModuleManager; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl; import com.intellij.openapi.roots.*; +import com.intellij.openapi.roots.ex.ProjectRootManagerEx; import com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable; import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.TestDialog; +import com.intellij.openapi.util.Couple; +import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VfsUtil; @@ -44,6 +53,7 @@ import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.ContainerUtilRt; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.jps.model.java.JavaResourceRootType; import org.jetbrains.jps.model.java.JavaSourceRootProperties; import org.jetbrains.jps.model.java.JavaSourceRootType; @@ -377,9 +387,46 @@ public abstract class ExternalSystemImportingTestCase extends ExternalSystemTest projects.add(projectSettings); systemSettings.setLinkedProjectsSettings(projects); + final Ref> error = Ref.create(); ExternalSystemUtil.refreshProjects( - new ImportSpecBuilder(myProject, getExternalSystemId()).use(ProgressExecutionMode.MODAL_SYNC) + new ImportSpecBuilder(myProject, getExternalSystemId()) + .use(ProgressExecutionMode.MODAL_SYNC) + .callback(new ExternalProjectRefreshCallback() { + @Override + public void onSuccess(@Nullable final DataNode externalProject) { + if (externalProject == null) { + System.err.println("Got null External project after import"); + return; + } + ExternalSystemApiUtil.executeProjectChangeAction(true, new DisposeAwareProjectChange(myProject) { + @Override + public void execute() { + ProjectRootManagerEx.getInstanceEx(myProject).mergeRootsChangesDuring(new Runnable() { + @Override + public void run() { + ServiceManager.getService(ProjectDataManager.class).importData( + externalProject.getKey(), Collections.singleton(externalProject), myProject, true); + } + }); + } + }); + System.out.println("External project was successfully imported"); + } + + @Override + public void onFailure(@NotNull String errorMessage, @Nullable String errorDetails) { + error.set(Couple.of(errorMessage, errorDetails)); + } + }) ); + + if (!error.isNull()) { + String failureMsg = "Import failed: " + error.get().first; + if (StringUtil.isNotEmpty(error.get().second)) { + failureMsg += "\nError details: \n" + error.get().second; + } + fail(failureMsg); + } } protected abstract ExternalProjectSettings getCurrentExternalProjectSettings();