Search in sources :

Example 11 with SourceScope

use of com.intellij.execution.testframework.SourceScope in project intellij-community by JetBrains.

the class TestObject method createJavaParameters.

@Override
protected JavaParameters createJavaParameters() throws ExecutionException {
    JavaParameters javaParameters = super.createJavaParameters();
    javaParameters.setMainClass(JUnitConfiguration.JUNIT_START_CLASS);
    javaParameters.getProgramParametersList().add(JUnitStarter.IDE_VERSION + JUnitStarter.VERSION);
    final StringBuilder buf = new StringBuilder();
    collectListeners(javaParameters, buf, IDEAJUnitListener.EP_NAME, "\n");
    if (buf.length() > 0) {
        try {
            myListenersFile = FileUtil.createTempFile("junit_listeners_", "", true);
            javaParameters.getProgramParametersList().add("@@" + myListenersFile.getPath());
            FileUtil.writeToFile(myListenersFile, buf.toString().getBytes(CharsetToolkit.UTF8_CHARSET));
        } catch (IOException e) {
            LOG.error(e);
        }
    }
    final Project project = getConfiguration().getProject();
    final SourceScope sourceScope = getSourceScope();
    if (isJUnit5(getConfiguration().getConfigurationModule().getModule(), sourceScope, project)) {
        javaParameters.getProgramParametersList().add(JUnitStarter.JUNIT5_PARAMETER);
        javaParameters.getClassPath().add(PathUtil.getJarPathForClass(JUnit5IdeaTestRunner.class));
        final PathsList classPath = javaParameters.getClassPath();
        classPath.add(PathUtil.getJarPathForClass(TestExecutionListener.class));
        classPath.add(PathUtil.getJarPathForClass(JupiterTestEngine.class));
        classPath.add(PathUtil.getJarPathForClass(JUnitException.class));
        classPath.add(PathUtil.getJarPathForClass(TestEngine.class));
        classPath.add(PathUtil.getJarPathForClass(JUnitPlatform.class));
        try {
            JUnitUtil.getTestCaseClass(sourceScope);
            classPath.add(PathUtil.getJarPathForClass(VintageTestEngine.class));
        } catch (JUnitUtil.NoJUnitException ignore) {
        }
    }
    return javaParameters;
}
Also used : JupiterTestEngine(org.junit.jupiter.engine.JupiterTestEngine) TestExecutionListener(org.junit.platform.launcher.TestExecutionListener) JUnitException(org.junit.platform.commons.JUnitException) JUnit5IdeaTestRunner(com.intellij.junit5.JUnit5IdeaTestRunner) IOException(java.io.IOException) VintageTestEngine(org.junit.vintage.engine.VintageTestEngine) Project(com.intellij.openapi.project.Project) SourceScope(com.intellij.execution.testframework.SourceScope) PathsList(com.intellij.util.PathsList) JavaParameters(com.intellij.execution.configurations.JavaParameters) TestEngine(org.junit.platform.engine.TestEngine) JupiterTestEngine(org.junit.jupiter.engine.JupiterTestEngine) VintageTestEngine(org.junit.vintage.engine.VintageTestEngine) JUnitPlatform(org.junit.platform.runner.JUnitPlatform)

Example 12 with SourceScope

use of com.intellij.execution.testframework.SourceScope in project intellij-community by JetBrains.

the class TestNGTestPattern method fillTestObjects.

