junit: tests for module path splitting

GitOrigin-RevId: 0092aac7459c8cfed18ee943e794cc3fa5007101
This commit is contained in:
Anna Kozlova
2019-09-27 20:03:34 +02:00
committed by intellij-monorepo-bot
parent 95b2eb7666
commit 73f6155155
14 changed files with 146 additions and 18 deletions

View File

@@ -395,9 +395,14 @@ public abstract class JavaTestFrameworkRunnableState<T extends
PathsList modulePath = javaParameters.getModulePath();
PathsList classPath = javaParameters.getClassPath();
List<String> pathStrings = classPath.getPathList();
Consumer<VirtualFile> putOnModulePath = virtualFile -> {
classPath.remove(virtualFile.getPath());
modulePath.add(virtualFile.getPath());
String path = virtualFile.getPath();
if (pathStrings.contains(path)) {
classPath.remove(path);
modulePath.add(path);
}
};
//put all transitive required modules on the module path
@@ -405,6 +410,7 @@ public abstract class JavaTestFrameworkRunnableState<T extends
JarFileSystem jarFS = JarFileSystem.getInstance();
ProjectFileIndex fileIndex = ProjectFileIndex.getInstance(module.getProject());
allRequires.stream()
.filter(javaModule -> !PsiJavaModule.JAVA_BASE.equals(javaModule.getName()))
.map(javaModule -> getClasspathEntry(javaModule, fileIndex, jarFS))
.filter(Objects::nonNull)
.forEach(putOnModulePath);

View File

@@ -0,0 +1 @@
module m1 {}

View File

@@ -0,0 +1,5 @@
package p;
class MyTest {
@org.junit.jupiter.api.Test
void foo() {}
}

View File

