/** * Determine if this {@code ContextConfigurationAttributes} instance has * path-based resource locations. * @return {@code true} if the {@link #getLocations() locations} array is not empty * @see #hasResources() * @see #hasClasses() */ public boolean hasLocations() { return (getLocations().length > 0); }
/** * For backwards compatibility with the {@link ContextLoader} SPI, the * default implementation simply delegates to {@link #processLocations(Class, String...)}, * passing it the {@link ContextConfigurationAttributes#getDeclaringClass() * declaring class} and {@link ContextConfigurationAttributes#getLocations() * resource locations} retrieved from the supplied * {@link ContextConfigurationAttributes configuration attributes}. The * processed locations are then * {@link ContextConfigurationAttributes#setLocations(String[]) set} in * the supplied configuration attributes. * <p>Can be overridden in subclasses — for example, to process * annotated classes instead of resource locations. * @since 3.1 * @see #processLocations(Class, String...) */ @Override public void processContextConfiguration(ContextConfigurationAttributes configAttributes) { String[] processedLocations = processLocations(configAttributes.getDeclaringClass(), configAttributes.getLocations()); configAttributes.setLocations(processedLocations); }
private void assertOneTwo(List<List<ContextConfigurationAttributes>> hierarchyAttributes) { assertEquals(2, hierarchyAttributes.size()); List<ContextConfigurationAttributes> configAttributesListClassLevel1 = hierarchyAttributes.get(0); List<ContextConfigurationAttributes> configAttributesListClassLevel2 = hierarchyAttributes.get(1); debugConfigAttributes(configAttributesListClassLevel1); debugConfigAttributes(configAttributesListClassLevel2); assertEquals(1, configAttributesListClassLevel1.size()); assertThat(configAttributesListClassLevel1.get(0).getLocations()[0], equalTo("one.xml")); assertEquals(1, configAttributesListClassLevel2.size()); assertThat(configAttributesListClassLevel2.get(0).getLocations()[0], equalTo("two.xml")); }
@Test public void buildContextHierarchyMapForTestClassHierarchyWithMultiLevelContextHierarchies() { Map<String, List<ContextConfigurationAttributes>> map = buildContextHierarchyMap(TestClass3WithMultiLevelContextHierarchy.class); assertThat(map.size(), is(3)); assertThat(map.keySet(), hasItems("alpha", "beta", "gamma")); List<ContextConfigurationAttributes> alphaConfig = map.get("alpha"); assertThat(alphaConfig.size(), is(3)); assertThat(alphaConfig.get(0).getLocations()[0], is("1-A.xml")); assertThat(alphaConfig.get(1).getLocations()[0], is("2-A.xml")); assertThat(alphaConfig.get(2).getLocations()[0], is("3-A.xml")); List<ContextConfigurationAttributes> betaConfig = map.get("beta"); assertThat(betaConfig.size(), is(3)); assertThat(betaConfig.get(0).getLocations()[0], is("1-B.xml")); assertThat(betaConfig.get(1).getLocations()[0], is("2-B.xml")); assertThat(betaConfig.get(2).getLocations()[0], is("3-B.xml")); List<ContextConfigurationAttributes> gammaConfig = map.get("gamma"); assertThat(gammaConfig.size(), is(1)); assertThat(gammaConfig.get(0).getLocations()[0], is("3-C.xml")); }
SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader; smartContextLoader.processContextConfiguration(configAttributes); locations.addAll(0, Arrays.asList(configAttributes.getLocations())); classes.addAll(0, Arrays.asList(configAttributes.getClasses())); configAttributes.getDeclaringClass(), configAttributes.getLocations()); locations.addAll(0, Arrays.asList(processedLocations));
@Test public void resolveContextHierarchyAttributesForTestClassHierarchyWithMultiLevelContextHierarchies() { List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(TestClass3WithMultiLevelContextHierarchy.class); assertEquals(3, hierarchyAttributes.size()); List<ContextConfigurationAttributes> configAttributesListClassLevel1 = hierarchyAttributes.get(0); debugConfigAttributes(configAttributesListClassLevel1); assertEquals(2, configAttributesListClassLevel1.size()); assertThat(configAttributesListClassLevel1.get(0).getLocations()[0], equalTo("1-A.xml")); assertThat(configAttributesListClassLevel1.get(1).getLocations()[0], equalTo("1-B.xml")); List<ContextConfigurationAttributes> configAttributesListClassLevel2 = hierarchyAttributes.get(1); debugConfigAttributes(configAttributesListClassLevel2); assertEquals(2, configAttributesListClassLevel2.size()); assertThat(configAttributesListClassLevel2.get(0).getLocations()[0], equalTo("2-A.xml")); assertThat(configAttributesListClassLevel2.get(1).getLocations()[0], equalTo("2-B.xml")); List<ContextConfigurationAttributes> configAttributesListClassLevel3 = hierarchyAttributes.get(2); debugConfigAttributes(configAttributesListClassLevel3); assertEquals(3, configAttributesListClassLevel3.size()); assertThat(configAttributesListClassLevel3.get(0).getLocations()[0], equalTo("3-A.xml")); assertThat(configAttributesListClassLevel3.get(1).getLocations()[0], equalTo("3-B.xml")); assertThat(configAttributesListClassLevel3.get(2).getLocations()[0], equalTo("3-C.xml")); }
@Test public void buildContextHierarchyMapForTestClassHierarchyWithMultiLevelContextHierarchiesAndPartiallyNamedConfig() { Map<String, List<ContextConfigurationAttributes>> map = buildContextHierarchyMap(TestClass2WithMultiLevelContextHierarchyAndPartiallyNamedConfig.class); String level1 = "parent"; String level2 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 2; String level3 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 3; assertThat(map.size(), is(3)); assertThat(map.keySet(), hasItems(level1, level2, level3)); Iterator<String> levels = map.keySet().iterator(); assertThat(levels.next(), is(level1)); assertThat(levels.next(), is(level2)); assertThat(levels.next(), is(level3)); List<ContextConfigurationAttributes> level1Config = map.get(level1); assertThat(level1Config.size(), is(2)); assertThat(level1Config.get(0).getLocations()[0], is("1-A.xml")); assertThat(level1Config.get(1).getLocations()[0], is("2-A.xml")); List<ContextConfigurationAttributes> level2Config = map.get(level2); assertThat(level2Config.size(), is(1)); assertThat(level2Config.get(0).getLocations()[0], is("1-B.xml")); List<ContextConfigurationAttributes> level3Config = map.get(level3); assertThat(level3Config.size(), is(1)); assertThat(level3Config.get(0).getLocations()[0], is("2-C.xml")); }
@Test public void buildContextHierarchyMapForTestClassHierarchyWithMultiLevelContextHierarchiesAndUnnamedConfig() { Map<String, List<ContextConfigurationAttributes>> map = buildContextHierarchyMap(TestClass3WithMultiLevelContextHierarchyAndUnnamedConfig.class); String level1 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 1; String level2 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 2; String level3 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 3; String level4 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 4; String level5 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 5; String level6 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 6; String level7 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 7; assertThat(map.size(), is(7)); assertThat(map.keySet(), hasItems(level1, level2, level3, level4, level5, level6, level7)); List<ContextConfigurationAttributes> level1Config = map.get(level1); assertThat(level1Config.size(), is(1)); assertThat(level1Config.get(0).getLocations()[0], is("1-A.xml")); List<ContextConfigurationAttributes> level2Config = map.get(level2); assertThat(level2Config.size(), is(1)); assertThat(level2Config.get(0).getLocations()[0], is("1-B.xml")); List<ContextConfigurationAttributes> level3Config = map.get(level3); assertThat(level3Config.size(), is(1)); assertThat(level3Config.get(0).getLocations()[0], is("2-A.xml")); // ... List<ContextConfigurationAttributes> level7Config = map.get(level7); assertThat(level7Config.size(), is(1)); assertThat(level7Config.get(0).getLocations()[0], is("3-C.xml")); }
@Test public void resolveContextHierarchyAttributesForTestClassHierarchyWithSingleLevelContextHierarchies() { List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(TestClass3WithSingleLevelContextHierarchy.class); assertEquals(3, hierarchyAttributes.size()); List<ContextConfigurationAttributes> configAttributesListClassLevel1 = hierarchyAttributes.get(0); debugConfigAttributes(configAttributesListClassLevel1); assertEquals(1, configAttributesListClassLevel1.size()); assertThat(configAttributesListClassLevel1.get(0).getLocations()[0], equalTo("one.xml")); List<ContextConfigurationAttributes> configAttributesListClassLevel2 = hierarchyAttributes.get(1); debugConfigAttributes(configAttributesListClassLevel2); assertEquals(1, configAttributesListClassLevel2.size()); assertArrayEquals(new String[] { "two-A.xml", "two-B.xml" }, configAttributesListClassLevel2.get(0).getLocations()); List<ContextConfigurationAttributes> configAttributesListClassLevel3 = hierarchyAttributes.get(2); debugConfigAttributes(configAttributesListClassLevel3); assertEquals(1, configAttributesListClassLevel3.size()); assertThat(configAttributesListClassLevel3.get(0).getLocations()[0], equalTo("three.xml")); }
/** * Used to reproduce bug reported in https://jira.spring.io/browse/SPR-10997 */ @Test public void buildContextHierarchyMapForTestClassHierarchyWithMultiLevelContextHierarchiesAndOverriddenInitializers() { Map<String, List<ContextConfigurationAttributes>> map = buildContextHierarchyMap(TestClass2WithMultiLevelContextHierarchyWithOverriddenInitializers.class); assertThat(map.size(), is(2)); assertThat(map.keySet(), hasItems("alpha", "beta")); List<ContextConfigurationAttributes> alphaConfig = map.get("alpha"); assertThat(alphaConfig.size(), is(2)); assertThat(alphaConfig.get(0).getLocations().length, is(1)); assertThat(alphaConfig.get(0).getLocations()[0], is("1-A.xml")); assertThat(alphaConfig.get(0).getInitializers().length, is(0)); assertThat(alphaConfig.get(1).getLocations().length, is(0)); assertThat(alphaConfig.get(1).getInitializers().length, is(1)); assertEquals(DummyApplicationContextInitializer.class, alphaConfig.get(1).getInitializers()[0]); List<ContextConfigurationAttributes> betaConfig = map.get("beta"); assertThat(betaConfig.size(), is(2)); assertThat(betaConfig.get(0).getLocations().length, is(1)); assertThat(betaConfig.get(0).getLocations()[0], is("1-B.xml")); assertThat(betaConfig.get(0).getInitializers().length, is(0)); assertThat(betaConfig.get(1).getLocations().length, is(0)); assertThat(betaConfig.get(1).getInitializers().length, is(1)); assertEquals(DummyApplicationContextInitializer.class, betaConfig.get(1).getInitializers()[0]); }
@Test public void processContextConfigurationWithConfigurationClass() { Class<?>[] classes = new Class<?>[] {getClass()}; ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes( getClass(), EMPTY_STRING_ARRAY, classes, true, null, true, ContextLoader.class); loader.processContextConfiguration(configAttributes); assertArrayEquals(classes, configAttributes.getClasses()); assertEmpty(configAttributes.getLocations()); }
@Test public void resolveContextHierarchyAttributesForTestClassHierarchyWithSingleLevelContextHierarchiesAndMetaAnnotations() { List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(TestClass3WithSingleLevelContextHierarchyFromMetaAnnotation.class); assertEquals(3, hierarchyAttributes.size()); List<ContextConfigurationAttributes> configAttributesListClassLevel1 = hierarchyAttributes.get(0); debugConfigAttributes(configAttributesListClassLevel1); assertEquals(1, configAttributesListClassLevel1.size()); assertThat(configAttributesListClassLevel1.get(0).getLocations()[0], equalTo("A.xml")); assertAttributes(configAttributesListClassLevel1.get(0), TestClass1WithSingleLevelContextHierarchyFromMetaAnnotation.class, new String[] { "A.xml" }, EMPTY_CLASS_ARRAY, ContextLoader.class, true); List<ContextConfigurationAttributes> configAttributesListClassLevel2 = hierarchyAttributes.get(1); debugConfigAttributes(configAttributesListClassLevel2); assertEquals(1, configAttributesListClassLevel2.size()); assertArrayEquals(new String[] { "B-one.xml", "B-two.xml" }, configAttributesListClassLevel2.get(0).getLocations()); assertAttributes(configAttributesListClassLevel2.get(0), TestClass2WithSingleLevelContextHierarchyFromMetaAnnotation.class, new String[] { "B-one.xml", "B-two.xml" }, EMPTY_CLASS_ARRAY, ContextLoader.class, true); List<ContextConfigurationAttributes> configAttributesListClassLevel3 = hierarchyAttributes.get(2); debugConfigAttributes(configAttributesListClassLevel3); assertEquals(1, configAttributesListClassLevel3.size()); assertThat(configAttributesListClassLevel3.get(0).getLocations()[0], equalTo("C.xml")); assertAttributes(configAttributesListClassLevel3.get(0), TestClass3WithSingleLevelContextHierarchyFromMetaAnnotation.class, new String[] { "C.xml" }, EMPTY_CLASS_ARRAY, ContextLoader.class, true); }
@Test public 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); assertArrayEquals(locations, configAttributes.getLocations()); assertEmpty(configAttributes.getClasses()); }
@Test public void processContextConfigurationWithDefaultConfigurationClassGeneration() { ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes( ConfigClassTestCase.class, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, true, null, true, ContextLoader.class); loader.processContextConfiguration(configAttributes); assertEquals(1, configAttributes.getClasses().length); assertEmpty(configAttributes.getLocations()); }
@Test public void processContextConfigurationWithDefaultXmlConfigGeneration() { ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes( XmlTestCase.class, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, true, null, true, ContextLoader.class); loader.processContextConfiguration(configAttributes); assertEquals(1, configAttributes.getLocations().length); assertEmpty(configAttributes.getClasses()); }
void assertAttributes(ContextConfigurationAttributes attributes, Class<?> expectedDeclaringClass, String[] expectedLocations, Class<?>[] expectedClasses, Class<? extends ContextLoader> expectedContextLoaderClass, boolean expectedInheritLocations) { assertEquals("declaring class", expectedDeclaringClass, attributes.getDeclaringClass()); assertArrayEquals("locations", expectedLocations, attributes.getLocations()); assertArrayEquals("classes", expectedClasses, attributes.getClasses()); assertEquals("inherit locations", expectedInheritLocations, attributes.isInheritLocations()); assertEquals("context loader", expectedContextLoaderClass, attributes.getContextLoaderClass()); }
/** * Determine if this {@code ContextConfigurationAttributes} instance has * path-based resource locations. * @return {@code true} if the {@link #getLocations() locations} array is not empty * @see #hasResources() * @see #hasClasses() */ public boolean hasLocations() { return (getLocations().length > 0); }
/** * Determine if this {@code ContextConfigurationAttributes} instance has * path-based resource locations. * @return {@code true} if the {@link #getLocations() locations} array is not empty * @see #hasResources() * @see #hasClasses() */ public boolean hasLocations() { return !ObjectUtils.isEmpty(getLocations()); }
/** * For backwards compatibility with the {@link ContextLoader} SPI, the * default implementation simply delegates to {@link #processLocations(Class, String...)}, * passing it the {@link ContextConfigurationAttributes#getDeclaringClass() * declaring class} and {@link ContextConfigurationAttributes#getLocations() * resource locations} retrieved from the supplied * {@link ContextConfigurationAttributes configuration attributes}. The * processed locations are then * {@link ContextConfigurationAttributes#setLocations(String[]) set} in * the supplied configuration attributes. * <p>Can be overridden in subclasses — for example, to process * annotated classes instead of resource locations. * @since 3.1 * @see #processLocations(Class, String...) */ @Override public void processContextConfiguration(ContextConfigurationAttributes configAttributes) { String[] processedLocations = processLocations(configAttributes.getDeclaringClass(), configAttributes.getLocations()); configAttributes.setLocations(processedLocations); }
/** * For backwards compatibility with the {@link ContextLoader} SPI, the * default implementation simply delegates to {@link #processLocations(Class, String...)}, * passing it the {@link ContextConfigurationAttributes#getDeclaringClass() * declaring class} and {@link ContextConfigurationAttributes#getLocations() * resource locations} retrieved from the supplied * {@link ContextConfigurationAttributes configuration attributes}. The * processed locations are then * {@link ContextConfigurationAttributes#setLocations(String[]) set} in * the supplied configuration attributes. * <p>Can be overridden in subclasses — for example, to process * annotated classes instead of resource locations. * @since 3.1 * @see #processLocations(Class, String...) */ @Override public void processContextConfiguration(ContextConfigurationAttributes configAttributes) { String[] processedLocations = processLocations(configAttributes.getDeclaringClass(), configAttributes.getLocations()); configAttributes.setLocations(processedLocations); }