Java Code Examples for org.junit.runners.model.FrameworkMethod#getAnnotation()

The following examples show how to use org.junit.runners.model.FrameworkMethod#getAnnotation() . 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: CmmnTestRunner.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) {
    if (method.getAnnotation(Ignore.class) == null && method.getAnnotation(CmmnDeployment.class) != null) {

        List<FrameworkMethod> befores = getTestClass().getAnnotatedMethods(Before.class);

        return new Statement() {

            @Override
            public void evaluate() throws Throwable {
                for (FrameworkMethod before : befores) {
                    before.invokeExplosively(target);
                }
                deploymentId = deployCmmnDefinition(method);
                statement.evaluate();
            }

        };
    } else {
        return super.withBefores(method, target, statement);
    }

}
 
Example 2
Source File: LocalRemoteTestRunner.java    From jetty-runtime with Apache License 2.0 6 votes vote down vote up
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
  Description description = describeChild(method);

  // check Ignore first
  if (method.getAnnotation(Ignore.class) != null) {
    notify("@Ignore", description);
    notifier.fireTestIgnored(description);

  } else if (localTestsEnabled && method.getAnnotation(RemoteOnly.class) != null) {

    // if running in local mode and @RemoteOnly annotation exists, ignore
    notify("Skip @RemoteOnly", description);
    notifier.fireTestIgnored(description);
  } else if (remoteTestsEnabled && method.getAnnotation(LocalOnly.class) != null) {

    // if running in remote mode and @LocalOnly annotation exists, ignore
    notify("Skip @LocalOnly", description);
    notifier.fireTestIgnored(description);
  } else {
    // default is run in either mode
    notify("Test[" + mode + "]", description);
    super.runChild(method, notifier);
  }
}
 
Example 3
Source File: IronJacamar.java    From ironjacamar with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Filter and sort
 * @param fms The FrameworkMethods
 * @param isStatic Filter static
 * @return The filtered and sorted FrameworkMethods
 * @exception Exception If an order definition is incorrect
 */
private Collection<FrameworkMethod> filterAndSort(List<FrameworkMethod> fms, boolean isStatic) throws Exception
{
   SortedMap<Integer, FrameworkMethod> m = new TreeMap<>();

   for (FrameworkMethod fm : fms)
   {
      SecurityActions.setAccessible(fm.getMethod());

      if (Modifier.isStatic(fm.getMethod().getModifiers()) == isStatic)
      {
         Deployment deployment = (Deployment)fm.getAnnotation(Deployment.class);
         int order = deployment.order();

         if (order <= 0 || m.containsKey(Integer.valueOf(order)))
            throw new Exception("Incorrect order definition '" + order + "' on " +
                                fm.getDeclaringClass().getName() + "#" + fm.getName());
         
         m.put(Integer.valueOf(order), fm);
      }
   }

   return m.values();
}
 
Example 4
Source File: EnqueueResponseRule.java    From contentful.java with Apache License 2.0 5 votes vote down vote up
@Override public Statement apply(Statement statement, FrameworkMethod method, Object o) {
  Enqueue enqueue = method.getAnnotation(Enqueue.class);
  if (enqueue != null) {
    if (!(o instanceof BaseTest)) {
      throw new RuntimeException("Test class must extend "
          + BaseTest.class.getName()
          + "when using @"
          + Enqueue.class.getSimpleName());
    }
    List<TestResponse> responses = new ArrayList<>(enqueueToTestResponse(enqueue));
    ((BaseTest) o).setResponseQueue(responses);
  }
  return statement;
}
 
Example 5
Source File: AppTestRunner.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
    String deploymentId = null;
    if (method.getAnnotation(Ignore.class) == null && method.getAnnotation(AppDeployment.class) != null) {
        deploymentId = deployAppDefinition(method);
    }
    
    super.runChild(method, notifier);
    
    if (deploymentId != null) {
        deleteDeployment(deploymentId);
    }
    assertDatabaseEmpty(method);
}
 
Example 6
Source File: MinVersionTestRunner.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public void runChild(FrameworkMethod method, RunNotifier notifier) {
    MinVersion methodCondition = method.getAnnotation(MinVersion.class);
    MinVersion classCondition = this.getTestClass().getJavaClass().getAnnotation(MinVersion.class);
    String versionStr = VersionInfo.getVersion();
    int version = VersionUtil.encodeVersion(versionStr);
    if (  (methodCondition == null || version >= VersionUtil.encodeVersion(methodCondition.value()))
       && (classCondition == null || version >= VersionUtil.encodeVersion(classCondition.value()))) {
        super.runChild(method, notifier);
    } else {
        notifier.fireTestIgnored(describeChild(method));
    }
}
 