@@ -46,7 +46,7 @@ public abstract class AbstractTestFrameworkIntegrationTest extends BaseConfigura
settings = new RunnerAndConfigurationSettingsImpl(RunManagerImpl.getInstanceImpl(project), configuration, false);
ExecutionEnvironment
environment = new ExecutionEnvironment(executor, ProgramRunnerUtil.getRunner(DefaultRunExecutor.EXECUTOR_ID, settings), settings, project);
JavaTestFrameworkRunnableState state = ((JavaTestConfigurationBase)configuration).getState(executor, environment);
JavaTestFrameworkRunnableState<?> state = ((JavaTestConfigurationBase)configuration).getState(executor, environment);
state.appendForkInfo(executor);
state.appendRepeatMode();
@@ -110,9 +110,15 @@ public abstract class AbstractTestFrameworkIntegrationTest extends BaseConfigura
return processOutput;
}
protected void addLibs(Module module,
JpsMavenRepositoryLibraryDescriptor descriptor,
ArtifactRepositoryManager repoManager) throws Exception {
protected static void addMavenLibs(Module module,
JpsMavenRepositoryLibraryDescriptor descriptor) throws Exception {
addMavenLibs(module, descriptor, getRepoManager());
}
protected static void addMavenLibs(Module module,
JpsMavenRepositoryLibraryDescriptor descriptor,
ArtifactRepositoryManager repoManager) throws Exception {
Collection<File> files = repoManager.resolveDependency(descriptor.getGroupId(), descriptor.getArtifactId(), descriptor.getVersion(),
descriptor.isIncludeTransitiveDependencies(), descriptor.getExcludedDependencies());
@@ -125,7 +131,7 @@ public abstract class AbstractTestFrameworkIntegrationTest extends BaseConfigura
}
}
protected ArtifactRepositoryManager getRepoManager() {
protected static ArtifactRepositoryManager getRepoManager() {
final File localRepo = new File(SystemProperties.getUserHome(), ".m2/repository");
return new ArtifactRepositoryManager(
localRepo,

View File

@@ -0,0 +1,104 @@
// 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.java.execution;
import com.intellij.codeInsight.TestFrameworks;
import com.intellij.execution.configurations.JavaParameters;
import com.intellij.execution.junit.JUnitConfiguration;
import com.intellij.openapi.application.ex.PathManagerEx;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.CompilerModuleExtension;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.openapi.roots.OrderEnumerator;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.testFramework.PsiTestUtil;
import com.intellij.util.PathsList;
import org.jetbrains.jps.model.library.JpsMavenRepositoryLibraryDescriptor;
import java.io.File;
import java.util.Arrays;
public class ModulePathTest extends BaseConfigurationTestCase {
public void testModuleInfoInProductionNonModularizedJunit() throws Exception {
Module module = createEmptyModule();
JpsMavenRepositoryLibraryDescriptor nonModularizedJupiterDescription =
new JpsMavenRepositoryLibraryDescriptor("org.junit.jupiter", "junit-jupiter-api", "5.3.0");
JUnitConfiguration configuration = setupConfiguration(nonModularizedJupiterDescription, "prod1", module);
JavaParameters params4Tests = configuration.getTestObject().createJavaParameters4Tests();
assertEquals("-ea" +
" --patch-module m1=" + CompilerModuleExtension.getInstance(module).getCompilerOutputPathForTests().getPath() +
" --add-reads m1=ALL-UNNAMED" +
" --add-modules m1 -Didea.test.cyclic.buffer.size=1048576", params4Tests.getVMParametersList().getParametersString());
//junit is on the classpath
PathsList classPath = params4Tests.getClassPath();
Arrays.stream(OrderEnumerator.orderEntries(module).getAllLibrariesAndSdkClassesRoots())
.map(f -> JarFileSystem.getInstance().getVirtualFileForJar(f).getPath())
.forEach(path -> assertTrue("path " + path + " is not located on the classpath: " + classPath.getPathsString(),
classPath.getPathList().contains(path)));
//production module output is on the module path
PathsList modulePath = params4Tests.getModulePath();
assertTrue("module path: " + modulePath.getPathsString(),
modulePath.getPathList().contains(CompilerModuleExtension.getInstance(module).getCompilerOutputPath().getPath()));
}
public void _testModuleInfoInProductionModularizedJunit() throws Exception {
Module module = createEmptyModule();
JpsMavenRepositoryLibraryDescriptor modularizedJupiterDescription =
new JpsMavenRepositoryLibraryDescriptor("org.junit.jupiter", "junit-jupiter-api", "5.5.2");
JUnitConfiguration configuration = setupConfiguration(modularizedJupiterDescription, "prod1", module);
JavaParameters params4Tests = configuration.getTestObject().createJavaParameters4Tests();
assertEquals("-ea" +
" --patch-module m1=" + CompilerModuleExtension.getInstance(module).getCompilerOutputPathForTests().getPath() +
" --add-reads m1=ALL-UNNAMED" +
" --add-modules m1 -Didea.test.cyclic.buffer.size=1048576", params4Tests.getVMParametersList().getParametersString());
//junit is on the classpath
PathsList classPath = params4Tests.getClassPath();
Arrays.stream(OrderEnumerator.orderEntries(module).getAllLibrariesAndSdkClassesRoots())
.map(f -> JarFileSystem.getInstance().getVirtualFileForJar(f).getPath())
.forEach(path -> assertTrue("path " + path + " is not located on the classpath: " + classPath.getPathsString(),
classPath.getPathList().contains(path)));
//production module output is on the module path
PathsList modulePath = params4Tests.getModulePath();
assertTrue("module path: " + modulePath.getPathsString(),
modulePath.getPathList().contains(CompilerModuleExtension.getInstance(module).getCompilerOutputPath().getPath()));
}
private JUnitConfiguration setupConfiguration(JpsMavenRepositoryLibraryDescriptor libraryDescriptor, String sources, Module module) throws Exception {
VirtualFile contentRoot = getContentRoot(sources);
ContentEntry contentEntry = PsiTestUtil.addContentRoot(module, contentRoot);
contentEntry.addSourceFolder(contentRoot.getUrl() + "/src",false);
contentEntry.addSourceFolder(contentRoot.getUrl() + "/test",true);
ModuleRootModificationUtil.updateModel(module, model -> {
CompilerModuleExtension moduleExtension = model.getModuleExtension(CompilerModuleExtension.class);
moduleExtension.inheritCompilerOutputPath(false);
moduleExtension.setCompilerOutputPath(contentRoot.findFileByRelativePath("out/production"));
moduleExtension.setCompilerOutputPathForTests(contentRoot.findFileByRelativePath("out/test"));
});
AbstractTestFrameworkIntegrationTest.addMavenLibs(module, libraryDescriptor);
Sdk mockJdk = IdeaTestUtil.getMockJdk9();
ModuleRootModificationUtil.setModuleSdk(module, mockJdk);
PsiClass aClass = findClass(module, "p.MyTest");
assertNotNull(aClass);
assertNotNull(TestFrameworks.detectFramework(aClass));
return createConfiguration(aClass);
}
protected static VirtualFile getContentRoot(String path) {
String filePath = PathManagerEx.getTestDataPath() + File.separator + "junit" + File.separator + "modulePath" +
File.separator + path;
return LocalFileSystem.getInstance().findFileByPath(filePath.replace(File.separatorChar, '/'));
}
}

View File

@@ -58,6 +58,7 @@ import com.intellij.util.text.VersionComparatorUtil;
import com.siyeh.ig.junit.JUnitCommonClassNames;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.idea.maven.utils.library.RepositoryLibraryProperties;
import java.io.File;
@@ -264,6 +265,11 @@ public abstract class TestObject extends JavaTestFrameworkRunnableState<JUnitCon
return javaParameters;
}
@TestOnly
public JavaParameters createJavaParameters4Tests() throws ExecutionException {
return createJavaParameters();
}
public void appendJUnit5LauncherClasses(JavaParameters javaParameters, Project project, GlobalSearchScope globalSearchScope) throws CantRunException {
JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);

View File

@@ -16,8 +16,8 @@ public class CustomJUnit5IntegrationTest extends AbstractTestFrameworkCompilingI
protected void setupModule() throws Exception {
super.setupModule();
final ArtifactRepositoryManager repoManager = getRepoManager();
addLibs(myModule, new JpsMavenRepositoryLibraryDescriptor("org.junit.jupiter", "junit-jupiter-api", "5.2.0"), repoManager);
addLibs(myModule, new JpsMavenRepositoryLibraryDescriptor("org.junit.platform", "junit-platform-engine", "1.2.0"), repoManager);
addMavenLibs(myModule, new JpsMavenRepositoryLibraryDescriptor("org.junit.jupiter", "junit-jupiter-api", "5.2.0"), repoManager);
addMavenLibs(myModule, new JpsMavenRepositoryLibraryDescriptor("org.junit.platform", "junit-platform-engine", "1.2.0"), repoManager);
}
@Override

View File

@@ -81,7 +81,7 @@ public class JUnit4IntegrationTest extends AbstractTestFrameworkIntegrationTest
String testDataPath = communityPath + File.separator + "plugins" + File.separator + "junit5_rt_tests" +
File.separator + "testData" + File.separator + "integration" + File.separator + methodName;
addLibs(module, new JpsMavenRepositoryLibraryDescriptor("junit", "junit", myJUnitVersion), getRepoManager());
addMavenLibs(module, new JpsMavenRepositoryLibraryDescriptor("junit", "junit", myJUnitVersion), getRepoManager());
ModuleRootModificationUtil.setModuleSdk(module, JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk());
ModuleRootModificationUtil.updateModel(module, model -> {

View File

@@ -61,8 +61,8 @@ public class JUnit5IntegrationTest extends AbstractTestFrameworkCompilingIntegra
ModuleRootModificationUtil.updateModel(myModule,
model -> model.addContentEntry(getTestContentRoot()).addSourceFolder(getTestContentRoot() + "/test1", true));
final ArtifactRepositoryManager repoManager = getRepoManager();
addLibs(myModule, new JpsMavenRepositoryLibraryDescriptor("org.junit.jupiter", "junit-jupiter-api", "5.3.0"), repoManager);
addLibs(myModule, new JpsMavenRepositoryLibraryDescriptor("junit", "junit", "4.12"), repoManager);
addMavenLibs(myModule, new JpsMavenRepositoryLibraryDescriptor("org.junit.jupiter", "junit-jupiter-api", "5.3.0"), repoManager);
addMavenLibs(myModule, new JpsMavenRepositoryLibraryDescriptor("junit", "junit", "4.12"), repoManager);
}
public void testRunPackage() throws Exception {

View File

@@ -51,13 +51,13 @@ public class JUnitForkIntegrationTest extends AbstractTestFrameworkCompilingInte
JpsMavenRepositoryLibraryDescriptor junit4Lib =
new JpsMavenRepositoryLibraryDescriptor("junit", "junit", "4.12");
addLibs(myModule, junit4Lib, repoManager);
addLibs(module2, junit4Lib, repoManager);
addMavenLibs(myModule, junit4Lib, repoManager);
addMavenLibs(module2, junit4Lib, repoManager);
JpsMavenRepositoryLibraryDescriptor junit5Lib =
new JpsMavenRepositoryLibraryDescriptor("org.junit.jupiter", "junit-jupiter-api", "5.3.0");
addLibs(myModule, junit5Lib, repoManager);
addLibs(module2, junit5Lib, repoManager);
addMavenLibs(myModule, junit5Lib, repoManager);
addMavenLibs(module2, junit5Lib, repoManager);
}
public void testForkPerModule() throws ExecutionException {

View File

@@ -70,7 +70,7 @@ public class TestDiscoveryJUnitIntegrationTest extends AbstractTestFrameworkComp
entry.addSourceFolder(getTestContentRoot() + "/src", false);
entry.addSourceFolder(getTestContentRoot() + "/test", true);
});
addLibs(myModule, new JpsMavenRepositoryLibraryDescriptor("junit", "junit", myJUnitVersion), getRepoManager());
addMavenLibs(myModule, new JpsMavenRepositoryLibraryDescriptor("junit", "junit", myJUnitVersion), getRepoManager());
}
@Before

View File

@@ -52,7 +52,7 @@ public class TestNGIntegrationTest extends AbstractTestFrameworkCompilingIntegra
@Override
protected void setupModule() throws Exception {
super.setupModule();
addLibs(myModule, new JpsMavenRepositoryLibraryDescriptor("org.testng", "testng", myTestNGVersion), getRepoManager());
addMavenLibs(myModule, new JpsMavenRepositoryLibraryDescriptor("org.testng", "testng", myTestNGVersion), getRepoManager());
assertNotNull("Test annotation not found",
JavaPsiFacade.getInstance(getProject())
.findClass(TestNGUtil.TEST_ANNOTATION_FQN, GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule)));