Skip to content

Commit 3bc2a97

Browse files
committed
Unit tests for @value Resource resolution
Issue: SPR-13731 (cherry picked from commit def1034)
1 parent ce315b2 commit 3bc2a97

File tree

1 file changed

+59
-36
lines changed

1 file changed

+59
-36
lines changed

spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.context.annotation.configuration;
1818

19+
import java.io.IOException;
1920
import java.util.List;
2021
import java.util.Optional;
2122
import javax.inject.Provider;
@@ -36,6 +37,7 @@
3637
import org.springframework.context.support.ClassPathXmlApplicationContext;
3738
import org.springframework.context.support.GenericApplicationContext;
3839
import org.springframework.core.io.ClassPathResource;
40+
import org.springframework.core.io.Resource;
3941
import org.springframework.tests.sample.beans.Colour;
4042
import org.springframework.tests.sample.beans.TestBean;
4143

@@ -53,38 +55,38 @@ public class AutowiredConfigurationTests {
5355

5456
@Test
5557
public void testAutowiredConfigurationDependencies() {
56-
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
58+
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
5759
AutowiredConfigurationTests.class.getSimpleName() + ".xml", AutowiredConfigurationTests.class);
5860

59-
assertThat(factory.getBean("colour", Colour.class), equalTo(Colour.RED));
60-
assertThat(factory.getBean("testBean", TestBean.class).getName(), equalTo(Colour.RED.toString()));
61+
assertThat(context.getBean("colour", Colour.class), equalTo(Colour.RED));
62+
assertThat(context.getBean("testBean", TestBean.class).getName(), equalTo(Colour.RED.toString()));
6163
}
6264

6365
@Test
6466
public void testAutowiredConfigurationMethodDependencies() {
65-
AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext(
67+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
6668
AutowiredMethodConfig.class, ColorConfig.class);
6769

68-
assertThat(factory.getBean(Colour.class), equalTo(Colour.RED));
69-
assertThat(factory.getBean(TestBean.class).getName(), equalTo("RED-RED"));
70+
assertThat(context.getBean(Colour.class), equalTo(Colour.RED));
71+
assertThat(context.getBean(TestBean.class).getName(), equalTo("RED-RED"));
7072
}
7173

7274
@Test
7375
public void testAutowiredConfigurationMethodDependenciesWithOptionalAndAvailable() {
74-
AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext(
76+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
7577
OptionalAutowiredMethodConfig.class, ColorConfig.class);
7678

77-
assertThat(factory.getBean(Colour.class), equalTo(Colour.RED));
78-
assertThat(factory.getBean(TestBean.class).getName(), equalTo("RED-RED"));
79+
assertThat(context.getBean(Colour.class), equalTo(Colour.RED));
80+
assertThat(context.getBean(TestBean.class).getName(), equalTo("RED-RED"));
7981
}
8082

8183
@Test
8284
public void testAutowiredConfigurationMethodDependenciesWithOptionalAndNotAvailable() {
83-
AnnotationConfigApplicationContext factory = new AnnotationConfigApplicationContext(
85+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
8486
OptionalAutowiredMethodConfig.class);
8587

86-
assertTrue(factory.getBeansOfType(Colour.class).isEmpty());
87-
assertThat(factory.getBean(TestBean.class).getName(), equalTo(""));
88+
assertTrue(context.getBeansOfType(Colour.class).isEmpty());
89+
assertThat(context.getBean(TestBean.class).getName(), equalTo(""));
8890
}
8991

9092
/**
@@ -93,76 +95,90 @@ public void testAutowiredConfigurationMethodDependenciesWithOptionalAndNotAvaila
9395
*/
9496
@Test(expected = BeanCreationException.class)
9597
public void testAutowiredConfigurationConstructorsAreNotSupported() {
96-
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
97-
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(
98+
DefaultListableBeanFactory context = new DefaultListableBeanFactory();
99+
new XmlBeanDefinitionReader(context).loadBeanDefinitions(
98100
new ClassPathResource("annotation-config.xml", AutowiredConstructorConfig.class));
99-
GenericApplicationContext ctx = new GenericApplicationContext(factory);
101+
GenericApplicationContext ctx = new GenericApplicationContext(context);
100102
ctx.registerBeanDefinition("config1", new RootBeanDefinition(AutowiredConstructorConfig.class));
101103
ctx.registerBeanDefinition("config2", new RootBeanDefinition(ColorConfig.class));
102104
ctx.refresh(); // should throw
103105
}
104106

105107
@Test
106108
public void testValueInjection() {
107-
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
109+
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
108110
"ValueInjectionTests.xml", AutowiredConfigurationTests.class);
109-
doTestValueInjection(factory);
111+
doTestValueInjection(context);
110112
}
111113

