org.springframework.test.context.ContextConfigurationAttributes#hasClasses ( )源码实例Demo

下面列出了org.springframework.test.context.ContextConfigurationAttributes#hasClasses ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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()));
	}
}
 
@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()));
	}
}
 
@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()));
	}
}
 
/**
 * Delegates to candidate {@code SmartContextLoaders} to process the supplied
 * {@link ContextConfigurationAttributes}.
 * <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 or annotated classes in the supplied
 * {@code ContextConfigurationAttributes} are not empty, the appropriate
 * candidate loader will be allowed to process the configuration <em>as is</em>,
 * without any checks for detection of defaults.</li>
 * <li>Otherwise, the XML-based loader will be allowed to process
 * the configuration in order to detect default resource locations. If
 * the XML-based loader detects default resource locations,
 * an {@code info} message will be logged.</li>
 * <li>Subsequently, the annotation-based loader will be allowed to
 * process the configuration in order to detect default configuration classes.
 * If the annotation-based loader detects default configuration
 * classes, an {@code info} message will be logged.</li>
 * </ul>
 * @param configAttributes the context configuration attributes to process
 * @throws IllegalArgumentException if the supplied configuration attributes are
 * {@code null}, or if the supplied configuration attributes include both
 * resource locations and annotated classes
 * @throws IllegalStateException if the XML-based loader detects default
 * configuration classes; if the annotation-based loader detects default
 * resource locations; if neither candidate loader detects defaults for the supplied
 * context configuration; or if both candidate loaders detect defaults for the
 * supplied context configuration
 */
@Override
public void processContextConfiguration(final ContextConfigurationAttributes configAttributes) {
	Assert.notNull(configAttributes, "configAttributes must not be null");
	Assert.isTrue(!(configAttributes.hasLocations() && configAttributes.hasClasses()),
			() -> String.format("Cannot process locations AND classes for context configuration %s: " +
					"configure one or the other, but not both.", configAttributes));

	// If the original locations or classes were not empty, there's no
	// need to bother with default detection checks; just let the
	// appropriate loader process the configuration.
	if (configAttributes.hasLocations()) {
		delegateProcessing(getXmlLoader(), configAttributes);
	}
	else if (configAttributes.hasClasses()) {
		delegateProcessing(getAnnotationConfigLoader(), configAttributes);
	}
	else {
		// Else attempt to detect defaults...

		// Let the XML loader process the configuration.
		delegateProcessing(getXmlLoader(), configAttributes);
		boolean xmlLoaderDetectedDefaults = configAttributes.hasLocations();

		if (xmlLoaderDetectedDefaults) {
			if (logger.isInfoEnabled()) {
				logger.info(String.format("%s detected default locations for context configuration %s.",
						name(getXmlLoader()), configAttributes));
			}
		}

		Assert.state(!configAttributes.hasClasses(), () -> String.format(
				"%s should NOT have detected default configuration classes for context configuration %s.",
				name(getXmlLoader()), configAttributes));

		// Now let the annotation config loader process the configuration.
		delegateProcessing(getAnnotationConfigLoader(), configAttributes);

		if (configAttributes.hasClasses()) {
			if (logger.isInfoEnabled()) {
				logger.info(String.format("%s detected default configuration classes for context configuration %s.",
						name(getAnnotationConfigLoader()), configAttributes));
			}
		}

		Assert.state(xmlLoaderDetectedDefaults || !configAttributes.hasLocations(), () -> String.format(
				"%s should NOT have detected default locations for context configuration %s.",
				name(getAnnotationConfigLoader()), configAttributes));

		if (configAttributes.hasLocations() && configAttributes.hasClasses()) {
			String msg = String.format(
					"Configuration error: both default locations AND default configuration classes " +
					"were detected for context configuration %s; configure one or the other, but not both.",
					configAttributes);
			logger.error(msg);
			throw new IllegalStateException(msg);
		}
	}
}
 
/**
 * Delegates to candidate {@code SmartContextLoaders} to process the supplied
 * {@link ContextConfigurationAttributes}.
 * <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 or annotated classes in the supplied
 * {@code ContextConfigurationAttributes} are not empty, the appropriate
 * candidate loader will be allowed to process the configuration <em>as is</em>,
 * without any checks for detection of defaults.</li>
 * <li>Otherwise, the XML-based loader will be allowed to process
 * the configuration in order to detect default resource locations. If
 * the XML-based loader detects default resource locations,
 * an {@code info} message will be logged.</li>
 * <li>Subsequently, the annotation-based loader will be allowed to
 * process the configuration in order to detect default configuration classes.
 * If the annotation-based loader detects default configuration
 * classes, an {@code info} message will be logged.</li>
 * </ul>
 * @param configAttributes the context configuration attributes to process
 * @throws IllegalArgumentException if the supplied configuration attributes are
 * {@code null}, or if the supplied configuration attributes include both
 * resource locations and annotated classes
 * @throws IllegalStateException if the XML-based loader detects default
 * configuration classes; if the annotation-based loader detects default
 * resource locations; if neither candidate loader detects defaults for the supplied
 * context configuration; or if both candidate loaders detect defaults for the
 * supplied context configuration
 */
