Search in sources :

Example 11 with RunWith

use of org.junit.runner.RunWith in project intellij by bazelbuild.

the class BlazeScalaTestRunLineMarkerContributorTest method testGetSpecs2TestInfo.

@Test
public void testGetSpecs2TestInfo() {
    createAndIndexFile(WorkspacePath.createIfValid("scala/org/junit/runner/RunWith.scala"), "package org.junit.runner", "class RunWith");
    createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/org/specs2/runner/JUnitRunner.scala"), "package org.specs2.runner", "class JUnitRunner");
    createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/org/specs2/mutable/SpecificationWithJUnit.scala"), "package org.specs2.mutable", "@org.junit.runner.RunWith(classOf[org.specs2.runner.JUnitRunner])", "abstract class SpecificationWithJUnit extends org.specs2.mutable.Specification");
    createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/org/specs2/mutable/Specification.scala"), "package org.specs2.mutable", "abstract class Specification extends org.specs2.mutable.SpecificationLike");
    createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/org/specs2/mutable/SpecificationLike.scala"), "package org.specs2.mutable", "trait SpecificationLike extends", "org.specs2.specification.core.mutable.SpecificationStructure");
    createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/org/specs2/specification/core/mutable/SpecificationStructure.scala"), "package org.specs2.specification.core.mutable", "trait SpecificationStructure extends", "org.specs2.specification.core.SpecificationStructure");
    createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/org/specs2/specification/core/SpecificationStructure.scala"), "package org.specs2.specification.core", "trait SpecificationStructure");
    PsiFile specs2TestFile = createAndIndexFile(WorkspacePath.createIfValid("src/test/scala/com/google/test/Specs2Test.scala"), "package com.google.test", "class Specs2Test extends org.specs2.mutable.SpecificationWithJUnit");
    List<LeafPsiElement> elements = PsiUtils.findAllChildrenOfClassRecursive(specs2TestFile, LeafPsiElement.class);
    LeafPsiElement classIdentifier = elements.stream().filter(e -> Objects.equal(e.getText(), "Specs2Test")).findFirst().orElse(null);
    assertThat(classIdentifier).isNotNull();
    Info info = markerContributor.getInfo(classIdentifier);
    assertThat(info).isNotNull();
    assertThat(info.icon).isEqualTo(AllIcons.RunConfigurations.TestState.Run_run);
    assertThat(info.actions).hasLength(2);
    assertThat(info.actions[0].getTemplatePresentation().getText()).startsWith("Run ");
    assertThat(info.actions[1].getTemplatePresentation().getText()).startsWith("Debug ");
    elements.stream().filter(e -> !Objects.equal(e, classIdentifier)).forEach(e -> assertThat(markerContributor.getInfo(e)).isNull());
}
Also used : AllIcons(com.intellij.icons.AllIcons) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) RunWith(org.junit.runner.RunWith) PsiUtils(com.google.idea.blaze.base.lang.buildfile.psi.util.PsiUtils) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) List(java.util.List) PsiFile(com.intellij.psi.PsiFile) Info(com.intellij.execution.lineMarker.RunLineMarkerContributor.Info) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) Objects(com.google.common.base.Objects) BlazeRunConfigurationProducerTestCase(com.google.idea.blaze.base.run.producer.BlazeRunConfigurationProducerTestCase) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiFile(com.intellij.psi.PsiFile) Info(com.intellij.execution.lineMarker.RunLineMarkerContributor.Info) Test(org.junit.Test)

Example 12 with RunWith

use of org.junit.runner.RunWith in project hbase by apache.

the class HBaseClassTestRule method getNumParameters.

/**
 * @param clazz Test class that is running.
 * @return the number of parameters for this given test class. If the test is not parameterized or
 *   if there is any issue determining the number of parameters, returns 1.
 */
static int getNumParameters(Class<?> clazz) {
    RunWith[] runWiths = clazz.getAnnotationsByType(RunWith.class);
    boolean testParameterized = runWiths != null && Arrays.stream(runWiths).anyMatch((r) -> r.value().equals(Parameterized.class));
    if (!testParameterized) {
        return 1;
    }
    for (Method method : clazz.getMethods()) {
        if (!isParametersMethod(method)) {
            continue;
        }
        // Found the parameters method. Figure out the number of parameters.
        Object parameters;
        try {
            parameters = method.invoke(clazz);
        } catch (IllegalAccessException | InvocationTargetException e) {
            LOG.warn("Error invoking parameters method {} in test class {}", method.getName(), clazz, e);
            continue;
        }
        if (parameters instanceof List) {
            return ((List) parameters).size();
        } else if (parameters instanceof Collection) {
            return ((Collection) parameters).size();
        } else if (parameters instanceof Iterable) {
            return Iterables.size((Iterable) parameters);
        } else if (parameters instanceof Object[]) {
            return ((Object[]) parameters).length;
        }
    }
    LOG.warn("Unable to determine parameters size. Returning the default of 1.");
    return 1;
}
Also used : Statement(org.junit.runners.model.Statement) Arrays(java.util.Arrays) TestRule(org.junit.rules.TestRule) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) LoggerFactory(org.slf4j.LoggerFactory) IntegrationTests(org.apache.hadoop.hbase.testclassification.IntegrationTests) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Timeout(org.junit.rules.Timeout) Method(java.lang.reflect.Method) Parameterized(org.junit.runners.Parameterized) Logger(org.slf4j.Logger) Iterables(org.apache.hbase.thirdparty.com.google.common.collect.Iterables) MediumTests(org.apache.hadoop.hbase.testclassification.MediumTests) Collection(java.util.Collection) Set(java.util.Set) LargeTests(org.apache.hadoop.hbase.testclassification.LargeTests) Sets(org.apache.hbase.thirdparty.com.google.common.collect.Sets) Description(org.junit.runner.Description) Category(org.junit.experimental.categories.Category) InvocationTargetException(java.lang.reflect.InvocationTargetException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) InterfaceAudience(org.apache.yetus.audience.InterfaceAudience) Modifier(java.lang.reflect.Modifier) SmallTests(org.apache.hadoop.hbase.testclassification.SmallTests) Collections(java.util.Collections) Method(java.lang.reflect.Method) RunWith(org.junit.runner.RunWith) InvocationTargetException(java.lang.reflect.InvocationTargetException) Collection(java.util.Collection) List(java.util.List)

Aggregations

RunWith (org.junit.runner.RunWith)12 Method (java.lang.reflect.Method)5 List (java.util.List)3 JUnit4 (org.junit.runners.JUnit4)3 IOException (java.io.IOException)2 Annotation (java.lang.annotation.Annotation)2 Modifier (java.lang.reflect.Modifier)2 Arrays (java.util.Arrays)2 Set (java.util.Set)2 Suite (org.junit.runners.Suite)2 Application (cn.edu.zjnu.acm.judge.Application)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Objects (com.google.common.base.Objects)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 Gson (com.google.gson.Gson)1 PsiUtils (com.google.idea.blaze.base.lang.buildfile.psi.util.PsiUtils)1 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)1 BlazeRunConfigurationProducerTestCase (com.google.idea.blaze.base.run.producer.BlazeRunConfigurationProducerTestCase)1 Info (com.intellij.execution.lineMarker.RunLineMarkerContributor.Info)1