Skip to content

Commit

Permalink
Do not use internal JUnit utilities.
Browse files Browse the repository at this point in the history
Signed-off-by: James R. Perkins <[email protected]>
  • Loading branch information
jamezp committed Aug 16, 2024
1 parent 0a8a6a1 commit 212b6d5
Showing 1 changed file with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

package dev.resteasy.junit.extension.extensions;

import static org.junit.platform.commons.util.AnnotationUtils.findAnnotatedFields;
import static org.junit.platform.commons.util.ReflectionUtils.makeAccessible;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ServiceLoader;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
Expand All @@ -24,8 +22,7 @@
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.platform.commons.util.ExceptionUtils;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.commons.support.AnnotationSupport;

import dev.resteasy.junit.extension.api.InjectionProducer;

Expand Down Expand Up @@ -109,18 +106,18 @@ public void afterAll(final ExtensionContext context) {
}

private void injectStaticFields(final ExtensionContext context, final Class<?> testClass) {
injectFields(context, null, testClass, ReflectionUtils::isStatic);
injectFields(context, null, testClass, (f) -> Modifier.isStatic(f.getModifiers()));
}

private void injectInstanceFields(final ExtensionContext context, final Object instance) {
injectFields(context, instance, instance.getClass(), ReflectionUtils::isNotStatic);
injectFields(context, instance, instance.getClass(), (f) -> !Modifier.isStatic(f.getModifiers()));
}

private void injectFields(final ExtensionContext context, final Object testInstance, final Class<?> testClass,
final Predicate<Field> predicate) {

findAnnotatedFields(testClass, Inject.class, predicate).forEach(field -> {
if (ReflectionUtils.isFinal(field)) {
AnnotationSupport.findAnnotatedFields(testClass, Inject.class, predicate).forEach(field -> {
if (Modifier.isFinal(field.getModifiers())) {
throw new ExtensionConfigurationException(
String.format("Field '%s' cannot be final for injecting a REST client.", field));
}
Expand All @@ -142,9 +139,15 @@ private void injectFields(final ExtensionContext context, final Object testInsta
if (value instanceof AutoCloseable) {
resources.add((AutoCloseable) value);
}
makeAccessible(field).set(testInstance, value);
if (field.trySetAccessible()) {
field.set(testInstance, value);
} else {
throw new ParameterResolutionException(
String.format("Could not make field %s accessible for injection.", field));
}
} catch (Throwable e) {
throw ExceptionUtils.throwAsUncheckedException(e);
throw new ParameterResolutionException(
String.format("Could not make field %s accessible for injection.", field));
}
});
}
Expand Down

0 comments on commit 212b6d5

Please sign in to comment.