1
1
/*
2
- * Copyright 2010-2014 the original author or authors.
2
+ * Copyright 2012-2015 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.
27
27
import java .util .List ;
28
28
import java .util .Properties ;
29
29
30
+ import ch .qos .logback .classic .BasicConfigurator ;
31
+ import ch .qos .logback .classic .Logger ;
32
+ import ch .qos .logback .classic .LoggerContext ;
30
33
import org .hamcrest .Description ;
31
34
import org .hamcrest .Matcher ;
32
35
import org .hamcrest .TypeSafeDiagnosingMatcher ;
33
36
import org .junit .After ;
37
+ import org .junit .Before ;
34
38
import org .junit .Rule ;
35
39
import org .junit .Test ;
36
40
import org .junit .rules .ExpectedException ;
41
+ import org .slf4j .LoggerFactory ;
37
42
38
43
import org .springframework .boot .SpringApplication ;
39
44
import org .springframework .boot .context .config .ConfigFileApplicationListener .ConfigurationPropertySources ;
40
45
import org .springframework .boot .context .event .ApplicationEnvironmentPreparedEvent ;
46
+ import org .springframework .boot .context .event .ApplicationPreparedEvent ;
41
47
import org .springframework .boot .env .EnumerableCompositePropertySource ;
42
48
import org .springframework .boot .test .EnvironmentTestUtils ;
49
+ import org .springframework .boot .test .OutputCapture ;
43
50
import org .springframework .context .ConfigurableApplicationContext ;
51
+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
44
52
import org .springframework .context .annotation .Configuration ;
45
53
import org .springframework .context .annotation .Profile ;
46
54
import org .springframework .context .annotation .PropertySource ;
55
63
import org .springframework .util .ReflectionUtils ;
56
64
import org .springframework .util .StringUtils ;
57
65
58
- import static org .hamcrest .Matchers .contains ;
59
- import static org .hamcrest .Matchers .equalTo ;
60
- import static org .hamcrest .Matchers .not ;
61
- import static org .hamcrest .Matchers .notNullValue ;
62
- import static org .hamcrest .Matchers .nullValue ;
63
- import static org .junit .Assert .assertEquals ;
64
- import static org .junit .Assert .assertThat ;
66
+ import static org .hamcrest .Matchers .*;
67
+ import static org .junit .Assert .*;
65
68
66
69
/**
67
70
* Tests for {@link ConfigFileApplicationListener}.
@@ -81,6 +84,17 @@ public class ConfigFileApplicationListenerTests {
81
84
@ Rule
82
85
public ExpectedException expected = ExpectedException .none ();
83
86
87
+ @ Rule
88
+ public OutputCapture out = new OutputCapture ();
89
+
90
+ @ Before
91
+ public void resetLogging () {
92
+ LoggerContext loggerContext = ((Logger ) LoggerFactory .getLogger (getClass ()))
93
+ .getLoggerContext ();
94
+ loggerContext .reset ();
95
+ BasicConfigurator .configure (loggerContext );
96
+ }
97
+
84
98
@ After
85
99
public void cleanup () {
86
100
System .clearProperty ("the.property" );
@@ -343,14 +357,63 @@ public void profilePropertiesUsedInPlaceholders() throws Exception {
343
357
344
358
@ Test
345
359
public void profilesAddedToEnvironmentAndViaProperty () throws Exception {
360
+ // External profile takes precedence over profile added via the environment
346
361
EnvironmentTestUtils .addEnvironment (this .environment ,
347
- "spring.profiles.active:foo " );
362
+ "spring.profiles.active:other " );
348
363
this .environment .addActiveProfile ("dev" );
349
364
this .initializer .onApplicationEvent (this .event );
350
- assertThat (this .environment .getActiveProfiles (),
351
- equalTo (new String [] { "foo" , "dev" }));
365
+ assertThat (Arrays .asList (this .environment .getActiveProfiles ()), containsInAnyOrder ("dev" , "other" ));
366
+ assertThat (this .environment .getProperty ("my.property" ),
367
+ equalTo ("fromotherpropertiesfile" ));
368
+ validateProfilePrecedence (null , "dev" , "other" );
369
+ }
370
+
371
+ @ Test
372
+ public void profilesAddedToEnvironmentAndViaPropertyDuplicate () throws Exception {
373
+ EnvironmentTestUtils .addEnvironment (this .environment ,
374
+ "spring.profiles.active:dev,other" );
375
+ this .environment .addActiveProfile ("dev" );
376
+ this .initializer .onApplicationEvent (this .event );
377
+ assertThat (Arrays .asList (this .environment .getActiveProfiles ()), containsInAnyOrder ("dev" , "other" ));
378
+ assertThat (this .environment .getProperty ("my.property" ),
379
+ equalTo ("fromotherpropertiesfile" ));
380
+ validateProfilePrecedence (null , "dev" , "other" );
381
+ }
382
+
383
+ @ Test
384
+ public void profilesAddedToEnvironmentAndViaPropertyDuplicateEnvironmentWins () throws Exception {
385
+ EnvironmentTestUtils .addEnvironment (this .environment ,
386
+ "spring.profiles.active:other,dev" );
387
+ this .environment .addActiveProfile ("other" );
388
+ this .initializer .onApplicationEvent (this .event );
389
+ assertThat (Arrays .asList (this .environment .getActiveProfiles ()), containsInAnyOrder ("dev" , "other" ));
352
390
assertThat (this .environment .getProperty ("my.property" ),
353
391
equalTo ("fromdevpropertiesfile" ));
392
+ validateProfilePrecedence (null , "other" , "dev" );
393
+ }
394
+
395
+ private void validateProfilePrecedence (String ... profiles ) {
396
+ this .initializer .onApplicationEvent (new ApplicationPreparedEvent (
397
+ new SpringApplication (), new String [0 ], new AnnotationConfigApplicationContext ()));
398
+ String log = this .out .toString ();
399
+
400
+ // First make sure that each profile got processed only once
401
+ for (String profile : profiles ) {
402
+ assertThat ("Wrong number of occurrences for profile '" + profile + "' --> " + log ,
403
+ StringUtils .countOccurrencesOf (log , createLogForProfile (profile )), equalTo (1 ));
404
+ }
405
+ // Make sure the order of loading is the right one
406
+ for (String profile : profiles ) {
407
+ String line = createLogForProfile (profile );
408
+ int index = log .indexOf (line );
409
+ assertTrue ("Loading profile '" + profile + "' not found in '" + log + "'" , index != -1 );
410
+ log = log .substring (index + line .length (), log .length ());
411
+ }
412
+ }
413
+
414
+ private String createLogForProfile (String profile ) {
415
+ String suffix = profile != null ? "-" + profile : "" ;
416
+ return "Loaded config file 'classpath:/application" + suffix + ".properties'" ;
354
417
}
355
418
356
419
@ Test
0 commit comments