org.junit.platform.commons.support.HierarchyTraversalMode Java Examples

The following examples show how to use org.junit.platform.commons.support.HierarchyTraversalMode. 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: TestcontainersConfiguration.java    From microprofile-sandbox with Apache License 2.0 5 votes vote down vote up
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;
}