ContextLoaderUtils.java 文件源码

java
阅读 22 收藏 0 点赞 0 评论 0

项目:spring4-understanding 作者:
/**
 * Build a <em>context hierarchy map</em> for the supplied {@linkplain Class
 * test class} and its superclasses, taking into account context hierarchies
 * declared via {@link ContextHierarchy @ContextHierarchy} and
 * {@link ContextConfiguration @ContextConfiguration}.
 * <p>Each value in the map represents the consolidated list of {@linkplain
 * ContextConfigurationAttributes context configuration attributes} for a
 * given level in the context hierarchy (potentially across the test class
 * hierarchy), keyed by the {@link ContextConfiguration#name() name} of the
 * context hierarchy level.
 * <p>If a given level in the context hierarchy does not have an explicit
 * name (i.e., configured via {@link ContextConfiguration#name}), a name will
 * be generated for that hierarchy level by appending the numerical level to
 * the {@link #GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX}.
 * @param testClass the class for which to resolve the context hierarchy map
 * (must not be {@code null})
 * @return a map of context configuration attributes for the context hierarchy,
 * keyed by context hierarchy level name; never {@code null}
 * @throws IllegalArgumentException if the lists of context configuration
 * attributes for each level in the {@code @ContextHierarchy} do not define
 * unique context configuration within the overall hierarchy.
 * @since 3.2.2
 * @see #resolveContextHierarchyAttributes(Class)
 */
static Map<String, List<ContextConfigurationAttributes>> buildContextHierarchyMap(Class<?> testClass) {
    final Map<String, List<ContextConfigurationAttributes>> map = new LinkedHashMap<String, List<ContextConfigurationAttributes>>();
    int hierarchyLevel = 1;

    for (List<ContextConfigurationAttributes> configAttributesList : resolveContextHierarchyAttributes(testClass)) {
        for (ContextConfigurationAttributes configAttributes : configAttributesList) {
            String name = configAttributes.getName();

            // Assign a generated name?
            if (!StringUtils.hasText(name)) {
                name = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + hierarchyLevel;
            }

            // Encountered a new context hierarchy level?
            if (!map.containsKey(name)) {
                hierarchyLevel++;
                map.put(name, new ArrayList<ContextConfigurationAttributes>());
            }

            map.get(name).add(configAttributes);
        }
    }

    // Check for uniqueness
    Set<List<ContextConfigurationAttributes>> set = new HashSet<List<ContextConfigurationAttributes>>(map.values());
    if (set.size() != map.size()) {
        String msg = String.format("The @ContextConfiguration elements configured via @ContextHierarchy in " +
                "test class [%s] and its superclasses must define unique contexts per hierarchy level.",
                testClass.getName());
        logger.error(msg);
        throw new IllegalStateException(msg);
    }

    return map;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号