Java Code Examples for org.junit.platform.commons.support.AnnotationSupport#findAnnotatedFields()
The following examples show how to use
org.junit.platform.commons.support.AnnotationSupport#findAnnotatedFields() .
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: ContainerGroup.java From microshed-testing with Apache License 2.0 | 6 votes |
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; }
Example 2
Source File: TestcontainersConfiguration.java From microprofile-sandbox with Apache License 2.0 | 6 votes |
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; }
Example 3
Source File: MicroProfileTestExtension.java From microprofile-sandbox with Apache License 2.0 | 6 votes |
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); } }
Example 4
Source File: TestcontainersConfiguration.java From microprofile-sandbox with Apache License 2.0 | 5 votes |
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; }
Example 5
Source File: GuiceyExtensionsSupport.java From dropwizard-guicey with MIT License | 5 votes |
@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)); } }