Search in sources :

Example 1 with ContextConfigurationAttributes

use of cn.taketoday.test.context.ContextConfigurationAttributes in project today-infrastructure by TAKETODAY.

the class DelegatingSmartContextLoaderTests method processContextConfigurationWithLocation.

@Test
void processContextConfigurationWithLocation() {
    String[] locations = new String[] { "classpath:/foo.xml" };
    ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes(getClass(), locations, EMPTY_CLASS_ARRAY, true, null, true, ContextLoader.class);
    loader.processContextConfiguration(configAttributes);
    assertThat(configAttributes.getLocations()).isEqualTo(locations);
    assertEmpty(configAttributes.getClasses());
}
Also used : ContextConfigurationAttributes(cn.taketoday.test.context.ContextConfigurationAttributes) Test(org.junit.jupiter.api.Test)

Example 2 with ContextConfigurationAttributes

use of cn.taketoday.test.context.ContextConfigurationAttributes in project today-infrastructure by TAKETODAY.

the class DelegatingSmartContextLoaderTests method processContextConfigurationWithDefaultXmlConfigGeneration.

// --- SmartContextLoader - processContextConfiguration() ------------------
@Test
void processContextConfigurationWithDefaultXmlConfigGeneration() {
    ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes(XmlTestCase.class, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, true, null, true, ContextLoader.class);
    loader.processContextConfiguration(configAttributes);
    assertThat(configAttributes.getLocations().length).isEqualTo(1);
    assertEmpty(configAttributes.getClasses());
}
Also used : ContextConfigurationAttributes(cn.taketoday.test.context.ContextConfigurationAttributes) Test(org.junit.jupiter.api.Test)

Example 3 with ContextConfigurationAttributes

use of cn.taketoday.test.context.ContextConfigurationAttributes in project today-infrastructure by TAKETODAY.

the class DelegatingSmartContextLoaderTests method processContextConfigurationWithDefaultXmlConfigAndConfigurationClassGeneration.

@Test
void processContextConfigurationWithDefaultXmlConfigAndConfigurationClassGeneration() {
    ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes(ImproperDuplicateDefaultXmlAndConfigClassTestCase.class, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, true, null, true, ContextLoader.class);
    assertThatIllegalStateException().isThrownBy(() -> loader.processContextConfiguration(configAttributes)).withMessageContaining("both default locations AND default configuration classes were detected");
}
Also used : ContextConfigurationAttributes(cn.taketoday.test.context.ContextConfigurationAttributes) Test(org.junit.jupiter.api.Test)

Example 4 with ContextConfigurationAttributes

use of cn.taketoday.test.context.ContextConfigurationAttributes in project today-infrastructure by TAKETODAY.

the class ContextLoaderUtils method buildContextHierarchyMap.

/**
 * 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.
 * @see #resolveContextHierarchyAttributes(Class)
 * @since 4.0
 */
static Map<String, List<ContextConfigurationAttributes>> buildContextHierarchyMap(Class<?> testClass) {
    Map<String, List<ContextConfigurationAttributes>> map = new LinkedHashMap<>();
    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<>());
            }
            map.get(name).add(configAttributes);
        }
    }
    // Check for uniqueness
    Set<List<ContextConfigurationAttributes>> set = new HashSet<>(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;
}
Also used : ContextConfigurationAttributes(cn.taketoday.test.context.ContextConfigurationAttributes) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet)

Example 5 with ContextConfigurationAttributes

use of cn.taketoday.test.context.ContextConfigurationAttributes in project today-infrastructure by TAKETODAY.

the class ApplicationTestContextBootstrapper method isFromConfiguration.

private boolean isFromConfiguration(MergedContextConfiguration candidateConfig, ContextConfiguration configuration) {
    ContextConfigurationAttributes attributes = new ContextConfigurationAttributes(candidateConfig.getTestClass(), configuration);
    Set<Class<?>> configurationClasses = new HashSet<>(Arrays.asList(attributes.getClasses()));
    for (Class<?> candidate : candidateConfig.getClasses()) {
        if (configurationClasses.contains(candidate)) {
            return true;
        }
    }
    return false;
}
Also used : ContextConfigurationAttributes(cn.taketoday.test.context.ContextConfigurationAttributes) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

ContextConfigurationAttributes (cn.taketoday.test.context.ContextConfigurationAttributes)22 Test (org.junit.jupiter.api.Test)10 ArrayList (java.util.ArrayList)6 ContextConfiguration (cn.taketoday.test.context.ContextConfiguration)4 HashSet (java.util.HashSet)4 LinkedHashSet (java.util.LinkedHashSet)4 List (java.util.List)4 ApplicationContextInitializer (cn.taketoday.context.ApplicationContextInitializer)2 ContextHierarchy (cn.taketoday.test.context.ContextHierarchy)2 UntypedAnnotationDescriptor (cn.taketoday.test.context.TestContextAnnotationUtils.UntypedAnnotationDescriptor)2 LinkedHashMap (java.util.LinkedHashMap)2