Search in sources :

Example 1 with Parameterized

use of org.junit.runners.Parameterized in project service-proxy by membrane.

the class ResolverTestTriggerTest method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) {
    try {
        Class<?> clazz = Class.forName("com.predic8.membrane.core.resolver.ResolverTest");
        clazz.getField("deployment").set(null, "J2EE");
        Object value = router.getResolverMap().getFileSchemaResolver();
        Object resolverMap = clazz.getField("resolverMap").get(null);
        resolverMap.getClass().getMethod("addSchemaResolver", SchemaResolver.class).invoke(resolverMap, value);
        Parameterized p = new Parameterized(clazz);
        JUnitCore c = new JUnitCore();
        Result run = c.run(Request.runner(p));
        StringBuilder sb = new StringBuilder();
        sb.append(MAGIC);
        for (Failure f : run.getFailures()) {
            sb.append(f.toString());
            StringWriter stringWriter = new StringWriter();
            f.getException().printStackTrace(new PrintWriter(stringWriter));
            sb.append(stringWriter.toString());
            sb.append("\n");
            sb.append("\n");
        }
        exc.setResponse(Response.ok().header(Header.CONTENT_TYPE, MimeType.TEXT_PLAIN_UTF8).body(sb.toString()).build());
    } catch (Throwable t) {
        LOG.error(t.getMessage(), t);
    }
    return Outcome.RETURN;
}
Also used : Parameterized(org.junit.runners.Parameterized) JUnitCore(org.junit.runner.JUnitCore) StringWriter(java.io.StringWriter) SchemaResolver(com.predic8.membrane.core.resolver.SchemaResolver) Failure(org.junit.runner.notification.Failure) Result(org.junit.runner.Result) PrintWriter(java.io.PrintWriter)

Example 2 with Parameterized

use of org.junit.runners.Parameterized 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

Parameterized (org.junit.runners.Parameterized)2 SchemaResolver (com.predic8.membrane.core.resolver.SchemaResolver)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Set (java.util.Set)1 TimeUnit (java.util.concurrent.TimeUnit)1 IntegrationTests (org.apache.hadoop.hbase.testclassification.IntegrationTests)1 LargeTests (org.apache.hadoop.hbase.testclassification.LargeTests)1 MediumTests (org.apache.hadoop.hbase.testclassification.MediumTests)1 SmallTests (org.apache.hadoop.hbase.testclassification.SmallTests)1 Iterables (org.apache.hbase.thirdparty.com.google.common.collect.Iterables)1 Sets (org.apache.hbase.thirdparty.com.google.common.collect.Sets)1