org.springframework.test.context.ContextConfiguration Java Examples

The following examples show how to use org.springframework.test.context.ContextConfiguration. 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: ContextLoaderUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Resolve the list of {@linkplain ContextConfigurationAttributes context
 * configuration attributes} for the supplied {@linkplain Class test class} and its
 * superclasses.
 * <p>Note that the {@link ContextConfiguration#inheritLocations inheritLocations} and
 * {@link ContextConfiguration#inheritInitializers() inheritInitializers} flags of
 * {@link ContextConfiguration @ContextConfiguration} will <strong>not</strong>
 * be taken into consideration. If these flags need to be honored, that must be
 * handled manually when traversing the list returned by this method.
 * @param testClass the class for which to resolve the configuration attributes
 * (must not be {@code null})
 * @return the list of configuration attributes for the specified class, ordered
 * <em>bottom-up</em> (i.e., as if we were traversing up the class hierarchy);
 * never {@code null}
 * @throws IllegalArgumentException if the supplied class is {@code null} or if
 * {@code @ContextConfiguration} is not <em>present</em> on the supplied class
 */
static List<ContextConfigurationAttributes> resolveContextConfigurationAttributes(Class<?> testClass) {
	Assert.notNull(testClass, "Class must not be null");

	List<ContextConfigurationAttributes> attributesList = new ArrayList<>();
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(testClass, annotationType);
	Assert.notNull(descriptor, () -> String.format(
				"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
				annotationType.getName(), testClass.getName()));

	while (descriptor != null) {
		convertContextConfigToConfigAttributesAndAddToList(descriptor.synthesizeAnnotation(),
				descriptor.getRootDeclaringClass(), attributesList);
		descriptor = findAnnotationDescriptor(descriptor.getRootDeclaringClass().getSuperclass(), annotationType);
	}

	return attributesList;
}
 
Example #2
Source File: OverriddenMetaAnnotationAttributesTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void overriddenContextConfigurationLocationsAndInheritLocations() throws Exception {
	Class<?> declaringClass = OverriddenMetaLocationsConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation attributes:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().locations());
	assertFalse(descriptor.getAnnotation().inheritLocations());

	// overridden attributes:
	AnnotationAttributes attributes = descriptor.getAnnotationAttributes();
	assertArrayEquals(new String[] { "bar.xml" }, attributes.getStringArray("locations"));
	assertTrue(attributes.getBoolean("inheritLocations"));
}
 
Example #3
Source File: OverriddenMetaAnnotationAttributesTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void contextConfigurationLocationsAndInheritLocations() throws Exception {
	Class<MetaLocationsConfigTestCase> declaringClass = MetaLocationsConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation attributes:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().locations());
	assertFalse(descriptor.getAnnotation().inheritLocations());
}
 
Example #4
Source File: OverriddenMetaAnnotationAttributesTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void overriddenContextConfigurationValue() throws Exception {
	Class<?> declaringClass = OverriddenMetaValueConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation value:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().value());

	// overridden attribute:
	AnnotationAttributes attributes = descriptor.getAnnotationAttributes();

	// NOTE: we would like to be able to override the 'value' attribute; however,
	// Spring currently does not allow overrides for the 'value' attribute.
	// See SPR-11393 for related discussions.
	assertArrayEquals(new String[] { "foo.xml" }, attributes.getStringArray("value"));
}
 
Example #5
Source File: MetaAnnotationUtilsTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesWithMetaAnnotationWithOverriddenAttributes() throws Exception {
	Class<?> startClass = MetaConfigWithOverriddenAttributesTestCase.class;
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(startClass, Service.class,
		ContextConfiguration.class, Order.class, Transactional.class);

	assertNotNull(descriptor);
	assertEquals(startClass, descriptor.getRootDeclaringClass());
	assertEquals(annotationType, descriptor.getAnnotationType());
	assertArrayEquals(new Class[] {}, ((ContextConfiguration) descriptor.getAnnotation()).value());
	assertArrayEquals(new Class[] { MetaAnnotationUtilsTests.class },
		descriptor.getAnnotationAttributes().getClassArray("classes"));
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaConfig.class, descriptor.getComposedAnnotationType());
}
 
