Java Code Examples for org.springframework.test.context.MergedContextConfiguration#hasClasses()

The following examples show how to use org.springframework.test.context.MergedContextConfiguration#hasClasses() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericPropertiesContextLoader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 2
Source File: GenericXmlContextLoader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 3
Source File: AbstractDelegatingSmartContextLoader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private boolean supports(SmartContextLoader loader, MergedContextConfiguration mergedConfig) {
	if (loader == getAnnotationConfigLoader()) {
		return (mergedConfig.hasClasses() && !mergedConfig.hasLocations());
	}
	else {
		return (mergedConfig.hasLocations() && !mergedConfig.hasClasses());
	}
}
 
Example 4
Source File: GenericPropertiesContextLoader.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 5
Source File: GenericXmlContextLoader.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 6
Source File: AbstractDelegatingSmartContextLoader.java    From java-technology-stack with MIT License 5 votes vote down vote up
private boolean supports(SmartContextLoader loader, MergedContextConfiguration mergedConfig) {
	if (loader == getAnnotationConfigLoader()) {
		return (mergedConfig.hasClasses() && !mergedConfig.hasLocations());
	}
	else {
		return (mergedConfig.hasLocations() && !mergedConfig.hasClasses());
	}
}
 
Example 7
Source File: GenericPropertiesContextLoader.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 8
Source File: GenericXmlContextLoader.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 9
Source File: AbstractDelegatingSmartContextLoader.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private boolean supports(SmartContextLoader loader, MergedContextConfiguration mergedConfig) {
	if (loader == getAnnotationConfigLoader()) {
		return mergedConfig.hasClasses() && !mergedConfig.hasLocations();
	}
	else {
		return mergedConfig.hasLocations() && !mergedConfig.hasClasses();
	}
}
 
Example 10
Source File: AbstractDelegatingSmartContextLoader.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Delegates to an appropriate candidate {@code SmartContextLoader} to load
 * an {@link ApplicationContext}.
 * <p>Delegation is based on explicit knowledge of the implementations of the
 * default loaders for {@linkplain #getXmlLoader() XML configuration files and
 * Groovy scripts} and {@linkplain #getAnnotationConfigLoader() annotated classes}.
 * Specifically, the delegation algorithm is as follows:
 * <ul>
 * <li>If the resource locations in the supplied {@code MergedContextConfiguration}
 * are not empty and the annotated classes are empty,
 * the XML-based loader will load the {@code ApplicationContext}.</li>
 * <li>If the annotated classes in the supplied {@code MergedContextConfiguration}
 * are not empty and the resource locations are empty,
 * the annotation-based loader will load the {@code ApplicationContext}.</li>
 * </ul>
 * @param mergedConfig the merged context configuration to use to load the application context
 * @throws IllegalArgumentException if the supplied merged configuration is {@code null}
 * @throws IllegalStateException if neither candidate loader is capable of loading an
 * {@code ApplicationContext} from the supplied merged context configuration
 */
@Override
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
	Assert.notNull(mergedConfig, "mergedConfig must not be null");
	List<SmartContextLoader> candidates = Arrays.asList(getXmlLoader(), getAnnotationConfigLoader());

	if (mergedConfig.hasLocations() && mergedConfig.hasClasses()) {
		throw new IllegalStateException(String.format(
			"Neither %s nor %s supports loading an ApplicationContext from %s: "
					+ "declare either 'locations' or 'classes' but not both.", name(getXmlLoader()),
			name(getAnnotationConfigLoader()), mergedConfig));
	}

	for (SmartContextLoader loader : candidates) {
		// Determine if each loader can load a context from the mergedConfig. If it
		// can, let it; otherwise, keep iterating.
		if (supports(loader, mergedConfig)) {
			return delegateLoading(loader, mergedConfig);
		}
	}

	// If neither of the candidates supports the mergedConfig based on resources but
	// ACIs were declared, then delegate to the annotation config loader.
	if (!mergedConfig.getContextInitializerClasses().isEmpty()) {
		return delegateLoading(getAnnotationConfigLoader(), mergedConfig);
	}

	// else...
	throw new IllegalStateException(String.format(
		"Neither %s nor %s was able to load an ApplicationContext from %s.", name(getXmlLoader()),
		name(getAnnotationConfigLoader()), mergedConfig));
}