/** * Determine if this {@code MergedContextConfiguration} instance has * class-based resources. * @return {@code true} if the {@link #getClasses() classes} array is not empty * @since 4.0.4 * @see #hasResources() * @see #hasLocations() */ public boolean hasClasses() { return !ObjectUtils.isEmpty(getClasses()); }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getClasses() classes}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasClasses()) { String msg = String.format( "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getClasses() classes}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasClasses()) { String msg = String.format( "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Register classes in the supplied {@link GenericApplicationContext context} * from the classes in the supplied {@link MergedContextConfiguration}. * <p>Each class must represent an <em>annotated class</em>. An * {@link AnnotatedBeanDefinitionReader} is used to register the appropriate * bean definitions. * <p>Note that this method does not call {@link #createBeanDefinitionReader} * since {@code AnnotatedBeanDefinitionReader} is not an instance of * {@link BeanDefinitionReader}. * @param context the context in which the annotated classes should be registered * @param mergedConfig the merged configuration from which the classes should be retrieved * @see AbstractGenericContextLoader#loadBeanDefinitions */ @Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { Class<?>[] annotatedClasses = mergedConfig.getClasses(); if (logger.isDebugEnabled()) { logger.debug("Registering annotated classes: " + ObjectUtils.nullSafeToString(annotatedClasses)); } new AnnotatedBeanDefinitionReader(context).register(annotatedClasses); }
@SuppressWarnings("unchecked") private static void assertCacheContents(DefaultContextCache cache, String... expectedNames) { Map<MergedContextConfiguration, ApplicationContext> contextMap = (Map<MergedContextConfiguration, ApplicationContext>) ReflectionTestUtils.getField(cache, "contextMap"); // @formatter:off List<String> actualNames = contextMap.keySet().stream() .map(cfg -> cfg.getClasses()[0]) .map(Class::getSimpleName) .collect(toList()); // @formatter:on assertEquals(asList(expectedNames), actualNames); }
@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()); }
/** * Determine if this {@code MergedContextConfiguration} instance has * class-based resources. * @return {@code true} if the {@link #getClasses() classes} array is not empty * @since 4.0.4 * @see #hasResources() * @see #hasLocations() */ public boolean hasClasses() { return !ObjectUtils.isEmpty(getClasses()); }
/** * Determine if this {@code MergedContextConfiguration} instance has * class-based resources. * @return {@code true} if the {@link #getClasses() classes} array is not empty * @since 4.0.4 * @see #hasResources() * @see #hasLocations() */ public boolean hasClasses() { return !ObjectUtils.isEmpty(getClasses()); }
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()); } }
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; }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getClasses() classes}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasClasses()) { String msg = String.format( "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getClasses() classes}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasClasses()) { String msg = String.format( "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getClasses() classes}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasClasses()) { String msg = String.format( "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getClasses() classes}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasClasses()) { String msg = String.format( "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Register classes in the supplied {@link GenericApplicationContext context} * from the classes in the supplied {@link MergedContextConfiguration}. * <p>Each class must represent an <em>annotated class</em>. An * {@link AnnotatedBeanDefinitionReader} is used to register the appropriate * bean definitions. * <p>Note that this method does not call {@link #createBeanDefinitionReader} * since {@code AnnotatedBeanDefinitionReader} is not an instance of * {@link BeanDefinitionReader}. * @param context the context in which the annotated classes should be registered * @param mergedConfig the merged configuration from which the classes should be retrieved * @see AbstractGenericContextLoader#loadBeanDefinitions */ @Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { Class<?>[] annotatedClasses = mergedConfig.getClasses(); if (logger.isDebugEnabled()) { logger.debug("Registering annotated classes: " + ObjectUtils.nullSafeToString(annotatedClasses)); } new AnnotatedBeanDefinitionReader(context).register(annotatedClasses); }
/** * Register classes in the supplied {@link GenericApplicationContext context} * from the classes in the supplied {@link MergedContextConfiguration}. * <p>Each class must represent an <em>annotated class</em>. An * {@link AnnotatedBeanDefinitionReader} is used to register the appropriate * bean definitions. * <p>Note that this method does not call {@link #createBeanDefinitionReader} * since {@code AnnotatedBeanDefinitionReader} is not an instance of * {@link BeanDefinitionReader}. * @param context the context in which the annotated classes should be registered * @param mergedConfig the merged configuration from which the classes should be retrieved * @see AbstractGenericContextLoader#loadBeanDefinitions */ @Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { Class<?>[] annotatedClasses = mergedConfig.getClasses(); if (logger.isDebugEnabled()) { logger.debug("Registering annotated classes: " + ObjectUtils.nullSafeToString(annotatedClasses)); } new AnnotatedBeanDefinitionReader(context).register(annotatedClasses); }
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; }
protected Class<?>[] getOrFindConfigurationClasses( MergedContextConfiguration mergedConfig) { Class<?>[] classes = mergedConfig.getClasses(); if (containsNonTestComponent(classes) || mergedConfig.hasLocations()) { return classes; } Class<?> found = new AnnotatedClassFinder(SpringBootConfiguration.class) .findFromClass(mergedConfig.getTestClass()); Assert.state(found != null, "Unable to find a @SpringBootConfiguration, you need to use " + "@ContextConfiguration or @SpringBootTest(classes=...) " + "with your test"); logger.info("Found @SpringBootConfiguration " + found.getName() + " for test " + mergedConfig.getTestClass()); return merge(found, classes); }
@Override public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception { Class<?>[] configClasses = config.getClasses(); String[] configLocations = config.getLocations(); Assert.state(