Example #6
Source File: MetaAnnotationUtilsTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesWithMetaAnnotationWithDefaultAttributes() throws Exception {
	Class<?> startClass = MetaConfigWithDefaultAttributesTestCase.class;
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(startClass, Service.class,
		ContextConfiguration.class, Order.class, Transactional.class);

	assertNotNull(descriptor);
	assertEquals(startClass, descriptor.getRootDeclaringClass());
	assertEquals(annotationType, descriptor.getAnnotationType());
	assertArrayEquals(new Class[] {}, ((ContextConfiguration) descriptor.getAnnotation()).value());
	assertArrayEquals(new Class[] { MetaConfig.DevConfig.class, MetaConfig.ProductionConfig.class },
		descriptor.getAnnotationAttributes().getClassArray("classes"));
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaConfig.class, descriptor.getComposedAnnotationType());
}
 
Example #7
Source File: GenericXmlContextLoaderResourceLocationsTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void assertContextConfigurationLocations() throws Exception {

	final ContextConfiguration contextConfig = this.testClass.getAnnotation(ContextConfiguration.class);
	final ContextLoader contextLoader = new GenericXmlContextLoader();
	final String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
	final String[] processedLocations = contextLoader.processLocations(this.testClass, configuredLocations);

	if (logger.isDebugEnabled()) {
		logger.debug("----------------------------------------------------------------------");
		logger.debug("Configured locations: " + ObjectUtils.nullSafeToString(configuredLocations));
		logger.debug("Expected   locations: " + ObjectUtils.nullSafeToString(this.expectedLocations));
		logger.debug("Processed  locations: " + ObjectUtils.nullSafeToString(processedLocations));
	}

	assertArrayEquals("Verifying locations for test [" + this.testClass + "].", this.expectedLocations,
		processedLocations);
}
 
Example #8
Source File: ContextLoaderUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Resolve the list of {@linkplain ContextConfigurationAttributes context
 * configuration attributes} for the supplied {@linkplain Class test class} and its
 * superclasses.
 * <p>Note that the {@link ContextConfiguration#inheritLocations inheritLocations} and
 * {@link ContextConfiguration#inheritInitializers() inheritInitializers} flags of
 * {@link ContextConfiguration @ContextConfiguration} will <strong>not</strong>
 * be taken into consideration. If these flags need to be honored, that must be
 * handled manually when traversing the list returned by this method.
 * @param testClass the class for which to resolve the configuration attributes
 * (must not be {@code null})
 * @return the list of configuration attributes for the specified class, ordered
 * <em>bottom-up</em> (i.e., as if we were traversing up the class hierarchy);
 * never {@code null}
 * @throws IllegalArgumentException if the supplied class is {@code null} or if
 * {@code @ContextConfiguration} is not <em>present</em> on the supplied class
 */
static List<ContextConfigurationAttributes> resolveContextConfigurationAttributes(Class<?> testClass) {
	Assert.notNull(testClass, "Class must not be null");

	List<ContextConfigurationAttributes> attributesList = new ArrayList<ContextConfigurationAttributes>();
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(testClass, annotationType);
	if (descriptor == null) {
		throw new IllegalArgumentException(String.format(
				"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
				annotationType.getName(), testClass.getName()));
	}

	while (descriptor != null) {
		convertContextConfigToConfigAttributesAndAddToList(descriptor.synthesizeAnnotation(),
				descriptor.getRootDeclaringClass(), attributesList);
		descriptor = findAnnotationDescriptor(descriptor.getRootDeclaringClass().getSuperclass(), annotationType);
	}

	return attributesList;
}
 
Example #9
Source File: OverriddenMetaAnnotationAttributesTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void overriddenContextConfigurationLocationsAndInheritLocations() throws Exception {
	Class<?> declaringClass = OverriddenMetaLocationsConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation attributes:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().locations());
	assertFalse(descriptor.getAnnotation().inheritLocations());

	// overridden attributes:
	AnnotationAttributes attributes = descriptor.getAnnotationAttributes();
	assertArrayEquals(new String[] { "bar.xml" }, attributes.getStringArray("locations"));
	assertTrue(attributes.getBoolean("inheritLocations"));
}
 
Example #10
Source File: OverriddenMetaAnnotationAttributesTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void contextConfigurationLocationsAndInheritLocations() throws Exception {
	Class<MetaLocationsConfigTestCase> declaringClass = MetaLocationsConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation attributes:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().locations());
	assertFalse(descriptor.getAnnotation().inheritLocations());
}
 
