com.intellij.psi.PsiClassOwner Java Examples

The following examples show how to use com.intellij.psi.PsiClassOwner. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CodeGeneratorAction.java    From code-generator with Apache License 2.0 6 votes vote down vote up
private Entity buildClassEntity(PsiClass psiClass) {
    PsiFile psiFile = psiClass.getContainingFile();
    String className = psiClass.getName();
    String packageName = ((PsiClassOwner) psiFile).getPackageName();

    List<Field> fields = Arrays.stream(psiClass.getAllFields()).map(field -> {
        String fieldType = field.getType().getPresentableText();
        String fieldName = field.getName();
        return new Field(fieldType, fieldName);
    }).collect(Collectors.toList());

    return Entity.builder()
        .name(className)
        .packageName(packageName)
        .fields(fields).build();
}
 
Example #2
Source File: Utils.java    From MavenHelper with Apache License 2.0 6 votes vote down vote up
public static String getQualifiedName(@Nullable PsiFile psiFile) {
	String result = NOT_RESOLVED;

	if (psiFile != null) {
		String packageName = null;
		if (psiFile instanceof PsiClassOwner) {
			packageName = ((PsiClassOwner) psiFile).getPackageName();
		}
		String name = psiFile.getName();
		name = StringUtils.substringBefore(name, ".");

		if (isNotBlank(packageName) && isNotBlank(name)) {
			result = packageName + "." + name;
		} else if (isNotBlank(name)) {
			result = name;
		}
	}

	return result;
}
 
Example #3
Source File: Utils.java    From MavenHelper with Apache License 2.0 6 votes vote down vote up
public static String getTestArgument(@Nullable PsiFile psiFile, ConfigurationContext context) {
	RunnerAndConfigurationSettings configuration = context.getConfiguration();
	String classAndMethod = null;
	if (configuration != null) {
		classAndMethod = configuration.getName().replace(".", "#");
	}

	String result;
	String packageName = null;
	if (psiFile instanceof PsiClassOwner) {
		packageName = ((PsiClassOwner) psiFile).getPackageName();
	}

	if (isNotBlank(packageName) && isNotBlank(classAndMethod)) {
		result = packageName + "." + classAndMethod;
	} else if (isNotBlank(classAndMethod)) {
		result = classAndMethod;
	} else {
		result = NOT_RESOLVED;
	}
	return result;
}
 
Example #4
Source File: RunTestFileAction.java    From MavenHelper with Apache License 2.0 6 votes vote down vote up
protected List<String> getGoals(AnActionEvent e, PsiClassOwner psiFile, MavenProject mavenProject) {
	List<String> goals = new ArrayList<String>();
	boolean skipTests = isSkipTests(mavenProject);
	// so many possibilities...
	if (skipTests || isExcludedFromSurefire(psiFile, mavenProject)) {
		MavenPlugin failsafePlugin = mavenProject.findPlugin("org.apache.maven.plugins", "maven-failsafe-plugin");
		if (failsafePlugin != null) {
               addFailSafeParameters(e, psiFile, goals, failsafePlugin);
           } else {
               addSurefireParameters(e, psiFile, goals);
           }
		goals.add("verify");
	} else {
		addSurefireParameters(e, psiFile, goals);
		goals.add("test-compile");
		goals.add("surefire:test");
	}

	return goals;
}
 
Example #5
Source File: RunTestFileAction.java    From MavenHelper with Apache License 2.0 6 votes vote down vote up
public void actionPerformed(AnActionEvent e) {
	MavenProject mavenProject = MavenActionUtil.getMavenProject(e.getDataContext());
	if (mavenProject != null) {

		PsiFile psiFile = LangDataKeys.PSI_FILE.getData(e.getDataContext());
		if (psiFile instanceof PsiClassOwner) {
			List<String> goals = getGoals(e, (PsiClassOwner) psiFile,
					MavenActionUtil.getMavenProject(e.getDataContext()));

			final DataContext context = e.getDataContext();
			MavenRunnerParameters params = new MavenRunnerParameters(true, mavenProject.getDirectory(), null, goals,
					MavenActionUtil.getProjectsManager(context).getExplicitProfiles());
			run(context, params);
		} else {
			Messages.showWarningDialog(e.getProject(), "Cannot run for current file", "Maven Test File");
		}
	}
}
 
