SPR-6158: Initial implementation and tests for @ImportXml
parent
20ec13ded5
commit
0a4463fb71
9 changed files with 208 additions and 25 deletions
@ -0,0 +1,14 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<beansProjectDescription> |
||||
<version>1</version> |
||||
<pluginVersion><![CDATA[2.2.7.200910141010-RELEASE]]></pluginVersion> |
||||
<configSuffixes> |
||||
<configSuffix><![CDATA[xml]]></configSuffix> |
||||
</configSuffixes> |
||||
<enableImports><![CDATA[false]]></enableImports> |
||||
<configs> |
||||
<config>src/test/java/org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml</config> |
||||
</configs> |
||||
<configSets> |
||||
</configSets> |
||||
</beansProjectDescription> |
@ -0,0 +1,34 @@ |
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.context.annotation; |
||||
|
||||
import java.lang.annotation.Documented; |
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@Target(ElementType.TYPE) |
||||
@Documented |
||||
public @interface ImportXml { |
||||
|
||||
String[] value(); |
||||
|
||||
Class<?> relativeTo() default void.class; |
||||
|
||||
} |
@ -0,0 +1,10 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<beans xmlns="http://www.springframework.org/schema/beans" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> |
||||
|
||||
<bean id="xmlDeclaredBean" class="org.springframework.beans.TestBean"> |
||||
<constructor-arg value="xml.declaraed"/> |
||||
</bean> |
||||
|
||||
</beans> |
@ -0,0 +1,90 @@ |
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.context.annotation.configuration; |
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo; |
||||
import static org.junit.Assert.assertThat; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
import org.junit.Ignore; |
||||
import org.junit.Test; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.ImportXml; |
||||
|
||||
import test.beans.TestBean; |
||||
|
||||
/** |
||||
* Integration tests for {@link ImportXml} support. |
||||
* |
||||
* @author Chris Beams |
||||
*/ |
||||
public class ImportXmlTests { |
||||
@Test |
||||
public void testImportXmlWorks() { |
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportXmlConfig.class); |
||||
assertTrue("did not contain java-declared bean", ctx.containsBean("javaDeclaredBean")); |
||||
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); |
||||
} |
||||
|
||||
@Configuration |
||||
@ImportXml("classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml") |
||||
static class ImportXmlConfig { |
||||
public @Bean TestBean javaDeclaredBean() { |
||||
return new TestBean("java.declared"); |
||||
} |
||||
} |
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Ignore |
||||
@Test |
||||
public void testImportXmlWorksWithRelativePathing() { |
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ImportsXmlWithRelativeTo.class); |
||||
assertTrue("did not contain java-declared bean", ctx.containsBean("javaDeclaredBean")); |
||||
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean")); |
||||
} |
||||
|
||||
@Configuration |
||||
@ImportXml(value="beans.xml", relativeTo=ImportXmlTests.class) |
||||
static class ImportsXmlWithRelativeTo { |
||||
public @Bean TestBean javaDeclaredBean() { |
||||
return new TestBean("java.declared"); |
||||
} |
||||
} |
||||
|
||||
@Ignore |
||||
@Test |
||||
public void testImportXmlWorksWithAutowired() { |
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AutowiredImportXml.class); |
||||
String name = ctx.getBean("xmlBeanName", String.class); |
||||
assertThat(name, equalTo("xmlBean")); |
||||
} |
||||
|
||||
@Configuration |
||||
@ImportXml(value="beans.xml", relativeTo=AutowiredImportXml.class) |
||||
static class AutowiredImportXml { |
||||
@Autowired TestBean xmlDeclaredBean; |
||||
|
||||
public @Bean String xmlBeanName() { |
||||
return xmlDeclaredBean.getName(); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue