move tests for facets into the community project

This commit is contained in:
Sergey Ignatov
2018-10-19 23:06:54 +03:00
parent 6554bfd3ed
commit 20d826e839
10 changed files with 499 additions and 0 deletions
@@ -0,0 +1,83 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.projectWizard;
import com.intellij.ide.actions.ImportModuleAction;
import com.intellij.ide.util.importProject.RootsDetectionStep;
import com.intellij.ide.util.newProjectWizard.AddModuleWizard;
import com.intellij.ide.util.projectWizard.ImportFromSourcesProvider;
import com.intellij.ide.util.projectWizard.ModuleImportProvider;
import com.intellij.ide.util.projectWizard.ModuleWizardStep;
import com.intellij.openapi.application.ex.PathManagerEx;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.OrderEntry;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.ContainerUtil;
import java.io.File;
import java.util.List;
import java.util.Set;
/**
* @author Dmitry Avdeev
*/
public class ImportActionTest extends ProjectWizardTestCase<AddModuleWizard> {
public void testImportModule() {
String path = PathManagerEx.getTestDataPath("/ide/importAction/module/foo.iml");
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(path);
assertNotNull(file);
assertEquals(1, ModuleManager.getInstance(getProject()).getModules().length);
Module module = importModuleFrom(new ModuleImportProvider(), file.getPath());
assertEquals("foo", module.getName());
assertEquals(2, ModuleManager.getInstance(getProject()).getModules().length);
}
public void testImportModuleFromSources() {
Module module = importModuleFrom(new ImportFromSourcesProvider(), PathManagerEx.getTestDataPath("/ide/importAction/sources"));
assertEquals("importAction", module.getName());
VirtualFile[] roots = ModuleRootManager.getInstance(module).getSourceRoots();
assertEquals(1, roots.length);
}
public void testImportProjectFromSources() throws Exception {
File file = createTempFile("Foo.java", "class Foo {}");
Module module = importProjectFrom(file.getParent(), step -> {
if (step != null) {
assertEquals("Existing Sources", myWizard.getSequence().getSelectedType());
if (step instanceof RootsDetectionStep) {
List<ModuleWizardStep> steps = myWizard.getSequence().getSelectedSteps();
assertEquals(6, steps.size());
}
}
}, new ImportFromSourcesProvider());
VirtualFile[] roots = ModuleRootManager.getInstance(module).getSourceRoots();
assertEquals(1, roots.length);
}
public void testImportProjectWithLibrary() throws Exception {
File tempDirectory = createTempDirectory();
FileUtil.copyDir(new File(PathManagerEx.getTestDataPath("/ide/importAction/project")), tempDirectory);
Module module = importProjectFrom(tempDirectory.getPath(), null, new ImportFromSourcesProvider());
LibraryTable table = LibraryTablesRegistrar.getInstance().getLibraryTable(module.getProject());
Library[] libraries = table.getLibraries();
assertEquals(1, libraries.length);
OrderEntry[] entries = ModuleRootManager.getInstance(module).getOrderEntries();
assertEquals(3, entries.length);
assertEquals("google-play-services", entries[2].getPresentableName());
}
public void testProvidersCompatibility() {
Set<Class> project = ContainerUtil.map2Set(ImportModuleAction.getProviders(null), p -> p.getClass());
assertFalse(project.contains(ModuleImportProvider.class));
Set<Class> modular = ContainerUtil.map2Set(ImportModuleAction.getProviders(getProject()), p -> p.getClass());
assertTrue(modular.contains(ModuleImportProvider.class));
}
}
@@ -0,0 +1,26 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.projectWizard;
import com.intellij.openapi.module.JavaModuleType;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.projectRoots.JavaSdk;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.vfs.VirtualFile;
/**
* @author Dmitry Avdeev
*/
public class ModuleWizardTest extends NewProjectWizardTestCase {
public void testPlainJava() throws Exception {
Module module = createModuleFromTemplate(JavaModuleType.JAVA_GROUP, null, null);
assertNotNull(module);
VirtualFile root = ModuleRootManager.getInstance(module).getContentRoots()[0];
assertEquals(1, root.getChildren().length);
}
@Override
protected void setUp() throws Exception {
super.setUp();
createSdk("foo", JavaSdk.getInstance());
}
}
@@ -0,0 +1,119 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.projectWizard;
import com.intellij.ide.util.projectWizard.ModuleWizardStep;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.JavaModuleType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.projectRoots.JavaSdk;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.util.Consumer;
import java.util.List;
/**
* @author Dmitry Avdeev
*/
@SuppressWarnings("unchecked")
public class NewProjectWizardTest extends NewProjectWizardTestCase {
public void testCreateProject() throws Exception {
Project project = createProject(step -> {
if (step instanceof ProjectTypeStep) {
assertTrue(((ProjectTypeStep)step).setSelectedTemplate(JavaModuleType.JAVA_GROUP, null));
List<ModuleWizardStep> steps = myWizard.getSequence().getSelectedSteps();
assertEquals(steps.toString(), 3, steps.size());
}
});
assertNotNull(project);
Sdk sdk = ProjectRootManager.getInstance(project).getProjectSdk();
assertNotNull(sdk);
JavaSdkVersion version = JavaSdk.getInstance().getVersion(sdk);
assertNotNull(version);
assertEquals(version.getMaxLanguageLevel(), LanguageLevelProjectExtension.getInstance(project).getLanguageLevel());
}
public void testDefaultLanguageLevel13() throws Exception {
Project project = doLanguageLevelTest(LanguageLevel.JDK_1_3, false);
assertFalse(LanguageLevelProjectExtension.getInstance(project).getDefault());
}
public void testDefaultLanguageLevel19() throws Exception {
Project project = doLanguageLevelTest(LanguageLevel.JDK_1_9, false);
assertFalse(LanguageLevelProjectExtension.getInstance(project).getDefault());
}
public void testDefaultLanguageLevel() throws Exception {
final Sdk defaultSdk = ProjectJdkTable.getInstance().findJdk(DEFAULT_SDK);
setProjectSdk(myProjectManager.getDefaultProject(), defaultSdk);
JavaSdkVersion version = JavaSdk.getInstance().getVersion(defaultSdk);
assertNotNull(version);
Project project = doLanguageLevelTest(version.getMaxLanguageLevel(), true);
Boolean def = LanguageLevelProjectExtension.getInstance(project).getDefault();
assertNotNull(def);
assertTrue(def);
}
public void testChangeSdk() throws Exception {
Project project = createProject(Consumer.EMPTY_CONSUMER);
Sdk jdk17 = IdeaTestUtil.getMockJdk17();
addSdk(jdk17);
setProjectSdk(project, jdk17);
LanguageLevelProjectExtension extension = LanguageLevelProjectExtension.getInstance(project);
assertTrue(extension.isDefault());
assertNotSame(LanguageLevel.JDK_1_8, extension.getLanguageLevel());
Sdk jdk18 = IdeaTestUtil.getMockJdk18();
addSdk(jdk18);
setProjectSdk(project, jdk18);
assertEquals(LanguageLevel.JDK_1_8, extension.getLanguageLevel());
}
public void testMigrateFromOldDefaults() throws Exception {
LanguageLevelProjectExtension defaultExt = LanguageLevelProjectExtension.getInstance(ProjectManager.getInstance().getDefaultProject());
defaultExt.setLanguageLevel(LanguageLevel.JDK_1_4);
defaultExt.setDefault(null); // emulate migration from previous build
Project project = createProject(Consumer.EMPTY_CONSUMER);
LanguageLevelProjectExtension extension = LanguageLevelProjectExtension.getInstance(project);
Sdk sdk = ProjectRootManager.getInstance(project).getProjectSdk();
JavaSdkVersion version = JavaSdk.getInstance().getVersion(sdk);
assertEquals(version.getMaxLanguageLevel(), extension.getLanguageLevel());
}
private static void setProjectSdk(final Project project, final Sdk jdk17) {
ApplicationManager.getApplication().runWriteAction(() -> ProjectRootManager.getInstance(project).setProjectSdk(jdk17));
}
private Project doLanguageLevelTest(LanguageLevel languageLevel, boolean detect) throws Exception {
ProjectManager projectManager = ProjectManager.getInstance();
Project defaultProject = projectManager.getDefaultProject();
LanguageLevel old = LanguageLevelProjectExtension.getInstance(defaultProject).getLanguageLevel();
try {
LanguageLevelProjectExtension.getInstance(defaultProject).setLanguageLevel(languageLevel);
LanguageLevelProjectExtension.getInstance(defaultProject).setDefault(detect);
@SuppressWarnings("unchecked") Project project = createProject(Consumer.EMPTY_CONSUMER);
assertEquals(languageLevel, LanguageLevelProjectExtension.getInstance(project).getLanguageLevel());
return project;
}
finally {
LanguageLevelProjectExtension.getInstance(defaultProject).setLanguageLevel(old);
LanguageLevelProjectExtension.getInstance(defaultProject).setDefault(true);
}
}
@Override
protected void setUp() throws Exception {
super.setUp();
configureJdk();
}
}
@@ -0,0 +1,67 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.projectWizard;
import com.intellij.configurationStore.XmlSerializer;
import com.intellij.facet.frameworks.beans.Artifact;
import com.intellij.openapi.module.ModuleType;
import com.intellij.platform.templates.ArchivedProjectTemplate;
import com.intellij.util.JdomKt;
import junit.framework.TestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Dmitry Avdeev
*/
public class ProjectTemplatesTest extends TestCase {
public void testArtifact() throws Exception {
Artifact artifact = XmlSerializer.deserialize(JdomKt.loadElement(" <artifact version=\"2.2.3\" name=\"Spring Batch\"\n" +
" urlPrefix=\"http://download.jetbrains.com/idea/j2ee_libs/spring/batch/2.2.3/\">\n" +
" <item name=\"spring-batch-core-2.2.3.RELEASE.jar\"/>\n" +
" <item name=\"spring-batch-infrastructure-2.2.3.RELEASE.jar\"/>\n" +
" </artifact>"), Artifact.class);
assertNotNull(artifact);
assertEquals(2, artifact.getItems().length);
}
public void testTemplate() throws Exception {
ArchivedProjectTemplate template = new ArchivedProjectTemplate("Display Name", "category") {
@Override
protected ModuleType getModuleType() {
return null;
}
@Override
public <T> T processStream(@NotNull StreamProcessor<T> consumer) {
return null;
}
@Nullable
@Override
public String getDescription() {
return null;
}
};
XmlSerializer.deserializeInto(JdomKt.loadElement("<template>\n" +
" <input-field default=\"com.springapp.batch\">IJ_BASE_PACKAGE</input-field>\n" +
" <icon-path>/icons/spring.png</icon-path>\n" +
" <framework>facet:Spring</framework>\n" +
" <framework>spring-batch</framework>\n" +
" <artifact version=\"2.2.3\" name=\"Spring Batch\"\n" +
" urlPrefix=\"http://download.jetbrains.com/idea/j2ee_libs/spring/batch/2.2.3/\">\n" +
" <item name=\"spring-batch-core-2.2.3.RELEASE.jar\"/>\n" +
" <item name=\"spring-batch-infrastructure-2.2.3.RELEASE.jar\"/>\n" +
" </artifact>\n" +
" <artifact version=\"2.2.4\" name=\"Spring Batch\"\n" +
" urlPrefix=\"http://download.jetbrains.com/idea/j2ee_libs/spring/batch/2.2.3/\">\n" +
" <item name=\"spring-batch-core-2.2.3.RELEASE.jar\"/>\n" +
" <item name=\"spring-batch-infrastructure-2.2.3.RELEASE.jar\"/>\n" +
" </artifact>\n" +
"</template>"), template);
assertEquals(2, template.getArtifacts().size());
assertEquals(2, template.getFrameworks().size());
}
}
@@ -0,0 +1,165 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.projectWizard;
import com.intellij.application.UtilKt;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.ide.fileTemplates.impl.FileTemplateManagerImpl;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.ide.util.projectWizard.ProjectTemplateParameterFactory;
import com.intellij.mock.MockProgressIndicator;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.StorageScheme;
import com.intellij.openapi.module.BasePackageParameterFactory;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.platform.ProjectTemplatesFactory;
import com.intellij.platform.templates.ArchivedTemplatesFactory;
import com.intellij.platform.templates.LocalArchivedTemplate;
import com.intellij.platform.templates.SaveProjectAsTemplateAction;
import com.intellij.project.ProjectKt;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.util.SystemProperties;
import com.intellij.util.text.DateFormatUtil;
import kotlin.Unit;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Date;
/**
* @author Dmitry Avdeev
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
public class SaveProjectAsTemplateTest extends NewProjectWizardTestCase {
private static final String FOO_BAR_JAVA = "foo/Bar.java";
private static final Date TEST_DATE = new Date(0);
public void testSaveProject() throws Exception {
doTest(true, true, "/** No comments */\n" +
"\n" +
"/**\n" +
" * Created by Dmitry.Avdeev on 1/22/13.\n" +
" */\n" +
"package foo;\n" +
"public class Bar {\n" +
"}", "/** No comments */\n" +
"\n" +
"/**\n" +
" * Created by Vasya Pupkin on " + DateFormatUtil.formatDate(TEST_DATE) + ".\n" +
" */\n" +
"\n" +
"package foo;\n" +
"public class Bar {\n" +
"}");
}
public void testSaveProjectUnescaped() throws Exception {
doTest(false, false, "/** No comments */\n" +
"\n" +
"/**\n" +
" * Created by Dmitry.Avdeev on 1/22/13.\n" +
" */\n" +
"package foo;\n" +
"public class Bar {\n" +
"}", "/** No comments */\n" +
"\n" +
"/**\n" +
" * Created by Vasya Pupkin on " + DateFormatUtil.formatDate(TEST_DATE) + ".\n" +
" */\n" +
"\n" +
"package foo;\n" +
"public class Bar {\n" +
"}");
}
private void doTest(boolean shouldEscape, boolean replaceParameters, String initialText, String expected) throws IOException {
assertEquals(StorageScheme.DIRECTORY_BASED, ProjectKt.getStateStore(getProject()).getStorageScheme());
VirtualFile root = ProjectRootManager.getInstance(getProject()).getContentRoots()[0];
File rootFile = new File(VfsUtilCore.virtualToIoFile(root), FOO_BAR_JAVA);
rootFile.getParentFile().mkdirs();
rootFile.createNewFile();
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(rootFile);
assertNotNull(file);
setFileText(file, initialText);
PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
String basePackage = new BasePackageParameterFactory().detectParameterValue(getProject());
assertEquals("foo", basePackage);
Path zipFile = ArchivedTemplatesFactory.getTemplateFile("foo");
UtilKt.runInAllowSaveMode(() -> {
SaveProjectAsTemplateAction.saveProject(getProject(), zipFile, null, "bar", replaceParameters, new MockProgressIndicator(), shouldEscape);
return Unit.INSTANCE;
});
assertEquals("foo.zip", zipFile.getFileName().toString());
assertTrue(Files.size(zipFile) > 0);
Project fromTemplate = createProjectFromTemplate(ProjectTemplatesFactory.CUSTOM_GROUP, "foo", null);
VirtualFile descriptionFile = SaveProjectAsTemplateAction.getDescriptionFile(fromTemplate, LocalArchivedTemplate.DESCRIPTION_PATH);
assertNotNull(descriptionFile);
assertEquals("bar", VfsUtilCore.loadText(descriptionFile));
VirtualFile[] roots = ProjectRootManager.getInstance(fromTemplate).getContentRoots();
VirtualFile child = roots[0].findFileByRelativePath(FOO_BAR_JAVA);
assertNotNull(Arrays.asList(roots[0].getChildren()).toString(), child);
String s = VfsUtilCore.loadText(child);
assertEquals(expected, StringUtil.convertLineSeparators(s));
assertNotNull(fromTemplate.getBaseDir().findFileByRelativePath(".idea/workspace.xml"));
}
@Override
protected void setUp() throws Exception {
super.setUp();
SystemProperties.setTestUserName("Vasya Pupkin");
((FileTemplateManagerImpl)FileTemplateManager.getDefaultInstance()).setTestDate(TEST_DATE);
PropertiesComponent.getInstance().unsetValue(ProjectTemplateParameterFactory.IJ_BASE_PACKAGE);
PlatformTestUtil.setLongMeaninglessFileIncludeTemplateTemporarilyFor(getProject(), getProject());
}
@Override
public void tearDown() throws Exception {
try {
SystemProperties.setTestUserName(null);
((FileTemplateManagerImpl)FileTemplateManager.getDefaultInstance()).setTestDate(null);
PropertiesComponent.getInstance().unsetValue(ProjectTemplateParameterFactory.IJ_BASE_PACKAGE);
}
finally {
super.tearDown();
}
}
@Override
protected Project doCreateProject(@NotNull Path projectFile) throws Exception {
FileUtil.ensureExists(projectFile.getParent().resolve(Project.DIRECTORY_STORE_FOLDER).toFile());
return createProject(projectFile.getParent().toFile(), getClass().getName() + "." + getName());
}
@NotNull
@Override
protected Module createMainModule() throws IOException {
final Module module = super.createMainModule();
ApplicationManager.getApplication().runWriteAction(() -> {
ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
VirtualFile baseDir = module.getProject().getBaseDir();
ContentEntry entry = model.addContentEntry(baseDir);
entry.addSourceFolder(baseDir, false);
model.commit();
});
return module;
}
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
@@ -0,0 +1 @@
doc=../../../docs/reference
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android;
import com.google.android.gms.common.Scopes;
// Stub java file to make inclusion into some IDE's work.
public final class UnusedStub {
private UnusedStub() {
String x = Scopes.PLUS_LOGIN;
}
}
@@ -0,0 +1 @@
class Foo {}