Example #6
Source File: BlazeJavaTestEventsHandlerTest.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Test
public void testParameterizedMethodLocationResolves() {
  PsiFile javaFile =
      workspace.createPsiFile(
          new WorkspacePath("java/com/google/lib/JavaClass.java"),
          "package com.google.lib;",
          "public class JavaClass {",
          "  public void testMethod() {}",
          "}");
  PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0];
  PsiMethod method = javaClass.findMethodsByName("testMethod", false)[0];
  assertThat(method).isNotNull();

  String url =
      handler.testLocationUrl(
          Label.create("//java/com/google/lib:JavaClass"),
          null,
          "testMethod",
          "[0] true (testMethod)",
          "com.google.lib.JavaClass");
  Location<?> location = getLocation(url);
  assertThat(location.getPsiElement()).isEqualTo(method);
}
 
Example #7
Source File: BlazeJavaTestEventsHandlerTest.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Test
public void testMethodLocationResolves() {
  PsiFile javaFile =
      workspace.createPsiFile(
          new WorkspacePath("java/com/google/lib/JavaClass.java"),
          "package com.google.lib;",
          "public class JavaClass {",
          "  public void testMethod() {}",
          "}");
  PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0];
  PsiMethod method = javaClass.findMethodsByName("testMethod", false)[0];
  assertThat(method).isNotNull();

  String url =
      handler.testLocationUrl(
          Label.create("//java/com/google/lib:JavaClass"),
          null,
          null,
          "testMethod",
          "com.google.lib.JavaClass");
  Location<?> location = getLocation(url);
  assertThat(location.getPsiElement()).isEqualTo(method);
}
 
Example #8
Source File: BlazeJavaTestEventsHandlerTest.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuiteLocationResolves() {
  PsiFile javaFile =
      workspace.createPsiFile(
          new WorkspacePath("java/com/google/lib/JavaClass.java"),
          "package com.google.lib;",
          "public class JavaClass {}");
  PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0];
  assertThat(javaClass).isNotNull();

  String url =
      handler.suiteLocationUrl(
          Label.create("//java/com/google/lib:JavaClass"), null, "com.google.lib.JavaClass");
  Location<?> location = getLocation(url);
  assertThat(location.getPsiElement()).isEqualTo(javaClass);
}
 
Example #9
Source File: BlazeJavaAbstractTestCaseConfigurationProducerTest.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Test
public void testIgnoreAbstractTestClassWithNoTestSubclasses() {
  PsiFile javaFile =
      createAndIndexFile(
          new WorkspacePath("java/com/google/test/TestClass.java"),
          "package com.google.test;",
          "@org.junit.runner.RunWith(org.junit.runners.JUnit4.class)",
          "public abstract class TestClass {",
          "  @org.junit.Test",
          "  public void testMethod1() {}",
          "  @org.junit.Test",
          "  public void testMethod2() {}",
          "}");

  PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0];
  assertThat(javaClass).isNotNull();

  ConfigurationContext context = createContextFromPsi(javaClass);
  ConfigurationFromContext fromContext =
      new BlazeJavaAbstractTestCaseConfigurationProducer()
          .createConfigurationFromContext(context);
  assertThat(fromContext).isNull();
}
 
Example #10
Source File: BlazeJavaAbstractTestCaseConfigurationProducerTest.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Test
public void testIgnoreTestClassWithNoTestSubclasses() {
  PsiFile javaFile =
      createAndIndexFile(
          new WorkspacePath("java/com/google/test/TestClass.java"),
          "package com.google.test;",
          "@org.junit.runner.RunWith(org.junit.runners.JUnit4.class)",
          "public class TestClass {",
          "  @org.junit.Test",
          "  public void testMethod1() {}",
          "  @org.junit.Test",
          "  public void testMethod2() {}",
          "}");

  PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0];
  assertThat(javaClass).isNotNull();

  ConfigurationContext context = createContextFromPsi(javaClass);
  ConfigurationFromContext fromContext =
      new BlazeJavaAbstractTestCaseConfigurationProducer()
          .createConfigurationFromContext(context);
  assertThat(fromContext).isNull();
}
 