Example #11
Source File: OverriddenMetaAnnotationAttributesTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void overriddenContextConfigurationValue() throws Exception {
	Class<?> declaringClass = OverriddenMetaValueConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation value:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().value());

	// overridden attribute:
	AnnotationAttributes attributes = descriptor.getAnnotationAttributes();

	// NOTE: we would like to be able to override the 'value' attribute; however,
	// Spring currently does not allow overrides for the 'value' attribute.
	// See SPR-11393 for related discussions.
	assertArrayEquals(new String[] { "foo.xml" }, attributes.getStringArray("value"));
}
 
Example #12
Source File: MetaAnnotationUtilsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesWithMetaAnnotationWithOverriddenAttributes() {
	Class<?> startClass = MetaConfigWithOverriddenAttributesTestCase.class;
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(
			startClass, Service.class, ContextConfiguration.class, Order.class, Transactional.class);

	assertNotNull(descriptor);
	assertEquals(startClass, descriptor.getRootDeclaringClass());
	assertEquals(annotationType, descriptor.getAnnotationType());
	assertArrayEquals(new Class<?>[] {}, ((ContextConfiguration) descriptor.getAnnotation()).value());
	assertArrayEquals(new Class<?>[] {MetaAnnotationUtilsTests.class},
			descriptor.getAnnotationAttributes().getClassArray("classes"));
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaConfig.class, descriptor.getComposedAnnotationType());
}
 
Example #13
Source File: MetaAnnotationUtilsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesWithMetaAnnotationWithDefaultAttributes() {
	Class<?> startClass = MetaConfigWithDefaultAttributesTestCase.class;
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(startClass,
			Service.class, ContextConfiguration.class, Order.class, Transactional.class);

	assertNotNull(descriptor);
	assertEquals(startClass, descriptor.getRootDeclaringClass());
	assertEquals(annotationType, descriptor.getAnnotationType());
	assertArrayEquals(new Class<?>[] {}, ((ContextConfiguration) descriptor.getAnnotation()).value());
	assertArrayEquals(new Class<?>[] {MetaConfig.DevConfig.class, MetaConfig.ProductionConfig.class},
			descriptor.getAnnotationAttributes().getClassArray("classes"));
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaConfig.class, descriptor.getComposedAnnotationType());
}
 
Example #14
Source File: GenericXmlContextLoaderResourceLocationsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void assertContextConfigurationLocations() throws Exception {

	final ContextConfiguration contextConfig = this.testClass.getAnnotation(ContextConfiguration.class);
	final ContextLoader contextLoader = new GenericXmlContextLoader();
	final String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
	final String[] processedLocations = contextLoader.processLocations(this.testClass, configuredLocations);

	if (logger.isDebugEnabled()) {
		logger.debug("----------------------------------------------------------------------");
		logger.debug("Configured locations: " + ObjectUtils.nullSafeToString(configuredLocations));
		logger.debug("Expected   locations: " + ObjectUtils.nullSafeToString(this.expectedLocations));
		logger.debug("Processed  locations: " + ObjectUtils.nullSafeToString(processedLocations));
	}

	assertArrayEquals("Verifying locations for test [" + this.testClass + "].", this.expectedLocations,
		processedLocations);
}
 