112114
@Test
113115
public void testValueInjectionWithProviderFields() {
114-
AnnotationConfigApplicationContext factory =
116+
AnnotationConfigApplicationContext context =
115117
new AnnotationConfigApplicationContext(ValueConfigWithProviderFields.class);
116-
doTestValueInjection(factory);
118+
doTestValueInjection(context);
117119
}
118120

119121
@Test
120122
public void testValueInjectionWithProviderConstructorArguments() {
121-
AnnotationConfigApplicationContext factory =
123+
AnnotationConfigApplicationContext context =
122124
new AnnotationConfigApplicationContext(ValueConfigWithProviderConstructorArguments.class);
123-
doTestValueInjection(factory);
125+
doTestValueInjection(context);
124126
}
125127

126128
@Test
127129
public void testValueInjectionWithProviderMethodArguments() {
128-
AnnotationConfigApplicationContext factory =
130+
AnnotationConfigApplicationContext context =
129131
new AnnotationConfigApplicationContext(ValueConfigWithProviderMethodArguments.class);
130-
doTestValueInjection(factory);
132+
doTestValueInjection(context);
131133
}
132134

133-
private void doTestValueInjection(BeanFactory factory) {
135+
private void doTestValueInjection(BeanFactory context) {
134136
System.clearProperty("myProp");
135137

136-
TestBean testBean = factory.getBean("testBean", TestBean.class);
138+
TestBean testBean = context.getBean("testBean", TestBean.class);
137139
assertNull(testBean.getName());
138140

139-
testBean = factory.getBean("testBean2", TestBean.class);
141+
testBean = context.getBean("testBean2", TestBean.class);
140142
assertNull(testBean.getName());
141143

142144
System.setProperty("myProp", "foo");
143145

144-
testBean = factory.getBean("testBean", TestBean.class);
146+
testBean = context.getBean("testBean", TestBean.class);
145147
assertThat(testBean.getName(), equalTo("foo"));
146148

147-
testBean = factory.getBean("testBean2", TestBean.class);
149+
testBean = context.getBean("testBean2", TestBean.class);
148150
assertThat(testBean.getName(), equalTo("foo"));
149151

150152
System.clearProperty("myProp");
151153

152-
testBean = factory.getBean("testBean", TestBean.class);
154+
testBean = context.getBean("testBean", TestBean.class);
153155
assertNull(testBean.getName());
154156

155-
testBean = factory.getBean("testBean2", TestBean.class);
157+
testBean = context.getBean("testBean2", TestBean.class);
156158
assertNull(testBean.getName());
157159
}
158160

159161
@Test
160-
public void testCustomProperties() {
161-
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
162+
public void testCustomPropertiesWithClassPathContext() throws IOException {
163+
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
162164
"AutowiredConfigurationTests-custom.xml", AutowiredConfigurationTests.class);
163165

164-
TestBean testBean = factory.getBean("testBean", TestBean.class);
166+
TestBean testBean = context.getBean("testBean", TestBean.class);
165167
assertThat(testBean.getName(), equalTo("localhost"));
168+
assertThat(testBean.getAge(), equalTo((int) new ClassPathResource("log4j.properties").contentLength()));
169+
}
170+
171+
@Test
172+
public void testCustomPropertiesWithGenericContext() throws IOException {
173+
GenericApplicationContext context = new GenericApplicationContext();
174+
// context.setResourceLoader(new FileSystemResourceLoader());
175+
new XmlBeanDefinitionReader(context).loadBeanDefinitions(
176+
new ClassPathResource("AutowiredConfigurationTests-custom.xml", AutowiredConfigurationTests.class));
177+
context.refresh();
178+
179+
TestBean testBean = context.getBean("testBean", TestBean.class);
180+
assertThat(testBean.getName(), equalTo("localhost"));
181+
assertThat(testBean.getAge(), equalTo((int) new ClassPathResource("log4j.properties").contentLength()));
166182
}
167183

168184

@@ -321,14 +337,21 @@ static class PropertiesConfig {
321337

322338
private String hostname;
323339

340+
private Resource resource;
341+
324342
@Value("#{myProps.hostname}")
325343
public void setHostname(String hostname) {
326344
this.hostname = hostname;
327345
}
328346

347+
@Value("log4j.properties")
348+
public void setResource(Resource resource) {
349+
this.resource = resource;
350+
}
351+
329352
@Bean
330-
public TestBean testBean() {
331-
return new TestBean(hostname);
353+
public TestBean testBean() throws IOException {
354+
return new TestBean(hostname, (int) resource.contentLength());
332355
}
333356
}
334357

0 commit comments

Comments
 (0)