Example #11
Source File: JavaClassQualifiedNameReferenceTest.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Test
public void testReferencesJavaClass() {
  PsiFile javaFile =
      workspace.createPsiFile(
          new WorkspacePath("java/com/google/bin/Main.java"),
          "package com.google.bin;",
          "public class Main {",
          "  public void main() {}",
          "}");
  PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0];

  BuildFile file =
      createBuildFile(
          new WorkspacePath("java/com/google/BUILD"),
          "java_binary(",
          "    name = 'binary',",
          "    main_class = 'com.google.bin.Main',",
          ")");

  ArgumentList args = file.firstChildOfClass(FuncallExpression.class).getArgList();
  assertThat(args.getKeywordArgument("main_class").getValue().getReferencedElement())
      .isEqualTo(javaClass);
}
 
Example #12
Source File: QualifiedClassNameHeuristic.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matchesSource(
    Project project,
    TargetInfo target,
    @Nullable PsiFile sourcePsiFile,
    File sourceFile,
    @Nullable TestSize testSize) {
  if (!(sourcePsiFile instanceof PsiClassOwner)) {
    return false;
  }
  String targetName = target.label.targetName().toString();
  if (!targetName.contains(".")) {
    return false;
  }
  return ReadAction.compute(() -> doMatchesSource((PsiClassOwner) sourcePsiFile, targetName));
}
 
Example #13
Source File: ClassPackagePathHeuristic.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matchesSource(
    Project project,
    TargetInfo target,
    @Nullable PsiFile sourcePsiFile,
    File sourceFile,
    @Nullable TestSize testSize) {
  if (!(sourcePsiFile instanceof PsiClassOwner)) {
    return false;
  }
  String targetName = target.label.targetName().toString();
  if (!targetName.contains("/")) {
    return false;
  }
  return ReadAction.compute(() -> doMatchesSource((PsiClassOwner) sourcePsiFile, targetName));
}
 
Example #14
Source File: BlazeAndroidTestEventsHandlerTest.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuiteLocationResolves() {
  PsiFile javaFile =
      workspace.createPsiFile(
          new WorkspacePath("java/com/google/lib/JavaClass.java"),
          "package com.google.lib;",
          "import org.junit.Test;",
          "import org.junit.runner.RunWith;",
          "import org.junit.runners.JUnit4;",
          "@RunWith(JUnit4.class)",
          "public class JavaClass {",
          "  @Test",
          "  public void testMethod() {}",
          "}");
  PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0];
  assertThat(javaClass).isNotNull();

  String url =
      handler.suiteLocationUrl(
          Label.create("//java/com/google/lib:JavaClass"), null, "JavaClass");
  Location<?> location = getLocation(url);
  assertThat(location.getPsiElement()).isEqualTo(javaClass);
}
 
Example #15
Source File: TestClassHeuristic.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matchesSource(
    Project project,
    TargetInfo target,
    @Nullable PsiFile sourcePsiFile,
    File sourceFile,
    @Nullable TestSize testSize) {
  if (!(sourcePsiFile instanceof PsiClassOwner)) {
    return false;
  }
  if (target.testClass == null) {
    return false;
  }
  return ReadAction.compute(
      () -> doMatchesSource((PsiClassOwner) sourcePsiFile, target.testClass));
}
 
Example #16
Source File: ProducerUtils.java    From intellij with Apache License 2.0 6 votes vote down vote up
/** Same as {@link JUnitUtil#getTestClass}, but handles classes outside the project. */
@Nullable
public static PsiClass getTestClass(final Location<?> location) {
  for (Iterator<Location<PsiClass>> iterator = location.getAncestors(PsiClass.class, false);
      iterator.hasNext(); ) {
    final Location<PsiClass> classLocation = iterator.next();
    if (isTestClass(classLocation.getPsiElement())) {
      return classLocation.getPsiElement();
    }
  }
  PsiElement element = location.getPsiElement();
  if (element instanceof PsiClassOwner) {
    PsiClass[] classes = ((PsiClassOwner) element).getClasses();
    if (classes.length == 1 && isTestClass(classes[0])) {
      return classes[0];
    }
  }
  return null;
}
 
