1
1
/*
2
- * Copyright 2002-2015 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 ;
45
46
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
46
47
import org .springframework .context .annotation .AnnotationConfigUtils ;
47
48
import org .springframework .context .support .GenericApplicationContext ;
48
- import org .springframework .core .convert .converter . Converter ;
49
+ import org .springframework .core .convert .support . DefaultConversionService ;
49
50
import org .springframework .core .convert .support .GenericConversionService ;
50
51
import org .springframework .core .io .ClassPathResource ;
51
52
import org .springframework .core .io .Resource ;
61
62
62
63
/**
63
64
* @author Juergen Hoeller
65
+ * @author Sam Brannen
64
66
* @since 3.0
65
67
*/
66
68
public class ApplicationContextExpressionTests {
@@ -100,6 +102,8 @@ public String getConversationId() {
100
102
}
101
103
});
102
104
105
+ ac .getBeanFactory ().setConversionService (new DefaultConversionService ());
106
+
103
107
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer ();
104
108
Properties placeholders = new Properties ();
105
109
placeholders .setProperty ("code" , "123" );
@@ -174,6 +178,9 @@ public String getConversationId() {
174
178
System .getProperties ().put ("country" , "UK" );
175
179
assertEquals ("123 UK" , tb3 .country );
176
180
assertEquals ("123 UK" , tb3 .countryFactory .getObject ());
181
+ assertEquals ("123" , tb3 .optionalValue1 .get ());
182
+ assertEquals ("123" , tb3 .optionalValue2 .get ());
183
+ assertFalse (tb3 .optionalValue3 .isPresent ());
177
184
assertSame (tb0 , tb3 .tb );
178
185
179
186
tb3 = (ValueTestBean ) SerializationTestUtils .serializeAndDeserialize (tb3 );
@@ -207,12 +214,7 @@ public void prototypeCreationReevaluatesExpressions() {
207
214
GenericApplicationContext ac = new GenericApplicationContext ();
208
215
AnnotationConfigUtils .registerAnnotationConfigProcessors (ac );
209
216
GenericConversionService cs = new GenericConversionService ();
210
- cs .addConverter (String .class , String .class , new Converter <String , String >() {
211
- @ Override
212
- public String convert (String source ) {
213
- return source .trim ();
214
- }
215
- });
217
+ cs .addConverter (String .class , String .class , String ::trim );
216
218
ac .getBeanFactory ().registerSingleton (GenericApplicationContext .CONVERSION_SERVICE_BEAN_NAME , cs );
217
219
RootBeanDefinition rbd = new RootBeanDefinition (PrototypeTestBean .class );
218
220
rbd .setScope (RootBeanDefinition .SCOPE_PROTOTYPE );
@@ -274,8 +276,7 @@ public void prototypeCreationIsFastEnough() {
274
276
275
277
@ Test
276
278
public void systemPropertiesSecurityManager () {
277
- GenericApplicationContext ac = new GenericApplicationContext ();
278
- AnnotationConfigUtils .registerAnnotationConfigProcessors (ac );
279
+ AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext ();
279
280
280
281
GenericBeanDefinition bd = new GenericBeanDefinition ();
281
282
bd .setBeanClass (TestBean .class );
@@ -311,8 +312,7 @@ public void checkPermission(Permission perm) {
311
312
312
313
@ Test
313
314
public void stringConcatenationWithDebugLogging () {
314
- GenericApplicationContext ac = new GenericApplicationContext ();
315
- AnnotationConfigUtils .registerAnnotationConfigProcessors (ac );
315
+ AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext ();
316
316
317
317
GenericBeanDefinition bd = new GenericBeanDefinition ();
318
318
bd .setBeanClass (String .class );
@@ -326,11 +326,10 @@ public void stringConcatenationWithDebugLogging() {
326
326
327
327
@ Test
328
328
public void resourceInjection () throws IOException {
329
- System .setProperty ("logfile" , "log4j.properties" );
330
- try {
331
- AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext (ResourceInjectionBean .class );
329
+ System .setProperty ("logfile" , "do_not_delete_me.txt" );
330
+ try (AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext (ResourceInjectionBean .class )) {
332
331
ResourceInjectionBean resourceInjectionBean = ac .getBean (ResourceInjectionBean .class );
333
- Resource resource = new ClassPathResource ("log4j.properties " );
332
+ Resource resource = new ClassPathResource ("do_not_delete_me.txt " );
334
333
assertEquals (resource , resourceInjectionBean .resource );
335
334
assertEquals (resource .getURL (), resourceInjectionBean .url );
336
335
assertEquals (resource .getURI (), resourceInjectionBean .uri );
@@ -364,6 +363,15 @@ public static class ValueTestBean implements Serializable {
364
363
@ Value ("${code} #{systemProperties.country}" )
365
364
public ObjectFactory <String > countryFactory ;
366
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
+
367
375
@ Autowired @ Qualifier ("original" )
368
376
public transient TestBean tb ;
369
377
}
0 commit comments