Example #15
Source File: ContextLoaderUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Resolve the list of {@linkplain ContextConfigurationAttributes context
 * configuration attributes} for the supplied {@linkplain Class test class} and its
 * superclasses.
 * <p>Note that the {@link ContextConfiguration#inheritLocations inheritLocations} and
 * {@link ContextConfiguration#inheritInitializers() inheritInitializers} flags of
 * {@link ContextConfiguration @ContextConfiguration} will <strong>not</strong>
 * be taken into consideration. If these flags need to be honored, that must be
 * handled manually when traversing the list returned by this method.
 * @param testClass the class for which to resolve the configuration attributes
 * (must not be {@code null})
 * @return the list of configuration attributes for the specified class, ordered
 * <em>bottom-up</em> (i.e., as if we were traversing up the class hierarchy);
 * never {@code null}
 * @throws IllegalArgumentException if the supplied class is {@code null} or if
 * {@code @ContextConfiguration} is not <em>present</em> on the supplied class
 */
static List<ContextConfigurationAttributes> resolveContextConfigurationAttributes(Class<?> testClass) {
	Assert.notNull(testClass, "Class must not be null");

	List<ContextConfigurationAttributes> attributesList = new ArrayList<>();
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(testClass, annotationType);
	Assert.notNull(descriptor, () -> String.format(
				"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
				annotationType.getName(), testClass.getName()));

	while (descriptor != null) {
		convertContextConfigToConfigAttributesAndAddToList(descriptor.synthesizeAnnotation(),
				descriptor.getRootDeclaringClass(), attributesList);
		descriptor = findAnnotationDescriptor(descriptor.getRootDeclaringClass().getSuperclass(), annotationType);
	}

	return attributesList;
}
 
Example #16
Source File: OverriddenMetaAnnotationAttributesTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void overriddenContextConfigurationLocationsAndInheritLocations() throws Exception {
	Class<?> declaringClass = OverriddenMetaLocationsConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation attributes:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().locations());
	assertFalse(descriptor.getAnnotation().inheritLocations());

	// overridden attributes:
	AnnotationAttributes attributes = descriptor.getAnnotationAttributes();
	assertArrayEquals(new String[] { "bar.xml" }, attributes.getStringArray("locations"));
	assertTrue(attributes.getBoolean("inheritLocations"));
}
 
Example #17
Source File: OverriddenMetaAnnotationAttributesTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void contextConfigurationLocationsAndInheritLocations() throws Exception {
	Class<MetaLocationsConfigTestCase> declaringClass = MetaLocationsConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation attributes:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().locations());
	assertFalse(descriptor.getAnnotation().inheritLocations());
}
 
Example #18
Source File: OverriddenMetaAnnotationAttributesTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void overriddenContextConfigurationValue() throws Exception {
	Class<?> declaringClass = OverriddenMetaValueConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation value:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().value());

	// overridden attribute:
	AnnotationAttributes attributes = descriptor.getAnnotationAttributes();

	// NOTE: we would like to be able to override the 'value' attribute; however,
	// Spring currently does not allow overrides for the 'value' attribute.
	// See SPR-11393 for related discussions.
	assertArrayEquals(new String[] { "foo.xml" }, attributes.getStringArray("value"));
}
 
Example #19
Source File: GenericXmlContextLoaderResourceLocationsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void assertContextConfigurationLocations() throws Exception {

	final ContextConfiguration contextConfig = this.testClass.getAnnotation(ContextConfiguration.class);
	final ContextLoader contextLoader = new GenericXmlContextLoader();
	final String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
	final String[] processedLocations = contextLoader.processLocations(this.testClass, configuredLocations);

	if (logger.isDebugEnabled()) {
		logger.debug("----------------------------------------------------------------------");
		logger.debug("Configured locations: " + ObjectUtils.nullSafeToString(configuredLocations));
		logger.debug("Expected   locations: " + ObjectUtils.nullSafeToString(this.expectedLocations));
		logger.debug("Processed  locations: " + ObjectUtils.nullSafeToString(processedLocations));
	}

	assertArrayEquals("Verifying locations for test [" + this.testClass + "].", this.expectedLocations,
		processedLocations);
}
 