@Override
public void processContextConfiguration(final ContextConfigurationAttributes configAttributes) {
	Assert.notNull(configAttributes, "configAttributes must not be null");
	Assert.isTrue(!(configAttributes.hasLocations() && configAttributes.hasClasses()),
			() -> String.format("Cannot process locations AND classes for context configuration %s: " +
					"configure one or the other, but not both.", configAttributes));

	// If the original locations or classes were not empty, there's no
	// need to bother with default detection checks; just let the
	// appropriate loader process the configuration.
	if (configAttributes.hasLocations()) {
		delegateProcessing(getXmlLoader(), configAttributes);
	}
	else if (configAttributes.hasClasses()) {
		delegateProcessing(getAnnotationConfigLoader(), configAttributes);
	}
	else {
		// Else attempt to detect defaults...

		// Let the XML loader process the configuration.
		delegateProcessing(getXmlLoader(), configAttributes);
		boolean xmlLoaderDetectedDefaults = configAttributes.hasLocations();

		if (xmlLoaderDetectedDefaults) {
			if (logger.isInfoEnabled()) {
				logger.info(String.format("%s detected default locations for context configuration %s.",
						name(getXmlLoader()), configAttributes));
			}
		}

		Assert.state(!configAttributes.hasClasses(), () -> String.format(
				"%s should NOT have detected default configuration classes for context configuration %s.",
				name(getXmlLoader()), configAttributes));

		// Now let the annotation config loader process the configuration.
		delegateProcessing(getAnnotationConfigLoader(), configAttributes);

		if (configAttributes.hasClasses()) {
			if (logger.isInfoEnabled()) {
				logger.info(String.format("%s detected default configuration classes for context configuration %s.",
						name(getAnnotationConfigLoader()), configAttributes));
			}
		}

		Assert.state(xmlLoaderDetectedDefaults || !configAttributes.hasLocations(), () -> String.format(
				"%s should NOT have detected default locations for context configuration %s.",
				name(getAnnotationConfigLoader()), configAttributes));

		if (configAttributes.hasLocations() && configAttributes.hasClasses()) {
			String msg = String.format(
					"Configuration error: both default locations AND default configuration classes " +
					"were detected for context configuration %s; configure one or the other, but not both.",
					configAttributes);
			logger.error(msg);
			throw new IllegalStateException(msg);
		}
	}
}
 
/**
 * Process <em>annotated classes</em> in the supplied {@link ContextConfigurationAttributes}.
 * <p>If the <em>annotated classes</em> are {@code null} or empty and
 * {@link #isGenerateDefaultLocations()} returns {@code true}, this
 * {@code SmartContextLoader} will attempt to {@linkplain
 * #detectDefaultConfigurationClasses detect default configuration classes}.
 * If defaults are detected they will be
 * {@linkplain ContextConfigurationAttributes#setClasses(Class[]) set} in the
 * supplied configuration attributes. Otherwise, properties in the supplied
 * configuration attributes will not be modified.
 * @param configAttributes the context configuration attributes to process
 * @see org.springframework.test.context.SmartContextLoader#processContextConfiguration(ContextConfigurationAttributes)
 * @see #isGenerateDefaultLocations()
 * @see #detectDefaultConfigurationClasses(Class)
 */
@Override
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
	if (!configAttributes.hasClasses() && isGenerateDefaultLocations()) {
		configAttributes.setClasses(detectDefaultConfigurationClasses(configAttributes.getDeclaringClass()));
	}
}
 
/**
 * Process <em>annotated classes</em> in the supplied {@link ContextConfigurationAttributes}.
 * <p>If the <em>annotated classes</em> are {@code null} or empty and
 * {@link #isGenerateDefaultLocations()} returns {@code true}, this
 * {@code SmartContextLoader} will attempt to {@link
 * #detectDefaultConfigurationClasses detect default configuration classes}.
 * If defaults are detected they will be
 * {@link ContextConfigurationAttributes#setClasses(Class[]) set} in the
 * supplied configuration attributes. Otherwise, properties in the supplied
 * configuration attributes will not be modified.
 * @param configAttributes the context configuration attributes to process
 * @see org.springframework.test.context.SmartContextLoader#processContextConfiguration(ContextConfigurationAttributes)
 * @see #isGenerateDefaultLocations()
 * @see #detectDefaultConfigurationClasses(Class)
 */