public static void fillTestObjects(final Map<PsiClass, Map<PsiMethod, List<String>>> classes, final Set<String> patterns, final TestSearchScope testSearchScope, final ModuleBasedConfiguration config, final GlobalSearchScope searchScope) throws CantRunException {
    for (final String pattern : patterns) {
        final String className;
        final String methodName;
        if (pattern.contains(",")) {
            methodName = StringUtil.getShortName(pattern, ',');
            className = StringUtil.getPackageName(pattern, ',');
        } else {
            className = pattern;
            methodName = null;
        }
        final PsiClass psiClass = ApplicationManager.getApplication().runReadAction(new Computable<PsiClass>() {

            @Nullable
            @Override
            public PsiClass compute() {
                return ClassUtil.findPsiClass(PsiManager.getInstance(config.getProject()), className.replace('/', '.'), null, true, searchScope);
            }
        });
        if (psiClass != null) {
            final Boolean hasTest = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

                @Override
                public Boolean compute() {
                    return TestNGUtil.hasTest(psiClass);
                }
            });
            if (hasTest) {
                if (StringUtil.isEmpty(methodName)) {
                    calculateDependencies(null, classes, searchScope, psiClass);
                } else {
                    collectTestMethods(classes, psiClass, methodName, searchScope);
                }
            } else {
                throw new CantRunException("No tests found in class " + className);
            }
        }
    }
    if (classes.size() != patterns.size()) {
        final List<Pattern> compilePatterns = new ArrayList<>();
        for (String p : patterns) {
            final Pattern compilePattern;
            try {
                compilePattern = Pattern.compile(p);
            } catch (PatternSyntaxException e) {
                continue;
            }
            compilePatterns.add(compilePattern);
        }
        final SourceScope sourceScope = testSearchScope.getSourceScope(config);
        TestClassFilter projectFilter = new TestClassFilter(sourceScope != null ? sourceScope.getGlobalSearchScope() : GlobalSearchScope.allScope(config.getProject()), config.getProject(), true, true) {

            @Override
            public boolean isAccepted(PsiClass psiClass) {
                if (super.isAccepted(psiClass)) {
                    final String qualifiedName = ReadAction.compute(psiClass::getQualifiedName);
                    LOG.assertTrue(qualifiedName != null);
                    for (Pattern pattern : compilePatterns) {
                        if (pattern.matcher(qualifiedName).matches())
                            return true;
                    }
                }
                return false;
            }
        };
        calculateDependencies(null, classes, searchScope, TestNGUtil.getAllTestClasses(projectFilter, false));
        if (classes.size() == 0) {
            throw new CantRunException("No tests found in for patterns \"" + StringUtil.join(patterns, " || ") + '\"');
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) PsiClass(com.intellij.psi.PsiClass) ArrayList(java.util.ArrayList) CantRunException(com.intellij.execution.CantRunException) SourceScope(com.intellij.execution.testframework.SourceScope) Nullable(org.jetbrains.annotations.Nullable) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 13 with SourceScope

use of com.intellij.execution.testframework.SourceScope in project intellij-community by JetBrains.

the class TestPackage method createJavaParameters.

@Override
protected JavaParameters createJavaParameters() throws ExecutionException {
    final JavaParameters javaParameters = super.createJavaParameters();
    final JUnitConfiguration.Data data = getConfiguration().getPersistentData();
    final Project project = getConfiguration().getProject();
    final SourceScope sourceScope = data.getScope().getSourceScope(getConfiguration());
    if (sourceScope == null || !isJUnit5(getConfiguration().getConfigurationModule().getModule(), sourceScope, project)) {
        //check for junit 5
        JUnitUtil.checkTestCase(sourceScope, project);
    }
    createTempFiles(javaParameters);
    createServerSocket(javaParameters);
    return javaParameters;
}
Also used : Project(com.intellij.openapi.project.Project) SourceScope(com.intellij.execution.testframework.SourceScope) JavaParameters(com.intellij.execution.configurations.JavaParameters)

Aggregations

SourceScope (com.intellij.execution.testframework.SourceScope)13 Module (com.intellij.openapi.module.Module)3 Project (com.intellij.openapi.project.Project)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 CantRunException (com.intellij.execution.CantRunException)2 JavaParameters (com.intellij.execution.configurations.JavaParameters)2 RuntimeConfigurationException (com.intellij.execution.configurations.RuntimeConfigurationException)2 PsiClass (com.intellij.psi.PsiClass)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 JUnitConfiguration (com.intellij.execution.junit.JUnitConfiguration)1 SearchForTestsTask (com.intellij.execution.testframework.SearchForTestsTask)1 TestSearchScope (com.intellij.execution.testframework.TestSearchScope)1 JUnit5IdeaTestRunner (com.intellij.junit5.JUnit5IdeaTestRunner)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PathsList (com.intellij.util.PathsList)1 TestNGConfiguration (com.theoryinpractice.testng.configuration.TestNGConfiguration)1 THashSet (gnu.trove.THashSet)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1