|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2016 the original author or authors. |
| 2 | + * Copyright 2002-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
25 | 25 | import java.net.URL;
|
26 | 26 | import java.security.AccessControlException;
|
27 | 27 | import java.security.Permission;
|
| 28 | +import java.util.Optional; |
28 | 29 | import java.util.Properties;
|
29 | 30 |
|
30 | 31 | import org.apache.commons.logging.Log;
|
31 | 32 | import org.apache.commons.logging.LogFactory;
|
32 |
| - |
33 | 33 | import org.junit.Test;
|
34 | 34 |
|
35 | 35 | import org.springframework.beans.factory.ObjectFactory;
|
|
46 | 46 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
47 | 47 | import org.springframework.context.annotation.AnnotationConfigUtils;
|
48 | 48 | import org.springframework.context.support.GenericApplicationContext;
|
49 |
| -import org.springframework.core.convert.converter.Converter; |
| 49 | +import org.springframework.core.convert.support.DefaultConversionService; |
50 | 50 | import org.springframework.core.convert.support.GenericConversionService;
|
51 | 51 | import org.springframework.core.io.ClassPathResource;
|
52 | 52 | import org.springframework.core.io.Resource;
|
@@ -102,6 +102,8 @@ public String getConversationId() {
|
102 | 102 | }
|
103 | 103 | });
|
104 | 104 |
|
| 105 | + ac.getBeanFactory().setConversionService(new DefaultConversionService()); |
| 106 | + |
105 | 107 | PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
|
106 | 108 | Properties placeholders = new Properties();
|
107 | 109 | placeholders.setProperty("code", "123");
|
@@ -176,6 +178,9 @@ public String getConversationId() {
|
176 | 178 | System.getProperties().put("country", "UK");
|
177 | 179 | assertEquals("123 UK", tb3.country);
|
178 | 180 | assertEquals("123 UK", tb3.countryFactory.getObject());
|
| 181 | + assertEquals("123", tb3.optionalValue1.get()); |
| 182 | + assertEquals("123", tb3.optionalValue2.get()); |
| 183 | + assertFalse(tb3.optionalValue3.isPresent()); |
179 | 184 | assertSame(tb0, tb3.tb);
|
180 | 185 |
|
181 | 186 | tb3 = (ValueTestBean) SerializationTestUtils.serializeAndDeserialize(tb3);
|
@@ -209,12 +214,7 @@ public void prototypeCreationReevaluatesExpressions() {
|
209 | 214 | GenericApplicationContext ac = new GenericApplicationContext();
|
210 | 215 | AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
|
211 | 216 | GenericConversionService cs = new GenericConversionService();
|
212 |
| - cs.addConverter(String.class, String.class, new Converter<String, String>() { |
213 |
| - @Override |
214 |
| - public String convert(String source) { |
215 |
| - return source.trim(); |
216 |
| - } |
217 |
| - }); |
| 217 | + cs.addConverter(String.class, String.class, String::trim); |
218 | 218 | ac.getBeanFactory().registerSingleton(GenericApplicationContext.CONVERSION_SERVICE_BEAN_NAME, cs);
|
219 | 219 | RootBeanDefinition rbd = new RootBeanDefinition(PrototypeTestBean.class);
|
220 | 220 | rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
|
@@ -276,8 +276,7 @@ public void prototypeCreationIsFastEnough() {
|
276 | 276 |
|
277 | 277 | @Test
|
278 | 278 | public void systemPropertiesSecurityManager() {
|
279 |
| - GenericApplicationContext ac = new GenericApplicationContext(); |
280 |
| - AnnotationConfigUtils.registerAnnotationConfigProcessors(ac); |
| 279 | + AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(); |
281 | 280 |
|
282 | 281 | GenericBeanDefinition bd = new GenericBeanDefinition();
|
283 | 282 | bd.setBeanClass(TestBean.class);
|
@@ -313,8 +312,7 @@ public void checkPermission(Permission perm) {
|
313 | 312 |
|
314 | 313 | @Test
|
315 | 314 | public void stringConcatenationWithDebugLogging() {
|
316 |
| - GenericApplicationContext ac = new GenericApplicationContext(); |
317 |
| - AnnotationConfigUtils.registerAnnotationConfigProcessors(ac); |
| 315 | + AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(); |
318 | 316 |
|
319 | 317 | GenericBeanDefinition bd = new GenericBeanDefinition();
|
320 | 318 | bd.setBeanClass(String.class);
|
@@ -365,6 +363,15 @@ public static class ValueTestBean implements Serializable {
|
365 | 363 | @Value("${code} #{systemProperties.country}")
|
366 | 364 | public ObjectFactory<String> countryFactory;
|
367 | 365 |
|
| 366 | + @Value("${code}") |
| 367 | + private transient Optional<String> optionalValue1; |
| 368 | + |
| 369 | + @Value("${code:#{null}}") |
| 370 | + private transient Optional<String> optionalValue2; |
| 371 | + |
| 372 | + @Value("${codeX:#{null}}") |
| 373 | + private transient Optional<String> optionalValue3; |
| 374 | + |
368 | 375 | @Autowired @Qualifier("original")
|
369 | 376 | public transient TestBean tb;
|
370 | 377 | }
|
|
0 commit comments