org.junit.platform.commons.util.ReflectionUtils Java Examples

The following examples show how to use org.junit.platform.commons.util.ReflectionUtils. 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: BQTestExtension.java    From bootique with Apache License 2.0 6 votes vote down vote up
protected Predicate<Field> isRunnable() {
    return f -> {

        // provide diagnostics for misapplied or missing annotations
        // TODO: will it be actually more useful to throw instead of print a warning?
        if (AnnotationSupport.isAnnotated(f, BQApp.class)) {

            if (!BQRuntime.class.isAssignableFrom(f.getType())) {
                logger.warn(() -> "Field '" + f.getName() + "' is annotated with @BQRun but is not a BQRuntime. Ignoring...");
                return false;
            }

            if (!ReflectionUtils.isStatic(f)) {
                logger.warn(() -> "BQRuntime field '" + f.getName() + "' is annotated with @BQRun but is not static. Ignoring...");
                return false;
            }

            return true;
        }

        return false;
    };
}
 
Example #2
Source File: BeanFactoryTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void init() throws ReflectiveOperationException
{
    String fieldName = "applicationContext";
    Field contextField = ReflectionUtils
            .findFields(BeanFactory.class, f -> f.getName().equals(fieldName), HierarchyTraversalMode.TOP_DOWN)
            .stream().findFirst().orElseThrow(() -> new NoSuchFieldException(fieldName));
    ReflectionUtils.makeAccessible(contextField);
    contextField.set(null, context);
}
 
Example #3
Source File: ParameterWriterHelper.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ConstantConditions")
public static String toSql(ParameterWriter writer) {
    ParamWriter w = (ParamWriter) writer;

    ReflectionUtils.invokeMethod(FLUSH_PARAMETER, w, (Void) null);
    return (String) ReflectionUtils.invokeMethod(TO_SQL, w);
}
 
Example #4
Source File: ParameterWriterHelper.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ConstantConditions")
public static String toSql(ParameterWriter writer) {
    ParamWriter w = (ParamWriter) writer;

    ReflectionUtils.invokeMethod(FLUSH_PARAMETER, w, (Void) null);
    return (String) ReflectionUtils.invokeMethod(TO_SQL, w);
}
 
Example #5
Source File: BQTestExtension.java    From bootique with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeAll(ExtensionContext context) {
    ExtensionContext.Store store = context.getStore(NAMESPACE);
    Class<?> testType = context.getRequiredTestClass();
    ReflectionUtils
            .findFields(testType, isRunnable(), ReflectionUtils.HierarchyTraversalMode.TOP_DOWN)
            .stream()
            .map(f -> getInstance(null, f))
            .forEach(r -> startAndRegisterForShutdown(r, store));
}
 
Example #6
Source File: TestcontainersExtension.java    From testcontainers-java with MIT License 5 votes vote down vote up
private List<StoreAdapter> findSharedContainers(Class<?> testClass) {
    return ReflectionUtils.findFields(
            testClass,
            isSharedContainer(),
            ReflectionUtils.HierarchyTraversalMode.TOP_DOWN)
        .stream()
        .map(f -> getContainerInstance(null, f))
        .collect(toList());
}
 
Example #7
Source File: TestcontainersExtension.java    From testcontainers-java with MIT License 5 votes vote down vote up
private Stream<StoreAdapter> findRestartContainers(Object testInstance) {
    return ReflectionUtils.findFields(
            testInstance.getClass(),
            isRestartContainer(),
            ReflectionUtils.HierarchyTraversalMode.TOP_DOWN)
        .stream()
        .map(f -> getContainerInstance(testInstance, f));
}
 