Example 7
Source File: RetryRunner.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Statement methodInvoker(final FrameworkMethod method,
                               Object test) {
    final Statement singleTryStatement = super.methodInvoker(method, test);
    return new Statement() {
        /**
         * Evaluate the statement.
         * We attempt several runs for the test, at most MAX_ATTEMPTS.
         * if one attempt succeeds, we succeed, if all attempts fail, we
         * fail with the reason corresponding to the last attempt
         */
        @Override
        public void evaluate() throws Throwable {
            Throwable failureReason = null;

            final Retry retry = method.getAnnotation(Retry.class);
            if (retry == null) {
                // Do a single test run attempt.
                singleTryStatement.evaluate();
            } else {
                final int numRetries = retry.value();

                for (int i = 0; i < numRetries; ++i) {
                    try {
                        // Do a single test run attempt.
                        singleTryStatement.evaluate();
                        // Attempt succeeded, stop evaluation here.
                        return;
                    } catch (Throwable t) {
                        // Attempt failed, store the reason.
                        failureReason = t;
                    }
                }

                // All attempts failed.
                throw failureReason;
            }
        }
    };
}
 
Example 8
Source File: Sample.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private String getSampleName(FrameworkMethod method) {
    String sampleName;
    UsesSample annotation = method.getAnnotation(UsesSample.class);
    if (annotation == null) {
        sampleName = defaultSampleName;
    } else {
        sampleName = annotation.value();
    }
    return sampleName;
}
 
Example 9
Source File: RetryableMethodRule.java    From esjc with MIT License 5 votes vote down vote up
@Override
public Statement apply(Statement base, FrameworkMethod method, Object target) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            Retryable retryable = method.getAnnotation(Retryable.class);

            if (retryable != null) {
                for (int i = 1; i <= retryable.maxAttempts(); i++) {
                    try {
                        base.evaluate();
                        break;
                    } catch (Throwable t) {
                        if (retryable.value().isAssignableFrom(t.getClass())) {
                            logger.warn("Attempt {}/{} failed.", i, retryable.maxAttempts(), t);

                            if (i == retryable.maxAttempts()) {
                                throw t;
                            } else {
                                if (retryable.delay() > 0) {
                                    sleepUninterruptibly(retryable.delay());
                                }
                            }
                        } else {
                            throw t;
                        }
                    }
                }
            } else {
                base.evaluate();
            }
        }
    };
}
 
Example 10
Source File: TestRunner.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Sorts the given array of methods in dependencies order.
 *
 * @param  methods  the methods to sort.
 */
private static void sortDependantTestsLast(final FrameworkMethod[] methods) {
    final Set<String> dependencies = new HashSet<>();
    int retryCount = methods.length;
    for (int i=methods.length-1; --i>=0;) {
        final FrameworkMethod method = methods[i];
        final DependsOnMethod depend = method.getAnnotation(DependsOnMethod.class);
        if (depend != null) {
            dependencies.addAll(Arrays.asList(depend.value()));
            for (int j=methods.length; --j>i;) {
                if (dependencies.contains(methods[j].getName())) {
                    /*
                     * Found a method j which is a dependency of i. Move i after j.
                     * The order of other methods relative to j is left unchanged.
                     *
                     * As a result of this move, maybe some methods between i and j
                     * need to be revisited. We should restart the iteration from j.
                     * Over unlimited retries could cause an infinite loop, so we
                     * arbitrarily limit the amount of time we retry.
                     */
                    System.arraycopy(methods, i+1, methods, i, j-i);
                    methods[j] = method;
                    if (--retryCount >= 0) {
                        i = j;                      // Revisit the methods between i and j.
                    }
                    break;
                }
            }
            dependencies.clear();
        }
    }
}
 
Example 11
Source File: RunUntilFailure.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override  
protected Description describeChild(FrameworkMethod method) {  
    if (method.getAnnotation(Repeat.class) != null &&  
            method.getAnnotation(Ignore.class) == null) {  
        return describeRepeatTest(method);  
    }  
    return super.describeChild(method);  
}
 
Example 12
Source File: ValidationRunner.java    From tomee with Apache License 2.0 5 votes vote down vote up
/**
 * Flags an error if you have annotated a test with both @Test and @Keys. Any method annotated with @Test will be ignored anyways.
 */
