下面列出了org.junit.platform.commons.support.AnnotationSupport#findAnnotatedFields ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Set<GenericContainer<?>> discoverContainers(Class<?> clazz) {
Set<GenericContainer<?>> discoveredContainers = new HashSet<>();
for (Field containerField : AnnotationSupport.findAnnotatedFields(clazz, Container.class)) {
if (!Modifier.isPublic(containerField.getModifiers()))
throw new ExtensionConfigurationException("@Container annotated fields must be public visibility");
if (!Modifier.isStatic(containerField.getModifiers()))
throw new ExtensionConfigurationException("@Container annotated fields must be static");
boolean isStartable = GenericContainer.class.isAssignableFrom(containerField.getType());
if (!isStartable)
throw new ExtensionConfigurationException("@Container annotated fields must be a subclass of " + GenericContainer.class);
try {
GenericContainer<?> startableContainer = (GenericContainer<?>) containerField.get(null);
discoveredContainers.add(startableContainer);
} catch (IllegalArgumentException | IllegalAccessException e) {
LOG.warn("Unable to access field " + containerField, e);
}
}
return discoveredContainers;
}
private Set<GenericContainer<?>> discoverContainers(Class<?> clazz) {
Set<GenericContainer<?>> discoveredContainers = new HashSet<>();
for (Field containerField : AnnotationSupport.findAnnotatedFields(clazz, Container.class)) {
if (!Modifier.isPublic(containerField.getModifiers()))
throw new ExtensionConfigurationException("@Container annotated fields must be public visibility");
if (!Modifier.isStatic(containerField.getModifiers()))
throw new ExtensionConfigurationException("@Container annotated fields must be static");
boolean isStartable = GenericContainer.class.isAssignableFrom(containerField.getType());
if (!isStartable)
throw new ExtensionConfigurationException("@Container annotated fields must be a subclass of " + GenericContainer.class);
try {
GenericContainer<?> startableContainer = (GenericContainer<?>) containerField.get(null);
discoveredContainers.add(startableContainer);
} catch (IllegalArgumentException | IllegalAccessException e) {
LOG.warn("Unable to access field " + containerField, e);
}
}
return discoveredContainers;
}
private static void injectRestClients(Class<?> clazz, TestcontainersConfiguration config) throws Exception {
List<Field> restClientFields = AnnotationSupport.findAnnotatedFields(clazz, Inject.class);
if (restClientFields.size() == 0)
return;
String mpAppURL = config.getApplicationURL();
for (Field restClientField : restClientFields) {
if (!Modifier.isPublic(restClientField.getModifiers()) ||
!Modifier.isStatic(restClientField.getModifiers()) ||
Modifier.isFinal(restClientField.getModifiers())) {
throw new ExtensionConfigurationException("REST-client field must be public, static, and non-final: " + restClientField.getName());
}
String jwt = createJwtIfNeeded(restClientField);
Object restClient = JAXRSUtilities.createRestClient(restClientField.getType(), mpAppURL, jwt);
//Object restClient = JAXRSUtilities.createRestClient(restClientField.getType(), mpAppURL);
restClientField.set(null, restClient);
LOGGER.debug("Injecting rest client for " + restClientField);
}
}
private MicroProfileApplication<?> autoDiscoverMPApp(Class<?> clazz, boolean errorIfNone) {
// First check for any MicroProfileApplicaiton directly present on the test class
List<Field> mpApps = AnnotationSupport.findAnnotatedFields(clazz, Container.class,
f -> Modifier.isStatic(f.getModifiers()) &&
Modifier.isPublic(f.getModifiers()) &&
MicroProfileApplication.class.isAssignableFrom(f.getType()),
HierarchyTraversalMode.TOP_DOWN);
if (mpApps.size() == 1)
try {
return (MicroProfileApplication<?>) mpApps.get(0).get(null);
} catch (IllegalArgumentException | IllegalAccessException e) {
// This should never happen because we only look for fields that are public+static
e.printStackTrace();
}
if (mpApps.size() > 1)
throw new ExtensionConfigurationException("Should be no more than 1 public static MicroProfileApplication field on " + clazz);
// If none found, check any SharedContainerConfig
String sharedConfigMsg = "";
if (sharedConfigClass != null) {
MicroProfileApplication<?> mpApp = autoDiscoverMPApp(sharedConfigClass, false);
if (mpApp != null)
return mpApp;
sharedConfigMsg = " or " + sharedConfigClass;
}
if (errorIfNone)
throw new ExtensionConfigurationException("No public static MicroProfileApplication fields annotated with @Container were located " +
"on " + clazz + sharedConfigMsg + " to auto-connect with REST-client fields.");
return null;
}
@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));
}
}