Unit tests for @Value Resource resolution
Issue: SPR-13731
This commit is contained in:
parent
edbb8bbb01
commit
def10343ea
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.context.annotation.configuration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import javax.inject.Provider;
|
||||
|
@ -36,6 +37,7 @@ import org.springframework.context.annotation.Scope;
|
|||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.tests.sample.beans.Colour;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
|
||||
|
@ -53,38 +55,38 @@ public class AutowiredConfigurationTests {
|
|||
|
||||
@Test
|
||||
public void testAutowiredConfigurationDependencies() {
|
||||
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
AutowiredConfigurationTests.class.getSimpleName() + ".xml", AutowiredConfigurationTests.class);
|
||||
|
||||
assertThat(factory.getBean("colour", Colour.class), equalTo(Colour.RED));
|
||||
assertThat(factory.getBean("testBean", TestBean.class).getName(), equalTo(Colour.RED.toString()));
|
||||
assertThat(context.getBean("colour", Colour.class), equalTo(Colour.RED));
|
||||
assertThat(context.getBean("testBean", TestBean.class).getName(), equalTo(Colour.RED.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAutowiredConfigurationMethodDependencies() {
|
||||
AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext(
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
AutowiredMethodConfig.class, ColorConfig.class);
|
||||
|
||||
assertThat(factory.getBean(Colour.class), equalTo(Colour.RED));
|
||||
assertThat(factory.getBean(TestBean.class).getName(), equalTo("RED-RED"));
|
||||
assertThat(context.getBean(Colour.class), equalTo(Colour.RED));
|
||||
assertThat(context.getBean(TestBean.class).getName(), equalTo("RED-RED"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAutowiredConfigurationMethodDependenciesWithOptionalAndAvailable() {
|
||||
AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext(
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
OptionalAutowiredMethodConfig.class, ColorConfig.class);
|
||||
|
||||
assertThat(factory.getBean(Colour.class), equalTo(Colour.RED));
|
||||
assertThat(factory.getBean(TestBean.class).getName(), equalTo("RED-RED"));
|
||||
assertThat(context.getBean(Colour.class), equalTo(Colour.RED));
|
||||
assertThat(context.getBean(TestBean.class).getName(), equalTo("RED-RED"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAutowiredConfigurationMethodDependenciesWithOptionalAndNotAvailable() {
|
||||
AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext(
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
OptionalAutowiredMethodConfig.class);
|
||||
|
||||
assertTrue(factory.getBeansOfType(Colour.class).isEmpty());
|
||||
assertThat(factory.getBean(TestBean.class).getName(), equalTo(""));
|
||||
assertTrue(context.getBeansOfType(Colour.class).isEmpty());
|
||||
assertThat(context.getBean(TestBean.class).getName(), equalTo(""));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -93,10 +95,10 @@ public class AutowiredConfigurationTests {
|
|||
*/
|
||||
@Test(expected = BeanCreationException.class)
|
||||
public void testAutowiredConfigurationConstructorsAreNotSupported() {
|
||||
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(
|
||||
DefaultListableBeanFactory context = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(context).loadBeanDefinitions(
|
||||
new ClassPathResource("annotation-config.xml", AutowiredConstructorConfig.class));
|
||||
GenericApplicationContext ctx = new GenericApplicationContext(factory);
|
||||
GenericApplicationContext ctx = new GenericApplicationContext(context);
|
||||
ctx.registerBeanDefinition("config1", new RootBeanDefinition(AutowiredConstructorConfig.class));
|
||||
ctx.registerBeanDefinition("config2", new RootBeanDefinition(ColorConfig.class));
|
||||
ctx.refresh(); // should throw
|
||||
|
@ -104,65 +106,79 @@ public class AutowiredConfigurationTests {
|
|||
|
||||
@Test
|
||||
public void testValueInjection() {
|
||||
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"ValueInjectionTests.xml", AutowiredConfigurationTests.class);
|
||||
doTestValueInjection(factory);
|
||||
doTestValueInjection(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValueInjectionWithProviderFields() {
|
||||
AnnotationConfigApplicationContext factory =
|
||||
AnnotationConfigApplicationContext context =
|
||||
new AnnotationConfigApplicationContext(ValueConfigWithProviderFields.class);
|
||||
doTestValueInjection(factory);
|
||||
doTestValueInjection(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValueInjectionWithProviderConstructorArguments() {
|
||||
AnnotationConfigApplicationContext factory =
|
||||
AnnotationConfigApplicationContext context =
|
||||
new AnnotationConfigApplicationContext(ValueConfigWithProviderConstructorArguments.class);
|
||||
doTestValueInjection(factory);
|
||||
doTestValueInjection(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValueInjectionWithProviderMethodArguments() {
|
||||
AnnotationConfigApplicationContext factory =
|
||||
AnnotationConfigApplicationContext context =
|
||||
new AnnotationConfigApplicationContext(ValueConfigWithProviderMethodArguments.class);
|
||||
doTestValueInjection(factory);
|
||||
doTestValueInjection(context);
|
||||
}
|
||||
|
||||
private void doTestValueInjection(BeanFactory factory) {
|
||||
private void doTestValueInjection(BeanFactory context) {
|
||||
System.clearProperty("myProp");
|
||||
|
||||
TestBean testBean = factory.getBean("testBean", TestBean.class);
|
||||
TestBean testBean = context.getBean("testBean", TestBean.class);
|
||||
assertNull(testBean.getName());
|
||||
|
||||
testBean = factory.getBean("testBean2", TestBean.class);
|
||||
testBean = context.getBean("testBean2", TestBean.class);
|
||||
assertNull(testBean.getName());
|
||||
|
||||
System.setProperty("myProp", "foo");
|
||||
|
||||
testBean = factory.getBean("testBean", TestBean.class);
|
||||
testBean = context.getBean("testBean", TestBean.class);
|
||||
assertThat(testBean.getName(), equalTo("foo"));
|
||||
|
||||
testBean = factory.getBean("testBean2", TestBean.class);
|
||||
testBean = context.getBean("testBean2", TestBean.class);
|
||||
assertThat(testBean.getName(), equalTo("foo"));
|
||||
|
||||
System.clearProperty("myProp");
|
||||
|
||||
testBean = factory.getBean("testBean", TestBean.class);
|
||||
testBean = context.getBean("testBean", TestBean.class);
|
||||
assertNull(testBean.getName());
|
||||
|
||||
testBean = factory.getBean("testBean2", TestBean.class);
|
||||
testBean = context.getBean("testBean2", TestBean.class);
|
||||
assertNull(testBean.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomProperties() {
|
||||
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
|
||||
public void testCustomPropertiesWithClassPathContext() throws IOException {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"AutowiredConfigurationTests-custom.xml", AutowiredConfigurationTests.class);
|
||||
|
||||
TestBean testBean = factory.getBean("testBean", TestBean.class);
|
||||
TestBean testBean = context.getBean("testBean", TestBean.class);
|
||||
assertThat(testBean.getName(), equalTo("localhost"));
|
||||
assertThat(testBean.getAge(), equalTo((int) new ClassPathResource("log4j.properties").contentLength()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomPropertiesWithGenericContext() throws IOException {
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
// context.setResourceLoader(new FileSystemResourceLoader());
|
||||
new XmlBeanDefinitionReader(context).loadBeanDefinitions(
|
||||
new ClassPathResource("AutowiredConfigurationTests-custom.xml", AutowiredConfigurationTests.class));
|
||||
context.refresh();
|
||||
|
||||
TestBean testBean = context.getBean("testBean", TestBean.class);
|
||||
assertThat(testBean.getName(), equalTo("localhost"));
|
||||
assertThat(testBean.getAge(), equalTo((int) new ClassPathResource("log4j.properties").contentLength()));
|
||||
}
|
||||
|
||||
|
||||
|
@ -321,14 +337,21 @@ public class AutowiredConfigurationTests {
|
|||
|
||||
private String hostname;
|
||||
|
||||
private Resource resource;
|
||||
|
||||
@Value("#{myProps.hostname}")
|
||||
public void setHostname(String hostname) {
|
||||
this.hostname = hostname;
|
||||
}
|
||||
|
||||
@Value("log4j.properties")
|
||||
public void setResource(Resource resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TestBean testBean() {
|
||||
return new TestBean(hostname);
|
||||
public TestBean testBean() throws IOException {
|
||||
return new TestBean(hostname, (int) resource.contentLength());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue