/** * Resolve the set of merged {@code ApplicationContextInitializer} classes for the * supplied list of {@code ContextConfigurationAttributes}. * * <p>Note that the {@link ContextConfiguration#inheritInitializers inheritInitializers} * flag of {@link ContextConfiguration @ContextConfiguration} will be taken into * consideration. Specifically, if the {@code inheritInitializers} flag is set to * {@code true} for a given level in the class hierarchy represented by the provided * configuration attributes, context initializer classes defined at the given level * will be merged with those defined in higher levels of the class hierarchy. * * @param configAttributesList the list of configuration attributes to process; must * not be {@code null} or <em>empty</em>; must be ordered <em>bottom-up</em> * (i.e., as if we were traversing up the class hierarchy) * @return the set of merged context initializer classes, including those from * superclasses if appropriate (never {@code null}) * @since 3.2 */ static Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> resolveInitializerClasses( List<ContextConfigurationAttributes> configAttributesList) { Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be empty"); final Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> initializerClasses = // new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>(); for (ContextConfigurationAttributes configAttributes : configAttributesList) { if (logger.isTraceEnabled()) { logger.trace(String.format("Processing context initializers for context configuration attributes %s", configAttributes)); } initializerClasses.addAll(Arrays.asList(configAttributes.getInitializers())); if (!configAttributes.isInheritInitializers()) { break; } } return initializerClasses; }
/** * Resolve the {@link ContextLoader} {@linkplain Class class} to use for the * supplied list of {@link ContextConfigurationAttributes} and then instantiate * and return that {@code ContextLoader}. * <p>If the user has not explicitly declared which loader to use, the value * returned from {@link #getDefaultContextLoaderClass} will be used as the * default context loader class. For details on the class resolution process, * see {@link #resolveExplicitContextLoaderClass} and * {@link #getDefaultContextLoaderClass}. * @param testClass the test class for which the {@code ContextLoader} should be * resolved; must not be {@code null} * @param configAttributesList the list of configuration attributes to process; must * not be {@code null} or <em>empty</em>; must be ordered <em>bottom-up</em> * (i.e., as if we were traversing up the class hierarchy) * @return the resolved {@code ContextLoader} for the supplied {@code testClass} * (never {@code null}) * @throws IllegalStateException if {@link #getDefaultContextLoaderClass(Class)} * returns {@code null} */ protected ContextLoader resolveContextLoader(Class<?> testClass, List<ContextConfigurationAttributes> configAttributesList) { Assert.notNull(testClass, "Class must not be null"); Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be empty"); Class<? extends ContextLoader> contextLoaderClass = resolveExplicitContextLoaderClass(configAttributesList); if (contextLoaderClass == null) { contextLoaderClass = getDefaultContextLoaderClass(testClass); if (contextLoaderClass == null) { throw new IllegalStateException("getDefaultContextLoaderClass() must not return null"); } } if (logger.isTraceEnabled()) { logger.trace(String.format("Using ContextLoader class [%s] for test class [%s]", contextLoaderClass.getName(), testClass.getName())); } return BeanUtils.instantiateClass(contextLoaderClass, ContextLoader.class); }
/** * Resolve the {@link ContextLoader} {@linkplain Class class} to use for the supplied * list of {@link ContextConfigurationAttributes}. * <p>Beginning with the first level in the context configuration attributes hierarchy: * <ol> * <li>If the {@link ContextConfigurationAttributes#getContextLoaderClass() * contextLoaderClass} property of {@link ContextConfigurationAttributes} is * configured with an explicit class, that class will be returned.</li> * <li>If an explicit {@code ContextLoader} class is not specified at the current * level in the hierarchy, traverse to the next level in the hierarchy and return to * step #1.</li> * </ol> * @param configAttributesList the list of configuration attributes to process; * must not be {@code null} or <em>empty</em>; must be ordered <em>bottom-up</em> * (i.e., as if we were traversing up the class hierarchy) * @return the {@code ContextLoader} class to use for the supplied configuration * attributes, or {@code null} if no explicit loader is found * @throws IllegalArgumentException if supplied configuration attributes are * {@code null} or <em>empty</em> */ protected Class<? extends ContextLoader> resolveExplicitContextLoaderClass( List<ContextConfigurationAttributes> configAttributesList) { Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be empty"); for (ContextConfigurationAttributes configAttributes : configAttributesList) { if (logger.isTraceEnabled()) { logger.trace(String.format("Resolving ContextLoader for context configuration attributes %s", configAttributes)); } Class<? extends ContextLoader> contextLoaderClass = configAttributes.getContextLoaderClass(); if (ContextLoader.class != contextLoaderClass) { if (logger.isDebugEnabled()) { logger.debug(String.format( "Found explicit ContextLoader class [%s] for context configuration attributes %s", contextLoaderClass.getName(), configAttributes)); } return contextLoaderClass; } } return null; }
/** * Resolve the list of {@linkplain ContextConfigurationAttributes context * configuration attributes} for the supplied {@linkplain Class test class} and its * superclasses. * <p>Note that the {@link ContextConfiguration#inheritLocations inheritLocations} and * {@link ContextConfiguration#inheritInitializers() inheritInitializers} flags of * {@link ContextConfiguration @ContextConfiguration} will <strong>not</strong> * be taken into consideration. If these flags need to be honored, that must be * handled manually when traversing the list returned by this method. * @param testClass the class for which to resolve the configuration attributes * (must not be {@code null}) * @return the list of configuration attributes for the specified class, ordered * <em>bottom-up</em> (i.e., as if we were traversing up the class hierarchy); * never {@code null} * @throws IllegalArgumentException if the supplied class is {@code null} or if * {@code @ContextConfiguration} is not <em>present</em> on the supplied class */ static List<ContextConfigurationAttributes> resolveContextConfigurationAttributes(Class<?> testClass) { Assert.notNull(testClass, "Class must not be null"); List<ContextConfigurationAttributes> attributesList = new ArrayList<ContextConfigurationAttributes>(); Class<ContextConfiguration> annotationType = ContextConfiguration.class; AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(testClass, annotationType); if (descriptor == null) { throw new IllegalArgumentException(String.format( "Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]", annotationType.getName(), testClass.getName())); } while (descriptor != null) { convertContextConfigToConfigAttributesAndAddToList(descriptor.synthesizeAnnotation(), descriptor.getRootDeclaringClass(), attributesList); descriptor = findAnnotationDescriptor(descriptor.getRootDeclaringClass().getSuperclass(), annotationType); } return attributesList; }
@Test public void resolveContextHierarchyAttributesForSingleTestClassWithTripleLevelContextHierarchy() { Class<SingleTestClassWithTripleLevelContextHierarchy> testClass = SingleTestClassWithTripleLevelContextHierarchy.class; List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(testClass); assertEquals(1, hierarchyAttributes.size()); List<ContextConfigurationAttributes> configAttributesList = hierarchyAttributes.get(0); assertNotNull(configAttributesList); assertEquals(3, configAttributesList.size()); debugConfigAttributes(configAttributesList); assertAttributes(configAttributesList.get(0), testClass, new String[] { "A.xml" }, EMPTY_CLASS_ARRAY, ContextLoader.class, true); assertAttributes(configAttributesList.get(1), testClass, new String[] { "B.xml" }, EMPTY_CLASS_ARRAY, ContextLoader.class, true); assertAttributes(configAttributesList.get(2), testClass, new String[] { "C.xml" }, EMPTY_CLASS_ARRAY, ContextLoader.class, true); }
@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")); }
@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 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")); }
/** * 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]); }
@Override public void processContextConfiguration(ContextConfigurationAttributes configAttributes) { String className = "io.leopard.javahost.AutoUnitRunnable"; try { Runnable runnable = (Runnable) Class.forName(className).newInstance(); runnable.run(); } catch (Exception e) { // System.err.println("init hosts error:" + e.toString()); // e.printStackTrace(); } String[] locations = new String[0]; if (locations.length == 0) { locations = new ApplicationContextLocationImpl().get(); } // files = ArrayUtil.insertFirst(files, "/leopard-test/annotation-config.xml"); locations = StringUtils.addStringToArray(locations, "/leopard-test/annotation-config.xml"); configAttributes.setLocations(locations); // System.err.println("processContextConfiguration:" + org.apache.commons.lang.StringUtils.join(configAttributes.getLocations(), ",")); }
@Override public ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configAttributes) { AutoConfigureEmbeddedDatabase databaseAnnotation = AnnotatedElementUtils.findMergedAnnotation(testClass, AutoConfigureEmbeddedDatabase.class); if (databaseAnnotation != null && databaseAnnotation.type() == EmbeddedDatabaseType.POSTGRES && databaseAnnotation.replace() != Replace.NONE) { return new PreloadableEmbeddedPostgresContextCustomizer(databaseAnnotation); } return null; }
private static void delegateProcessing(SmartContextLoader loader, ContextConfigurationAttributes configAttributes) { if (logger.isDebugEnabled()) { logger.debug(String.format("Delegating to %s to process context configuration %s.", name(loader), configAttributes)); } loader.processContextConfiguration(configAttributes); }
/** * 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; }
/** * Convenience method for creating a {@link ContextConfigurationAttributes} * instance from the supplied {@link ContextConfiguration} annotation and * declaring class and then adding the attributes to the supplied list. */ private static void convertContextConfigToConfigAttributesAndAddToList(ContextConfiguration contextConfiguration, Class<?> declaringClass, final List<ContextConfigurationAttributes> attributesList) { if (logger.isTraceEnabled()) { logger.trace(String.format("Retrieved @ContextConfiguration [%s] for declaring class [%s].", contextConfiguration, declaringClass.getName())); } ContextConfigurationAttributes attributes = new ContextConfigurationAttributes(declaringClass, contextConfiguration); if (logger.isTraceEnabled()) { logger.trace("Resolved context configuration attributes: " + attributes); } attributesList.add(attributes); }
@Override public void processContextConfiguration(ContextConfigurationAttributes configAttributes) { // Detect default XML configuration files: super.processContextConfiguration(configAttributes); // Detect default configuration classes: if (!configAttributes.hasClasses() && isGenerateDefaultLocations()) { configAttributes.setClasses(detectDefaultConfigurationClasses(configAttributes.getDeclaringClass())); } }
@Test public void resolveConfigAttributesWithBareAnnotations() { Class<BareAnnotations> testClass = BareAnnotations.class; List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(testClass); assertNotNull(attributesList); assertEquals(1, attributesList.size()); assertAttributes(attributesList.get(0), testClass, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, ContextLoader.class, true); }
@Test public void resolveConfigAttributesWithLocalAnnotationAndLocations() { List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(LocationsFoo.class); assertNotNull(attributesList); assertEquals(1, attributesList.size()); assertLocationsFooAttributes(attributesList.get(0)); }
@Test public void resolveConfigAttributesWithMetaAnnotationAndLocations() { Class<MetaLocationsFoo> testClass = MetaLocationsFoo.class; List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(testClass); assertNotNull(attributesList); assertEquals(1, attributesList.size()); assertAttributes(attributesList.get(0), testClass, new String[] {"/foo.xml"}, EMPTY_CLASS_ARRAY, ContextLoader.class, true); }
@Test public void resolveConfigAttributesWithMetaAnnotationAndLocationsAndOverrides() { Class<MetaLocationsFooWithOverrides> testClass = MetaLocationsFooWithOverrides.class; List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(testClass); assertNotNull(attributesList); assertEquals(1, attributesList.size()); assertAttributes(attributesList.get(0), testClass, new String[] {"/foo.xml"}, EMPTY_CLASS_ARRAY, ContextLoader.class, true); }
@Test public void resolveConfigAttributesWithMetaAnnotationAndLocationsAndOverriddenAttributes() { Class<MetaLocationsFooWithOverriddenAttributes> testClass = MetaLocationsFooWithOverriddenAttributes.class; List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(testClass); assertNotNull(attributesList); assertEquals(1, attributesList.size()); assertAttributes(attributesList.get(0), testClass, new String[] {"foo1.xml", "foo2.xml"}, EMPTY_CLASS_ARRAY, ContextLoader.class, true); }
@Test public void resolveConfigAttributesWithMetaAnnotationAndLocationsInClassHierarchy() { Class<MetaLocationsBar> testClass = MetaLocationsBar.class; List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(testClass); assertNotNull(attributesList); assertEquals(2, attributesList.size()); assertAttributes(attributesList.get(0), testClass, new String[] {"/bar.xml"}, EMPTY_CLASS_ARRAY, ContextLoader.class, true); assertAttributes(attributesList.get(1), MetaLocationsFoo.class, new String[] {"/foo.xml"}, EMPTY_CLASS_ARRAY, ContextLoader.class, true); }
@Test public void resolveConfigAttributesWithLocalAnnotationAndClasses() { List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(ClassesFoo.class); assertNotNull(attributesList); assertEquals(1, attributesList.size()); assertClassesFooAttributes(attributesList.get(0)); }
@Test public void resolveConfigAttributesWithLocalAndInheritedAnnotationsAndLocations() { List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(LocationsBar.class); assertNotNull(attributesList); assertEquals(2, attributesList.size()); assertLocationsBarAttributes(attributesList.get(0)); assertLocationsFooAttributes(attributesList.get(1)); }
@Test public void resolveConfigAttributesWithLocalAndInheritedAnnotationsAndClasses() { List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(ClassesBar.class); assertNotNull(attributesList); assertEquals(2, attributesList.size()); assertClassesBarAttributes(attributesList.get(0)); assertClassesFooAttributes(attributesList.get(1)); }
/** * Verifies change requested in <a href="https://jira.spring.io/browse/SPR-11634">SPR-11634</a>. * @since 4.0.4 */ @Test public void resolveConfigAttributesWithLocationsAndClasses() { List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(LocationsAndClasses.class); assertNotNull(attributesList); assertEquals(1, attributesList.size()); }
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()); }
@Test public void processContextConfigurationWithoutLocationsAndConfigurationClassesForBogusTestClass() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage(startsWith("Neither")); expectedException.expectMessage(containsString("was able to detect defaults")); ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes(getClass(), EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, true, null, true, ContextLoader.class); loader.processContextConfiguration(configAttributes); }
@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()); }
@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 processContextConfigurationWithDefaultXmlConfigAndConfigurationClassGeneration() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage(containsString("both default locations AND default configuration classes were detected")); ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes( ImproperDuplicateDefaultXmlAndConfigClassTestCase.class, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, true, null, true, ContextLoader.class); loader.processContextConfiguration(configAttributes); }
@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 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 resolveContextHierarchyAttributesForSingleTestClassWithSingleLevelContextHierarchy() { List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(SingleTestClassWithSingleLevelContextHierarchy.class); assertEquals(1, hierarchyAttributes.size()); List<ContextConfigurationAttributes> configAttributesList = hierarchyAttributes.get(0); assertEquals(1, configAttributesList.size()); debugConfigAttributes(configAttributesList); }
@Test public void resolveContextHierarchyAttributesForSingleTestClassWithSingleLevelContextHierarchyFromMetaAnnotation() { Class<SingleTestClassWithSingleLevelContextHierarchyFromMetaAnnotation> testClass = SingleTestClassWithSingleLevelContextHierarchyFromMetaAnnotation.class; List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(testClass); assertEquals(1, hierarchyAttributes.size()); List<ContextConfigurationAttributes> configAttributesList = hierarchyAttributes.get(0); assertNotNull(configAttributesList); assertEquals(1, configAttributesList.size()); debugConfigAttributes(configAttributesList); assertAttributes(configAttributesList.get(0), testClass, new String[] { "A.xml" }, EMPTY_CLASS_ARRAY, ContextLoader.class, true); }
@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); }
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 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 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")); }
@Override public ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configurationAttributes) { OverrideAutoConfiguration annotation = AnnotatedElementUtils .findMergedAnnotation(testClass, OverrideAutoConfiguration.class); if (annotation != null && !annotation.enabled()) { return new DisableAutoConfigurationContextCustomizer(); } return null; }
@Override public ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configurationAttributes) { AnnotationsPropertySource propertySource = new AnnotationsPropertySource( testClass); return new PropertyMappingContextCustomizer(propertySource); }