Example #20
Source File: MetaAnnotationUtilsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesWithMetaAnnotationWithDefaultAttributes() {
	Class<?> startClass = MetaConfigWithDefaultAttributesTestCase.class;
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(startClass,
			Service.class, ContextConfiguration.class, Order.class, Transactional.class);

	assertNotNull(descriptor);
	assertEquals(startClass, descriptor.getRootDeclaringClass());
	assertEquals(annotationType, descriptor.getAnnotationType());
	assertArrayEquals(new Class<?>[] {}, ((ContextConfiguration) descriptor.getAnnotation()).value());
	assertArrayEquals(new Class<?>[] {MetaConfig.DevConfig.class, MetaConfig.ProductionConfig.class},
			descriptor.getAnnotationAttributes().getClassArray("classes"));
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaConfig.class, descriptor.getComposedAnnotationType());
}
 
Example #21
Source File: SpringFlowableTestCase.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected void initializeCmmnEngine() {
    ContextConfiguration contextConfiguration = getClass().getAnnotation(ContextConfiguration.class);
    String[] value = contextConfiguration.value();
    boolean hasOneArg = value != null && value.length == 1;
    String key = hasOneArg ? value[0] : CmmnEngine.class.getName();
    CmmnEngine engine = this.cachedCmmnEngines.containsKey(key) ? this.cachedCmmnEngines.get(key) : this.applicationContext.getBean(CmmnEngine.class);

    this.cachedCmmnEngines.put(key, engine);
    this.cmmnEngine = engine;
}
 
Example #22
Source File: SpringFlowableTestCase.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
protected void initializeContentEngine() {
    ContextConfiguration contextConfiguration = getClass().getAnnotation(ContextConfiguration.class);
    String[] value = contextConfiguration.value();
    boolean hasOneArg = value != null && value.length == 1;
    String key = hasOneArg ? value[0] : ContentEngine.class.getName();
    ContentEngine engine = this.cachedContentEngines.containsKey(key) ? this.cachedContentEngines.get(key) : this.applicationContext.getBean(ContentEngine.class);

    this.cachedContentEngines.put(key, engine);
    this.contentEngine = engine;
}
 
Example #23
Source File: SpringFlowableTestCase.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
protected void initializeDmnEngine() {
    ContextConfiguration contextConfiguration = getClass().getAnnotation(ContextConfiguration.class);
    String[] value = contextConfiguration.value();
    boolean hasOneArg = value != null && value.length == 1;
    String key = hasOneArg ? value[0] : DmnEngine.class.getName();
    DmnEngine engine = this.cachedDmnEngines.containsKey(key) ? this.cachedDmnEngines.get(key) : this.applicationContext.getBean(DmnEngine.class);

    this.cachedDmnEngines.put(key, engine);
    this.dmnEngine = engine;
}
 
Example #24
Source File: ContextLoaderUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Convenience method for creating a {@link ContextConfigurationAttributes}
 * instance from the supplied {@link ContextConfiguration} annotation and
 * declaring class and then adding the attributes to the supplied list.
 */
private static void convertContextConfigToConfigAttributesAndAddToList(ContextConfiguration contextConfiguration,
		Class<?> declaringClass, final List<ContextConfigurationAttributes> attributesList) {

	if (logger.isTraceEnabled()) {
		logger.trace(String.format("Retrieved @ContextConfiguration [%s] for declaring class [%s].",
				contextConfiguration, declaringClass.getName()));
	}
	ContextConfigurationAttributes attributes =
			new ContextConfigurationAttributes(declaringClass, contextConfiguration);
	if (logger.isTraceEnabled()) {
		logger.trace("Resolved context configuration attributes: " + attributes);
	}
	attributesList.add(attributes);
}
 
Example #25
Source File: SpringFlowableTestCase.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
protected void initializeProcessEngine() {
    ContextConfiguration contextConfiguration = getClass().getAnnotation(ContextConfiguration.class);
    String[] value = contextConfiguration.value();
    boolean hasOneArg = value != null && value.length == 1;
    String key = hasOneArg ? value[0] : ProcessEngine.class.getName();
    ProcessEngine engine = this.cachedProcessEngines.containsKey(key) ? this.cachedProcessEngines.get(key) : this.applicationContext.getBean(ProcessEngine.class);

    this.cachedProcessEngines.put(key, engine);
    this.processEngine = engine;
}
 