Example #17
Source File: QualifiedClassNameHeuristic.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static boolean doMatchesSource(PsiClassOwner source, String targetName) {
  for (PsiClass psiClass : source.getClasses()) {
    String fqcn = psiClass.getQualifiedName();
    if (fqcn != null && fqcn.endsWith(targetName)) {
      return true;
    }
  }
  return false;
}
 
Example #18
Source File: LithoPluginIntellijTest.java    From litho with Apache License 2.0 5 votes vote down vote up
/**
 * Converts list of class names to the list of psi classes.
 *
 * @param handler calling class should pass handler for the psi classes. Passes null if
 *     conversion was unsuccessful at any step.
 * @param clsNames names of the classes to found in the test root directory (MyClass.java)
 */
public void getPsiClass(Function<List<PsiClass>, Boolean> handler, String... clsNames) {
  ApplicationManager.getApplication()
      .invokeAndWait(
          () -> {
            List<PsiClass> psiClasses =
                Stream.of(clsNames)
                    .filter(Objects::nonNull)
                    .map(
                        clsName -> {
                          String content = getContentOrNull(clsName);
                          if (content != null) {
                            return PsiFileFactory.getInstance(fixture.getProject())
                                .createFileFromText(clsName, JavaFileType.INSTANCE, content);
                          }
                          return null;
                        })
                    .filter(PsiJavaFile.class::isInstance)
                    .map(PsiJavaFile.class::cast)
                    .map(PsiClassOwner::getClasses)
                    .filter(fileClasses -> fileClasses.length > 0)
                    .map(fileClasses -> fileClasses[0])
                    .collect(Collectors.toList());
            if (psiClasses.isEmpty()) {
              handler.apply(null);
            } else {
              handler.apply(psiClasses);
            }
          });
}
 
Example #19
Source File: RunTestFileAction.java    From MavenHelper with Apache License 2.0 5 votes vote down vote up
@NotNull
private String getPsiFilePath(PsiClassOwner psiFile) {
	String packageName = psiFile.getPackageName();
	String fullName;
	if (packageName.isEmpty()) {
		fullName = psiFile.getName();
	} else {
		fullName = packageName.replace(".", "/") + "/" + psiFile.getVirtualFile().getName();
	}
	return fullName;
}
 
Example #20
Source File: RunTestFileAction.java    From MavenHelper with Apache License 2.0 5 votes vote down vote up
private boolean isExcludedFromSurefire(PsiClassOwner psiFile, MavenProject mavenProject) {
	boolean excluded = false;
	try {
		Element pluginConfiguration = mavenProject.getPluginConfiguration("org.apache.maven.plugins",
				"maven-surefire-plugin");
		excluded = false;
		String fullName = null;
		if (pluginConfiguration != null) {
			Element excludes = pluginConfiguration.getChild("excludes");
			if (excludes != null) {
				List<Element> exclude = excludes.getChildren("exclude");
				for (Element element : exclude) {
					if (fullName == null) {
						fullName = getPsiFilePath(psiFile);
					}
					excluded = matchClassRegexPatter(fullName, element.getText());
					if (excluded) {
						break;
					}
				}
			}
		}
	} catch (Exception e) {
		LOG.warn(e);
	}
	return excluded;
}
 
Example #21
Source File: RunTestFileAction.java    From MavenHelper with Apache License 2.0 5 votes vote down vote up
private void addFailSafeParameters(AnActionEvent e, PsiClassOwner psiFile, List<String> goals, MavenPlugin mavenProjectPlugin) {
	ComparableVersion version = new ComparableVersion(mavenProjectPlugin.getVersion());
	ComparableVersion minimumForMethodTest = new ComparableVersion("2.7.3");
	if (minimumForMethodTest.compareTo(version) == 1) {
		goals.add("-Dit.test=" + Utils.getTestArgumentWithoutMethod(e, psiFile));
       } else {
		goals.add("-Dit.test=" + Utils.getTestArgument(psiFile, ConfigurationContext.getFromContext(e.getDataContext())));
       }
}
 