@Override
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
	if (!configAttributes.hasClasses() && isGenerateDefaultLocations()) {
		configAttributes.setClasses(detectDefaultConfigurationClasses(configAttributes.getDeclaringClass()));
	}
}
 
/**
 * Process <em>annotated classes</em> in the supplied {@link ContextConfigurationAttributes}.
 * <p>If the <em>annotated classes</em> are {@code null} or empty and
 * {@link #isGenerateDefaultLocations()} returns {@code true}, this
 * {@code SmartContextLoader} will attempt to {@linkplain
 * #detectDefaultConfigurationClasses detect default configuration classes}.
 * If defaults are detected they will be
 * {@linkplain ContextConfigurationAttributes#setClasses(Class[]) set} in the
 * supplied configuration attributes. Otherwise, properties in the supplied
 * configuration attributes will not be modified.
 * @param configAttributes the context configuration attributes to process
 * @see org.springframework.test.context.SmartContextLoader#processContextConfiguration(ContextConfigurationAttributes)
 * @see #isGenerateDefaultLocations()
 * @see #detectDefaultConfigurationClasses(Class)
 */
@Override
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
	if (!configAttributes.hasClasses() && isGenerateDefaultLocations()) {
		configAttributes.setClasses(detectDefaultConfigurationClasses(configAttributes.getDeclaringClass()));
	}
}
 
/**
 * Process <em>annotated classes</em> in the supplied {@link ContextConfigurationAttributes}.
 * <p>If the <em>annotated classes</em> are {@code null} or empty and
 * {@link #isGenerateDefaultLocations()} returns {@code true}, this
 * {@code SmartContextLoader} will attempt to {@link
 * #detectDefaultConfigurationClasses detect default configuration classes}.
 * If defaults are detected they will be
 * {@link ContextConfigurationAttributes#setClasses(Class[]) set} in the
 * supplied configuration attributes. Otherwise, properties in the supplied
 * configuration attributes will not be modified.
 * @param configAttributes the context configuration attributes to process
 * @see org.springframework.test.context.SmartContextLoader#processContextConfiguration(ContextConfigurationAttributes)
 * @see #isGenerateDefaultLocations()
 * @see #detectDefaultConfigurationClasses(Class)
 */
@Override
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
	if (!configAttributes.hasClasses() && isGenerateDefaultLocations()) {
		configAttributes.setClasses(detectDefaultConfigurationClasses(configAttributes.getDeclaringClass()));
	}
}
 
/**
 * Process <em>annotated classes</em> in the supplied {@link ContextConfigurationAttributes}.
 *
 * <p>If the <em>annotated classes</em> are {@code null} or empty and
 * {@link #isGenerateDefaultLocations()} returns {@code true}, this
 * {@code SmartContextLoader} will attempt to {@linkplain
 * #detectDefaultConfigurationClasses detect default configuration classes}.
 * If defaults are detected they will be
 * {@linkplain ContextConfigurationAttributes#setClasses(Class[]) set} in the
 * supplied configuration attributes. Otherwise, properties in the supplied
 * configuration attributes will not be modified.
 *
 * @param configAttributes the context configuration attributes to process
 * @see org.springframework.test.context.SmartContextLoader#processContextConfiguration(ContextConfigurationAttributes)
 * @see #isGenerateDefaultLocations()
 * @see #detectDefaultConfigurationClasses(Class)
 */
@Override
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
	if (!configAttributes.hasClasses() && isGenerateDefaultLocations()) {
		configAttributes.setClasses(detectDefaultConfigurationClasses(configAttributes.getDeclaringClass()));
	}
}
 
/**
 * Process <em>annotated classes</em> in the supplied {@link ContextConfigurationAttributes}.
 *
 * <p>If the <em>annotated classes</em> are {@code null} or empty and
 * {@link #isGenerateDefaultLocations()} returns {@code true}, this
 * {@code SmartContextLoader} will attempt to {@link
 * #detectDefaultConfigurationClasses detect default configuration classes}.
 * If defaults are detected they will be
 * {@link ContextConfigurationAttributes#setClasses(Class[]) set} in the
 * supplied configuration attributes. Otherwise, properties in the supplied
 * configuration attributes will not be modified.
 *
 * @param configAttributes the context configuration attributes to process
 * @see org.springframework.test.context.SmartContextLoader#processContextConfiguration(ContextConfigurationAttributes)
 * @see #isGenerateDefaultLocations()
 * @see #detectDefaultConfigurationClasses(Class)
 */
@Override
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
	if (!configAttributes.hasClasses() && isGenerateDefaultLocations()) {
		configAttributes.setClasses(detectDefaultConfigurationClasses(configAttributes.getDeclaringClass()));
	}
}