Example #26
Source File: OverriddenMetaAnnotationAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void contextConfigurationValue() throws Exception {
	Class<MetaValueConfigTestCase> declaringClass = MetaValueConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation value:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().value());
}
 
Example #27
Source File: MetaAnnotationUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void findAnnotationDescriptorForClassWithLocalMetaAnnotationAndAnnotatedSuperclass() {
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(
			MetaAnnotatedAndSuperAnnotatedContextConfigClass.class, ContextConfiguration.class);

	assertNotNull("AnnotationDescriptor should not be null", descriptor);
	assertEquals("rootDeclaringClass", MetaAnnotatedAndSuperAnnotatedContextConfigClass.class, descriptor.getRootDeclaringClass());
	assertEquals("declaringClass", MetaConfig.class, descriptor.getDeclaringClass());
	assertEquals("annotationType", ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull("composedAnnotation should not be null", descriptor.getComposedAnnotation());
	assertEquals("composedAnnotationType", MetaConfig.class, descriptor.getComposedAnnotationType());

	assertArrayEquals("configured classes", new Class<?>[] {String.class},
			descriptor.getAnnotationAttributes().getClassArray("classes"));
}
 
Example #28
Source File: MetaAnnotationUtilsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void findAnnotationDescriptorForClassWithLocalMetaAnnotationAndAnnotatedSuperclass() {
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(
		MetaAnnotatedAndSuperAnnotatedContextConfigClass.class, ContextConfiguration.class);

	assertNotNull("AnnotationDescriptor should not be null", descriptor);
	assertEquals("rootDeclaringClass", MetaAnnotatedAndSuperAnnotatedContextConfigClass.class, descriptor.getRootDeclaringClass());
	assertEquals("declaringClass", MetaConfig.class, descriptor.getDeclaringClass());
	assertEquals("annotationType", ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull("composedAnnotation should not be null", descriptor.getComposedAnnotation());
	assertEquals("composedAnnotationType", MetaConfig.class, descriptor.getComposedAnnotationType());

	assertArrayEquals("configured classes", new Class[] { String.class },
		descriptor.getAnnotationAttributes().getClassArray("classes"));
}
 
Example #29
Source File: ContextLoaderUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Convenience method for creating a {@link ContextConfigurationAttributes}
 * instance from the supplied {@link ContextConfiguration} annotation and
 * declaring class and then adding the attributes to the supplied list.
 */
private static void convertContextConfigToConfigAttributesAndAddToList(ContextConfiguration contextConfiguration,
		Class<?> declaringClass, final List<ContextConfigurationAttributes> attributesList) {

	if (logger.isTraceEnabled()) {
		logger.trace(String.format("Retrieved @ContextConfiguration [%s] for declaring class [%s].",
				contextConfiguration, declaringClass.getName()));
	}
	ContextConfigurationAttributes attributes =
			new ContextConfigurationAttributes(declaringClass, contextConfiguration);
	if (logger.isTraceEnabled()) {
		logger.trace("Resolved context configuration attributes: " + attributes);
	}
	attributesList.add(attributes);
}
 
Example #30
Source File: ContextLoaderUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Convenience method for creating a {@link ContextConfigurationAttributes}
 * instance from the supplied {@link ContextConfiguration} annotation and
 * declaring class and then adding the attributes to the supplied list.
 */
private static void convertContextConfigToConfigAttributesAndAddToList(ContextConfiguration contextConfiguration,
		Class<?> declaringClass, final List<ContextConfigurationAttributes> attributesList) {

	if (logger.isTraceEnabled()) {
		logger.trace(String.format("Retrieved @ContextConfiguration [%s] for declaring class [%s].",
				contextConfiguration, declaringClass.getName()));
	}
	ContextConfigurationAttributes attributes =
			new ContextConfigurationAttributes(declaringClass, contextConfiguration);
	if (logger.isTraceEnabled()) {
		logger.trace("Resolved context configuration attributes: " + attributes);
	}
	attributesList.add(attributes);
}