Example #22
Source File: BlazeAndroidTestEventsHandlerTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Test
public void testMethodLocationResolves() {
  PsiFile javaFile =
      workspace.createPsiFile(
          new WorkspacePath("java/com/google/lib/JavaClass.java"),
          "package com.google.lib;",
          "import org.junit.Test;",
          "import org.junit.runner.RunWith;",
          "import org.junit.runners.JUnit4;",
          "@RunWith(JUnit4.class)",
          "public class JavaClass {",
          "  @Test",
          "  public void testMethod() {}",
          "}");
  PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0];
  PsiMethod method = javaClass.findMethodsByName("testMethod", false)[0];
  assertThat(method).isNotNull();

  String url =
      handler.testLocationUrl(
          Label.create("//java/com/google/lib:JavaClass"),
          null,
          null,
          "JavaClass-testMethod",
          "com.google.test.AndroidTestBase");
  Location<?> location = getLocation(url);
  assertThat(location.getPsiElement()).isEqualTo(method);
}
 
Example #23
Source File: BlazeSourceJarNavigationPolicy.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private Result<PsiFile> getSourceFileResult(ClsFileImpl clsFile, VirtualFile root) {
  // This code is adapted from JavaPsiImplementationHelperImpl#getClsFileNavigationElement
  PsiClass[] classes = clsFile.getClasses();
  if (classes.length == 0) {
    return null;
  }

  String sourceFileName = ((ClsClassImpl) classes[0]).getSourceFileName();
  String packageName = clsFile.getPackageName();
  String relativePath =
      packageName.isEmpty()
          ? sourceFileName
          : packageName.replace('.', '/') + '/' + sourceFileName;

  VirtualFile source = root.findFileByRelativePath(relativePath);
  if (source != null && source.isValid()) {
    // Since we have an actual source jar tracked down, use that source jar as the modification
    // tracker. This means the result will continue to be cached unless that source jar changes.
    // If we didn't find a source jar, we use a modification tracker that invalidates on every
    // Blaze sync, which is less efficient.
    PsiFile psiSource = clsFile.getManager().findFile(source);
    if (psiSource instanceof PsiClassOwner) {
      return Result.create(psiSource, source);
    }
    return Result.create(null, source);
  }

  return null;
}
 
Example #24
Source File: SourceJarGenerator.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
private static Optional<String> findPackageName(PsiFile psiFile) {
  if (psiFile instanceof PsiClassOwner) {
    String packageName = ((PsiClassOwner) psiFile).getPackageName();
    if (!packageName.equals("")) return Optional.of(packageName);
  }
  return Optional.empty();
}
 
Example #25
Source File: ExternalFilePositionManager.java    From intellij with Apache License 2.0 5 votes vote down vote up
private void indexQualifiedClassNames(PsiFile psiFile) {
  if (!(psiFile instanceof PsiClassOwner)) {
    return;
  }
  ReadAction.run(
      () -> {
        for (PsiClass psiClass : ((PsiClassOwner) psiFile).getClasses()) {
          qualifiedNameToPsiFile.put(psiClass.getQualifiedName(), psiFile);
        }
      });
}
 
Example #26
Source File: ClassPackagePathHeuristic.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static boolean doMatchesSource(PsiClassOwner source, String targetName) {
  for (PsiClass psiClass : source.getClasses()) {
    String qualifiedName = psiClass.getQualifiedName();
    if (qualifiedName == null) {
      continue;
    }
    String classPackagePath = psiClass.getQualifiedName().replace('.', '/');
    if (targetName.contains(classPackagePath)) {
      return true;
    }
  }
  return false;
}
 
Example #27
Source File: BlazeJavaWebTestEventsHandlerTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Test
public void testSuiteLocationResolves() {
  TargetMap targetMap =
      TargetMapBuilder.builder()
          .addTarget(
              TargetIdeInfo.builder()
                  .setLabel("//java/com/google/lib:JavaClass_chrome-linux")
                  .setKind("java_web_test")
                  .setBuildFile(src("java/com/google/lib/BUILD"))
                  .addDependency("//java/com/google/lib:JavaClass_wrapped_test"))
          .addTarget(
              TargetIdeInfo.builder()
                  .setLabel("//java/com/google/lib:JavaClass_wrapped_test")
                  .setKind("java_test")
                  .setBuildFile(src("java/com/google/lib/BUILD"))
                  .addSource(src("java/com/google/lib/JavaClass.java")))
          .build();

  registerProjectService(
      BlazeProjectDataManager.class,
      new MockBlazeProjectDataManager(
          MockBlazeProjectDataBuilder.builder(workspaceRoot).setTargetMap(targetMap).build()));

  PsiFile javaFile =
      workspace.createPsiFile(
          new WorkspacePath("java/com/google/lib/JavaClass.java"),
          "package com.google.lib;",
          "public class JavaClass {}");
  PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0];
  assertThat(javaClass).isNotNull();

  String url =
      handler.suiteLocationUrl(
          Label.create("//java/com/google/lib:JavaClass_chrome-linux"),
          RuleTypes.JAVA_WEB_TEST.getKind(),
          "com.google.lib.JavaClass");
  Location<?> location = getLocation(url);
  assertThat(location).isNotNull();
  assertThat(location.getPsiElement()).isEqualTo(javaClass);
}
 