@Override
protected void collectInitializationErrors(final List<Throwable> errors) {
    super.collectInitializationErrors(errors);
    final List<FrameworkMethod> methodsAnnotatedWithKeys = getTestClass().getAnnotatedMethods(Keys.class);
    for (final FrameworkMethod frameworkMethod : methodsAnnotatedWithKeys) {
        if (frameworkMethod.getAnnotation(Test.class) != null) {
            final String gripe = "The method " + frameworkMethod.getName() + "() can only be annotated with @Keys";
            errors.add(new Exception(gripe));
        }
    }
}
 
Example 13
Source File: FlinkStandaloneHiveRunner.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
	Description description = describeChild(method);
	if (method.getAnnotation(Ignore.class) != null) {
		notifier.fireTestIgnored(description);
	} else {
		EachTestNotifier eachNotifier = new EachTestNotifier(notifier, description);
		eachNotifier.fireTestStarted();
		try {
			runTestMethod(method, eachNotifier);
		} finally {
			eachNotifier.fireTestFinished();
		}
	}
}
 
Example 14
Source File: DesugarRunner.java    From bazel with Apache License 2.0 4 votes vote down vote up
private JdkSuppress getJdkSuppress(FrameworkMethod child) {
  JdkSuppress jdkSuppress = child.getAnnotation(JdkSuppress.class);
  return jdkSuppress != null ? jdkSuppress : getTestClass().getAnnotation(JdkSuppress.class);
}
 
Example 15
Source File: ConditionallyIgnoreTestRule.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
private static boolean hasConditionalIgnoreAnnotation(FrameworkMethod method) {
    return method.getAnnotation(ConditionalIgnore.class) != null;
}
 
Example 16
Source File: ATHJUnitRunner.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Override
protected Statement methodInvoker(FrameworkMethod method, Object test) {
    Statement next =  super.methodInvoker(method, test);
    return new Statement() {
        @Override
        public void evaluate() throws Throwable {
            LoginPage loginPage = injector.getInstance(LoginPage.class);

            Login methodLogin = method.getAnnotation(Login.class);

            // determine whether test should login first or not
            // first check test method, then walk up hierarchy at class level only
            if(methodLogin != null ) {
                if(!methodLogin.disable()) {
                    loginPage.login();
                }
            } else {
                Class<?> clazz = test.getClass();

                while (clazz != null) {
                    Login classLogin = clazz.getAnnotation(Login.class);

                    if (classLogin != null) {
                        if (!classLogin.disable()) {
                            loginPage.login();
                        }
                        break;
                    }

                    clazz = clazz.getSuperclass();
                }
            }

            try {
                if (LocalDriver.isSauceLabsMode()) {
                    logger.info("SauceOnDemandSessionID=" + ((RemoteWebDriver) LocalDriver.getDriver()).getSessionId().toString());
                }

                next.evaluate();
                outputConsoleLogs();
                LocalDriver.executeSauce("job-result=passed");
            } catch (Exception e) {
                LocalDriver.executeSauce("job-result=failed");
                writeScreenShotCause(e, test, method);
                outputConsoleLogs();
                throw e;
            }

            WebDriver driver = injector.getInstance(WebDriver.class);
            driver.close();
            driver.quit();
        }
    };
}
 
Example 17
Source File: ClassReflectionInjectionAvailableRule.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
public Statement apply(Statement base, FrameworkMethod method, Object target) {
    return !ClassInjector.UsingReflection.isAvailable() && method.getAnnotation(Enforce.class) != null
            ? new NoOpStatement()
            : base;
}
 
Example 18
Source File: JQF.java    From JQF with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override public Statement methodBlock(FrameworkMethod method) {
    if (method.getAnnotation(Fuzz.class) != null) {
        return new FuzzStatement(method, getTestClass(), generatorRepository);
    }
    return super.methodBlock(method);
}
 
Example 19
Source File: BrowserVersionClassRunner.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * Returns if not yet implemented.
 * @param method the method
 * @return if not yet implemented
 */
protected boolean isNotYetImplemented(final FrameworkMethod method) {
    final NotYetImplemented notYetImplementedBrowsers = method.getAnnotation(NotYetImplemented.class);
    return notYetImplementedBrowsers != null && isDefinedIn(notYetImplementedBrowsers.value());
}
 
Example 20
Source File: SpringJUnit4ClassRunner.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Get the {@code exception} that the supplied {@linkplain FrameworkMethod
 * test method} is expected to throw.
 * <p>Supports JUnit's {@link Test#expected() @Test(expected=...)} annotation.
 * <p>Can be overridden by subclasses.
 * @return the expected exception, or {@code null} if none was specified
 */
protected Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod) {
	Test test = frameworkMethod.getAnnotation(Test.class);
	return (test != null && test.expected() != Test.None.class ? test.expected() : null);
}