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

The following examples show how to use org.springframework.test.context.ContextConfigurationAttributes#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: HybridContextLoader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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()));
	}
}
 
Example 2
Source File: HybridContextLoader.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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()));
	}
}
 
Example 3
Source File: HybridContextLoader.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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()));
	}
}
 
Example 4
Source File: AbstractDelegatingSmartContextLoader.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * 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);
		}
	}
}
 
Example 5
Source File: AbstractDelegatingSmartContextLoader.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * 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);
		}
	}
}
 
Example 6
Source File: AnnotationConfigWebContextLoader.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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()));
	}
}
 
Example 7
Source File: AnnotationConfigContextLoader.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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()));
	}
}
 
Example 8
Source File: AnnotationConfigWebContextLoader.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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()));
	}
}
 
Example 9
Source File: AnnotationConfigContextLoader.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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()));
	}
}
 
Example 10
Source File: AnnotationConfigWebContextLoader.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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()));
	}
}
 
Example 11
Source File: AnnotationConfigContextLoader.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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()));
	}
}