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