Search in sources :

Example 16 with PsiMethod

use of com.intellij.psi.PsiMethod in project intellij-community by JetBrains.

the class JUnit4IntegrationTest method ignoredTestMethod.

@Test
public void ignoredTestMethod() throws Throwable {
    EdtTestUtil.runInEdtAndWait(() -> {
        PsiClass psiClass = findClass(getModule1(), CLASS_NAME);
        assertNotNull(psiClass);
        PsiMethod testMethod = psiClass.findMethodsByName(METHOD_NAME, false)[0];
        JUnitConfiguration configuration = createConfiguration(testMethod);
        Executor executor = DefaultRunExecutor.getRunExecutorInstance();
        RunnerAndConfigurationSettingsImpl settings = new RunnerAndConfigurationSettingsImpl(RunManagerImpl.getInstanceImpl(getProject()), configuration, false);
        ExecutionEnvironment environment = new ExecutionEnvironment(executor, ProgramRunnerUtil.getRunner(DefaultRunExecutor.EXECUTOR_ID, settings), settings, getProject());
        TestObject state = configuration.getState(executor, environment);
        JavaParameters parameters = state.getJavaParameters();
        parameters.setUseDynamicClasspath(getProject());
        GeneralCommandLine commandLine = parameters.toCommandLine();
        StringBuffer buf = new StringBuffer();
        StringBuffer err = new StringBuffer();
        OSProcessHandler process = new OSProcessHandler(commandLine);
        process.addProcessListener(new ProcessAdapter() {

            @Override
            public void onTextAvailable(ProcessEvent event, Key outputType) {
                String text = event.getText();
                try {
                    if (outputType == ProcessOutputTypes.STDOUT && !text.isEmpty() && ServiceMessage.parse(text.trim()) == null) {
                        buf.append(text);
                    }
                    if (outputType == ProcessOutputTypes.STDERR) {
                        err.append(text);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });
        process.startNotify();
        process.waitFor();
        process.destroyProcess();
        String testOutput = buf.toString();
        assertEmpty(err.toString());
        switch(myJUnitVersion) {
            //shouldn't work for old versions
            case "4.4":
            //shouldn't work for old versions
            case "4.5":
                break;
            default:
                assertTrue(testOutput, testOutput.contains("Test1"));
        }
    });
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) PsiMethod(com.intellij.psi.PsiMethod) ProcessEvent(com.intellij.execution.process.ProcessEvent) JUnitConfiguration(com.intellij.execution.junit.JUnitConfiguration) PsiClass(com.intellij.psi.PsiClass) TestObject(com.intellij.execution.junit.TestObject) DefaultRunExecutor(com.intellij.execution.executors.DefaultRunExecutor) Executor(com.intellij.execution.Executor) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) RunnerAndConfigurationSettingsImpl(com.intellij.execution.impl.RunnerAndConfigurationSettingsImpl) JavaParameters(com.intellij.execution.configurations.JavaParameters) ParseException(java.text.ParseException) Key(com.intellij.openapi.util.Key) Test(org.junit.Test)

Example 17 with PsiMethod

use of com.intellij.psi.PsiMethod in project intellij-community by JetBrains.

the class JUnit5AcceptanceTest method testFactoryMethods.

@Test
void testFactoryMethods() {
    doTest(() -> {
        PsiClass aClass = myFixture.addClass("class MyTest {@org.junit.jupiter.api.TestFactory java.util.List<org.junit.jupiter.api.DynamicTest> tests() {return null;}}");
        PsiMethod factoryMethod = aClass.getMethods()[0];
        assertNotNull(factoryMethod);
        assertTrue(JUnitUtil.isTestAnnotated(factoryMethod));
    });
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) Test(org.junit.jupiter.api.Test)

Example 18 with PsiMethod

use of com.intellij.psi.PsiMethod in project intellij-community by JetBrains.

the class JUnitRerunFailedTestsTest method testIncludeMethodsToRerunFromChildClass.

public void testIncludeMethodsToRerunFromChildClass() throws Exception {
    myFixture.addClass("abstract class ATest extends junit.framework.TestCase {" + "  public void testMe() {}\n" + "}");
    myFixture.addClass("public class ChildTest extends ATest {}");
    final SMTestProxy testProxy = new SMTestProxy("testMe", false, "java:test://ChildTest.testMe");
    final Project project = getProject();
    final GlobalSearchScope searchScope = GlobalSearchScope.projectScope(project);
    testProxy.setLocator(JavaTestLocator.INSTANCE);
    final Location location = testProxy.getLocation(project, searchScope);
    assertNotNull(location);
    assertInstanceOf(location, MethodLocation.class);
    //navigation to the method in abstract super class
    final PsiElement element = location.getPsiElement();
    assertInstanceOf(element, PsiMethod.class);
    final PsiMethod method = (PsiMethod) element;
    assertEquals("testMe", method.getName());
    final PsiClass containingClass = method.getContainingClass();
    assertNotNull(containingClass);
    assertEquals("ATest", containingClass.getQualifiedName());
    //include method "from" child class to rerun
    final String presentation = TestMethods.getTestPresentation(testProxy, project, searchScope);
    assertEquals("ChildTest,testMe", presentation);
}
Also used : SMTestProxy(com.intellij.execution.testframework.sm.runner.SMTestProxy) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) PsiElement(com.intellij.psi.PsiElement) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location)

