/** * Determine if this {@code MergedContextConfiguration} instance has * path-based context resource locations. * @return {@code true} if the {@link #getLocations() locations} array is not empty * @since 4.0.4 * @see #hasResources() * @see #hasClasses() */ public boolean hasLocations() { return !ObjectUtils.isEmpty(getLocations()); }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getLocations() locations}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasLocations()) { String msg = String.format("Test class [%s] has been configured with @ContextConfiguration's 'locations' " + "(or 'value') attribute %s, but %s does not support resource locations.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getLocations()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Load bean definitions into the supplied {@link GenericApplicationContext context} * from the locations in the supplied {@code MergedContextConfiguration} using a * {@link GroovyBeanDefinitionReader}. * @param context the context into which the bean definitions should be loaded * @param mergedConfig the merged context configuration * @see org.springframework.test.context.support.AbstractGenericContextLoader#loadBeanDefinitions */ @Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { new GroovyBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations()); }
/** * Load the {@code ApplicationContext} for the supplied merged context configuration. * <p>Supports both the {@link SmartContextLoader} and {@link ContextLoader} SPIs. * @throws Exception if an error occurs while loading the application context */ protected ApplicationContext loadContextInternal(MergedContextConfiguration mergedContextConfiguration) throws Exception { ContextLoader contextLoader = mergedContextConfiguration.getContextLoader(); Assert.notNull(contextLoader, "Cannot load an ApplicationContext with a NULL 'contextLoader'. " + "Consider annotating your test class with @ContextConfiguration or @ContextHierarchy."); ApplicationContext applicationContext; if (contextLoader instanceof SmartContextLoader) { SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader; applicationContext = smartContextLoader.loadContext(mergedContextConfiguration); } else { String[] locations = mergedContextConfiguration.getLocations(); Assert.notNull(locations, "Cannot load an ApplicationContext with a NULL 'locations' array. " + "Consider annotating your test class with @ContextConfiguration or @ContextHierarchy."); applicationContext = contextLoader.loadContext(locations); } return applicationContext; }
/** * Load bean definitions into the supplied {@link GenericApplicationContext context} * from the locations or classes in the supplied {@code MergedContextConfiguration}. * * <p>The default implementation delegates to the {@link BeanDefinitionReader} * returned by {@link #createBeanDefinitionReader(GenericApplicationContext)} to * {@link BeanDefinitionReader#loadBeanDefinitions(String) load} the * bean definitions. * * <p>Subclasses must provide an appropriate implementation of * {@link #createBeanDefinitionReader(GenericApplicationContext)}. Alternatively subclasses * may provide a <em>no-op</em> implementation of {@code createBeanDefinitionReader()} * and override this method to provide a custom strategy for loading or * registering bean definitions. * * @param context the context into which the bean definitions should be loaded * @param mergedConfig the merged context configuration * @since 3.1 * @see #loadContext(MergedContextConfiguration) */ protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { createBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations()); }
@Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { // Order doesn't matter: <bean> always wins over @Bean. new XmlBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations()); new AnnotatedBeanDefinitionReader(context).register(mergedConfig.getClasses()); }
void assertMergedConfig( MergedContextConfiguration mergedConfig, Class<?> expectedTestClass, String[] expectedLocations, Class<?>[] expectedClasses, Set<Class<? extends ApplicationContextInitializer<?>>> expectedInitializerClasses, Class<? extends ContextLoader> expectedContextLoaderClass) { assertNotNull(mergedConfig); assertEquals(expectedTestClass, mergedConfig.getTestClass()); assertNotNull(mergedConfig.getLocations()); assertArrayEquals(expectedLocations, mergedConfig.getLocations()); assertNotNull(mergedConfig.getClasses()); assertArrayEquals(expectedClasses, mergedConfig.getClasses()); assertNotNull(mergedConfig.getActiveProfiles()); if (expectedContextLoaderClass == null) { assertNull(mergedConfig.getContextLoader()); } else { assertEquals(expectedContextLoaderClass, mergedConfig.getContextLoader().getClass()); } assertNotNull(mergedConfig.getContextInitializerClasses()); assertEquals(expectedInitializerClasses, mergedConfig.getContextInitializerClasses()); }
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { if (log.isDebugEnabled()) { log.debug("Loading ApplicationContext for merged context configuration [" + mergedConfig + "]."); } ResourceLoadingXmlApplicationContext context = new ResourceLoadingXmlApplicationContext(mergedConfig.getLocations(), testResourceLoader, false); context.getEnvironment().setActiveProfiles(mergedConfig.getActiveProfiles()); prepareContext(context); context.refresh(); context.registerShutdownHook(); return context; }
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { if (log.isDebugEnabled()) { log.debug("Loading ApplicationContext for merged context configuration [" + mergedConfig + "]."); } ResourceLoadingXmlApplicationContext context = new ResourceLoadingXmlApplicationContext(mergedConfig.getLocations(), testResourceLoader, false); context.getEnvironment().setActiveProfiles(mergedConfig.getActiveProfiles()); prepareContext(context); context.refresh(); context.registerShutdownHook(); return context; }
/** * Determine if this {@code MergedContextConfiguration} instance has * path-based context resource locations. * @return {@code true} if the {@link #getLocations() locations} array is not empty * @since 4.0.4 * @see #hasResources() * @see #hasClasses() */ public boolean hasLocations() { return !ObjectUtils.isEmpty(getLocations()); }
/** * Determine if this {@code MergedContextConfiguration} instance has * path-based context resource locations. * @return {@code true} if the {@link #getLocations() locations} array is not empty * @since 4.0.4 * @see #hasResources() * @see #hasClasses() */ public boolean hasLocations() { return !ObjectUtils.isEmpty(getLocations()); }
private static boolean supports(SmartContextLoader loader, MergedContextConfiguration mergedConfig) { if (loader instanceof AnnotationConfigContextLoader) { return ObjectUtils.isEmpty(mergedConfig.getLocations()) && !ObjectUtils.isEmpty(mergedConfig.getClasses()); } else { return !ObjectUtils.isEmpty(mergedConfig.getLocations()) && ObjectUtils.isEmpty(mergedConfig.getClasses()); } }
/** * Added in order to support new version of Spring. There is a new * {@link org.springframework.test.context.SmartContextLoader} interface implemented in Spring. */ @Override public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { return mergedConfig.getContextLoader().loadContext(mergedConfig.getLocations()); }
/** * Load bean definitions into the supplied {@link GenericApplicationContext context} * from the locations in the supplied {@code MergedContextConfiguration} using a * {@link GroovyBeanDefinitionReader}. * @param context the context into which the bean definitions should be loaded * @param mergedConfig the merged context configuration * @see org.springframework.test.context.support.AbstractGenericContextLoader#loadBeanDefinitions */ @Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { new GroovyBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations()); }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getLocations() locations}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasLocations()) { String msg = String.format("Test class [%s] has been configured with @ContextConfiguration's 'locations' " + "(or 'value') attribute %s, but %s does not support resource locations.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getLocations()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Load bean definitions into the supplied {@link GenericApplicationContext context} * from the locations in the supplied {@code MergedContextConfiguration} using a * {@link GroovyBeanDefinitionReader}. * @param context the context into which the bean definitions should be loaded * @param mergedConfig the merged context configuration * @see org.springframework.test.context.support.AbstractGenericContextLoader#loadBeanDefinitions */ @Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { new GroovyBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations()); }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getLocations() locations}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasLocations()) { String msg = String.format("Test class [%s] has been configured with @ContextConfiguration's 'locations' " + "(or 'value') attribute %s, but %s does not support resource locations.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getLocations()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
public TinyMergedContextConfiguration(ContextLoader contextLoader, CacheAwareContextLoaderDelegate contextLoaderDelegate, MergedContextConfiguration mergedContextConfiguration, MergedContextConfiguration parentConfiguration) { super(mergedContextConfiguration.getTestClass(), mergedContextConfiguration.getLocations(), mergedContextConfiguration.getClasses(), mergedContextConfiguration.getActiveProfiles(), contextLoader); this.mergedContextConfiguration = mergedContextConfiguration; this.contextLoaderDelegate = contextLoaderDelegate; this.parentConfiguration = parentConfiguration; }
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { if (logger.isDebugEnabled()) { logger.debug("Loading ApplicationContext for merged context configuration [" + mergedConfig + "]."); } GenericWebApplicationContext context = new GenericWebApplicationContext(); ConfigurableEnvironment environment = context.getEnvironment(); environment.setActiveProfiles(mergedConfig.getActiveProfiles()); return loadContext(context, mergedConfig.getLocations()); }
/** * Create a new {@link MergedContextConfiguration} with different classes and * properties. * @param mergedConfig the source config * @param classes the replacement classes * @param propertySourceProperties the replacement properties * @return a new {@link MergedContextConfiguration} */ protected final MergedContextConfiguration createModifiedConfig( MergedContextConfiguration mergedConfig, Class<?>[] classes, String[] propertySourceProperties) { return new MergedContextConfiguration(mergedConfig.getTestClass(), mergedConfig.getLocations(), classes, mergedConfig.getContextInitializerClasses(), mergedConfig.getActiveProfiles(), mergedConfig.getPropertySourceLocations(), propertySourceProperties, mergedConfig.getContextCustomizers(), mergedConfig.getContextLoader(), getCacheAwareContextLoaderDelegate(), mergedConfig.getParent()); }