|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2013 the original author or authors. |
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -20,13 +20,9 @@ import java.util.Collections; |
|
|
|
|
|
|
|
|
|
|
|
import org.aspectj.lang.annotation.Aspect; |
|
|
|
import org.aspectj.lang.annotation.Aspect; |
|
|
|
import org.aspectj.lang.annotation.Before; |
|
|
|
import org.aspectj.lang.annotation.Before; |
|
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.*; |
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Ignore; |
|
|
|
import org.junit.Ignore; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
import org.springframework.tests.sample.beans.TestBean; |
|
|
|
|
|
|
|
import org.springframework.aop.support.AopUtils; |
|
|
|
import org.springframework.aop.support.AopUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
@ -39,6 +35,10 @@ import org.springframework.context.annotation.Configuration; |
|
|
|
import org.springframework.context.annotation.ImportResource; |
|
|
|
import org.springframework.context.annotation.ImportResource; |
|
|
|
import org.springframework.core.env.MapPropertySource; |
|
|
|
import org.springframework.core.env.MapPropertySource; |
|
|
|
import org.springframework.core.env.PropertySource; |
|
|
|
import org.springframework.core.env.PropertySource; |
|
|
|
|
|
|
|
import org.springframework.tests.sample.beans.TestBean; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.*; |
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Integration tests for {@link ImportResource} support. |
|
|
|
* Integration tests for {@link ImportResource} support. |
|
|
|
@ -47,6 +47,7 @@ import org.springframework.core.env.PropertySource; |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Juergen Hoeller |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class ImportResourceTests { |
|
|
|
public class ImportResourceTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testImportXml() { |
|
|
|
public void testImportXml() { |
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlConfig.class); |
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlConfig.class); |
|
|
|
@ -56,16 +57,6 @@ public class ImportResourceTests { |
|
|
|
assertEquals("myName", tb.getName()); |
|
|
|
assertEquals("myName", tb.getName()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
@ImportResource("classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml") |
|
|
|
|
|
|
|
static class ImportXmlConfig { |
|
|
|
|
|
|
|
@Value("${name}") |
|
|
|
|
|
|
|
private String name; |
|
|
|
|
|
|
|
public @Bean TestBean javaDeclaredBean() { |
|
|
|
|
|
|
|
return new TestBean(this.name); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Ignore // TODO: SPR-6310
|
|
|
|
@Ignore // TODO: SPR-6310
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testImportXmlWithRelativePath() { |
|
|
|
public void testImportXmlWithRelativePath() { |
|
|
|
@ -76,14 +67,6 @@ public class ImportResourceTests { |
|
|
|
assertEquals("myName", tb.getName()); |
|
|
|
assertEquals("myName", tb.getName()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
@ImportResource("ImportXmlConfig-context.xml") |
|
|
|
|
|
|
|
static class ImportXmlWithRelativePathConfig { |
|
|
|
|
|
|
|
public @Bean TestBean javaDeclaredBean() { |
|
|
|
|
|
|
|
return new TestBean("java.declared"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Ignore // TODO: SPR-6310
|
|
|
|
@Ignore // TODO: SPR-6310
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testImportXmlByConvention() { |
|
|
|
public void testImportXmlByConvention() { |
|
|
|
@ -91,11 +74,6 @@ public class ImportResourceTests { |
|
|
|
assertTrue("context does not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); |
|
|
|
assertTrue("context does not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
//@ImportXml
|
|
|
|
|
|
|
|
static class ImportXmlByConventionConfig { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testImportXmlIsInheritedFromSuperclassDeclarations() { |
|
|
|
public void testImportXmlIsInheritedFromSuperclassDeclarations() { |
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FirstLevelSubConfig.class); |
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FirstLevelSubConfig.class); |
|
|
|
@ -109,6 +87,78 @@ public class ImportResourceTests { |
|
|
|
assertTrue("failed to pick up parent-declared XML bean", ctx.containsBean("xmlDeclaredBean")); |
|
|
|
assertTrue("failed to pick up parent-declared XML bean", ctx.containsBean("xmlDeclaredBean")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testImportXmlWithNamespaceConfig() { |
|
|
|
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlWithAopNamespaceConfig.class); |
|
|
|
|
|
|
|
Object bean = ctx.getBean("proxiedXmlBean"); |
|
|
|
|
|
|
|
assertTrue(AopUtils.isAopProxy(bean)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testImportXmlWithOtherConfigurationClass() { |
|
|
|
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlWithConfigurationClass.class); |
|
|
|
|
|
|
|
assertTrue("did not contain java-declared bean", ctx.containsBean("javaDeclaredBean")); |
|
|
|
|
|
|
|
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); |
|
|
|
|
|
|
|
TestBean tb = ctx.getBean("javaDeclaredBean", TestBean.class); |
|
|
|
|
|
|
|
assertEquals("myName", tb.getName()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Ignore // TODO: SPR-6327
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testImportDifferentResourceTypes() { |
|
|
|
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SubResourceConfig.class); |
|
|
|
|
|
|
|
assertTrue(ctx.containsBean("propertiesDeclaredBean")); |
|
|
|
|
|
|
|
assertTrue(ctx.containsBean("xmlDeclaredBean")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void importWithPlaceholder() throws Exception { |
|
|
|
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
|
|
|
|
|
|
|
PropertySource<?> propertySource = new MapPropertySource("test", |
|
|
|
|
|
|
|
Collections.<String, Object> singletonMap("test", "springframework")); |
|
|
|
|
|
|
|
ctx.getEnvironment().getPropertySources().addFirst(propertySource); |
|
|
|
|
|
|
|
ctx.register(ImportXmlConfig.class); |
|
|
|
|
|
|
|
ctx.refresh(); |
|
|
|
|
|
|
|
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testImportXmlWithAutowiredConfig() { |
|
|
|
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlAutowiredConfig.class); |
|
|
|
|
|
|
|
String name = ctx.getBean("xmlBeanName", String.class); |
|
|
|
|
|
|
|
assertThat(name, equalTo("xml.declared")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testImportNonXmlResource() { |
|
|
|
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportNonXmlResourceConfig.class); |
|
|
|
|
|
|
|
assertTrue(ctx.containsBean("propertiesDeclaredBean")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
@ImportResource("classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml") |
|
|
|
|
|
|
|
static class ImportXmlConfig { |
|
|
|
|
|
|
|
@Value("${name}") |
|
|
|
|
|
|
|
private String name; |
|
|
|
|
|
|
|
public @Bean TestBean javaDeclaredBean() { |
|
|
|
|
|
|
|
return new TestBean(this.name); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
@ImportResource("ImportXmlConfig-context.xml") |
|
|
|
|
|
|
|
static class ImportXmlWithRelativePathConfig { |
|
|
|
|
|
|
|
public @Bean TestBean javaDeclaredBean() { |
|
|
|
|
|
|
|
return new TestBean("java.declared"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
//@ImportXml
|
|
|
|
|
|
|
|
static class ImportXmlByConventionConfig { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
@ImportResource("classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml") |
|
|
|
@ImportResource("classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml") |
|
|
|
static class BaseConfig { |
|
|
|
static class BaseConfig { |
|
|
|
@ -123,13 +173,6 @@ public class ImportResourceTests { |
|
|
|
static class SecondLevelSubConfig extends BaseConfig { |
|
|
|
static class SecondLevelSubConfig extends BaseConfig { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testImportXmlWithNamespaceConfig() { |
|
|
|
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlWithAopNamespaceConfig.class); |
|
|
|
|
|
|
|
Object bean = ctx.getBean("proxiedXmlBean"); |
|
|
|
|
|
|
|
assertTrue(AopUtils.isAopProxy(bean)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
@ImportResource("classpath:org/springframework/context/annotation/configuration/ImportXmlWithAopNamespace-context.xml") |
|
|
|
@ImportResource("classpath:org/springframework/context/annotation/configuration/ImportXmlWithAopNamespace-context.xml") |
|
|
|
static class ImportXmlWithAopNamespaceConfig { |
|
|
|
static class ImportXmlWithAopNamespaceConfig { |
|
|
|
@ -141,11 +184,9 @@ public class ImportResourceTests { |
|
|
|
public void advice() { } |
|
|
|
public void advice() { } |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Configuration |
|
|
|
public void testImportXmlWithAutowiredConfig() { |
|
|
|
@ImportResource("classpath:org/springframework/context/annotation/configuration/ImportXmlWithConfigurationClass-context.xml") |
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlAutowiredConfig.class); |
|
|
|
static class ImportXmlWithConfigurationClass { |
|
|
|
String name = ctx.getBean("xmlBeanName", String.class); |
|
|
|
|
|
|
|
assertThat(name, equalTo("xml.declared")); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
@ -158,47 +199,16 @@ public class ImportResourceTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testImportNonXmlResource() { |
|
|
|
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportNonXmlResourceConfig.class); |
|
|
|
|
|
|
|
assertTrue(ctx.containsBean("propertiesDeclaredBean")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
@ImportResource(value="classpath:org/springframework/context/annotation/configuration/ImportNonXmlResourceConfig-context.properties", |
|
|
|
@ImportResource(value="classpath:org/springframework/context/annotation/configuration/ImportNonXmlResourceConfig-context.properties", |
|
|
|
reader=PropertiesBeanDefinitionReader.class) |
|
|
|
reader=PropertiesBeanDefinitionReader.class) |
|
|
|
static class ImportNonXmlResourceConfig { |
|
|
|
static class ImportNonXmlResourceConfig { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Ignore // TODO: SPR-6327
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testImportDifferentResourceTypes() { |
|
|
|
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SubResourceConfig.class); |
|
|
|
|
|
|
|
assertTrue(ctx.containsBean("propertiesDeclaredBean")); |
|
|
|
|
|
|
|
assertTrue(ctx.containsBean("xmlDeclaredBean")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
@ImportResource(value="classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml", |
|
|
|
@ImportResource(value="classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml", |
|
|
|
reader=XmlBeanDefinitionReader.class) |
|
|
|
reader=XmlBeanDefinitionReader.class) |
|
|
|
static class SubResourceConfig extends ImportNonXmlResourceConfig { |
|
|
|
static class SubResourceConfig extends ImportNonXmlResourceConfig { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void importWithPlaceHolder() throws Exception { |
|
|
|
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
|
|
|
|
|
|
|
PropertySource<?> propertySource = new MapPropertySource("test", |
|
|
|
|
|
|
|
Collections.<String, Object> singletonMap("test", "springframework")); |
|
|
|
|
|
|
|
ctx.getEnvironment().getPropertySources().addFirst(propertySource); |
|
|
|
|
|
|
|
ctx.register(ImportXmlConfig.class); |
|
|
|
|
|
|
|
ctx.refresh(); |
|
|
|
|
|
|
|
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
@ImportResource("classpath:org/${test}/context/annotation/configuration/ImportXmlConfig-context.xml") |
|
|
|
|
|
|
|
static class ImportWithPlaceHolder { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|