Example #28
Source File: BlazeJUnitTestFilterFlagsIntegrationTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Test
public void testParameterizedMethods() {
  PsiFile javaFile =
      workspace.createPsiFile(
          new WorkspacePath("java/com/google/lib/JavaClass.java"),
          "package com.google.lib;",
          "import org.junit.Test;",
          "import org.junit.runner.RunWith;",
          "import org.junit.runners.JUnit4;",
          "@RunWith(JUnit4.class)",
          "public class JavaClass {",
          "  @Test",
          "  public void testMethod1() {}",
          "  @Test",
          "  public void testMethod2() {}",
          "}");
  PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0];

  PsiMethod method1 = javaClass.findMethodsByName("testMethod1", false)[0];
  Location<?> location1 =
      new PsiMemberParameterizedLocation(getProject(), method1, javaClass, "[param]");

  PsiMethod method2 = javaClass.findMethodsByName("testMethod2", false)[0];
  Location<?> location2 =
      new PsiMemberParameterizedLocation(getProject(), method2, javaClass, "[3]");

  assertThat(
          BlazeJUnitTestFilterFlags.testFilterForClassesAndMethods(
              ImmutableMap.of(javaClass, ImmutableList.of(location1, location2))))
      .isEqualTo("com.google.lib.JavaClass#(testMethod1\\[param\\]|testMethod2\\[3\\])$");
}
 
Example #29
Source File: BlazeWebJavaTestEventsHandlerTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Test
public void testSuiteLocationResolves() {
  TargetMap targetMap =
      TargetMapBuilder.builder()
          .addTarget(
              TargetIdeInfo.builder()
                  .setLabel("//java/com/google/lib:JavaClass_chrome-linux")
                  .setKind("web_test")
                  .setBuildFile(src("java/com/google/lib/BUILD"))
                  .addDependency("//java/com/google/lib:JavaClass_wrapped_test"))
          .addTarget(
              TargetIdeInfo.builder()
                  .setLabel("//java/com/google/lib:JavaClass_wrapped_test")
                  .setKind("java_test")
                  .setBuildFile(src("java/com/google/lib/BUILD"))
                  .addSource(src("java/com/google/lib/JavaClass.java")))
          .build();

  registerProjectService(
      BlazeProjectDataManager.class,
      new MockBlazeProjectDataManager(
          MockBlazeProjectDataBuilder.builder(workspaceRoot).setTargetMap(targetMap).build()));

  PsiFile javaFile =
      workspace.createPsiFile(
          new WorkspacePath("java/com/google/lib/JavaClass.java"),
          "package com.google.lib;",
          "public class JavaClass {}");
  PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0];
  assertThat(javaClass).isNotNull();

  String url =
      handler.suiteLocationUrl(
          Label.create("//java/com/google/lib:JavaClass_chrome-linux"),
          RuleTypes.WEB_TEST.getKind(),
          "com.google.lib.JavaClass");
  Location<?> location = getLocation(url);
  assertThat(location).isNotNull();
  assertThat(location.getPsiElement()).isEqualTo(javaClass);
}
 
Example #30
Source File: JUnitTestHeuristic.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private JUnitVersion junitVersion(@Nullable PsiFile psiFile) {
  if (!(psiFile instanceof PsiClassOwner)) {
    return null;
  }
  return ReadAction.compute(() -> junitVersion((PsiClassOwner) psiFile));
}