Example 19 with PsiMethod

use of com.intellij.psi.PsiMethod in project intellij-community by JetBrains.

the class JUnitRerunFailedTestsTest method testInnerClass.

public void testInnerClass() throws Exception {
    myFixture.addClass("public class TestClass {\n" + "    public static class Tests extends junit.framework.TestCase {\n" + "        public void testFoo() throws Exception {}\n" + "    }\n" + "}");
    final SMTestProxy testProxy = new SMTestProxy("testFoo", false, "java:test://TestClass$Tests.testFoo");
    final Project project = getProject();
    final GlobalSearchScope searchScope = GlobalSearchScope.projectScope(project);
    testProxy.setLocator(JavaTestLocator.INSTANCE);
    Location location = testProxy.getLocation(project, searchScope);
    assertNotNull(location);
    PsiElement element = location.getPsiElement();
    assertTrue(element instanceof PsiMethod);
    String name = ((PsiMethod) element).getName();
    assertEquals("testFoo", name);
}
Also used : SMTestProxy(com.intellij.execution.testframework.sm.runner.SMTestProxy) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiMethod(com.intellij.psi.PsiMethod) PsiElement(com.intellij.psi.PsiElement) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location)

Example 20 with PsiMethod

use of com.intellij.psi.PsiMethod in project intellij-community by JetBrains.

the class TestMethods method getTestPresentation.

@Nullable
public static String getTestPresentation(AbstractTestProxy testInfo, Project project, GlobalSearchScope searchScope) {
    final Location location = testInfo.getLocation(project, searchScope);
    final PsiElement element = location != null ? location.getPsiElement() : null;
    if (element instanceof PsiMethod) {
        final PsiClass containingClass = location instanceof MethodLocation ? ((MethodLocation) location).getContainingClass() : location instanceof PsiMemberParameterizedLocation ? ((PsiMemberParameterizedLocation) location).getContainingClass() : ((PsiMethod) element).getContainingClass();
        if (containingClass != null) {
            final String proxyName = testInfo.getName();
            final String methodName = ((PsiMethod) element).getName();
            return JavaExecutionUtil.getRuntimeQualifiedName(containingClass) + "," + (proxyName.contains(methodName) ? proxyName.substring(proxyName.indexOf(methodName)) : methodName);
        }
    }
    return null;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) PsiElement(com.intellij.psi.PsiElement) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiMethod (com.intellij.psi.PsiMethod)229 PsiClass (com.intellij.psi.PsiClass)97 PsiElement (com.intellij.psi.PsiElement)71 ArrayList (java.util.ArrayList)24 NotNull (org.jetbrains.annotations.NotNull)22 Nullable (org.jetbrains.annotations.Nullable)19 Project (com.intellij.openapi.project.Project)16 PsiField (com.intellij.psi.PsiField)13 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)12 Location (com.intellij.execution.Location)11 JavaEvaluator (com.android.tools.klint.client.api.JavaEvaluator)9 PsiReference (com.intellij.psi.PsiReference)9 PsiFile (com.intellij.psi.PsiFile)8 List (java.util.List)7 Nullable (com.android.annotations.Nullable)6 Module (com.intellij.openapi.module.Module)6 PsiType (com.intellij.psi.PsiType)6 SearchScope (com.intellij.psi.search.SearchScope)6 PsiParameter (com.intellij.psi.PsiParameter)5 UExpression (org.jetbrains.uast.UExpression)5