Example #8
Source File: AbstractRuleBuilderTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void checkStaticMethods() {
    final Map<Class<?>, Class<? extends AbstractRuleBuilder>> classes =
            ImmutableMap.of(
                    RetryRule.class, RetryRuleBuilder.class,
                    CircuitBreakerRule.class, CircuitBreakerRuleBuilder.class);

    classes.forEach((rule, builder) -> {
        final List<Method> builderMethods =
                ReflectionUtils.findMethods(builder,
                                            method -> !Modifier.isStatic(method.getModifiers()) &&
                                                      Modifier.isPublic(method.getModifiers()) &&
                                                      method.getName().startsWith("on") &&
                                                      !"onResponseHeaders".equals(method.getName()) &&
                                                      !"onResponseTrailers".equals(method.getName()) &&
                                                      !"onUnprocessed".equals(method.getName()) &&
                                                      !method.isVarArgs());

        final List<Method> ruleMethods =
                ReflectionUtils.findMethods(rule,
                                            method -> Modifier.isStatic(method.getModifiers()) &&
                                                      Modifier.isPublic(method.getModifiers()) &&
                                                      method.getName().startsWith("on"));
        assertThat(builderMethods).isNotEmpty();
        for (Method builderMethod : builderMethods) {
            final Predicate<Method> predicate = ruleMethod ->
                    ruleMethod.getName().equals(builderMethod.getName()) &&
                    Arrays.equals(ruleMethod.getParameterTypes(), builderMethod.getParameterTypes());

            assertThat(ruleMethods.stream().filter(predicate).collect(toImmutableList())).hasSize(1);
        }
    });
}
 
Example #9
Source File: GuiceyExtensionsSupport.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
@SuppressWarnings({"unchecked", "checkstyle:Indentation"})
private void activateFieldHooks(final Class<?> testClass) {
    final List<Field> fields = AnnotationSupport.findAnnotatedFields(testClass, EnableHook.class);
    HooksUtil.validateFieldHooks(fields);
    if (!fields.isEmpty()) {
        HooksUtil.register((List<GuiceyConfigurationHook>)
                (List) ReflectionUtils.readFieldValues(fields, null));
    }
}
 
Example #10
Source File: DataProviderInvocationContext.java    From junit-dataprovider with Apache License 2.0 5 votes vote down vote up
@Override
public String getDisplayName(int invocationIndex) {
    Class<? extends DataProviderTestNameFormatter> formatter = displayNameContext.getFormatter();
    if (formatter == null || DataProviderPlaceholderFormatter.class.equals(formatter)) {
        return new DataProviderPlaceholderFormatter(displayNameContext.getFormat(),
                displayNameContext.getPlaceholders()).format(testMethod, invocationIndex, arguments);
    }
    return ReflectionUtils.newInstance(formatter).format(testMethod, invocationIndex, arguments);
}
 
Example #11
Source File: ParameterWriterHelper.java    From r2dbc-mysql with Apache License 2.0 4 votes vote down vote up
public static ParameterWriter get(int parameters) {
    assertThat(parameters).isGreaterThan(0);

    return ReflectionUtils.newInstance(CONSTRUCTOR, new StringBuilder(), new Iter(parameters));
}
 
Example #12
Source File: VarIntUtilsTest.java    From r2dbc-mysql with Apache License 2.0 4 votes vote down vote up
@Test
void eofHeaderEqualsInt64Code() {
    short v = (Short) ReflectionUtils.tryToReadFieldValue(ServerMessageDecoder.class, "EOF", null)
        .getOrThrow(RuntimeException::new);
    assertEquals(v, VarIntUtils.VAR_INT_8_BYTE_CODE);
}
 
Example #13
Source File: ParameterWriterHelper.java    From r2dbc-mysql with Apache License 2.0 4 votes vote down vote up
public static ParameterWriter get(int parameters) {
    assertThat(parameters).isGreaterThan(0);

    return ReflectionUtils.newInstance(CONSTRUCTOR, new StringBuilder(), new Iter(parameters));
}
 
Example #14
Source File: VarIntUtilsTest.java    From r2dbc-mysql with Apache License 2.0 4 votes vote down vote up
@Test
void eofHeaderEqualsInt64Code() {
    short v = (Short) ReflectionUtils.tryToReadFieldValue(ServerMessageDecoder.class, "EOF", null)
        .getOrThrow(RuntimeException::new);
    assertEquals(v, VarIntUtils.VAR_INT_8_BYTE_CODE);
}
 
Example #15
Source File: TestcontainersExtension.java    From testcontainers-java with MIT License 4 votes vote down vote up
private Predicate<Field> isSharedContainer() {
    return isContainer().and(ReflectionUtils::isStatic);
}
 
Example #16
Source File: TestcontainersExtension.java    From testcontainers-java with MIT License 4 votes vote down vote up
private Predicate<Field> isRestartContainer() {
    return isContainer().and(ReflectionUtils::isNotStatic);
}